Overview
set
method (after exercise 4)Problem: How do we do depth-first traversal that is not preorder.
Do depth-first right-to-left postorder: push key/value, push left, push right
public boolean hasNext() {
return !stack.isEmpty();
} // hasNext()
public Pair<K,V> next() {
while (true) {
Object o = stack.pop();
if (o instanceof Node<?,?>) {
@UnsafeCastOrSomethingLikeThat
Node<K,V> node = (Node<K,V>) o;
push o.pair;
if (o.right != null) { push o.right; }
if (o.left != null) { push o.left; }
} // if
else {
return (Pair<K,V>) o;
}
} // while
} // next()
Writeup: Corrected set
method (probably after exercise 4).