structure5
Class BinaryTree<E>

java.lang.Object
  extended by structure5.BinaryTree<E>

public class BinaryTree<E>
extends Object

This class implements a single node of a binary tree. It is a recursive structure. Relationships between nodes are doubly linked, with parent and child references. Many characteristics of trees may be detected with static methods.

See Also:
structure.BinaryTree, structure.BinarySearchTree

Constructor Summary
BinaryTree()
          A one-time constructor, for constructing empty trees.
BinaryTree(E value)
          Constructs a tree node with no children.
BinaryTree(E value, BinaryTree<E> left, BinaryTree<E> right)
          Constructs a tree node with two children.
 
Method Summary
 int depth()
          Compute the depth of a node.
 int hashCode()
           
 int height()
          Returns height of node in tree.
 AbstractIterator<E> inorderIterator()
          Return an iterator to traverse the elements of subtree in-order
 boolean isBalanced()
          Return true iff the tree is height balanced.
 boolean isComplete()
          Return whether tree is complete.
 boolean isEmpty()
          Returns true if tree is empty.
 boolean isFull()
          Returns true if tree is full.
 boolean isLeftChild()
          Determine if this node is a left child
 boolean isRightChild()
          Determine if this node is a right child
 Iterator<E> iterator()
          Generate an in-order iterator of subtree
 BinaryTree<E> left()
          Get left subtree of current node
 AbstractIterator<E> levelorderIterator()
          Method to return a level-order iterator of subtree
 BinaryTree<E> parent()
          Get reference to parent of this node
 AbstractIterator<E> postorderIterator()
          Return an iterator to traverse the elements of subtree in post-order
 AbstractIterator<E> preorderIterator()
          Return an iterator to traverse nodes of subtree in-order
 BinaryTree<E> right()
          Get right subtree of current node
 BinaryTree<E> root()
          Returns reference to root of tree containing n
 void setLeft(BinaryTree<E> newLeft)
          Update the left subtree of this node.
 void setRight(BinaryTree<E> newRight)
          Update the right subtree of this node.
 void setValue(E value)
          Set's value associated with this node
 int size()
          Returns the number of descendants of node
 String toString()
          Returns a representation the subtree rooted at this node
 String treeString()
          Returns a string representing the tree rooted at this node.
 E value()
          Returns value associated with this node
 
Methods inherited from class java.lang.Object
equals, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

BinaryTree

public BinaryTree()
A one-time constructor, for constructing empty trees. Space efficiencies are possible if empty trees are reused.


BinaryTree

public BinaryTree(E value)
Constructs a tree node with no children. Value of the node and subtrees are provided by the user

Parameters:
value - A (possibly null) value to be referenced by node

BinaryTree

public BinaryTree(E value,
                  BinaryTree<E> left,
                  BinaryTree<E> right)
Constructs a tree node with two children. Value of the node and subtrees are provided by the user.

Parameters:
value - A (possibly null) value to be referenced by node
left - The subtree to be left subtree of node
right - The subtree to be right subtree of node
Method Detail

left

public BinaryTree<E> left()
Get left subtree of current node

Returns:
The left subtree of this node

right

public BinaryTree<E> right()
Get right subtree of current node

Returns:
The right subtree of this node

parent

public BinaryTree<E> parent()
Get reference to parent of this node

Returns:
Reference to parent of this node

setLeft

public void setLeft(BinaryTree<E> newLeft)
Update the left subtree of this node. Parent of the left subtree is updated consistently. Existing subtree is detached

Parameters:
newLeft - The root of the new left subtree

setRight

public void setRight(BinaryTree<E> newRight)
Update the right subtree of this node. Parent of the right subtree is updated consistently. Existing subtree is detached

Parameters:
newRight - A reference to the new right subtree of this node

size

public int size()
Returns the number of descendants of node

Returns:
Size of subtree

root

public BinaryTree<E> root()
Returns reference to root of tree containing n

Returns:
Root of tree

height

public int height()
Returns height of node in tree. Height is maximum path length to descendant

Returns:
The height of the node in the tree

depth

public int depth()
Compute the depth of a node. The depth is the path length from node to root

Returns:
The path length to root of tree

isFull

public boolean isFull()
Returns true if tree is full. A tree is full if adding a node to tree would necessarily increase its height

Returns:
True iff tree is full

isEmpty

public boolean isEmpty()
Returns true if tree is empty.

Returns:
True iff tree is empty

isComplete

public boolean isComplete()
Return whether tree is complete. A complete tree has minimal height and any holes in tree would appear in last level to right.

Returns:
True iff the subtree is complete

isBalanced

public boolean isBalanced()
Return true iff the tree is height balanced. A tree is height balanced iff at every node the difference in heights of subtrees is no greater than one

Returns:
True if tree is height balanced

iterator

public Iterator<E> iterator()
Generate an in-order iterator of subtree

Returns:
In-order iterator on subtree rooted at this

preorderIterator

public AbstractIterator<E> preorderIterator()
Return an iterator to traverse nodes of subtree in-order

Returns:
AbstractIterator to traverse subtree

inorderIterator

public AbstractIterator<E> inorderIterator()
Return an iterator to traverse the elements of subtree in-order

Returns:
An in-order iterator over descendants of node

postorderIterator

public AbstractIterator<E> postorderIterator()
Return an iterator to traverse the elements of subtree in post-order

Returns:
An iterator that traverses descendants of node in postorder

levelorderIterator

public AbstractIterator<E> levelorderIterator()
Method to return a level-order iterator of subtree

Returns:
An iterator to traverse subtree in level-order

isLeftChild

public boolean isLeftChild()
Determine if this node is a left child

Returns:
True iff this node is a left child of parent

isRightChild

public boolean isRightChild()
Determine if this node is a right child

Returns:
True iff this node is a right child of parent

value

public E value()
Returns value associated with this node

Returns:
The node's value

setValue

public void setValue(E value)
Set's value associated with this node

Parameters:
value - The new value of this node

hashCode

public int hashCode()
Overrides:
hashCode in class Object

treeString

public String treeString()
Returns a string representing the tree rooted at this node. WARNING this can be a very long string.

Returns:
A string representing the tree rooted at this node.

toString

public String toString()
Returns a representation the subtree rooted at this node

Overrides:
toString in class Object
Returns:
String representing this subtree