What Is Vertical Order Traversal Of A Binary Tree?

Are you someone who is associated with programming and daily gets involved in solving different problem statements?

Well, as a coder, chances are you've heard of the term "vertical order traversal" about binary tree data structures. This is an important concept for coders to understand and use properly, but what exactly does it mean?

A listing of top-to-bottom orderings from the leftmost column to the rightmost column is what is meant by a binary tree's vertical order traversal.

In this blog post, we'll be diving deep into vertical order traversal so that you can have a solid foundation on which to build your coding skills. We'll discuss what exactly makes a binary tree unique, how the vertical order traversal works and why understanding it matters when programming.

By the end of this blog post, you will have a good grasp of what vertical order traversal means and why it's important when working with trees!

Let’s first understand in depth what is vertical order traversal of a binary tree.

What is the vertical order traversal of a binary tree?

A listing of top-to-bottom sequences for each column index, starting with the leftmost column and finishing on the rightmost column, makes up a binary tree's vertical order traversal.

Within the same row and column, there may be more than one node. Sort these nodes based on their values in this scenario.

Additionally, it can be useful to carry out specific operations that might be connected to locating the subsequence in a given sequence using the vertical order traversal of a binary tree.

The vertical order traversal, for instance, could be employed to process the characters in a certain way to find the longest subsequence palindrome if the nodes of the tree represented characters in a string.

Let’s understand this problem with a relevant example.

Example 1:

Root = [1,2,3,4,5,6,7] as an input

[[4],[2],[1,5,6],[3],[7]] is the output.

Explanation:

Column 2: The only node in this column is node 4.

Column -1: This column contains only node 2.

Column 0: This column contains the nodes 1, 5, and 6.

Since 1 is on top, it comes first.

Considering that 5 and 6 are both at position (2, 0), we should put 5 in front of 6 according to their values.

Column 1: This column only contains node 3.

Column 2: The only node present is node 7.

Method 1: Utilising hashing to traverse the binary tree in vertical order

Follow the method below to solve the issue:

Checking the horizontal distances of each node from the root is necessary. The very same vertical line connects two nodes if their horizontal distances (HD) are equal. HD is based on an easy concept.

HD for the root is 0, whereas HD for the right edge (the edge connecting to the right subtree) is determined to be +1 and HD for the left edge to be -1.

To solve the problem using this method, follow the procedures listed below:

  • Preorder traverse the binary tree that has been provided.

  • We can recursively determine HDs while moving across the tree. The horizontal distance is originally passed to the root as 0.

  • The horizontal distance for the left subtree is passed as the root's horizontal distance minus 1.

  • We multiply the horizontal distance for the right subtree by 1 to obtain the horizontal distance.

  • We keep track of a hash map's nodes for every HD value. We visit the hashmap entries whenever we come across a node in the traverse as well as add the node there to use HD as a key in the map.

Method 2: Binary tree vertical order traversal using the Unordered_map technique

The ordered map that we have seen so far is O(N log N) complex, and it also does not output the vertical nodes with the identical horizontal distance in the proper sequence.

Here, we use an unordered map to create it because it has a lower complexity than an ordered map (which is achieved using a BST) and can be done so using a hash table.

To solve the problem using this method, follow the procedures listed below:

  • To keep the node as well as its horizontal distance within the tree, create a queue of pairs.

  • Make a map to keep the values of the nodes at every horizontal distance.

  • Put the tree through a BFS now.

  • Save the nodes with a specific horizontal distance for each iteration in the map.

  • Place the tree's left and right children with horizontal distances of 1 and 1 into the queue, respectively.

  • Map out the solution to print.

Method 3: Print the binary tree's vertical order traversal in the exact order that it appears

With the use of the breadth-first search (BFS) traversal technique can be used to publish a binary tree's vertical order traversal in the precise order that it appears.

Here is a general description of the steps that you can take:

  • To maintain the vertical order traversal, make an empty list and a hash map. A list of the nodes at a certain level will be the value and the vertical level will be the key in the hash map.

  • Mark the root node as being located at the vertical level 0 and add it to the queue.

  • Do the following while there are still people in line:

  • Remove the front node from the queue and add it to the hashmap at the appropriate vertical level.

  • If the node has a left child, add it to the queue and mark it as being at the vertical level one less than the parent node.

  • If the node has a right child, add it to the queue and mark it as being at the vertical level one more than the parent node.

After the queue is empty, iterate through the hashmap and then, print the nodes at each vertical level in the order they appear in the list.

Conclusion

Vertical order traversal is an important concept for coders to understand and use properly when programming. A listing of top-to-bottom orderings from the leftmost column to the rightmost column is what is meant by a binary tree's vertical order traversal.

The vertical order traversal is also useful for finding the longest subsequence palindrome.

This blog post has hopefully given you a good foundation on which to build your coding skills related to vertical order traversal.