// tag::calculateDepth[] public static int calculateDepth(BinaryTreeNode<int> t, int n) { int depth = 0; if (t.getValue () == n) return depth; if (n < t.getValue () && t.getLeft () != null) return 1 + calculateDepth (t.getLeft (), n); if (n > t.getValue () && t.getRight () != null) return 1 + calculateDepth (t.getRight (), n); throw new TreeException ("Value not found in tree!"); }
private static BinaryTreeNode<int> getChildNode( BinaryTreeNode<int> t, int n) { if (n < t.getValue()) { return t.getLeft(); } else { return t.getRight(); } }
// tag::calculateDepth[] public static int calculateDepth(BinaryTreeNode<int> t, int n) { int depth = 0; if (t.getValue () == n) { return depth; } else { if (n < t.getValue ()) { BinaryTreeNode<int> left = t.getLeft (); if (left == null) { throw new TreeException ("Value not found in tree!"); } else { return 1 + calculateDepth (left, n); } } else { BinaryTreeNode<int> right = t.getRight (); if (right == null) { throw new TreeException ("Value not found in tree!"); } else { return 1 + calculateDepth (right, n); } } } }