top view of a binary tree code example

Example 1: gfg top view of tree

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

Example 2: top view of binary tree c++

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

Tags:

Cpp Example