Given a binary tree, print it vertically. The following example illustrates vertical order traversal.
1 / \ 2 3 / \ / \ 4 5 6 7 \ \ 8 9 The output of print this tree vertically will be: 4 2 1 5 6 3 8 7 9
The idea is to traverse the tree once and get the minimum and maximum horizontal distance with respect to root. For the tree shown above, minimum distance is -2 (for node with value 4) and maximum distance is 3 (For node with value 9).
Once we have maximum and minimum distances from root, we iterate for each vertical line at distance minimum to maximum from root, and for each vertical line traverse the tree and print the nodes which lie on that vertical line.
Algorithm:
// min --> Minimum horizontal distance from root // max --> Maximum horizontal distance from root // hd --> Horizontal distance of current node from root findMinMax(tree, min, max, hd) if tree is NULL then return; if hd is less than min then *min = hd; else if hd is greater than max then *max = hd; findMinMax(tree->left, min, max, hd-1); findMinMax(tree->right, min, max, hd+1); printVerticalLine(tree, line_no, hd) if tree is NULL then return; if hd is equal to line_no, then print(tree->data); printVerticalLine(tree->left, line_no, hd-1); printVerticalLine(tree->right, line_no, hd+1);
Implementation:
Following is the implementation of above algorithm.
C++
#include <iostream> using namespace std; // A node of binary tree struct Node { int data; struct Node *left, *right; }; // A utility function to create a new Binary Tree node Node* newNode( int data) { Node *temp = new Node; temp->data = data; temp->left = temp->right = NULL; return temp; } // A utility function to find min and max distances with respect // to root. void findMinMax(Node *node, int *min, int *max, int hd) { // Base case if (node == NULL) return ; // Update min and max if (hd < *min) *min = hd; else if (hd > *max) *max = hd; // Recur for left and right subtrees findMinMax(node->left, min, max, hd-1); findMinMax(node->right, min, max, hd+1); } // A utility function to print all nodes on a given line_no. // hd is horizontal distance of current node with respect to root. void printVerticalLine(Node *node, int line_no, int hd) { // Base case if (node == NULL) return ; // If this node is on the given line number if (hd == line_no) cout << node->data << " " ; // Recur for left and right subtrees printVerticalLine(node->left, line_no, hd-1); printVerticalLine(node->right, line_no, hd+1); } // The main function that prints a given binary tree in // vertical order void verticalOrder(Node *root) { // Find min and max distances with resepect to root int min = 0, max = 0; findMinMax(root, &min, &max, 0); // Iterate through all possible vertical lines starting // from the leftmost line and print nodes line by line for ( int line_no = min; line_no <= max; line_no++) { printVerticalLine(root, line_no, 0); cout << endl; } } // Driver program to test above functions int main() { // Create binary tree shown in above figure Node *root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->left->left = newNode(4); root->left->right = newNode(5); root->right->left = newNode(6); root->right->right = newNode(7); root->right->left->right = newNode(8); root->right->right->right = newNode(9); cout << "Vertical order traversal is \n" ; verticalOrder(root); return 0; } |
Java
// Java program to print binary tree in reverse order // A binary tree node class Node { int data; Node left, right; Node( int item) { data = item; left = right = null ; } } class Values { int max, min; } class BinaryTree { Node root; Values val = new Values(); // A utility function to find min and max distances with respect // to root. void findMinMax(Node node, Values min, Values max, int hd) { // Base case if (node == null ) return ; // Update min and max if (hd < min.min) min.min = hd; else if (hd > max.max) max.max = hd; // Recur for left and right subtrees findMinMax(node.left, min, max, hd - 1 ); findMinMax(node.right, min, max, hd + 1 ); } // A utility function to print all nodes on a given line_no. // hd is horizontal distance of current node with respect to root. void printVerticalLine(Node node, int line_no, int hd) { // Base case if (node == null ) return ; // If this node is on the given line number if (hd == line_no) System.out.print(node.data + " " ); // Recur for left and right subtrees printVerticalLine(node.left, line_no, hd - 1 ); printVerticalLine(node.right, line_no, hd + 1 ); } // The main function that prints a given binary tree in // vertical order void verticalOrder(Node node) { // Find min and max distances with resepect to root findMinMax(node, val, val, 0 ); // Iterate through all possible vertical lines starting // from the leftmost line and print nodes line by line for ( int line_no = val.min; line_no <= val.max; line_no++) { printVerticalLine(node, line_no, 0 ); System.out.println( "" ); } } // Driver program to test the above functions public static void main(String args[]) { BinaryTree tree = new BinaryTree(); /* Let us construct the tree shown in above diagram */ tree.root = new Node( 1 ); tree.root.left = new Node( 2 ); tree.root.right = new Node( 3 ); tree.root.left.left = new Node( 4 ); tree.root.left.right = new Node( 5 ); tree.root.right.left = new Node( 6 ); tree.root.right.right = new Node( 7 ); tree.root.right.left.right = new Node( 8 ); tree.root.right.right.right = new Node( 9 ); System.out.println( "vertical order traversal is :" ); tree.verticalOrder(tree.root); } } // This code has been contributed by Mayank Jaiswal |
Python3
# Program to print binary tree in vertical order # A binary tree class Node: # Constructor to create a new node def __init__( self , key): self .data = key self .left = None self .right = None # A utility function to find min and max distances with # respect to root def findMinMax(node, minimum, maximum, hd): # Base Case if node is None : return # Update min and max if hd < minimum[ 0 ] : minimum[ 0 ] = hd elif hd > maximum[ 0 ]: maximum[ 0 ] = hd # Recur for left and right subtrees findMinMax(node.left, minimum, maximum, hd - 1 ) findMinMax(node.right, minimum, maximum, hd + 1 ) # A utility function to print all nodes on a given line_no # hd is horizontal distance of current node with respect to root def printVerticalLine(node, line_no, hd): # Base Case if node is None : return # If this node is on the given line number if hd = = line_no: print (node.data,end = " " ) # Recur for left and right subtrees printVerticalLine(node.left, line_no, hd - 1 ) printVerticalLine(node.right, line_no, hd + 1 ) def verticalOrder(root): # Find min and max distances with respect to root minimum = [ 0 ] maximum = [ 0 ] findMinMax(root, minimum, maximum, 0 ) # Iterate through all possible lines starting # from the leftmost line and print nodes line by line for line_no in range (minimum[ 0 ], maximum[ 0 ] + 1 ): printVerticalLine(root, line_no, 0 ) print () # Driver program to test above function root = Node( 1 ) root.left = Node( 2 ) root.right = Node( 3 ) root.left.left = Node( 4 ) root.left.right = Node( 5 ) root.right.left = Node( 6 ) root.right.right = Node( 7 ) root.right.left.right = Node( 8 ) root.right.right.right = Node( 9 ) print ( "Vertical order traversal is" ) verticalOrder(root) # This code is contributed by Nikhil Kumar Singh(nickzuck_007) |
C#
// C# program to print binary tree in reverse order using System; // A binary tree node public class Node { public int data; public Node left, right; public Node( int item) { data = item; left = right = null ; } } class Values { public int max, min; } public class BinaryTree { Node root; Values val = new Values(); // A utility function to find min and // max distances with respect to root. void findMinMax(Node node, Values min, Values max, int hd) { // Base case if (node == null ) return ; // Update min and max if (hd < min.min) min.min = hd; else if (hd > max.max) max.max = hd; // Recur for left and right subtrees findMinMax(node.left, min, max, hd - 1); findMinMax(node.right, min, max, hd + 1); } // A utility function to print // all nodes on a given line_no. // hd is horizontal distance of // current node with respect to root. void printVerticalLine(Node node, int line_no, int hd) { // Base case if (node == null ) return ; // If this node is on the given line number if (hd == line_no) Console.Write(node.data + " " ); // Recur for left and right subtrees printVerticalLine(node.left, line_no, hd - 1); printVerticalLine(node.right, line_no, hd + 1); } // The main function that prints // a given binary tree in vertical order void verticalOrder(Node node) { // Find min and max distances with resepect to root findMinMax(node, val, val, 0); // Iterate through all possible // vertical lines starting from the // leftmost line and print nodes line by line for ( int line_no = val.min; line_no <= val.max; line_no++) { printVerticalLine(node, line_no, 0); Console.WriteLine( "" ); } } // Driver code public static void Main() { BinaryTree tree = new BinaryTree(); /* Let us construct the tree shown in above diagram */ tree.root = new Node(1); tree.root.left = new Node(2); tree.root.right = new Node(3); tree.root.left.left = new Node(4); tree.root.left.right = new Node(5); tree.root.right.left = new Node(6); tree.root.right.right = new Node(7); tree.root.right.left.right = new Node(8); tree.root.right.right.right = new Node(9); Console.WriteLine( "vertical order traversal is :" ); tree.verticalOrder(tree.root); } } /* This code is contributed PrinciRaj1992 */ |
Javascript
<script> // JavaScript program to print binary tree // in reverse order class Node { constructor(item) { this .left = null ; this .right = null ; this .data = item; } } let root, min = 0, max = 0; // A utility function to find min and // max distances with respect // to root. function findMinMax(node, hd) { // Base case if (node == null ) return ; // Update min and max if (hd < min) min = hd; else if (hd > max) max = hd; // Recur for left and right subtrees findMinMax(node.left, hd - 1); findMinMax(node.right, hd + 1); } // A utility function to print all nodes on a given line_no. // hd is horizontal distance of // current node with respect to root. function printVerticalLine(node, line_no, hd) { // Base case if (node == null ) return ; // If this node is on the given line number if (hd == line_no) document.write(node.data + " " ); // Recur for left and right subtrees printVerticalLine(node.left, line_no, hd - 1); printVerticalLine(node.right, line_no, hd + 1); } // The main function that prints a given binary tree in // vertical order function verticalOrder(node) { // Find min and max distances with resepect to root findMinMax(node, 0); // Iterate through all possible vertical lines starting // from the leftmost line and print nodes line by line for (let line_no = min; line_no <= max; line_no++) { printVerticalLine(node, line_no, 0); document.write( "</br>" ); } } /* Let us construct the tree shown in above diagram */ root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.left = new Node(4); root.left.right = new Node(5); root.right.left = new Node(6); root.right.right = new Node(7); root.right.left.right = new Node(8); root.right.right.right = new Node(9); document.write( "Vertical order traversal is :" + "</br>" ); verticalOrder(root); </script> |
Vertical order traversal is 4 2 1 5 6 3 8 7 9
Time Complexity: Time complexity of above algorithm is O(w*n) where w is width of Binary Tree and n is number of nodes in Binary Tree. In worst case, the value of w can be O(n) (consider a complete tree for example) and time complexity can become O(n2).
Auxiliary Space: O(1), as we are not using any extra space.
This problem can be solved more efficiently using the technique discussed in this post. We will soon be discussing complete algorithm and implementation of more efficient method.
This article is contributed by Shalki Agarwal. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Method -2 – More Optimized Method: (With MinHeaps)
Algorithm:
- So here we need the traversal in a customized way.
- If we number the vertical lines in such numbering as per the above image.
- We need the Traversal in Ascending order of vertical lines, IF SAME Ascending Order of Level, IF SAME Levelorder Traversal ordering.
- So we Need 4 parameters = Vertical Line Index, Level Index, Level order traversal (BFS) Number, Value of Node.
- Vertical Index Numbering = start with 0 in the root.
- IF u go left v=v-1
- IF u go right v = v+1 (Look at the diagram to get deeper idea on why it is?)
- Level Index = start with 0 in the root.
- Just make l = l+1, as left or right we are going down only.
- So with the above modification do level order traversal and store the popped nodes in the MINHEAP.
- Then simply pop from MINHEAP and print it.
- When u go to next vertical line, then print a newline as needed.
NOTE:
- MinHeap, why? As we need Ascending order, minimum thing first at the top.
- we used pair<pair<int,int>,pair<int,int>> to store all 4 parameters {{ },{ }}
C++
#include <bits/stdc++.h> using namespace std; #define Nii pair<Node*, pair<int, int>> #define ppi pair<pair<int,int>,pair<int,int>> // A node of binary tree struct Node { int data; struct Node *left, *right; }; // A utility function to create a new Binary Tree node Node* newNode( int data) { Node *temp = new Node; temp->data = data; temp->left = temp->right = NULL; return temp; } //Function to find the vertical order traversal of Binary Tree. void verticalOrder(Node *root) { queue<Nii> qu; //node, vertical, level priority_queue<ppi,vector<ppi>,greater<ppi>> minH; //Vertical, Level, BFSNo, Val int v = 0; int l = 0; qu.push({root,{v,l}}); //LEVEL order traversal while (!qu.empty()){ int s = qu.size(); int i = 0; while (i<s){ Node* node = qu.front().first; v = qu.front().second.first; l = qu.front().second.second; //Vertical indx , Levelindx , BFSNo, Val - Insertion minH.push({{v,l},{i,node->data}}); qu.pop(); if (node->left !=NULL) qu.push({node->left,{v-1,l+1}}); if (node->right !=NULL) qu.push({node->right,{v+1,l+1}}); i++; } } while (!minH.empty()){ int vi = minH.top().first.first; cout<< minH.top().second.second<< " " ; minH.pop(); if (vi!=minH.top().first.first) cout<< "\n" ; } } int main() { // Create binary tree shown in above figure Node *root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->left->left = newNode(4); root->left->right = newNode(5); root->right->left = newNode(6); root->right->right = newNode(7); root->right->left->right = newNode(8); root->right->right->right = newNode(9); cout << "Vertical order traversal is \n" ; verticalOrder(root); return 0; } //Code Done by Balakrishnan R (rbkraj000) |
C#
using System; using System.Collections.Generic; // Node class definition public class Node { public int data; public Node left, right; } public class VerticalOrderTraversal { // Driver code static void Main() { // Create binary tree shown in above figure Node root = newNode(1); root.left = newNode(2); root.right = newNode(3); root.left.left = newNode(4); root.left.right = newNode(5); root.right.left = newNode(6); root.right.right = newNode(7); root.right.left.right = newNode(8); root.right.right.right = newNode(9); Console.WriteLine( "Vertical order traversal is " ); VerticalOrder(root); } // A utility function to create a new Binary Tree node static Node newNode( int data) { Node temp = new Node(); temp.data = data; temp.left = temp.right = null ; return temp; } // Function to find the vertical order traversal of // Binary Tree. static void VerticalOrder(Node root) { Queue<Tuple<Node, Tuple< int , int > > > qu = new Queue<Tuple<Node, Tuple< int , int > > >(); // node, vertical, level var minH = new SortedSet< Tuple<Tuple< int , int >, Tuple< int , int > > >(); // Vertical, Level, BFSNo, Val int v = 0; int l = 0; qu.Enqueue( new Tuple<Node, Tuple< int , int > >( root, new Tuple< int , int >(v, l))); // LEVEL order traversal while (qu.Count != 0) { int s = qu.Count; int i = 0; while (i < s) { Node node = qu.Peek().Item1; v = qu.Peek().Item2.Item1; l = qu.Peek().Item2.Item2; // Vertical indx, Levelindx, BFSNo, Val - // Insertion minH.Add( new Tuple<Tuple< int , int >, Tuple< int , int > >( new Tuple< int , int >(v, l), new Tuple< int , int >(i, node.data))); qu.Dequeue(); if (node.left != null ) qu.Enqueue( new Tuple<Node, Tuple< int , int > >( node.left, new Tuple< int , int >( v - 1, l + 1))); if (node.right != null ) qu.Enqueue( new Tuple<Node, Tuple< int , int > >( node.right, new Tuple< int , int >( v + 1, l + 1))); i++; } } while (minH.Count != 0) { int vi = minH.Min.Item1.Item1; Console.Write(minH.Min.Item2.Item2 + " " ); minH.Remove(minH.Min); if (minH.Count != 0 && vi != minH.Min.Item1.Item1) Console.WriteLine(); } } } |
Java
import java.util.LinkedList; import java.util.Queue; import java.util.SortedSet; import java.util.TreeSet; // Tuple class definition class Tuple<T1, T2> { // Data members public final T1 first; public final T2 second; // Constructor public Tuple(T1 item1, T2 item2) { this .first = item1; this .second = item2; } } // Node class definition class Node { public int data; public Node left, right; } class VerticalOrderTraversal { // Driver code public static void main(String[] args) { // Create binary tree shown in above figure Node root = newNode( 1 ); root.left = newNode( 2 ); root.right = newNode( 3 ); root.left.left = newNode( 4 ); root.left.right = newNode( 5 ); root.right.left = newNode( 6 ); root.right.right = newNode( 7 ); root.right.left.right = newNode( 8 ); root.right.right.right = newNode( 9 ); System.out.println( "Vertical order traversal is " ); verticalOrder(root); } // A utility function to create a new Binary Tree node public static Node newNode( int data) { Node temp = new Node(); temp.data = data; temp.left = temp.right = null ; return temp; } // Function to find the vertical order traversal of // Binary Tree. public static void verticalOrder(Node root) { Queue<Tuple<Node, Tuple<Integer, Integer> > > qu = new LinkedList<>(); // node, vertical, level SortedSet<Tuple<Tuple<Integer, Integer>, Tuple<Integer, Integer> > > minH = new TreeSet<>((o1, o2) -> { if (o1.first.first.equals( o2.first.first)) { if (o1.first.second.equals( o2.first.second)) { return o1.second.first.compareTo( o2.second.first); } return o1.first.second.compareTo( o2.first.second); } return o1.first.first.compareTo( o2.first.first); }); // Vertical, Level, BFSNo, Val int v = 0 ; int l = 0 ; qu.add( new Tuple<Node, Tuple<Integer, Integer> >( root, new Tuple<Integer, Integer>(v, l))); // LEVEL order traversal while (!qu.isEmpty()) { int s = qu.size(); int i = 0 ; while (i < s) { Node node = qu.peek().first; v = qu.peek().second.first; l = qu.peek().second.second; // Vertical indx, Levelindx, BFSNo, Val - // Insertion minH.add( new Tuple<Tuple<Integer, Integer>, Tuple<Integer, Integer> >( new Tuple<Integer, Integer>(v, l), new Tuple<Integer, Integer>( i, node.data))); qu.remove(); if (node.left != null ) qu.add( new Tuple<Node, Tuple<Integer, Integer> >( node.left, new Tuple<Integer, Integer>( v - 1 , l + 1 ))); if (node.right != null ) qu.add( new Tuple<Node, Tuple<Integer, Integer> >( node.right, new Tuple<Integer, Integer>( v + 1 , l + 1 ))); i++; } } // If the minHeap is not empty while (!minH.isEmpty()) { int vi = minH.first().first.first; System.out.print(minH.first().second.second + " " ); minH.remove(minH.first()); if (!minH.isEmpty() && vi != minH.first().first.first) System.out.println(); } } } |
Python3
from typing import Tuple import queue import heapq # Tuple class definition class Tuple : # Data members def __init__( self , item1, item2): self .first = item1 self .second = item2 def __lt__( self , other): return ( self .first, self .second) < (other.first, other.second) # Node class definition class Node: def __init__( self ): self .data = 0 self .left = None self .right = None class VerticalOrderTraversal: # Driver code @staticmethod def main(): # Create binary tree shown in above figure root = VerticalOrderTraversal.newNode( 1 ) root.left = VerticalOrderTraversal.newNode( 2 ) root.right = VerticalOrderTraversal.newNode( 3 ) root.left.left = VerticalOrderTraversal.newNode( 4 ) root.left.right = VerticalOrderTraversal.newNode( 5 ) root.right.left = VerticalOrderTraversal.newNode( 6 ) root.right.right = VerticalOrderTraversal.newNode( 7 ) root.right.left.right = VerticalOrderTraversal.newNode( 8 ) root.right.right.right = VerticalOrderTraversal.newNode( 9 ) print ( "Vertical order traversal is " ) VerticalOrderTraversal.verticalOrder(root) # A utility function to create a new Binary Tree node @staticmethod def newNode(data): temp = Node() temp.data = data temp.left = temp.right = None return temp # Function to find the vertical order traversal of # Binary Tree. @staticmethod def verticalOrder(root): qu = queue.Queue() # node, vertical, level minH = [] # Vertical, Level, BFSNo, Val v = 0 l = 0 qu.put( Tuple (root, Tuple (v, l))) # LEVEL order traversal while not qu.empty(): s = qu.qsize() i = 0 while i < s: node = qu.queue[ 0 ].first v = qu.queue[ 0 ].second.first l = qu.queue[ 0 ].second.second # Vertical indx, Levelindx, BFSNo, Val - # Insertion heapq.heappush( minH, Tuple ( Tuple (v, l), Tuple (i, node.data) ) ) qu.get() if node.left ! = None : qu.put( Tuple ( node.left, Tuple (v - 1 , l + 1 ) ) ) if node.right ! = None : qu.put( Tuple ( node.right, Tuple (v + 1 , l + 1 ) ) ) i + = 1 # If the minHeap is not empty while len (minH) > 0 : vi = minH[ 0 ].first.first print (minH[ 0 ].second.second, end = " " ) heapq.heappop(minH) if len (minH) > 0 and vi ! = minH[ 0 ].first.first: print () # Driver code if __name__ = = '__main__' : VerticalOrderTraversal.main() |
Javascript
// Node class definition class Node { constructor(data) { this .data = data; this .left = null ; this .right = null ; } } function verticalOrderTraversal() { // Create binary tree shown in above figure const root = newNode(1); root.left = newNode(2); root.right = newNode(3); root.left.left = newNode(4); root.left.right = newNode(5); root.right.left = newNode(6); root.right.right = newNode(7); root.right.left.right = newNode(8); root.right.right.right = newNode(9); process.stdout.write( "Vertical order traversal is \n" ); verticalOrder(root); } // A utility function to create a new Binary Tree node function newNode(data) { const temp = new Node(); temp.data = data; temp.left = temp.right = null ; return temp; } // Function to find the vertical order traversal of Binary Tree. function verticalOrder(root) { const queue = []; const minHeap = new Map(); let v = 0; let l = 0; queue.push([root, [v, l]]); // LEVEL order traversal while (queue.length !== 0) { const s = queue.length; for (let i = 0; i < s; i++) { const [node, [v, l]] = queue.shift(); // Vertical indx, Levelindx, BFSNo, Val - // Insertion const key = `${v},${l}`; const value = [i, node.data]; if (!minHeap.has(key)) { minHeap.set(key, []); } minHeap.get(key).push(value); if (node.left !== null ) { queue.push([node.left, [v - 1, l + 1]]); } if (node.right !== null ) { queue.push([node.right, [v + 1, l + 1]]); } } } for (const [key, values] of [...minHeap.entries()].sort( function ([a_k, a_v], [b_k, b_v]) { let a = parseInt(a_k.split( "," )[0]) let b = parseInt(b_k.split( "," )[0]) return a - b; } )) { for (const value of values) { process.stdout.write(value[1] + " " ); } process.stdout.write( " \n" ); } } verticalOrderTraversal(); |
Vertical order traversal is 4 2 1 5 6 3 8 7 9
Time Complexity: O(N*LogN) Time
Reason:
- Normal Level Order (BFS) Traversal takes O(N).
- But here we are pushing into MinHeap – Single push O(LogN).
- So Overall O(N*LogN).
- Also while popping out from minHeap O(N*LogN).
Auxiliary Space: O(N)
Reason:
- The queue will have max at Last Level O(N/2) = O(N).
- The heap also stores all the nodes at a point, so O(N).
N is the Size of the Binary Tree. (Total no. of nodes)
The above Method-2 Idea, Algorithm, and Code are done by Balakrishnan R (rbkraj000 – GFG ID). If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!