previous | start | next

Java Code outline for Key floor(Key k)

public class BST<Key extends Comparable<Key>, Value>
{

  public Key floor(Key k)
  {
   
    return floor(k, null, root);
  }

  /**
   * returns the floor of k
   * @param k the search key
   * @param candidate largest key in the tree but not in subtree t 
   * that is less than k (might be null)
   * @param t subtree that contains the floor of k or that is empty if
   * the floor of k is candidate
   *
   * @return the floor of k in the subtree t or candidate if 
   * k is larger than all keys in subtree t.
   */
  private Key floor(Key k, Key candidate, Node t)
  {

    /*
     * (Base case) If t is null, floor is candidate
     * if k is equal to t.key, floor is just k.
     * if k is smaller than t.key, then candidate doesn't change,
     *    but t should be updated to be the left subtree, t.left.
     * if k is larger than t.key, then candidate should be updated
     *    to be t.key and t should be updated to be the right subtree, t.right.
     */
  }

}


previous | start | next