Vertical Traversal of a Binary tree
Vertical Traversal of a Binary Tree involves traversing the tree vertically from top to bottom, considering horizontal distances. Nodes that are at the same horizontal distance are grouped together, and their values are printed in order. This traversal helps us visualize the structure of the binary tree based on the horizontal positions of nodes.
Let's break down the process step by step:
1. Understanding Vertical Traversal:
Vertical traversal involves assigning horizontal distances to nodes. The root is assigned a distance of 0, and as we move left, the distance decreases, and as we move right, the distance increases.
2. Data Structure:
We need a data structure to store nodes along with their horizontal distances. A map or a hash table is suitable for this purpose.
3. Algorithm:
- Start with the root node and horizontal distance 0.
- Traverse the tree using any traversal technique (e.g., inorder, preorder, or postorder).
- Keep track of the horizontal distance for each node.
- Store nodes in a map or hash table based on their horizontal distances.
- Print nodes in order of increasing horizontal distances.
Consider the below tree:

In the given tree, where 'a' serves as the root node, Hd (horizontal distance) is initialized to 0 for this node. Applying rules, since 'b' is the left child of node 'a,' Hd for 'b' is set to 0 minus 1 (0-1), resulting in Hd equal to -1. Node 'c' being the right child of 'a' follows rule number 3, assigning Hd to 0 plus 1 (0+1), making Hd of 'c' equal to 1.
Continuing, as 'd' is the left child of 'b,' Hd for 'd' is calculated as the Hd value of its parent minus one, thus Hd equals -1-1, giving -2. 'e,' the right child of 'b,' adheres to rule 3, with Hd being the Hd value of its parent plus one, resulting in -1 + 1, which is 0.
Similarly, 'f' being the left child of 'c' leads to Hd of 'f' being (1-1), resulting in zero. 'g' being the right child of 'c' causes Hd of 'g' to be (1+1), giving 2.
For 'h,' the left child of 'd,' Hd is calculated as (-2-1), yielding -3. 'i,' the right child of 'd,' has Hd equal to -1. For 'j,' the left child of 'e,' Hd is -1, and for 'k,' the right child of 'e,' Hd is 1.
Continuing with 'l,' the left child of 'g,' Hd of 'l' is (2-1), resulting in 1. Finally, 'm,' the right child of 'g,' has Hd of (2+1), which is 3.
The node with the minimum Hd value is 'h' at -3, and the node with the maximum Hd value is 'm' at 3. Nodes sharing the same Hd value exist in the same vertical line. Vertical lines are created for each unique Hd value, passing through the corresponding nodes.
Algorithm Steps for Vertical Traversal:
- Enqueue the root.
- Update Hd distance for the root as 0.
- Add Hd as 0 in a hash table, with the root as the associated value.
- Perform Dequeue operation and:
- Check for left and right child nodes, updating Hd in the hash table.
- Enqueue the left and right child nodes.
These steps are repeated until the traversal is complete, creating vertical lines based on Hd values.
Consider the below tree:

We will use two data structures such as Queue, and hash table to implement the vertical traversal,
- We first insert the node 'a' in a queue and update the horizontal distance value of node 'a' as 0. We will also add the Hd of node 'a' and the value in a hash table shown as below:


According to Step 4 specified in the algorithm, element 'a' is dequeued from the queue and update the hash table with Hd value of left and right child of node 'a' shown as below:

Once the hash table is updated, we will enqueue the left and right child of node 'a' in a queue shown as below:

Step 4 is in loop, and it will iterate till the queue does not become empty.
- Check whether the queue is empty or not. The queue is not empty, so dequeue the element 'b' from the queue, and check the left and the right child of 'b' node. Since the node 'b' has both left and right child so we will update the hash table with the Hd value of node 'd' and 'e' shown as below:


Once the hash table gets updated, enqueue the nodes 'd' and 'e' in a queue shown as below:
- Check whether the queue is empty or not. The queue is not empty so dequeue the element 'c' from the queue, and check the left and right child of 'c' node. Since the node 'c' has both left and right child so we will update the hash table with Hd values of node 'f' and 'g' shown as below:

Once the hash table gets updated, enqueue the nodes 'f' and 'g' in a queue shown as below:

- Check again whether the queue is empty or not. The queue is not empty so dequeue the element 'd' from the queue, and check the left and right child of node 'd'. Since the node 'd' has both left and right child so we will update the hash table with Hd values of 'h' and 'i' shown as below:

Once the hash table gets updated, enqueue the nodes 'h' and 'i' in a queue shown as below:

- Check again whether the queue is empty or not. The queue is not empty so dequeue the element 'e' from the queue, and check the left and right child of node 'e'. Since node 'e' does not have any left and right child so there will be no updation in the hash table:
- We will check again whether the queue is empty or not. The queue is not empty so dequeue the element 'f' from the queue, and check the left and right of node 'f'. Since the node 'f' has both left and right child, we will update the hash table with Hd values of 'j' and 'k'.

Once the hash table gets updated, enqueue the nodes 'j' and 'k' in a queue shown as below:

- We will check again whether the queue is empty or not. The queue is not empty so dequeue the element 'g' from the queue, and check the left and right of node 'l'. Since the node 'g' has the only right child, we will update the hash table with Hd values of 'l'.
