You are on page 1of 2

# (depth-first) Tree Traversals

When performing a depth-first tree traversal, it is clear in what order the leaves are to be visited, namely left to right. In contrast there are several choices as to when to visit an interior (i.e. non-leaf) node. The traversal can visit an interior node Before visiting any 1. of its children. 2. Between visiting its children. 3. After visiting all of its children. I do not like the book's pseudocode as I feel the names chosen confuse the traversal with visiting the nodes. I prefer the pseudocode below, which uses the following conventions. Comments are introduced by -- and terminate at the end of the line (as in the programming language Ada). Indenting is significant so begin/end or {} are not used (from the programming language family B2/ABC/Python)
traverse (n : treeNode) if leaf(n) -- visit leaves once; base of recursion visit(n) else -- interior node, at least 1 child -- visit(n) -- visit node PRE visiting any children traverse(first child) -- recursive call while (more children remain) -- excluding first child -- visit(n) -- visit node IN-between visiting children traverse (next child) -- recursive call -- visit(n) -- visit node POST visiting all children

Note the following properties If you uncomment just the first (interior node) visit, you get a preorder traversal, in which each node is visited before (i.e., pre) visiting any of its children. 1. If you uncomment just the last visit, you get a postorder traversal, in which each node is visited after (i.e., post) visiting all of its children. 2. If you uncomment only the middle visit, you get an inorder traversal, in which the node is visited (in-) between visiting its children. Inorder traversals are normally defined only for binary trees, i.e., trees in which every interior node has exactly two children. Although the code with only the middle visit uncommented works for any tree, we will, like everyone else, reserve the name inorder traversal for binary trees. In the case of binary search trees (everything in the left subtree is smaller than the root of that subtree, which in tern is smaller than everything in the corresponding right subtree) an inorder traversal prints the values of the nodes in (numerical) order. 3. 4. If you uncomment two of the three visits, you get a traversal without a name. If you uncomment none of the three visits, you get a program that simply prints the leaves in the natural (depth-first) order. 5. If you uncomment all of the three visits, you get an Euler-tour traversal. To explain the name Euler-tour traversal, recall that an Eulerian tour on a directed graph is one that traverses each edge once. If we view the tree on the right as undirected and replace each edge with two arcs, one in each direction, we see that the pink curve is indeed an Eulerian tour. It is easy to see that the

curve visits the nodes in the order of the pseudocode (with all visits uncommented). Normally, the Euler-tour traversal is defined only for a binary tree, but this time I will differ from convention and use the pseudocode above to define it for all trees. Note the following points. A node with k children is visited k+1 times. The diagram shows nodes with 0, 1, 2, and 3 children. In a binary tree, a leaf is visited once and an interior node is visited three times. This is one of the standard definitions of an Euler-tour traversal for a binary tree. The other standard definition has all nodes visited 3 times. For a leaf the three visits are in succession. Modifying the pseudocode to obtain this definition simply requires replacing the leaf visit with
visit(n); visit(n); visit(n)

Do the Euler-tour traversal for the tree in the notes and then for a binary tree.