Tree traversal


In computer science, tree traversal is a form of graph traversal and refers to the process of visiting each node in a tree data structure, exactly once. Such traversals are classified by the order in which the nodes are visited. The following algorithms are described for a binary tree, but they may be generalized to other trees as well.

Types

Unlike linked lists, one-dimensional arrays and other linear data structures, which are canonically traversed in linear order, trees may be traversed in multiple ways. They may be traversed in depth-first or breadth-first order. There are three common ways to traverse them in depth-first order: in-order, pre-order and post-order. Beyond these basic traversals, various more complex or hybrid schemes are possible, such as depth-limited searches like iterative deepening depth-first search. The latter, as well as breadth-first search, can also be used to traverse infinite trees, see [|below].

Data structures for tree traversal

Traversing a tree involves iterating over all nodes in some manner. Because from a given node there is more than one possible next node, then, assuming sequential computation, some nodes must be deferred—stored in some way for later visiting. This is often done via a stack or queue. As a tree is a self-referential data structure, traversal can be defined by recursion or, more subtly, corecursion, in a natural and clear fashion; in these cases the deferred nodes are stored implicitly in the call stack.
Depth-first search is easily implemented via a stack, including recursively, while breadth-first search is easily implemented via a queue, including corecursively.

Depth-first search

In depth-first search, the search tree is deepened as much as possible before going to the next sibling.
To traverse binary trees with depth-first search, perform the following operations at each node:
  1. If the current node is empty then return.
  2. Execute the following three operations in a certain order:
  3. : N: Visit the current node.
  4. : L: Recursively traverse the current node's left subtree.
  5. : R: Recursively traverse the current node's right subtree.
The trace of a traversal is called a sequentialisation of the tree. The traversal trace is a list of each visited node. No one sequentialisation according to pre-, in- or post-order describes the underlying tree uniquely. Given a tree with distinct elements, either pre-order or post-order paired with in-order is sufficient to describe the tree uniquely. However, pre-order with post-order leaves some ambiguity in the tree structure.
There are three methods at which position of the traversal relative to the node the visit of the node shall take place. The choice of exactly one color determines exactly one visit of a node as described below. Visit at all three colors results in a threefold visit of the same node yielding the “all-order” sequentialisation:

Pre-order, NLR

  1. Visit the current node.
  2. Recursively traverse the current node's left subtree.
  3. Recursively traverse the current node's right subtree.
The pre-order traversal is a topologically sorted one, because a parent node is processed before any of its child nodes is done.

Post-order, LRN

  1. Recursively traverse the current node's left subtree.
  2. Recursively traverse the current node's right subtree.
  3. Visit the current node.
Post-order traversal can be useful to get postfix expression of a binary expression tree.

In-order, LNR

  1. Recursively traverse the current node's left subtree.
  2. Visit the current node.
  3. Recursively traverse the current node's right subtree.
In a binary search tree ordered such that in each node the key is greater than all keys in its left subtree and less than all keys in its right subtree, in-order traversal retrieves the keys in ascending sorted order.

Reverse pre-order, NRL

  1. Visit the current node.
  2. Recursively traverse the current node's right subtree.
  3. Recursively traverse the current node's left subtree.

    Reverse post-order, RLN

  4. Recursively traverse the current node's right subtree.
  5. Recursively traverse the current node's left subtree.
  6. Visit the current node.

    Reverse in-order, RNL

  7. Recursively traverse the current node's right subtree.
  8. Visit the current node.
  9. Recursively traverse the current node's left subtree.
In a binary search tree ordered such that in each node the key is greater than all keys in its left subtree and less than all keys in its right subtree, reverse in-order traversal retrieves the keys in descending sorted order.

Arbitrary trees

To traverse arbitrary trees with depth-first search, perform the following operations at each node:
  1. If the current node is empty then return.
  2. Visit the current node for pre-order traversal.
  3. For each i from 1 to the current node's number of subtrees − 1, or from the latter to the former for reverse traversal, do:
  4. # Recursively traverse the current node's i-th subtree.
  5. # Visit the current node for in-order traversal.
  6. Recursively traverse the current node's last subtree.
  7. Visit the current node for post-order traversal.
Depending on the problem at hand, pre-order, post-order, and especially one of the number of subtrees − 1 in-order operations may be optional. Also, in practice more than one of pre-order, post-order, and in-order operations may be required. For example, when inserting into a ternary tree, a pre-order operation is performed by comparing items. A post-order operation may be needed afterwards to re-balance the tree.

Breadth-first search

In breadth-first search or level-order search, the search tree is broadened as much as possible before going to the next depth.

Other types

There are also tree traversal algorithms that classify as neither depth-first search nor breadth-first search. One such algorithm is Monte Carlo tree search, which concentrates on analyzing the most promising moves, basing the expansion of the search tree on random sampling of the search space.

Applications

Pre-order traversal can be used to make a prefix expression from expression trees: traverse the expression tree pre-orderly. For example, traversing the depicted arithmetic expression in pre-order yields "+ * AB ''C + D'' E". In prefix notation, there is no need for any parentheses as long as each operator has a fixed number of operands. Pre-order traversal is also used to create a copy of the tree.
Post-order traversal can generate a postfix representation of a binary tree. Traversing the depicted arithmetic expression in post-order yields "A ''B C'' − * D ''E'' + +"; the latter can easily be transformed into machine code to evaluate the expression by a stack machine. Post-order traversal is also used to delete the tree. Each node is freed after freeing its children.
In-order traversal is very commonly used on binary search trees because it returns values from the underlying set in order, according to the comparator that set up the binary search tree.

Implementations

Depth-first search implementation

Below are examples of stack-based implementation for pre-order, post-order and in-order traversal in recursive approach as well as iterative approach.
Implementations in iterative approach are able to avoid the drawbacks of recursion, particularly limitations of stack space and performance issues.
Several alternative implementations are also mentioned.

Pre-order implementation

Post-order implementation

In-order implementation

Another variant of pre-order

If the tree is represented by an array, it is possible to calculate the index of the next element:
procedure bubbleUp
k ← 1
i ← /2
while % ≠ k
i ← /2
k ← 2 * k
return i
procedure preorder
i ← 0
while i ≠ array.size
visit
if i = size - 1
i ← size
else if i < size/2
i ← i * 2 + 1
else
leaf ← i - size/2
parent ← bubble_up
i ← parent * 2 + 2

Advancing to the next or previous node

The node to be started with may have been found in the binary search tree bst by means of a standard search function, which is shown here in an implementation without parent pointers, i.e. it uses a stack for holding the ancestor pointers.
procedure search
// returns a
node ← bst.root
stack ← empty stack
while node ≠ null
stack.push
if key = node.key
return
if key < node.key
node ← node.left
else
node ← node.right
return
The function inorderNext returns an in-order-neighbor of node, either the or the , and the updated stack, so that the binary search tree may be sequentially in-order-traversed and searched in the given direction dir further on.
procedure inorderNext
newnode ← node.child
if newnode ≠ null
do
node ← newnode
stack.push
newnode ← node.child
until newnode = null
return
// node does not have a dir-child:
do
if stack.isEmpty
return
oldnode ← node
node ← stack.pop // parent of oldnode
until oldnode ≠ node.child
// now oldnode = node.child,
// i.e. node = ancestor of original node
return
Note that the function does not use keys, which means that the sequential structure is completely recorded by the binary search tree’s edges. For traversals without change of direction, the average complexity is because a full traversal takes steps for a BST of size 1 step for edge up and 1 for edge down. The worst-case complexity is with as the height of the tree.
All the above implementations require stack space proportional to the height of the tree which is a call stack for the recursive and a parent stack for the iterative ones. In a poorly balanced tree, this can be considerable. With the iterative implementations we can remove the stack requirement by maintaining parent pointers in each node, or by [|threading the tree].