Your code is very hard to read . Not only are the names of the methods non-descriptive , but also , there aren't any comments for them. Also , your assignment speaks to implement recursion where-ever possible , but i dont see you doing that. Recursion is bad for a lot of things , but it really makes things simple when it comes to trees. As for example , in your printInOder()
method , if you implemented that using recursion , here is what it can be changed to :
public Iterable<Key> keys(){
Queue<Key> q = new Queue<Key>(); // queue implements iterable , so contract is valid
inorder(root, q); // call the recursion
return q
}
private void inorder(Node x, Queue<Key> q) {
if (x == null)
return; // will signify the end of recursion , all the stacks starts
// returning hereonwards
inorder(x.left, q);
q.enqueue(x.key); // as stacks start evaluating , nodes will be put into
// the queue starting from the left most leaf , ie , the
// smallest node , ending with the root
inorder(x.right, q); // and now enqueue all the nodes to the right
}
So all you do is call print on keys()
and it takes care of all the traversing and printing. Resulting in cleaner , better , lesser (amount of) inspection needed when something goes wrong. like now.
( comments are what i understand of the code , i can be wrong though. )
Also , reagrding your …