"vertical traversal of binary tree" Code Answer's

You're definitely familiar with the best coding language C++ that developers use to develop their projects and they get all their queries like "vertical traversal of binary tree" answered properly. Developers are finding an appropriate answer about vertical traversal of binary tree related to the C++ coding language. By visiting this online portal developers get answers concerning C++ codes question like vertical traversal of binary tree. Enter your desired code related query in the search bar and get every piece of information about C++ code related question on vertical traversal of binary tree. 

vertical traversal of binary tree

By Rid09Rid09 on Jun 09, 2020
/* This is just a function of vertical traversal of binary tree. You need to 
   write required code. Thank you. */

// Class to store node and it's distance from parent.
class Obj
{
    public:
        Node *root;
        int dis;

        Obj(Node *node, int dist)
        {
            root = node;
            dis = dist;
        }
};

// Main logic of vertical traversal.
void verticalTraversal(Node *root)
{
    queue<Obj*> q;
    Obj *ob = new Obj(root, 0);
    q.push(ob);

    map<int, vector<int>> m;

    while(!q.empty())
    {
        Obj *ob = q.front();
        q.pop();

        if(m.find(ob->dis) != m.end())
        {            
            m[ob->dis].push_back(ob->root->data);
        }   
        else
        {   
            vector<int> v;
            v.push_back(ob->root->data);
            m[ob->dis] = v;
        }

        if(ob->root->left != NULL)
            q.push(new Obj(ob->root->left, ob->dis-1));
        if(ob->root->right != NULL)
            q.push(new Obj(ob->root->right, ob->dis+1));
    }

    for(auto it=m.begin(); it!=m.end(); it++)
    {
        vector<int> v1 = (*it).second;
        for(int j = 0; j<v1.size(); j++)
            cout << v1[j] << "\t";
    }

    cout << endl;
}

Add Comment

2

vertical traversal of binary tree

By DarkPhilosopherDarkPhilosopher on May 04, 2020
// Java program for printing vertical order of a given binary tree 
import java.util.TreeMap; 
import java.util.Vector; 
import java.util.Map.Entry; 

public class VerticalOrderBtree 
{ 
	// Tree node 
	static class Node 
	{ 
		int key; 
		Node left; 
		Node right; 
		
		// Constructor 
		Node(int data) 
		{ 
			key = data; 
			left = null; 
			right = null; 
		} 
	} 
	
	// Utility function to store vertical order in map 'm' 
	// 'hd' is horizontal distance of current node from root. 
	// 'hd' is initially passed as 0 
	static void getVerticalOrder(Node root, int hd, 
								TreeMap<Integer,Vector<Integer>> m) 
	{ 
		// Base case 
		if(root == null) 
			return; 
		
		//get the vector list at 'hd' 
		Vector<Integer> get = m.get(hd); 
		
		// Store current node in map 'm' 
		if(get == null) 
		{ 
			get = new Vector<>(); 
			get.add(root.key); 
		} 
		else
			get.add(root.key); 
		
		m.put(hd, get); 
		
		// Store nodes in left subtree 
		getVerticalOrder(root.left, hd-1, m); 
		
		// Store nodes in right subtree 
		getVerticalOrder(root.right, hd+1, m); 
	} 
	
	// The main function to print vertical order of a binary tree 
	// with the given root 
	static void printVerticalOrder(Node root) 
	{ 
		// Create a map and store vertical order in map using 
		// function getVerticalOrder() 
		TreeMap<Integer,Vector<Integer>> m = new TreeMap<>(); 
		int hd =0; 
		getVerticalOrder(root,hd,m); 
		
		// Traverse the map and print nodes at every horigontal 
		// distance (hd) 
		for (Entry<Integer, Vector<Integer>> entry : m.entrySet()) 
		{ 
			System.out.println(entry.getValue()); 
		} 
	} 
	
	// Driver program to test above functions 
	public static void main(String[] args) { 

		// TO DO Auto-generated method stub 
		Node 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); 
		System.out.println("Vertical Order traversal is"); 
		printVerticalOrder(root); 
	} 
}

Add Comment

0

All those coders who are working on the C++ based application and are stuck on vertical traversal of binary tree can get a collection of related answers to their query. Programmers need to enter their query on vertical traversal of binary tree related to C++ code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about vertical traversal of binary tree for the programmers working on C++ code while coding their module. Coders are also allowed to rectify already present answers of vertical traversal of binary tree while working on the C++ language code. Developers can add up suggestions if they deem fit any other answer relating to "vertical traversal of binary tree". Visit this developer's friendly online web community, CodeProZone, and get your queries like vertical traversal of binary tree resolved professionally and stay updated to the latest C++ updates. 

C++ answers related to "vertical traversal of binary tree"

View All C++ queries

C++ queries related to "vertical traversal of binary tree"

vertical traversal of binary tree binary search tree in cpp using class binary index tree c++ binary indexed tree binary tree search deletion in a binary search tree binary tree deletion top view of binary tree c++ heap sort heapify and max heap in binary tree binary search tree sorted order searching display insert in a binary serach tree Write a program in C++ to find post-order predecessor of a node in a Binary Tree Print Nodes in Top View of Binary Tree bst traversal code in data structure with c++ bfs traversal of graph in c preorder traversal c++ bfs traversal code how to type a vertical stack program c++ gfg bottom view of tree gfg right view of tree bst to insert tree gfg left view of tree tree in c++ stl gfg top view of tree dfenwick tree code c++ avl tree implementation c++ find the graph is minimal spanig tree or not diameter of tree using dfs centroid of a tree convert binary to decimal c++ stl how to do binary search in c++ using STL binary search program c++ binary exponentiation binary sort c++ binary addition using bitwise operators convert decimal to binary in c++ convert int to binary string c++ binary exponentiation modulo m binary search stl c++ display numbers as binary built in function in c++ for binary to decimal binary search function in c++ binary search in c++ write and read string binary file c++ convert long int to binary string c++ print binary in c how to do decimal to binary converdsion in c++ Decimal to binary c++ c++ binary search binary search algorithm binary heap decimal to binary predefined function find number of 1s in a binary cv::mat image c++ vector decimal to binary Print Decimal to binary using stack is obje file binary?? how to find the left most bit 1 in binary of any number c++ binary search lower bound how to show c++ binary files in sublime text binary algebra cpp building native binary with il2cpp unity C Binary Search binary search in java Binary Search implementation binary search in c binary search in stl

Browse Other Code Languages

CodeProZone