top view of binary tree c 2b 2b

Solutions on MaxInterview for top view of binary tree c 2b 2b by the best coders in the world

showing results for - "top view of binary tree c 2b 2b"
Santiago
26 Jul 2020
1/* This is not the entire code. It's just the function which implements 
2   bottom view. You need to write required code. */
3
4// Obj class is used to store node with it's distance from parent.
5class Obj
6{
7    public:
8        Node *root;
9        int dis; // distance from parent node. distance of root node will be 0.
10
11        Obj(Node *node, int dist)
12        {
13            root = node;
14            dis = dist;
15        }
16};
17
18void topView(Node *root)
19{
20    queue<Obj*> q;
21    q.push(new Obj(root, 0));
22    map<int,int> m;
23
24    while(!q.empty())
25    {
26        Obj *ob = q.front();
27        q.pop();
28		
29      	/* insert node of unique distance from parent node. ignore repitation 
30           of distance. */
31        if(m.find(ob->dis) == m.end())
32            m[ob->dis] = ob->root->data;
33
34        if(ob->root->left != NULL)
35            q.push(new Obj(ob->root->left, ob->dis-1)); 
36        if(ob->root->right != NULL)
37            q.push(new Obj(ob->root->right, ob->dis+1));
38    }
39
40  	// printing nodes.
41    for(auto it=m.begin(); it!=m.end(); it++)
42        cout << it->second << "\t";
43
44    cout << endl;
45}
Jasper
23 Jan 2020
1// C++ Program to print Top View of a binary Tree
2
3#include <iostream>
4#include <queue>
5#include <stack>
6using namespace std;
7
8// class for Tree node
9class Node {
10public:
11	Node *left, *right;
12	int data;
13	Node() { left = right = 0; }
14	Node(int data)
15	{
16		left = right = 0;
17		this->data = data;
18	}
19};
20
21/*
22		1
23		/ \
24		2 3
25		\
26		4
27		\
28			5
29			\
30			6
31	Top view of the above binary tree is
32	2 1 3 6
33*/
34
35// class for Tree
36class Tree {
37public:
38	Node* root;
39	Tree() { root = 0; }
40
41	void topView()
42	{
43		// queue for holding nodes and their horizontal
44		// distance from the root node
45		queue<pair<Node*, int> > q;
46
47		// pushing root node with distance 0
48		q.push(make_pair(root, 0));
49
50		// hd is currect node's horizontal distance from
51		// root node l is currect left min horizontal
52		// distance (or max in magnitude) so far from the
53		// root node r is currect right max horizontal
54		// distance so far from the root node
55
56		int hd = 0, l = 0, r = 0;
57
58		// stack is for holding left node's data because
59		// they will appear in reverse order that is why
60		// using stack
61		stack<int> left;
62
63		// vector is for holding right node's data
64		vector<int> right;
65
66		Node* node;
67
68		while (q.size()) {
69
70			node = q.front().first;
71			hd = q.front().second;
72
73			if (hd < l) {
74				left.push(node->data);
75				l = hd;
76			}
77			else if (hd > r) {
78				right.push_back(node->data);
79				r = hd;
80			}
81
82			if (node->left) {
83				q.push(make_pair(node->left, hd - 1));
84			}
85			if (node->right) {
86				q.push(make_pair(node->right, hd + 1));
87			}
88
89			q.pop();
90		}
91		// printing the left node's data in reverse order
92		while (left.size()) {
93			cout << left.top() << " ";
94			left.pop();
95		}
96
97		// then printing the root node's data
98		cout << root->data << " ";
99
100		// finally printing the right node's data
101		for (auto x : right) {
102			cout << x << " ";
103		}
104	}
105};
106
107// Driver code
108int main()
109{
110	// Tree object
111	Tree t;
112	t.root = new Node(1);
113	t.root->left = new Node(2);
114	t.root->right = new Node(3);
115	t.root->left->right = new Node(4);
116	t.root->left->right->right = new Node(5);
117	t.root->left->right->right->right = new Node(6);
118	t.topView();
119	cout << endl;
120	return 0;
121}
122
queries leading to this page
top view of the treetopview of tree c 2b 2btop view binary tree c 2b 2btop view of a tree in javatop view of a treetop view of binary search treetop view of binary tree is inordertop view of binary tree in ctop view of binary tretop view of binary tree practice gfgc 23 binary tree top downall view of the tree in dstree 3a top view pythonprint nodes in top view of binary treebinary tree right top view top view practice gfgtop view of a tree algorithm solutiontop view solution java visualizationprint top view of bstget top view of a binary tree javahow to print a binary top tree c 2b 2btop view of binary tree c 2b 2bget top view of a tree javarint the top view of the binary tree top view on binary treetop view of binary tree in c codeou are given a pointer to the root of a binary tree print the top view of the binary tree top view of binary tree implementationtree 3atop view javascript solutionreverse top view binary treetop view of a binary tree is the set of nodes visible when the tree is viewed from the top given a binary tree 2c print the top view of it top view recursive javatop view binary treeprint binary tree c 2b 2b top downtopview of binary tree pythontop view of a tree top of the tree gfgtop view of tree geekspicture of binary tree in ordertopview of treegfg top view of treetop view of binary tree gfgtop view of tree 3fhow to find top view of a bsttop view of a tree c 2b 2btop view of the binary tree top view of tree javatop view of bttop view of a binary treetop view of tree in c 2b 2bviews of a treetop view of binary tree gfg solutionbinary tree viewstop view of a tree c 23what is top view of a binary search treetop view of binary treebinary tree top viewwhat is tree top viewwrite full code to print 360 view 28top 2b bottom 29 of a given binary treetop view of a tree exampletop view of binary tree pythontop view of a binary tree print top view of a binary tree cwrite a program to print top view of a binary treewhat is binary tree top view 3ftop view of binary tree javascripttop order traversal of binary treeprint nodes in a top view of binary treetop view of a binary tree codetop view of binary tree geeksforgeekstop view of a binary tree gfgtop view of binary tree in javatop view of binry treegiven a binary tree 2c print the nodes in left to right manner as visible from below the tree cpptop view of a tree 3dtop view tree gfgwhats top view of binary treeprint top view of a binary tree using recursiontop view of binary tree without hashingtop view of binary tree recursive pseudocodetop view of tree gfgtop view of a binary tree javatop view of binary tree recursivetop view of binary tree tech dosetop view of binary tree javatop view of bst in ctree top view javatop view of a tree gfgprint top view of binary treeprint top view of a binary treetree top view codetop view solution javayou are given a pointer to the root of a binary tree print the top view of the binary tree top view means when you look the tree from the top the nodes 2c what you will see will be called the top view of the tree see the example below top view of binary tree ltop level view of binary treetree top viewwhat is top view of binary treetop view of a binary tree in cpptop view tree c 2b 2btop tree gfgprint binary tree top downtop view of binary tree jsprint top view of a binary tree in cwhagt is topview of a treetop view of treetop view of binary tree recursive pythontree 3a top viewbinary tree top view 3fview of treetopview binary treetop view of binary tree c 2b 2b