class BinaryTree { private T value; private BinaryTree left; private BinaryTree right; public BinaryTree(T value) { this.value = value; this.left = null; this.right = null; } public BinaryTree(T value, BinaryTree left, BinaryTree right) { this.value = value; this.left = left; this.right = right; } public void setLeft(BinaryTree left) { this.left = left; } public void setRight(BinaryTree right) { this.right = right; } public void setValue(T value) { this.value = value; } public T getValue() { return value; } public BinaryTree getLeft() { return left; } public BinaryTree getRight() { return right; } /** * Computes the (edge) height. * Note that this is an instance method, so * it must have an instance of BinaryTree, * which complicates the implementation. */ public int getHeight() { if (left == null && right == null) { return 0; } else if (left == null) { return 1 + right.getHeight(); } else if (right == null) { return 1 + left.getHeight(); } else { return 1 + Math.max(left.getHeight(), right.getHeight()); } } /** * Computes the (edge) height. * Note that this is a static method. Since * it can always check that root == null directly, * the implementation matches the simple recursive * height definition shown in class. */ public static int getHeight(BinaryTree root) { if (root == null) { return -1; } else { return 1 + Math.max(getHeight(root.getLeft()), getHeight(root.getRight())); } } }