Tree Traversal Techniques

COS2611 - Programming: Data Structures · Trees

Tree Traversal Techniques

Tree traversal refers to the process of visiting each node in a tree data structure in a specific order. Understanding tree traversal is crucial for performing operations on trees, such as searching, inserting, or deleting nodes. In this section, we will explore three main types of tree traversal techniques: pre-order, in-order, and post-order traversal.

Pre-order Traversal

In pre-order traversal, each node is processed before (pre) its child nodes. The order of operations is as follows:

  1. Visit the root node.
  2. Traverse the left subtree in pre-order.
  3. Traverse the right subtree in pre-order.

This means that for each node, you first visit the node itself and then recursively visit its left and right children.

Here is a step-by-step example of pre-order traversal:

      A
     / \
    B   C
   / \   \
  D   E   F

1. Start at the root node (A). Visit A.

2. Move to the left child (B). Visit B.

3. Move to the left child of B (D). Visit D. D has no children, so backtrack to B.

4. Now move to the right child of B (E). Visit E. E has no children, so backtrack to A.

5. Move to the right child of A (C). Visit C.

6. Move to the right child of C (F). Visit F. F has no children.

The result of the pre-order traversal is: A, B, D, E, C, F.

Remember: In pre-order traversal, the root is visited first.

In-order Traversal

In-order traversal processes nodes in a left-root-right order. The order of operations is as follows:

  1. Traverse the left subtree in in-order.
  2. Visit the root node.
  3. Traverse the right subtree in in-order.

This traversal method is particularly useful for binary search trees because it visits nodes in ascending order.

Using the same tree as before, we will perform in-order traversal:

      A
     / \
    B   C
   / \   \
  D   E   F

1. Start at the root node (A). Move to the left child (B).

2. Move to the left child of B (D). Visit D. D has no children, so backtrack to B.

3. Visit B.

4. Move to the right child of B (E). Visit E. E has no children, so backtrack to A.

5. Visit A.

6. Move to the right child of A (C). Visit C.

7. Move to the right child of C (F). Visit F. F has no children.

The result of the in-order traversal is: D, B, E, A, C, F.

Remember: In in-order traversal, the left child is visited before the root.

Post-order Traversal

In post-order traversal, each node is processed after (post) its child nodes. The order of operations is as follows:

  1. Traverse the left subtree in post-order.
  2. Traverse the right subtree in post-order.
  3. Visit the root node.

This method is useful for deleting trees or freeing memory, as it ensures that child nodes are processed before their parent nodes.

Let us perform post-order traversal on the same tree:

      A
     / \
    B   C
   / \   \
  D   E   F

1. Start at the root node (A). Move to the left child (B).

2. Move to the left child of B (D). Visit D. D has no children.

3. Move to the right child of B (E). Visit E. E has no children.

4. Visit B.

5. Move to the right child of A (C). Move to the right child of C (F). Visit F. F has no children.

6. Visit C.

7. Finally, visit A.

The result of the post-order traversal is: D, E, B, F, C, A.

Remember: In post-order traversal, the root is visited last.

Summary of Traversal Techniques

To summarise, here are the key points about tree traversal techniques:

  • Pre-order: Visit root, traverse left, traverse right.
  • In-order: Traverse left, visit root, traverse right.
  • Post-order: Traverse left, traverse right, visit root.

Self-Check Questions

  1. What is the order of visiting nodes in pre-order traversal?
  2. How does in-order traversal differ from post-order traversal?
  3. In which situation would you prefer post-order traversal?
  4. What is the output of in-order traversal for a binary search tree?