public class BinaryTreeNode { protected T value; protected BinaryTreeNode left; protected BinaryTreeNode right; /** * Constructs a BinaryTreeNode with the given value. * @param value A value. */ public BinaryTreeNode(T value) { this.value = value; } /** * Get the value. */ public T get() { return value; } /** * Gets the left subtree. */ public BinaryTreeNode getLeft() { return left; } /** * Gets the right subtree. */ public BinaryTreeNode getRight() { return right; } /** * Sets the left subtree. * @param t A tree. */ public void setLeft(BinaryTreeNode t) { left = t; } /** * Sets the right subtree. * @param t A tree. */ public void setRight(BinaryTreeNode t) { right = t; } /** * Computes the number of nodes in the tree. */ public int size() { int l = left != null ? left.size() : 0; int r = right != null ? right.size() : 0; return 1 + l + r; } /** * Returns a string representation of the tree. */ public String toString() { // (v l r) if (left == null && right == null) { return value.toString(); } String l = left != null ? left.toString() : ""; String r = right != null ? right.toString() : ""; return "(" + value.toString() + " " + l + " " + r + ")"; } /** * Returns the edge height of the given tree. * @param t A tree or null for the empty tree. */ public static int getHeight(BinaryTreeNode t) { if (t == null) { return -1; } int lh = getHeight(t.getLeft()); int rh = getHeight(t.getRight()); return lh > rh ? lh + 1 : rh + 1; } }