Traversing Trees with Iterators

Steven J. Zeil

Old Dominion University, Dept. of Computer Science

Table of Contents

1. Iterating over Trees
1.1. begin()
1.2. end()
1.3. end() [cont.]
1.4. operator++
1.5. operator++ [cont.]
2. Iterators using Parent Pointers
2.1. Basic operations
2.2. begin() and end()
2.3. operator++ (1/7)
2.4. operator++ (2/7)
2.5. operator++ (3/7)
2.6. operator++ (4/7)
2.7. operator++ (5/7)
2.8. operator++ (6/7)
2.9. operator++ (7/7)
3. Threads

The recursive traversal algorithms work well for implementing tree-based ADT member functions, but if we are trying to hide the trees inside some ADT (e.g., std::set), we may need to provide iterators for walking though the contents of the tree.

Iterators for tree-based data structures can be more complicated than those for linear structures.

For arrays (and vectors and deques and other array-like structures) and linked lists, a single pointer can implement an iterator:

In the Forum:

(no threads at this time)