previous | start | next

Final Java Version of Iterable<Key> keys()

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

  Iterable<Key> keys()
  {
    Queue<Key> keylst = new Queue<Key>();
    addKeys(root, keylst);
    return keylst;
  }

  /**
   * adds the keys in subtree t in order to the end of lst
   * @param t the subtree
   * @param lst list of keys smaller than the keys in t.
   */

  private void addKeys(Node t, Queue<Key> lst)
  {
    if (t == null) return lst;
    addKeys(t.left, lst);
    lst.add(t.key);
    addKeys(t.right, lst);
  }

}


previous | start | next