Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- // Queue with List
- struct node
- {
- int data;
- struct node *lc;
- struct node *rc;
- };
- struct node *lc=NULL;
- struct node *rc=NULL;
- struct node *root=NULL;
- void insertion(int x)
- {
- struct node *N=new node;
- struct node *t=root;
- struct node *t1;
- N->data=x;
- N->lc=NULL;
- N->rc=NULL;
- if(root==NULL){
- root=N;
- }
- else
- {
- while(t!=NULL)
- {
- if(x>t->data)
- {
- t1=t;
- t=t->rc;
- }
- else{
- t1=t;
- t=t->lc;
- }
- }
- if(t1->data>x){
- t1->lc=N;
- }
- else{
- t1->rc=N;
- }
- }
- }
- void search(int x)
- {
- int flag=0;
- struct node *t=root;
- while(t!=NULL)
- {
- if(x==t->data)
- {
- flag=1;
- break;
- }
- else if(x>t->data)
- {
- t=t->rc;
- }
- else
- {
- t=t->lc;
- }
- }
- if(flag==1)
- {
- cout<<"The root itself is equal to your value!!"<<endl;
- }
- if(flag==0)
- {
- cout<<"Your value has been found"<<endl;
- }
- }
- void deletion(int x)
- {
- int flag=0;
- struct node *t=root;
- struct node *t1;
- while(t!=NULL)
- {
- if(x==t->data)
- {
- flag=1;
- break;
- }
- else if(x>t->data)
- {
- t1=t;
- t=t->rc;
- }
- else
- {
- t1=t;
- t=t->lc;
- }
- }
- if(flag==1)
- {
- if(t->rc==NULL && t->lc==NULL)
- {
- if(t1->lc==t)
- {
- t1->lc=NULL;
- }
- else
- {
- t1->rc=NULL;
- }
- }
- else if(t->rc!=NULL && t->lc!=NULL){
- cout<<"Pardon me ,we cannot delete"<<endl;
- }
- else
- {
- if(t1->lc==t)
- {
- if(t->rc!=NULL)
- {
- t1->lc=t->rc;
- }
- else
- {
- t1->lc=t->lc;
- }
- }
- else
- {
- if(t->rc==NULL)
- {
- t1->rc=t->lc;
- }
- else
- {
- t1->rc=t->rc;
- }
- }
- }
- }
- else{
- cout<<"No deletion possible "<<endl;
- }
- }
- void display(struct node *t)
- {
- if(t!=NULL)
- {
- display(t->lc);
- cout<<t->data<<" ";
- display(t->rc);
- }
- }
- int main()
- {
- insertion(10);
- insertion(20);
- insertion(30);
- search(10);
- display(root);
- cout<<endl;
- deletion(30);
- display(root);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement