"top view of binary tree c++" 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 "top view of binary tree c++" answered properly. Developers are finding an appropriate answer about top view of binary tree c++ related to the C++ coding language. By visiting this online portal developers get answers concerning C++ codes question like top view of binary tree c++. Enter your desired code related query in the search bar and get every piece of information about C++ code related question on top view of binary tree c++. 

gfg top view of tree

By Rid09Rid09 on Jun 08, 2020
/* This is not the entire code. It's just the function which implements 
   bottom view. You need to write required code. */

// Obj class is used to store node with it's distance from parent.
class Obj
{
    public:
        Node *root;
        int dis; // distance from parent node. distance of root node will be 0.

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

void topView(Node *root)
{
    queue<Obj*> q;
    q.push(new Obj(root, 0));
    map<int,int> m;

    while(!q.empty())
    {
        Obj *ob = q.front();
        q.pop();
		
      	/* insert node of unique distance from parent node. ignore repitation 
           of distance. */
        if(m.find(ob->dis) == m.end())
            m[ob->dis] = ob->root->data;

        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));
    }

  	// printing nodes.
    for(auto it=m.begin(); it!=m.end(); it++)
        cout << it->second << "\t";

    cout << endl;
}

Add Comment

2

top view of binary tree c++

By Creepy CapybaraCreepy Capybara on Apr 08, 2021
// C++ Program to print Top View of a binary Tree

#include <iostream>
#include <queue>
#include <stack>
using namespace std;

// class for Tree node
class Node {
public:
	Node *left, *right;
	int data;
	Node() { left = right = 0; }
	Node(int data)
	{
		left = right = 0;
		this->data = data;
	}
};

/*
		1
		/ \
		2 3
		\
		4
		\
			5
			\
			6
	Top view of the above binary tree is
	2 1 3 6
*/

// class for Tree
class Tree {
public:
	Node* root;
	Tree() { root = 0; }

	void topView()
	{
		// queue for holding nodes and their horizontal
		// distance from the root node
		queue<pair<Node*, int> > q;

		// pushing root node with distance 0
		q.push(make_pair(root, 0));

		// hd is currect node's horizontal distance from
		// root node l is currect left min horizontal
		// distance (or max in magnitude) so far from the
		// root node r is currect right max horizontal
		// distance so far from the root node

		int hd = 0, l = 0, r = 0;

		// stack is for holding left node's data because
		// they will appear in reverse order that is why
		// using stack
		stack<int> left;

		// vector is for holding right node's data
		vector<int> right;

		Node* node;

		while (q.size()) {

			node = q.front().first;
			hd = q.front().second;

			if (hd < l) {
				left.push(node->data);
				l = hd;
			}
			else if (hd > r) {
				right.push_back(node->data);
				r = hd;
			}

			if (node->left) {
				q.push(make_pair(node->left, hd - 1));
			}
			if (node->right) {
				q.push(make_pair(node->right, hd + 1));
			}

			q.pop();
		}
		// printing the left node's data in reverse order
		while (left.size()) {
			cout << left.top() << " ";
			left.pop();
		}

		// then printing the root node's data
		cout << root->data << " ";

		// finally printing the right node's data
		for (auto x : right) {
			cout << x << " ";
		}
	}
};

// Driver code
int main()
{
	// Tree object
	Tree t;
	t.root = new Node(1);
	t.root->left = new Node(2);
	t.root->right = new Node(3);
	t.root->left->right = new Node(4);
	t.root->left->right->right = new Node(5);
	t.root->left->right->right->right = new Node(6);
	t.topView();
	cout << endl;
	return 0;
}

Add Comment

0

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

C++ answers related to "top view of binary tree c++"

View All C++ queries

C++ queries related to "top view of binary tree c++"

top view of binary tree c++ Print Nodes in Top View of Binary Tree gfg top view of 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 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 vertical traversal of binary tree gfg bottom view of tree gfg right view of tree gfg left view of tree C++ Book an appointment 2. Change an appointment 3. Cancel an appointment 4. View appointment by last name 5. View all appointment bst to insert tree tree in c++ stl 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 print stack from bottom to top c++ print stack from top to bottom 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 how to get the player view point location and rotation in ue4 c++ ue4 set view target with blend c++

Browse Other Code Languages

CodeProZone