Created
February 22, 2017 14:11
-
-
Save shubham100795/b3e5184a8326494df3317a9ab75bcb37 to your computer and use it in GitHub Desktop.
print the bottom view , vertical view and top view of the BST
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<iostream> | |
| #include<cstdlib> | |
| #include<queue> | |
| #include<map> | |
| using namespace std; | |
| struct node{ | |
| int data; | |
| struct node *left; | |
| struct node *right; | |
| }; | |
| class qitem{ | |
| public: | |
| struct node *tnode; | |
| int hd; | |
| qitem(struct node *n,int x) | |
| { | |
| tnode=n; | |
| hd=x; | |
| } | |
| }; | |
| struct node *newnode(int x) | |
| { | |
| struct node* ptr=(struct node*)malloc(sizeof(struct node)); | |
| ptr->data=x; | |
| ptr->left=NULL; | |
| ptr->right=NULL; | |
| return ptr; | |
| } | |
| struct node* insert(struct node *root,int x) | |
| { | |
| struct node* par; | |
| if(root==NULL) | |
| { | |
| root=newnode(x); | |
| return root; | |
| } | |
| else | |
| { | |
| struct node *proot=root; | |
| while(proot!=NULL) | |
| { | |
| if(x<proot->data) | |
| { | |
| par=proot; | |
| proot=proot->left; | |
| } | |
| else | |
| { | |
| par=proot; | |
| proot=proot->right; | |
| } | |
| } | |
| if(x<par->data) | |
| par->left=newnode(x); | |
| else | |
| par->right=newnode(x); | |
| return root; | |
| } | |
| } | |
| void display(struct node *root) | |
| { | |
| if(root==NULL) | |
| return; | |
| display(root->left); | |
| cout<<"\t"<<root->data; | |
| display(root->right); | |
| } | |
| void topbottomverticalview(struct node *root,map<int,vector<int> >&mp) | |
| { | |
| queue<qitem*> q; | |
| if(root==NULL) | |
| return; | |
| q.push(new qitem(root,0)); | |
| while(!q.empty()) | |
| { | |
| qitem *element=q.front(); | |
| int d=element->hd; | |
| struct node *n=element->tnode; | |
| q.pop(); | |
| mp[d].push_back(n->data); | |
| if(n->left) | |
| { | |
| q.push(new qitem(n->left,d-1)); | |
| } | |
| if(n->right) | |
| { | |
| q.push(new qitem(n->right,d+1)); | |
| } | |
| } | |
| } | |
| int main() | |
| { | |
| int x,i; | |
| map<int,vector<int> >mp; | |
| map<int,vector<int> >::iterator it; | |
| struct node *root=NULL; | |
| while(1) | |
| { | |
| cin>>x; | |
| if(x==-1) | |
| break; | |
| root=insert(root,x); | |
| } | |
| display(root); | |
| cout<<endl; | |
| topbottomverticalview(root,mp); | |
| cout<<"vertical view--"; | |
| cout<<endl; | |
| for(it=mp.begin();it!=mp.end();it++) | |
| { | |
| for(i=0;i<(it->second.size());i++) | |
| { | |
| cout<<it->second[i]<<" "; | |
| } | |
| cout<<endl; | |
| } | |
| cout<<endl; | |
| cout<<"top view--"; | |
| cout<<endl; | |
| for(it=mp.begin();it!=mp.end();it++) | |
| { | |
| cout<<it->second[0]<<" "; | |
| } | |
| cout<<endl; | |
| cout<<"bottom view--"; | |
| for(it=mp.begin();it!=mp.end();it++) | |
| { | |
| cout<<it->second[it->second.size()-1]<<" "; | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment