Javatpoint Logo
Javatpoint Logo

Vertical Order Traversal of a Binary Tree in Java

In this section, we will discuss the vertical order traversal of a binary tree in Java and the different approaches to achieve it. In the vertical order traversal, we print the node of the binary tree vertically, i.e., from top to bottom. For example, consider the following binary tree.

Vertical Order Traversal of a Binary Tree in Java

The vertical order traversal is:

Approach 1: Using Horizontal Distance

In this approach, we traverse the tree only once and find the maximum and minimum horizontal distance by taking root as the reference. Assume that the root node of the binary tree is located at a distance of 0. Also, assume that going one step in the left direction is -1, and going one step in the right direction is +1. For the above-mentioned binary tree, the minimum distance is -2 (node with the value of 1), and the maximum distance is 3 (node with the value of 19).

After finding the minimum and maximum distances from the root, iterate across each vertical line within the range minimum to maximum. While iterating for each vertical line, print the nodes that are present on the vertical line (see the above diagram).

Implementation

Let's see the implementation of the vertical order traversal of a binary tree using the horizontal distance approach.

FileName: VerticalTraversalExample.java

Output:

The vertical order traversal of the binary tree is:
1 
2 
4 3 6 
5 18 
7 
19   

Time Complexity: The time complexity of the above algorithm is O(wid * no), where "wid" is the width of the given binary tree, and "no" is the number of nodes in the binary tree. In the worst case, the value of wid can be O(no) (for example, think of a complete tree) and, in such a case, the time complexity can become O(no2).

Approach 2: Using TreeMap

In the previous approach, we have discussed an O(no2) solution. In this approach, we will be using the TreeMap, which gives a better solution than the TreeMap. In this approach also, it is required to check the horizontal distances of all the nodes using the root node as the reference. Similar to the previous approach, when we move to a node which is one unit left of the root node, the horizontal distance is considered as -1. For the node on the right side of the root node, the horizontal distance is considered as +1. While performing the preorder traversal of the tree, we can compute the horizontal distances. For every horizontal distance value, a list of nodes is maintained in the TreeMap.

Implementation

Let's see the implementation of the vertical order traversal of a binary tree using the TreeMap.

FileName: VerticalTraversalExample1.java

Output:

The vertical order traversal of the binary tree is:
[1]
[2]
[4, 3, 6]
[5, 18]
[7]
[19]    

Time Complexity: The above solution is based on the technique of hashing, whose time complexity is considered as O(n), where n is the total number of nodes present in the binary tree. Note that the O(n) time complexity is achieved when we use a good hashing method, which permits the retrieval as well as the insertion operation in O(1) time.

Approach 3: Using Level Order Traversal

One can also use the concept of the level order traversal to achieve the vertical order traversal. We take the help of a queue to do the traversal of nodes. Each element of the queue provides information about the horizontal distance and the node of the binary tree.

Similar to other approaches, in this approach also, we find the horizontal distance by taking the root node of the tree as the reference point. Also, the leftward movement from the root node adds -1 on each of the successive nodes. Similarly, the rightward movement from the root node adds +1 on each successive node. After the level order traversal of the tree is completed, we pop out the elements from the queue one by one.

The vertical lines, as shown in the above diagram, can be considered as a level on which the nodes are lying. To point out which node is lying on the same vertical line can be found out using the horizontal distance (horDis). We can put these nodes in an array list, and corresponding to the list, there will be the horizontal distance. We put the list and the horizontal distance in a map. Eventually, we iterate over the map to display the results.

Implementation

Let's see the implementation of the vertical order traversal of a binary tree using the level order traversal.

FileName: VerticalTraversalExample1.java

Output:

The vertical order traversal of the binary tree is: 
1 
2 
4 3 6 
5 18 
7 
19   

Time Complexity: The time complexity of the above program is O(n), where n is the total number of nodes present in the tree.


Next TopicGroup By in Java 8





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA