BST Iterators: Overview
The Problem: Iterator-defined traversal should encounter elements in order
40 20 50
/ \ / \ / \
20 60 10 50 30 60
/ \ / \ / \ / \ \
10 30 50 70 40 70 20 40 70
/ / /
30 60 10
Output should be linear regardless of internal structure: 10 20 30 40 50 60 70
Possible Strategies:
- Navigation: (aka "tree monkey") Add a parent pointer to each node, and use
the three pointers to navigate ("climb") around in the tree
- Threading: Use the null pointers in a tree to store "next node"
information when it cannot be computed by descending in the tree
- ADT Control: Use a Stack or Queue to keep track of where an iterator is in a
tree
- Graph Algorithms: Use a standard Graph Depth-First Iterator or Breadth-First
Iterator, applied specifically to the tree considered as a directed graph.
|