📖 Scenario: Imagine you have a family tree represented as a binary tree. You want to see the family members grouped by their vertical positions when you look at the tree from the front.
🎯 Goal: Build a program in C that performs vertical order traversal of a binary tree and prints the nodes grouped by their vertical columns from left to right.
📋 What You'll Learn
Create a binary tree with the exact structure given
Use a variable to track the horizontal distance (column) of nodes
Implement vertical order traversal logic using a queue and a map-like structure
Print the nodes grouped by their vertical columns in order
💡 Why This Matters
🌍 Real World
Vertical order traversal helps visualize hierarchical data like family trees, organizational charts, or layered maps from a vertical perspective.
💼 Career
Understanding tree traversals and queue usage is essential for software engineering roles involving data structures, algorithms, and system design.
Progress0 / 4 steps
1
Create the Binary Tree Structure
Define a struct called Node with int data, Node *left, and Node *right. Then create nodes to build this exact tree:
1 / \ 2 3 / \ \ 4 5 6
DSA C
Hint
Start by defining the Node struct and a helper function newNode to create nodes. Then build the tree exactly as shown.
2
Set Up Data Structures for Vertical Traversal
Create a struct called QueueNode with Node *node and int hd (horizontal distance). Then create a queue using an array of QueueNode with size 100. Also create an integer array columnTable[201] initialized to 0 to store sums of nodes at each horizontal distance shifted by 100.
DSA C
Hint
Create a QueueNode struct to hold a tree node and its horizontal distance. Then create a queue array and an integer array columnTable to store sums indexed by horizontal distance + 100.
3
Implement Vertical Order Traversal Logic
Use a while loop to perform level order traversal with the queue. Enqueue the root with horizontal distance 100. While queue is not empty, dequeue a QueueNode, add its node's data to columnTable[hd]. Enqueue left child with hd - 1 and right child with hd + 1 if they exist.
DSA C
Hint
Start by enqueuing the root with horizontal distance 100. Then use a while loop to dequeue nodes, add their data to columnTable, and enqueue their children with updated horizontal distances.
4
Print the Vertical Order Traversal Result
Use a for loop from i = 0 to 200. If columnTable[i] is not zero, print the value followed by a space. This will print the sum of nodes in each vertical column from left to right.
DSA C
Hint
Loop through columnTable and print all non-zero sums separated by spaces. This shows the vertical order sums from left to right.
Practice
(1/5)
1. What does the vertical order traversal of a binary tree represent?
easy
A. Nodes grouped by their vertical columns from left to right
B. Nodes grouped by their depth levels from top to bottom
C. Nodes visited in preorder traversal order
D. Nodes visited in postorder traversal order
Solution
Step 1: Understand vertical order traversal concept
Vertical order traversal groups nodes by their vertical columns, where the root is at column 0, left child at column -1, right child at column +1, and so on.
Step 2: Compare with other traversal types
Depth levels group nodes horizontally, preorder and postorder are depth-first traversals, not vertical grouping.
Final Answer:
Nodes grouped by their vertical columns from left to right -> Option A
Quick Check:
Vertical order = vertical columns grouping [OK]
Hint: Vertical order groups nodes by columns, not levels or order [OK]
Common Mistakes:
Confusing vertical order with level order traversal
Thinking preorder or postorder is vertical order
Mixing horizontal levels with vertical columns
2. Which data structure is commonly used to store nodes during vertical order traversal?
easy
A. Queue
B. HashMap or Dictionary
C. Stack
D. Linked List
Solution
Step 1: Identify storage needs for vertical order traversal
We need to group nodes by their vertical column index, so a map (like HashMap or Dictionary) is used to store lists of nodes per column.
Step 2: Understand role of other data structures
Queue is used for level order traversal but not for grouping columns; stack and linked list do not directly map columns to nodes.
Final Answer:
HashMap or Dictionary -> Option B
Quick Check:
Use map to group nodes by column [OK]
Hint: Use a map to group nodes by column index [OK]
Common Mistakes:
Using stack instead of map for grouping
Confusing queue usage for traversal with storage
Not grouping nodes by column properly
3. What is the output of vertical order traversal for this tree?
1
/ \
2 3
/ \ \
4 5 6
medium
A. [1, 2, 3, 4, 5, 6]
B. [4, 2, 1, 3, 5, 6]
C. [4, 2, 5, 1, 3, 6]
D. [4, 2, 1, 5, 3, 6]
Solution
Step 1: Assign vertical columns to each node
Root 1 is at column 0; 2 is at -1; 3 is at +1; 4 is at -2; 5 is at 0; 6 is at +2.
Step 2: Group nodes by columns and read left to right