General Tree Traversal
For trees in general (not just binary trees), object of traversal is to visit (perform some operation at) each node
Lists had one natural traversal order (first to last)
Trees have several different traversal orders
We will look primarily at depth-first traversal
- In depth-first traversal, once we begin traversal of any subtree of a node, we traverse the entire subtree before beginning traversal of any other subtree of that node
Breadth-first is another possible traversal order (see exercise E13, p. 395)
- In breadth-first traversal, we traverse all nodes at a given level before traversing any nodes at the next level
- To do this, we use a queue of nodes
- This is more difficult, but useful in many applications
- Game playing programs (chess, for example) often use this method to traverse a tree of possible moves