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.
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:
The prefix "In" helps you remember that the Root is visited in the middle of the left and right children!
In-Order Traversal: Notice how the numbering moves from the bottom-left, up to the root, and down to the right.
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
2.Step 3: Moving Up
5.Step 4: Processing the Inner Leaves
7.Step 5: The Main Root
10.Step 6: The Right Subtree
12. No Right child. Move up to 15.15.20. No Right. Move up.Final Output: 2, 5, 7, 10, 12, 15, 20
(Notice how the final output is in perfectly sorted, ascending order!)
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).
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!
# 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)
O(n). Every single node in the tree is visited exactly once. Therefore, the time it takes scales linearly with the total number of nodes (n) in the tree.O(h). Where h is the maximum height of the tree. This space is required by the computer's Call Stack to remember where it was during the recursive calls. log n, making the space complexity O(log n). n, making it O(n) space.What is the exact execution order for an In-Order Traversal?
What happens if you run an In-Order Traversal on a valid Binary Search Tree (BST)?