1. Overview

In Computer Science, linear data structures can be traversed in only one logical way. However, a tree data structure can be traversed in several different ways.

In this tutorial, we’ll discuss various ways to traverse a tree and the time complexity of such an operation.

2. What Is Tree Traversal?

Tree traversal is the process of visiting each node in a tree exactly once. Now from a current node, there might be more than one node we can reach. Therefore, it’s possible to have several different orders in which the nodes are visited. Let’s take an example:

zffddf

The nodes of this tree can be traversed in many different ways. Some traversal orders are:

  • Order 1: A \rightarrow B \rightarrow C \rightarrow D \rightarrow E \rightarrow F
  • Order 2: A \rightarrow B \rightarrow D \rightarrow E \rightarrow C \rightarrow F
  • Order 3: D \rightarrow E \rightarrow F \rightarrow B \rightarrow C \rightarrow A

Hence depending on the nodes being visited, the traversal is classified into two categories: depth-first traversal and level order or breadth-first traversal.

The depth-first traversal can be further divided into three types of traversal: in-order, pre-order, and post-order traversal.

3. Tree Traversals Techniques

According to in-order traversal, we visit the nodes in the left subtree first, followed by the root node and the nodes in the right subtree. We first search for the left leaf node of the left subtree to start the in-order traversal.

In pre-order traversal, we visit the root node first, then the left subtree, and finally the right subtree.

In the case of post-order traversal, we visit the left subtree, then the right subtree, and finally the root node.

Finally, for level-order or breadth-first traversal, we visit all the nodes in a tree level by level fashion. We start with the root node and goes to the next level to visit the nodes.

4. Example

Now we’re taking an example tree, and let’s find the various traversals of the tree:

zffddf-1

If we apply in-order traversal in this example tree, the in-order traversal of this tree would be G \rightarrow D \rightarrow H \rightarrow B \rightarrow E \rightarrow A \rightarrow C \rightarrow F

In the case of pre-order traversal, we visit the root node first. Hence the pre-order traversal of this tree would be: A \rightarrow B \rightarrow D \rightarrow G \rightarrow H \rightarrow E \rightarrow C \rightarrow F

In the post-order traversal, we traverse the root node at the end. Therefore the post-order traversal of this tree would be: G \rightarrow H \rightarrow D \rightarrow E \rightarrow B \rightarrow F \rightarrow C \rightarrow A

The level-order traversal of the example tree would be: A \rightarrow B \rightarrow C \rightarrow D \rightarrow E \rightarrow F \rightarrow G \rightarrow H

5. Time Complexity of the Tree Traversals

In the general case, all the traversal algorithms visit each node in a tree exactly once. Therefore the time complexity of all the traversal algorithms would be \mathcal{O}(N) when a tree contains N nodes.

In order to verify the overall time complexity, we’re taking a corner case, and we’re going to find the time complexity to visit all the nodes. If a tree has N nodes, then the time complexity T(N) of the tree can be defined as:

    [T(N) = T(L) + (N - L - 1) + C]

L is the number of nodes on the left side of the tree, and C denotes a constant time. Now let’s assume that the given tree is a right-skewed tree. In the case of a right-skewed tree, the left of the tree will be empty. So L = 0:

    [T(N) = T(0) + (N - 1) + C]

    [T(N) = 2T(0) + T(N-2) + 2C]

    [T(N) = 3T(0) + T(N-3) + 3C]

    [T(N) = 4T(0) + T(N-4) + 4C]

If we continue, we’ll get:

    [T(N) = (N-1)T(0) + T(1) + (N-1)C]

    [T(N) = NT(0) + (N)C]

T(0) denotes traversing an empty tree which takes constant time:

    [T(N) = NT(0) + (N)C = NC + NC = 2NC = \mathcal{O}(N)]

Therefore, we verified that all the tree traversal algorithms take \mathbf{\mathcal{O}(N)}} time.

6. Conclusion

In this tutorial, we learned several ways to traverse a tree. We presented algorithms for each traversal and demonstrated with an example. Finally, we analyzed the time complexity of the traversal algorithms.