Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int remove (elem* &T, int key)
- {
- cout<<"\nDeleting "<<key<<" Key... ";
- elem *parent=NULL;
- elem *p=T;
- while ( (p!=NULL) && (p->key!=key) )
- {
- parent=p;
- if (p->key<key) { p=p->R; }
- else { p=p->L; }
- }
- if (p==NULL)
- {
- cout<<"ERROR 404. Element doesn't exisit!"<<endl;
- return -1;
- }
- if (p->R==NULL && p->L==NULL)
- {
- if (p==T)
- {
- cout<<"Removing...Korzen?";
- delete T;
- T=NULL;
- return 0;
- }
- if (parent->R==p) { parent->R=NULL; }
- else { parent->L=NULL; }
- }
- if (p->R==NULL)
- {
- if (parent->R==p) { parent->R=p->L; }
- else { parent->L=p->L; }
- delete p;
- return 0;
- }
- if (p->L==NULL)
- {
- if (parent->R==p) { parent->R=p->R; }
- else { parent->L=p->R; }
- delete p;
- return 0;
- }
- elem *preparent=p;
- elem *child=p->L;
- while(child->R!=NULL)
- {
- preparent=child;
- child=child->R;
- }
- elem *foo=child->L;
- if (foo!=NULL) { while(foo!=NULL) { foo=foo->L; } }
- foo=preparent;
- preparent->R=NULL;
- p=child;
- return 0;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement