The Department of Computer Science & Engineering
cse@buffalo
STUART C. SHAPIRO: CSE 116 B

CSE 116
Introduction To Computer Science for Majors 2
Lecture B
Lecture Notes
Stuart C. Shapiro
Spring, 2003


Binary Tree Iterators

Readings
Riley Chapter 9

Preorder and inorder iterators may be implemented for recursive binary trees with the help of a stack.
See IteratedBinaryTree.java

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.

First Previous Next

Copyright © 2003 by Stuart C. Shapiro. All rights reserved.
Trees drawn by dot

Stuart C. Shapiro <shapiro@cse.buffalo.edu>