PlanetMath (more info)
 Math for the people, by the people.
Encyclopedia | Requests | Forums | Docs | Wiki | Random | RSS  
Login
create new user
name:
pass:
forget your password?
Main Menu
Owner confidence rating: High Entry average rating: No information on entry rating
tree traversals (Algorithm)

A tree traversal is an algorithm for visiting all the nodes in a rooted tree exactly once. The constraint is on rooted trees, because the root is taken to be the starting point of the traversal. A traversal is also defined on a forest in the sense that each tree in the forest can be iteratively traversed (provided one knows the roots of every tree beforehand). This entry presents a few common and simple tree traversals.

In the description of a tree, the notion of rooted-subtrees was presented. Full understanding of this notion is necessary to understand the traversals presented here, as each of these traversals depends heavily upon this notion.

In a traversal, there is the notion of visiting a node. Visiting a node often consists of doing some computation with that node. The traversals are defined here without any notion of what is being done to visit a node, and simply indicate where the visit occurs (and most importantly, in what order).

Examples of each traversal will be illustrated on the following binary tree.

$\displaystyle \begin{xy} *!C\xybox{ \xymatrix{ &&&\bullet \ar@{-}[dll] \ar@{-}[... ...r@{-}[dl] \ar@{-}[dr] \ \bullet && \bullet && \bullet && \bullet } } \end{xy}$

Vertices will be numbered in the order they are visited, and edges will be drawn with arrows indicating the path of the traversal.

Preorder Traversal

Given a rooted tree, a preorder traversal consists of first visiting the root, and then executing a preorder traversal on each of the root's children (if any).

For example

$\displaystyle \begin{xy} *!C\xybox{ \xymatrix{ &&&1 \ar@/^/[dll]^a \ar@/_/[drr]... ...dr] \ 3 \ar@/^/[ur]^c && 4 \ar@/_/[ul]_e && 6 \ar@/^/[ur]^i && 7 } } \end{xy}$

The term preorder refers to the fact that a node is visited before any of its descendents. A preorder traversal is defined for any rooted tree. As pseudocode, the preorder traversal is


\begin{program} \mathrm{PreorderTraversal}(x,\mathrm{Visit}) \text{{\bf Input}: ... ...it}) \mathrm{PreorderTraversal}(\mathrm{right}(x), \mathrm{Visit}) \end{program}

Postorder Traversal

Given a rooted tree, a postorder traversal consists of first executing a postorder traversal on each of the root's children (if any), and then visiting the root.

For example

$\displaystyle \begin{xy} *!C\xybox{ \xymatrix{ &&&7 \ar@/^/[dll]^a \ar@/_/[drr]... ...dr] \ 1 \ar@/^/[ur]^c && 2 \ar@/_/[ul]_e && 4 \ar@/^/[ur]^i && 5 } } \end{xy}$

As with the preorder traversal, the term postorder here refers to the fact that a node is visited after all of its descendents. A postorder traversal is defined for any rooted tree. As pseudocode, the postorder traversal is


\begin{program} \mathrm{PostorderTraversal}(x,\mathrm{Visit}) \text{{\bf Input}:... ...derTraversal}(\mathrm{right}(x), \mathrm{Visit}) \mathrm{Visit}(x) \end{program}

In-order Traversal

Given a binary tree, an in-order traversal consists of executing an in-order traversal on the root's left child (if present), then visiting the root, then executing an in-order traversal on the root's right child (if present). Thus all of a root's left descendents are visited before the root, and the root is visited before any of its right descendents.

For example

$\displaystyle \begin{xy} *!C\xybox{ \xymatrix{ &&&4 \ar@/^/[dll]^a \ar@/_/[drr]... ...dr] \ 1 \ar@/^/[ur]^c && 3 \ar@/_/[ul]_e && 5 \ar@/^/[ur]^i && 7 } } \end{xy}$

As can be seen, the in-order traversal has the wonderful property of traversing a tree from left to right (if the tree is visualized as it has been drawn here). The term in-order comes from the fact that an in-order traversal of a binary search tree visits the data associated with the nodes in sorted order. As pseudocode, the in-order traversal is


\begin{program} \mathrm{InOrderTraversal}(x,\mathrm{Visit}) \text{{\bf Input}: A... ...t}(x) \mathrm{InOrderTraversal}(\mathrm{right}(x), \mathrm{Visit}) \end{program}



Anyone with an account can edit this entry. Please help improve it!

"tree traversals" is owned by mps. [ full author list (4) | owner history (3) ]
(view preamble)

View style:

See Also: tree

Other names:  inorder traversal
Also defines:  preorder traversal, postorder traversal, in-order traversal
Log in to rate this entry.
(view current ratings)

Cross-references: binary search tree, right descendents, left descendents, right child, left child, nodes, children, path, edges, vertices, binary tree, necessary, tree, forest, point, root, rooted tree, algorithm
There are 3 references to this entry.

This is version 7 of tree traversals, born on 2002-02-25, modified 2006-07-24.
Object id is 2699, canonical name is TreeTraversals.
Accessed 19357 times total.

Classification:
AMS MSC05C05 (Combinatorics :: Graph theory :: Trees)

Pending Errata and Addenda
None.
[ View all 3 ]
Discussion
Style: Expand: Order:
forum policy

No messages.

Interact
post | correct | update request | add example | add (any)