Class BinaryTree

java.lang.Object
  extended by BinaryTree

public class BinaryTree
extends java.lang.Object

Implements loop-free binary trees, allowing shared subtrees.

Version:
Jan 25, 2004
Author:
David Matuszek

Field Summary
 java.lang.Object value
          The value (data) in this node of the binary tree; may be of any object type.
 
Constructor Summary
BinaryTree(java.lang.Object value)
          Constructor for a BinaryTree leaf node (that is, with no children).
BinaryTree(java.lang.Object value, BinaryTree leftChild, BinaryTree rightChild)
          Constructor for BinaryTree.
 
Method Summary
 boolean equals(java.lang.Object o)
          Tests whether this BinaryTree is equal to the given object.
 BinaryTree getLeftChild()
          Getter method for left child of this BinaryTree node.
 BinaryTree getRightChild()
          Getter method for right child of this BinaryTree node.
 java.lang.Object getValue()
          Getter method for the value in this BinaryTree node.
 int hashCode()
          Computes a hash code for the complete binary tree rooted at this BinaryTree node.
 boolean isLeaf()
          Tests whether this node is a leaf node.
 void print()
          Prints the binary tree rooted at this BinaryTree node.
 void setLeftChild(BinaryTree subtree)
          Sets the left child of this BinaryTree node to be the given subtree.
 void setRightChild(BinaryTree subtree)
          Sets the right child of this BinaryTree node to be the given subtree.
 void setValue(java.lang.Object value)
          Sets the value in this BinaryTree node.
 java.lang.String toString()
          Returns a String representation of this BinaryTree.
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

value

public java.lang.Object value
The value (data) in this node of the binary tree; may be of any object type.

Constructor Detail

BinaryTree

public BinaryTree(java.lang.Object value,
                  BinaryTree leftChild,
                  BinaryTree rightChild)
Constructor for BinaryTree.

Parameters:
value - The value to be placed in the root.
leftChild - The left child of the root (may be null).
rightChild - The right child of the root (may be null).

BinaryTree

public BinaryTree(java.lang.Object value)
Constructor for a BinaryTree leaf node (that is, with no children).

Parameters:
value - The value to be placed in the root.
Method Detail

getValue

public java.lang.Object getValue()
Getter method for the value in this BinaryTree node.

Returns:
The value in this node.

getLeftChild

public BinaryTree getLeftChild()
Getter method for left child of this BinaryTree node.

Returns:
The left child (null if no left child).

getRightChild

public BinaryTree getRightChild()
Getter method for right child of this BinaryTree node.

Returns:
The right child (null if no right child).

setLeftChild

public void setLeftChild(BinaryTree subtree)
                  throws java.lang.IllegalArgumentException
Sets the left child of this BinaryTree node to be the given subtree. If the node previously had a left child, it is discarded. Throws an IllegalArgumentException if the operation would cause a loop in the binary tree.

Parameters:
subtree - The node to be added as the new left child.
Throws:
java.lang.IllegalArgumentException - If the operation would cause a loop in the binary tree.

setRightChild

public void setRightChild(BinaryTree subtree)
                   throws java.lang.IllegalArgumentException
Sets the right child of this BinaryTree node to be the given subtree. If the node previously had a right child, it is discarded. Throws an IllegalArgumentException if the operation would cause a loop in the binary tree.

Parameters:
subtree - The node to be added as the new right child.
Throws:
java.lang.IllegalArgumentException - If the operation would cause a loop in the binary tree.

setValue

public void setValue(java.lang.Object value)
Sets the value in this BinaryTree node.

Parameters:
value - The new value.

isLeaf

public boolean isLeaf()
Tests whether this node is a leaf node.

Returns:
true if this BinaryTree node has no children.

equals

public boolean equals(java.lang.Object o)
Tests whether this BinaryTree is equal to the given object. To be considered equal, the object must be a BinaryTree, and the two binary trees must have equal values in their roots, equal left subtrees, and equal right subtrees.

Overrides:
equals in class java.lang.Object
Returns:
true if the binary trees are equal.
See Also:
Object.equals(java.lang.Object)

toString

public java.lang.String toString()
Returns a String representation of this BinaryTree.

Overrides:
toString in class java.lang.Object
Returns:
A String representation of this BinaryTree.
See Also:
Object.toString()

hashCode

public int hashCode()
Computes a hash code for the complete binary tree rooted at this BinaryTree node.

Overrides:
hashCode in class java.lang.Object
Returns:
A hash code for the binary tree with this root.
See Also:
Object.hashCode()

print

public void print()
Prints the binary tree rooted at this BinaryTree node.