In-Order Traversal

In-Order Traversal

Key Takeaways: In-Order Traversal is a Depth-First Search (DFS) algorithm for trees. The visiting order is strictly: Left, Root, Right. When used on a Binary Search Tree, it retrieves the data in perfectly sorted order!

In the previous tutorial, we explored Pre-Order Traversal.

Now, we will look at the second major Depth-First Search technique: In-Order Traversal.

This is perhaps the most famous of all tree traversals because of its special mathematical properties when dealing with sorted data.


1. What is In-Order Traversal?

Tree traversal means visiting every single node in the tree exactly one time.

Because trees are hierarchical, we need a specific, repeatable path to follow.

In-Order Traversal follows this strict path:

  1. Left: First, recursively visit the entire Left subtree.
  2. Root: Second, visit the current node (the Root of the subtree).
  3. Right: Third, recursively visit the entire Right subtree.

The prefix "In" helps you remember that the Root is visited in the middle of the left and right children!

Visual representation of In-Order Traversal

In-Order Traversal: Notice how the numbering moves from the bottom-left, up to the root, and down to the right.


2. A Step-by-Step Example

Let's look at the tree in the diagram above and trace the exact execution path.

Assume we have a Binary Search Tree with a Root of 10, a Left Child of 5, and a Right Child of 15. The 5 has its own children: 2 (Left) and 7 (Right). The 15 has its own children: 12 (Left) and 20 (Right).

Step 1: The Descent

Step 2: Processing the Leftmost Node

Step 3: Moving Up

Step 4: Processing the Inner Leaves

Step 5: The Main Root

Step 6: The Right Subtree

Final Output: 2, 5, 7, 10, 12, 15, 20

(Notice how the final output is in perfectly sorted, ascending order!)


3. Why Use In-Order Traversal?

Every traversal has a specific superpower. What is In-Order good for?

1. Sorting Data (Binary Search Trees) If you use In-Order Traversal on a Binary Search Tree (BST), it will magically print out all the values in perfectly sorted, ascending order. This is its absolute most common use case in software engineering!

2. Flattening Trees If you want to convert a Binary Search Tree into a sorted 1D Array or Linked List, you just run an In-Order traversal and push the printed values into your array.

3. Infix Notation In mathematical expression trees, In-Order traversal generates standard human-readable math equations (like A + B).


4. Code Example (Python)

Let's write the code for In-Order Traversal.

Just like Pre-Order, the code relies heavily on Recursion. The only difference is where we place the print() statement!

In-Order Traversal Implementation

# 1. Create the Node Class
class Node:
    def __init__(self, data):
        self.left = None
        self.right = None
        self.data = data

# 2. Define the In-Order function def inorder_traversal(node): # Base case: If the node is None, stop! if node is not None: # 1. Traverse the Left Subtree inorder_traversal(node.left) # 2. Visit the Root (Print it) print(node.data, end=" ") # 3. Traverse the Right Subtree inorder_traversal(node.right)

# 3. Build the Binary Search Tree root = Node(10) root.left = Node(5) root.right = Node(15)

root.left.left = Node(2) root.left.right = Node(7)

root.right.left = Node(12) root.right.right = Node(20)

# 4. Run the Algorithm print("In-Order Output:") inorder_traversal(root)


5. Time and Space Complexity


Exercise 1 of 2

?

What is the exact execution order for an In-Order Traversal?

Exercise 2 of 2

?

What happens if you run an In-Order Traversal on a valid Binary Search Tree (BST)?