public class BinaryNode {

    private Object item;
    private BinaryNode left;
    private 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));
    }

    // 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.toString() + "\n";
	if (left != null)
            s += spacing + left.toTree(blank, blank+spacing);
	if (right != null)
            s += spacing + right.toTree(blank, blank+spacing);
	return s;
    }

} // Class BinaryNode