Introduction To Computer Science for Majors 2
Stuart C. Shapiro
Binary Tree Iterators
Riley Chapter 9
Preorder and inorder iterators may be implemented for recursive
binary trees with the help of a stack.
Example Application: equalFringe
equalFringe(tree1, tree2) should return
true iff the two trees have equal leaves, when traversing
the leaves left-to-right.
We do not want to gather the two sets of leaves, and then compare
them, because we want to stop as soon as different leaves are found.
Yet this method cannot be written by recursing down the two trees in
parallel because they have different structures. So parallel
iterators must be used.