For an assignment, I need to create an iterable that contains all keys for a symbol table backed by a binary search tree. I'm familiar with how to do this for a linked list, but can't seem to find any examples online about how to do this for a BST. For a linked list, for example, I'd use something like this:
public Iterable<Key> keys() {
Queue<Key> queue = new Queue<Key>();
for (Node x = first; x != null; x = x.next)
queue.enqueue(x.key);
return queue;
}
But I'm not quite sure how to convert that so it holds all keys for my BST. Can someone provide guidance or a link to a source that covers this topic?
If you want to traverse left first, you can implement it using a stack.
and
and
output: