previous | start | next

The keys() method for TrieST

A String key in a TrieST is not stored in in a single node.

The node structure does not contain a String (or a char) member!

So iterating through the Strings (in alphabetical order) is a bit different.

public Iterable<String> keys()
{
  Queue<String> q = new Queue<String>();
  collect(root, "", q);
  return q;
}

private void collect(Node<V> x, String key, Queue<String> queue) 
{
  if (x == null) return;

  if (x.val != null) {
    queue.enqueue(key);
  }

  for (int c = 0; c < TrieST.R; c++) {
    collect(x.next[c], key + (char) c, queue);
  }
}

Egad! How long does this take to execute?



previous | start | next