structure
Class BinaryTree

java.lang.Object
  extended by structure.BinaryTree

public class BinaryTree
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:
BinaryTree, BinarySearchTree

Field Summary
static BinaryTree EMPTY
          The unique empty node
 
Constructor Summary
BinaryTree(Object value)
          Constructs a tree node with no children.
BinaryTree(Object value, BinaryTree left, BinaryTree right)
          Constructs a tree node with no children.
 
Method Summary
 int depth()
          Compute the depth of a node.
 int hashCode()
           
 int height()
          Returns height of node in tree.
 AbstractIterator 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 iterator()
          Generate an in-order iterator of subtree
 BinaryTree left()
          Get left subtree of current node
 AbstractIterator levelorderIterator()
          Method to return a level-order iterator of subtree
 BinaryTree parent()
          Get reference to parent of this node
 AbstractIterator postorderIterator()
          Return an iterator to traverse the elements of subtree in post-order
 AbstractIterator preorderIterator()
          Return an iterator to traverse nodes of subtree in-order
 BinaryTree right()
          Get right subtree of current node
 BinaryTree root()
          Returns reference to root of tree containing n
 void setLeft(BinaryTree newLeft)
          Update the left subtree of this node.
 void setRight(BinaryTree newRight)
          Update the right subtree of this node.
 void setValue(Object 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
 Object value()
          Returns value associated with this node
 
Methods inherited from class java.lang.Object
equals, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

EMPTY

public static final BinaryTree EMPTY
The unique empty node

Constructor Detail

BinaryTree

public BinaryTree(Object value)
Constructs a tree node with no children. Value of the node is provided by the user

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

BinaryTree

public BinaryTree(Object value,
                  BinaryTree left,
                  BinaryTree right)
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
left - The subtree to be left subtree of node
right - The subtree to be right subtree of node
Method Detail

left

public BinaryTree left()
Get left subtree of current node

Returns:
The left subtree of this node

right

public BinaryTree right()
Get right subtree of current node

Returns:
The right subtree of this node

parent

public BinaryTree parent()
Get reference to parent of this node

Returns:
Reference to parent of this node

setLeft

public void setLeft(BinaryTree 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 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 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 iterator()
Generate an in-order iterator of subtree

Returns:
In-order iterator on subtree rooted at this

preorderIterator

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

Returns:
AbstractIterator to traverse subtree

inorderIterator

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

Returns:
An in-order iterator over descendants of node

postorderIterator

public AbstractIterator 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 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 Object value()
Returns value associated with this node

Returns:
The node's value

setValue

public void setValue(Object 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

toString

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

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