Question: Implement the following method in BST.java that returns the keys in level order (in order of their distance from the root, with nodes on each
Implement the following method in BST.java that returns the keys in level order (in order of their distance from the root, with nodes on each level in order from left to right). Hint: Study keys(..) method in term of how to return an iterable instance. Use a Queue for traversal. public Iterable KeysInLevelOrder()
public Iterable
private void keys(Node node, Queue
int cmplo = lo.compareTo(node.key); int cmphi = hi.compareTo(node.key); if (cmplo < 0) // if lo < node.key, visit left subtree keys(node.left, queue, lo, hi); if (cmplo <= 0 && cmphi >= 0) // if node.key within range, add to the queue queue.add(node.key); if (cmphi > 0) // if hi > node.key, visit right subtree keys(node.right, queue, lo, hi); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
