Class TreeNode

java.lang.Object
  extended byTreeNode
All Implemented Interfaces:
java.lang.Comparable

public class TreeNode
extends java.lang.Object
implements java.lang.Comparable

Author: Clare So
Revised: March 15, 2007

Description: Specifies a node in a binary tree
Features:

  1. Any type of comparable objects can be stored
  2. Contents of the node cannot be updated


Constructor Summary
TreeNode(java.lang.Comparable contents)
          Constructor of TreeNode
 
Method Summary
 int compareTo(java.lang.Object node)
          Compare two objects of (probably) the same kind.
 boolean equals(java.lang.Object o)
          Check if the current node is equal to the other node.
 TreeNode getLeft()
          Get the left child
 TreeNode getRight()
          Get the right child
 boolean haveLeft()
          Test if the left child is not null
 boolean haveRight()
          Test if the right child is not null
 boolean isGreaterThan(TreeNode node)
          See if the current node is larger
 boolean isLeaf()
          Test if the current node is a leaf
 boolean isLessThan(TreeNode node)
          See if the current node is less
 TreeNode setLeft(TreeNode node)
          Store the new node to the left of the current one only if the current node's left child is null.
 TreeNode setRight(TreeNode node)
          Store the new node to the right of the current one only if the current node's right child is null.
 java.lang.String toString()
          Create a string representation of the node
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TreeNode

public TreeNode(java.lang.Comparable contents)
Constructor of TreeNode

Parameters:
contents - Data to be stored in the node
Method Detail

setLeft

public TreeNode setLeft(TreeNode node)
Store the new node to the left of the current one only if the current node's left child is null.

Parameters:
node - New node to be inserted
Returns:
null if the left child not null, the new node otherwise.

setRight

public TreeNode setRight(TreeNode node)
Store the new node to the right of the current one only if the current node's right child is null.

Parameters:
node - New node to be inserted
Returns:
null if the right child not null, the new node otherwise.

getLeft

public TreeNode getLeft()
Get the left child

Returns:
current node's left child

getRight

public TreeNode getRight()
Get the right child

Returns:
current node's right child

haveRight

public boolean haveRight()
Test if the right child is not null

Returns:
true if right child is not null, false otherwise

haveLeft

public boolean haveLeft()
Test if the left child is not null

Returns:
true if left child is not null, false otherwise

isLeaf

public boolean isLeaf()
Test if the current node is a leaf

Returns:
true if both left and right child are null, false otherwise

compareTo

public int compareTo(java.lang.Object node)
              throws java.lang.ClassCastException
Compare two objects of (probably) the same kind. Note that this method is compulsory for all classes implementing comparable interface.

Specified by:
compareTo in interface java.lang.Comparable
Parameters:
node - Object to be compared
Returns:
Postive if current object is larger. Equal if two objects are the same. Negative if the current object is smaller.
Throws:
java.lang.ClassCastException

isGreaterThan

public boolean isGreaterThan(TreeNode node)
See if the current node is larger

Parameters:
node - the node to be compared
Returns:
true if the current node is larger

isLessThan

public boolean isLessThan(TreeNode node)
See if the current node is less

Parameters:
node - the node to be compared
Returns:
true if the current node is smaller

equals

public boolean equals(java.lang.Object o)
Check if the current node is equal to the other node.

Parameters:
o - the node to be compared
Returns:
true if the current node is the same as the other node

toString

public java.lang.String toString()
Create a string representation of the node

Returns:
a string containing the contents of the node