Discrete Math Notes:
Chapter 6: Graphs and Trees
6.10 Tree traversals
Traversal, to process the information stored in the vertices by systematically visiting each vertex.
pre-order traversal, a vertex is visited before its descendants
overview:
Example of Pre-Order Tree Traversal
Qutput The Solar System Inner Solar System Inner Planets Astroid Belt Outer Solar System Quter Planets Centaurs
The Solar System
preorder(v) v := Solar System Printname at v Indent For each child of v preorder(w) Unindent Return
Inner Solar System
Outer Solar Systern
steroic Belt
uter
Centaurs
Returned fron initial call to root Pre-order traversal finished. Outline is complete
Captions ^
1. Preorder(v) is called with v = "The Solar System". v's name is printed. 2. The first child of the current v is "Inner Solar System". Preorder(v) is called with v = "Inner Solar System". 3. v's name is printed. The first child of the current v is "Inner Planets". Preorder(v) is called with v = "Inner Planets"
5. v is now "Inner Solar System" again. Preorder(v) is called with v equal to the next child of "Inner Solar System" which is Asteroid Belt" 6."Asteroid Belt"is printed and Preorder returns because "Asteroid Belt"has no children in the tree. 7. The current v is "Inner Solar System" again. All the leaves of the current v have been visited, so Preorder returns. 8. The current v is the root, "The Solar System", again. The right subtree of the root is traversed and printed. The initial call to the root returns and the pre-order traversal is finished.
Computer applications utilize trees to organize and store information. The processing of the information held in the vertices by systematically visiting each vertex is a frequent procedure performed on a tree (called tree traversal). A processing step is conducted for each vertex visited, which may include a computation or the output of data. A tree, for example, could be used to store the hierarchical structure of a book's sections. The title of the related piece of the book is printed out as each vertex is visited in order to print out the table of contents. A vertex is visited before its descendants in pre-order traversal. A vertex is visited after its descendants in a post-order traversal