/** * 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 *
* * @author W. M. Farmer */ public class TreeNode implements Comparable { private Comparable contents; private TreeNode myLeft; private TreeNode myRight; /** * Constructor of TreeNode * @param contents Data to be stored in the node */ public TreeNode(Comparable contents) { this.contents = contents; this.myLeft = null; this.myRight = null; } /** * Store the new node to the left of the current one * only if the current node's left child is null. * * @param node New node to be inserted * @return null if the left child not null, the new node otherwise. */ public TreeNode setLeft(TreeNode node) { if (myLeft==null) { myLeft = node; return node; } else return null; } /** * Store the new node to the right of the current one * only if the current node's right child is null. * * @param node New node to be inserted * @return null if the right child not null, the new node otherwise. */ public TreeNode setRight(TreeNode node) { if (myRight==null) { myRight = node; return node; } else return null; } /** * Get the left child * * @return current node's left child */ public TreeNode getLeft() { return myLeft; } /** * Get the right child * * @return current node's right child */ public TreeNode getRight() { return myRight; } /** * Test if the right child is not null * * @return true if right child is not null, false otherwise */ public boolean haveRight() { return (myRight!=null); } /** * Test if the left child is not null * * @return true if left child is not null, false otherwise */ public boolean haveLeft() { return (myLeft!=null); } /** * Test if the current node is a leaf * * @return true if both left and right child are null, false otherwise */ public boolean isLeaf() { return (myLeft==null) && (myRight==null); } /** * Compare two objects of (probably) the same kind. Note that * this method is compulsory for all classes implementing * comparable interface. * * @param node Object to be compared * @return Postive if current object is larger. Equal if two objects * are the same. Negative if the current object is smaller. */ public int compareTo(Object node) throws ClassCastException { if (node instanceof TreeNode) return contents.compareTo(((TreeNode)node).contents); else throw new ClassCastException(); } /** * See if the current node is larger * * @param node the node to be compared * @return true if the current node is larger */ public boolean isGreaterThan(TreeNode node) { try { return (this.compareTo(node) > 0); } catch (ClassCastException cce) { return false; } } /** * See if the current node is less * * @param node the node to be compared * @return true if the current node is smaller */ public boolean isLessThan(TreeNode node) { try { return (this.compareTo(node) < 0); } catch (ClassCastException cce) { return false; } } /** * Check if the current node is equal to the * other node. * * @param o the node to be compared * @return true if the current node is the same as the other node */ public boolean equals(Object o) { try { return this.compareTo(o)==0; } catch (ClassCastException cce) { return false; } } /** * Create a string representation of the node * * @return a string containing the contents of the node */ public String toString() { return "("+contents.toString()+")"; } }