gshute.trees
Class Tree

java.lang.Object
  |
  +--gshute.trees.Tree
Direct Known Subclasses:
SearchTree

public class Tree
extends java.lang.Object

A Tree is a binary tree with a cursor. The cursor is a TreeEdge that can be moved around in the tree. Nodes can be replaced, added, or removed at the cursor to position.


Field Summary
protected  TreeEdge cursor
          cursor is the cursor edge for this tree.
protected  java.lang.reflect.Constructor nodeConstructor
          nodeConstructor is the node constructor for this tree.
protected  TreeNode root
          root is the root of this tree.
protected  java.lang.StringBuffer stringBuffer
          stringBuffer is the string buffer used by this tree for building its toString() returned value.
 
Constructor Summary
Tree(java.lang.Class nc)
          new Tree(nc) returns an empty binary tree whose nodes are constructed from class nc.
 
Method Summary
 void add(java.lang.Object d)
          t.add(d) modifies t, adding a node with data d at the cursor to position of t.
protected  void addSubtreeString(TreeNode n, int l, int sz)
          addSubtreeString(n, l, sz) adds a string to stringBuffer.
 boolean containsNode(TreeNode n)
          t.containsNode() returns true if n is a node in t.
 TreeEdge getCursor()
          t.getCursor() returns the cursor of t.
 TreeNode getCursorTo()
          t.getCursorTo() returns the cursor to node of t.
 java.util.Iterator getInorderIterator()
          t.getInorderIterator() returns an inorder iterator for the data in t.
 java.util.Iterator getPostorderIterator()
          t.getPostorderIterator() returns a postorder iterator for the data in t.
 java.util.Iterator getPreorderIterator()
          t.getPreorderIterator() returns a preorder iterator for the data in t.
 TreeNode getRoot()
          t.getRoot() returns the root of t.
 boolean isEmpty()
          t.isEmpty() returns true if t is empty.
 void remove()
          t.remove() removes the data at the to node of the cursor of t.
 void replace(TreeEdge tl, TreeNode n)
          t.replace(n) modifies t, replacing the cursor to node of t by n.
 void rotateLeft(TreeNode n)
          t.rotateLeft(n) performs a left rotation in t at n.
 void rotateRight(TreeNode n)
          t.rotateRight(n) performs a right rotation in t at n.
 void setRoot(TreeNode rn)
          t.setRoot(rn) makes rn the root node of t.
 java.lang.String toString()
          t.toString() returns a string that describes t and its data, using an indent size of 6 between different levels of t.
 java.lang.String toString(int sz)
          t.toString(sz) returns a string that describes t and its data, using an indent size of sz between different levels of t.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

root

protected TreeNode root
root is the root of this tree.

cursor

protected TreeEdge cursor
cursor is the cursor edge for this tree.

nodeConstructor

protected java.lang.reflect.Constructor nodeConstructor
nodeConstructor is the node constructor for this tree.

stringBuffer

protected java.lang.StringBuffer stringBuffer
stringBuffer is the string buffer used by this tree for building its toString() returned value.
Constructor Detail

Tree

public Tree(java.lang.Class nc)
new Tree(nc) returns an empty binary tree whose nodes are constructed from class nc.

Preconditions: TreeNode.class.isAssignableFrom(c) is true.

Method Detail

isEmpty

public boolean isEmpty()
t.isEmpty() returns true if t is empty.

getRoot

public TreeNode getRoot()
t.getRoot() returns the root of t.

getCursorTo

public TreeNode getCursorTo()
t.getCursorTo() returns the cursor to node of t.

getCursor

public TreeEdge getCursor()
t.getCursor() returns the cursor of t.

containsNode

public boolean containsNode(TreeNode n)
t.containsNode() returns true if n is a node in t.

setRoot

public void setRoot(TreeNode rn)
t.setRoot(rn) makes rn the root node of t. After the call, the contents of t consists of the subtree rooted at rn.

replace

public void replace(TreeEdge tl,
                    TreeNode n)
t.replace(n) modifies t, replacing the cursor to node of t by n.

add

public void add(java.lang.Object d)
t.add(d) modifies t, adding a node with data d at the cursor to position of t.

Preconditions:
t.getCursor().getTo() == null


remove

public void remove()
t.remove() removes the data at the to node of the cursor of t. If one of the children of the to node is null then the other child replaces the to node. If both children of the to node are non-null then the data from its inorder successor is copied to the to node and then the successor node is replaced by its right child.

Preconditions:
t.getCursor().getTo() != null


rotateLeft

public void rotateLeft(TreeNode n)
t.rotateLeft(n) performs a left rotation in t at n.

rotateRight

public void rotateRight(TreeNode n)
t.rotateRight(n) performs a right rotation in t at n.

getPreorderIterator

public java.util.Iterator getPreorderIterator()
t.getPreorderIterator() returns a preorder iterator for the data in t.

getInorderIterator

public java.util.Iterator getInorderIterator()
t.getInorderIterator() returns an inorder iterator for the data in t.

getPostorderIterator

public java.util.Iterator getPostorderIterator()
t.getPostorderIterator() returns a postorder iterator for the data in t.

addSubtreeString

protected void addSubtreeString(TreeNode n,
                                int l,
                                int sz)
addSubtreeString(n, l, sz) adds a string to stringBuffer. The added string describes the subtree rooted at n, indented by l*sz spaces.

toString

public java.lang.String toString()
t.toString() returns a string that describes t and its data, using an indent size of 6 between different levels of t.
Overrides:
toString in class java.lang.Object

toString

public java.lang.String toString(int sz)
t.toString(sz) returns a string that describes t and its data, using an indent size of sz between different levels of t.