Height
by Isai Damier

/****************************************************************
 * Author: Isai Damier
 * Title: Height
 * Project: geekviewpoint
 * Package: algorithms
 *
 * Time Complexity of Solution:
 *   Best = Average = Worst = O(n).
 *
 * Description: Calculate the height of the given tree. This
 *    method returns the size of the longest path from the root
 *    to a leaf.
 *
 * Alternate Algorithm: If built-in function is acceptable,
 *    then the recursive portion of the algorithm may be as
 *    below:
 *     private int height(Node n) {
 *      if(null == n) return 0;
 *      return 1 + Math.max(height(n.left), height(n.right));
 *    }
 *
 ***************************************************************/ 
 public int height() {
  return height(root);
}//height

private int height(Node n) {
  if (null == n) {
    return 0;
  }
  int l = height(n.left);
  int r = height(n.right);
  return 1 + (l > r ? l : r);//highest subtree plus self
}
import org.junit.Test;
import static org.junit.Assert.*;

public class BSTTest {

/**
   * Test of height method, of class BST.
   */
  @Test
  public void testHeight() {
    System.out.println(""height"");
    BST bst = new BST();
    int[] treeTape = {200, 100, 300, 50, 150, 250, 350, 25, 75, 125,
      175, 225, 275, 325, 375, 35, 212, 312, 400};
    assertEquals(0, bst.height());
    //set expectation
    int expected = 5;//draw the tree on paper

    //load data into tree
    for (int i : treeTape) {
      bst.add(i);
    }
    assertEquals(treeTape.length, bst.size());//has correct size

    //test height
    assertEquals(expected, bst.height());
    assertEquals(treeTape.length, bst.size());//has correct size
  }
}