public class BinaryNode { protected Object item; protected BinaryNode left; protected BinaryNode right; public BinaryNode() { this(null,null,null); } public BinaryNode(Object item) { this(item,null,null); } public BinaryNode(Object item, BinaryNode left, BinaryNode right) { this.item=item; this.left=left; this.right=right; } public Object getItem() { return item; } public BinaryNode getLeft() { return left; } public BinaryNode getRight() { return right; } public void setItem(Object o) { item = o; } public void setLeft(BinaryNode n) { left = n; } public void setRight(BinaryNode n) { right = n; } public boolean isLeaf() { return (left==null) && (right == null); } public int getHeight() { return getHeight(this); } private int getHeight(BinaryNode node) { // bottom of tree: height==0 if (node == null) return 0; // node to children adds 1 to height of node: else return 1 + Math.max(getHeight(node.left), getHeight(node.right)); } public int getNodes() { return getNodes(this); } private int getNodes(BinaryNode node) { if (node == null) return 0; else { return 1 + getNodes(node.left) + getNodes(node.right) ; } } // using pre-order for clarity: public String toString() { return ""+item+"("+left+","+right+")"; } // handier way to view tree, esp for debugging: public String toTree() { return toTree("| ","|___"); } public String toTree(String blank,String spacing) { String s = "" + item + "\n"; if (left == null) s += spacing + "\n"; else s += spacing + left.toTree(blank, blank+spacing); if (right == null) s += spacing + "\n"; else s += spacing + right.toTree(blank, blank+spacing); return s; } public boolean naivefind(Object o,BinaryNode n) { if (n==null) return false; else if (o.equals(n.item)) return true; else return naivefind(o,n.left) || naivefind(o,n.right); } } // Class BinaryNode