/** * A node in a binary search tree. */ public class NewBSTNode { // +--------+-------------------------------------------------- // | Fields | // +--------+ /** * The contents of this node. */ Object contents; /** * Things smaller than the current element. */ NewBSTNode left; /** * Things larger than the current element. */ NewBSTNode right; /** * The height of the subtree rooted at this node. */ int height; // +--------------+-------------------------------------------- // | Constructors | // +--------------+ /** * Create a new node, specifying only the contents. */ public NewBSTNode(Object contents) { this.contents = contents; this.left = null; this.right = null; this.height = 1; } // NewBSTNode(Object) // +------------------+---------------------------------------- // | Exported Methods | // +------------------+ /** * Recompute the height of this node. * Pre: Heights of subtrees are correct. */ public void updateHeight() { this.height = 1 + max(heightOf(this.left), heightOf(this.right)); } // updateHeight() // +---------------+------------------------------------------- // | Local Methods | // +---------------+ static int heightOf(NewBSTNode n) { if (null == n) return 0; else return n.height; } // heightOf(NewBSTNode) /** * Compute the largest of two integers. */ int max(int x, int y) { if (x > y) return x; else return y; } // max(int, int) } // class NewBSTNode