Created
February 24, 2013 17:13
-
-
Save girishkvs/5024634 to your computer and use it in GitHub Desktop.
This is a c++ program for data structure splay tree
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> | |
| using namespace std; | |
| class splay | |
| { | |
| private: | |
| int val; | |
| splay *left; | |
| splay *right; | |
| public: | |
| void create(splay **hptr,int num); | |
| void insert(splay *hptr,int num); | |
| splay *leftr(splay *ptr); | |
| splay *rightr(splay *ptr); | |
| splay *leftrightr(splay *ptr); | |
| splay *rightleftr(splay *ptr); | |
| splay *rightright(splay *ptr); | |
| splay *leftleft(splay *ptr); | |
| void rotation(splay **hptr,int num); | |
| void find1(splay *hptr,int num); | |
| void find2(splay *hptr); | |
| void find3(splay *hptr); | |
| void tran(splay *hptr); | |
| void rotation1(splay **hptr,int num); | |
| }; | |
| splay *pptr=NULL,*gpptr=NULL,*ggpptr=NULL; | |
| void splay::create(splay **hptr,int num) | |
| { | |
| splay *temp=new splay; | |
| temp->val=num; | |
| temp->left=NULL; | |
| temp->right=NULL; | |
| *hptr=temp; | |
| } | |
| void splay::find1(splay *hptr,int num) | |
| { | |
| splay *temp=hptr; | |
| if(num==hptr->val) | |
| { | |
| pptr=NULL; | |
| gpptr=NULL; | |
| ggpptr=NULL; | |
| } | |
| else | |
| { | |
| while(hptr!=NULL) | |
| { | |
| pptr=hptr; | |
| if(hptr->val>num) | |
| hptr=hptr->left; | |
| else | |
| hptr=hptr->right; | |
| if(hptr->val==num) | |
| break; | |
| } | |
| cout<<"pptr "<<pptr->val; | |
| } | |
| } | |
| void splay::find2(splay *hptr) | |
| { | |
| if(pptr->val==hptr->val) | |
| {cout<<"\n Hello 22"; | |
| gpptr=NULL; | |
| ggpptr=NULL; | |
| } | |
| else | |
| { | |
| while(hptr!=NULL) | |
| { | |
| gpptr=hptr; | |
| if(pptr->val<hptr->val) | |
| hptr=hptr->left; | |
| else | |
| hptr=hptr->right; | |
| if(pptr->val==hptr->val) | |
| break; | |
| } | |
| cout<<"\n gpptr "<<gpptr->val; | |
| } | |
| } | |
| void splay::find3(splay *hptr) | |
| { | |
| if(gpptr!=NULL) | |
| { | |
| if(hptr->val==gpptr->val) | |
| ggpptr=NULL; | |
| else | |
| { | |
| while(hptr!=NULL) | |
| { | |
| ggpptr=hptr; | |
| if(gpptr->val<hptr->val) | |
| hptr=hptr->left; | |
| else | |
| hptr=hptr->right; | |
| if(gpptr->val==hptr->val) | |
| break; | |
| } | |
| cout<<" ggpptr "<<ggpptr->val; | |
| } | |
| } | |
| else if(gpptr==NULL) | |
| ggpptr=NULL; | |
| } | |
| void splay::insert(splay *hptr,int num) | |
| { | |
| splay *temp,*temp1,*temp3; | |
| temp=hptr; | |
| temp1=new splay; | |
| while(temp!=NULL) | |
| { | |
| temp3=temp; | |
| if(temp->val>num) | |
| temp=temp->left; | |
| else if(temp->val<num) | |
| temp=temp->right; | |
| } | |
| if(temp3->val>num) | |
| { | |
| cout<<"\n Inserting to left\n"; | |
| temp3->left=temp1; | |
| } | |
| else | |
| { | |
| cout<<"\n Inserting to Right\n"; | |
| temp3->right=temp1; | |
| } | |
| temp1->val=num; | |
| temp1->left=NULL; | |
| temp1->right=NULL; | |
| } | |
| void splay::rotation1(splay **hptr,int num) | |
| { | |
| if(pptr==NULL); | |
| else if(pptr->val==(*hptr)->val&&gpptr==NULL) | |
| { cout<<"\n Hello\n"; | |
| if(num>pptr->val) | |
| *hptr=leftr(pptr); | |
| else | |
| *hptr=rightr(pptr); | |
| } | |
| else if(gpptr==*hptr) | |
| { | |
| if(pptr->val<num&&pptr->val>gpptr->val) | |
| *hptr=leftleft(gpptr); | |
| else if(pptr->val<num&&pptr->val<gpptr->val) | |
| *hptr=leftrightr(gpptr); | |
| else if(pptr->val>num&&pptr->val<gpptr->val) | |
| *hptr=rightright(gpptr); | |
| else if(pptr->val>num&&gpptr->val<pptr->val) | |
| *hptr=rightleftr(gpptr); | |
| } | |
| else | |
| { | |
| if(pptr->val<num&&pptr->val>gpptr->val) | |
| { | |
| splay *head=*hptr; | |
| if(ggpptr->left==gpptr) | |
| ggpptr->left=leftleft(gpptr); | |
| else | |
| ggpptr->right=leftleft(gpptr); | |
| find1(head,num); | |
| find2(head); | |
| find3(head); | |
| rotation1(hptr,num); | |
| } | |
| else if(pptr->val<num&&gpptr->val>pptr->val) | |
| { | |
| splay *head=*hptr; | |
| if(ggpptr->left==gpptr) | |
| ggpptr->left=leftrightr(gpptr); | |
| else | |
| ggpptr->right=leftrightr(gpptr); | |
| find1(head,num); | |
| find2(head); | |
| find3(head); | |
| rotation1(hptr,num); | |
| } | |
| else if(pptr->val>num&&gpptr->val>pptr->val) | |
| { | |
| splay *head=*hptr; | |
| if(ggpptr->left==gpptr) | |
| ggpptr->left=rightright(gpptr); | |
| else | |
| ggpptr->right=rightright(gpptr); | |
| find1(head,num); | |
| find2(head); | |
| find3(head); | |
| rotation1(hptr,num); | |
| } | |
| else if(pptr->val>num&&gpptr->val<pptr->val) | |
| { | |
| splay *head=*hptr; | |
| if(ggpptr->left==gpptr) | |
| ggpptr->left=rightleftr(gpptr); | |
| else | |
| ggpptr->right=rightleftr(gpptr); | |
| find1(head,num); | |
| find2(head); | |
| find3(head); | |
| rotation1(hptr,num); | |
| } | |
| } | |
| cout<<"\n Errors\n"; | |
| } | |
| splay *splay::leftr(splay *ptr) | |
| { | |
| splay *jptr=ptr; | |
| splay *kptr=jptr->right; | |
| jptr->right=kptr->left; | |
| kptr->left=jptr; | |
| return kptr; | |
| } | |
| splay *splay::rightr(splay *ptr) | |
| { | |
| splay *jptr=ptr; | |
| splay *kptr=jptr->left; | |
| jptr->left=kptr->right; | |
| kptr->right=jptr; | |
| return kptr; | |
| } | |
| splay *splay::leftrightr(splay *ptr) | |
| { | |
| splay *jptr=ptr; | |
| splay *kptr=jptr->left; | |
| jptr->left=leftr(kptr); | |
| return(rightr(jptr)); | |
| } | |
| splay *splay::rightleftr(splay *ptr) | |
| { | |
| splay *jptr=ptr; | |
| splay *kptr=jptr->right; | |
| jptr->right=rightr(kptr); | |
| return(leftr(jptr)); | |
| } | |
| splay *splay::leftleft(splay *ptr) | |
| { | |
| splay *jptr=ptr; | |
| splay *kptr; | |
| kptr=leftr(jptr); | |
| return leftr(kptr); | |
| } | |
| splay *splay::rightright(splay *ptr) | |
| { | |
| splay *jptr=ptr; | |
| splay *kptr; | |
| kptr=rightr(jptr); | |
| return rightr(kptr); | |
| } | |
| void splay::tran(splay *hptr) | |
| { | |
| if(hptr!=NULL) | |
| { | |
| cout<<hptr->val<<" "; | |
| tran(hptr->left); | |
| tran(hptr->right); | |
| } | |
| } | |
| int main() | |
| { | |
| splay *head=NULL,a; | |
| int n,flag; | |
| cout<<"\n Enter the val\n"; | |
| cin>>n; | |
| a.create(&head,n); | |
| while(1) | |
| { | |
| cout<<"\n Enter the valu -- "; | |
| cin>>n; | |
| a.insert(head,n); | |
| a.find1(head,n); | |
| a.find2(head); | |
| a.find3(head); | |
| a.rotation1(&head,n); | |
| cout<<"\n"; | |
| a.tran(head); | |
| cout<<"\n Enter the Flag ---- "; | |
| cin>>flag; | |
| if(flag==0) | |
| break; | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment