top view of binary tree c++

/* 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;
}

4.33
3
Awgiedawgie 440215 points

                                    // C++ Program to print Top View of a binary Tree

#include &lt;iostream&gt;
#include &lt;queue&gt;
#include &lt;stack&gt;
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-&gt;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&lt;pair&lt;Node*, int&gt; &gt; 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&lt;int&gt; left;

		// vector is for holding right node's data
		vector&lt;int&gt; right;

		Node* node;

		while (q.size()) {

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

			if (hd &lt; l) {
				left.push(node-&gt;data);
				l = hd;
			}
			else if (hd &gt; r) {
				right.push_back(node-&gt;data);
				r = hd;
			}

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

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

		// then printing the root node's data
		cout &lt;&lt; root-&gt;data &lt;&lt; &quot; &quot;;

		// finally printing the right node's data
		for (auto x : right) {
			cout &lt;&lt; x &lt;&lt; &quot; &quot;;
		}
	}
};

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

4.33 (3 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
top tree gfg top view tree c++ top view of tree geeks top view of binary tree l topview of tree c++ Top View of Binary Tree implementation top view of tree in c++ binary tree top view top of the tree gfg top view binary tree c++ top view of a tree gfg top view of the binary tree. reverse-top view binary tree top view of a tree example top view of binary tree recursive pseudocode views of a tree top view of the tree rint the top view of the binary tree. top view of binary tree tech dose Print nodes in a top view of Binary Tree print top view of bst top view of binary tree js top view of tree java what is top view of binary tree top view of binary search tree top view of binary tree in java print top view of binary tree top view of bt top view of a binary tree code top view of a binary tree java print top view of a binary tree in c top view of a tree = top view of bst in c Top View of tree ? top view of binary tree geeksforgeeks Print Nodes in Top View of Binary Tree top view of binary tree practice gfg top view of binary tree javascript view of tree top view of a tree algorithm solution binary tree views picture of binary tree in order top view of binary tree java top view of binary tree is inorder all view of the tree in ds top view of binary tree recursive topview of tree get top view of a binary tree java get top view of a tree java c# binary tree top down print top view of a binary tree using recursion top view of binary tree without hashing Top View of Binary Tre top view tree gfg print top view of a binary tree C Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. Given a binary tree, print the top view of it. top view of tree print binary tree c++ top down print binary tree top down top view of binary tree in c what is top view of a binary search tree whats top view of binary tree whagt is topview of a tree Tree:Top view javascript solution Top View of a Tree in java write a program to print top view of a binary tree tree top view java how to print a binary top tree c++ top view of a tree top view of binary tree in C code what is binary tree top view? binary tree top view? top view recursive java topview binary tree how to find top view of a bst top level view of binary tree top view of a binary tree in cpp write full code to print 360 view(top + bottom) of a given binary tree top view binary tree top view of binary tree gfg top view of binary tree recursive python top view solution java visualization top view solution java top view of binary tree c++ topview of binary tree python tree top view code top view of a tree c# You 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, what you will see will be called the top view of the tree. See the example below. top view of binary tree python Given a binary tree , print the nodes in left to right manner as visible from below the tree cpp tree top view ou are given a pointer to the root of a binary tree. Print the top view of the binary tree. Top view of a tree. print top view of a binary tree Tree : Top View python top order traversal of binary tree top view of Binry Tree Tree : Top View what is tree top view top view of a binary tree top view of binary tree gfg top view of tree
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source