Skip to content

Commit

Permalink
Merge pull request #186 from favjava/main
Browse files Browse the repository at this point in the history
Added TopView of Tree
  • Loading branch information
TheFenrisLycaon authored Oct 9, 2022
2 parents 04a2131 + 5c27105 commit e5af8c1
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions C++/Algorithms/TopViewOfTree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include<bits/stdc++.h>
using namespace std;
// structure of node
struct Node
{
Node *left; //left pointer
Node *right;//right pointer
int data;
int line;//line denotes the line number 0 for root
//left side of root starts with negative line numbers
//right side of root starts with positive line numbers
};
//creation of new Node
Node* newNode(int data)
{
Node* node = new Node();
node->left = node->right = NULL;//assign NUll to both left and right pointer
node->data = data;//assign the value to the data field
return node;//return the node.
}
// this function identifies the nodes present at the top
void topView(Node *root)
{
vector<int>ans;// vector to store the final view nodes

if(root == NULL)return;
// map to store the node with line number
map<int,int> mp;
//queue to store the Node and line number
queue<pair<Node*,int>>q;
q.push({root,0});//push the very first Node i.e. root.
//traverse while the queue is not empty
while(!q.empty()){
auto it = q.front();//pointer to store the front of queue
q.pop();// then pop the element
// store the node and line in the respective variables
Node* node = it.first;
int line = it.second;

//if line not present in the map
//then add the line number with respective node.
if(mp.find(line)==mp.end())
{
mp[line] = node->data;
}

//if line present the traverse the other nodes.

//if left node present push the left node and decrement the line number
if(node->left != NULL)q.push({node->left ,line-1});
// if ritgh node present push the right node and increment the line number.
if(node->right != NULL)q.push({node->right,line+1});

}
// display the value of map which contains the top view
cout<<"Top View of Tree is: ";
for(auto i:mp)
{
cout<<i.second<<" ";
}
}
int main()
{
//create a tree with root and its childrens
Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right= newNode(5);
root->left->right->left = newNode(6);
root->right->right= newNode(7);
//call the function to calculate the top view.
topView(root);
return 0;
}

0 comments on commit e5af8c1

Please sign in to comment.