Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. int remove (elem* &T, int key)
  2. {
  3. cout<<"\nDeleting "<<key<<" Key... ";
  4. elem *parent=NULL;
  5. elem *p=T;
  6. while ( (p!=NULL) && (p->key!=key) )
  7. {
  8. parent=p;
  9. if (p->key<key) { p=p->R; }
  10. else { p=p->L; }
  11. }
  12. if (p==NULL)
  13. {
  14. cout<<"ERROR 404. Element doesn't exisit!"<<endl;
  15. return -1;
  16. }
  17. if (p->R==NULL && p->L==NULL)
  18. {
  19. if (p==T)
  20. {
  21. cout<<"Removing...Korzen?";
  22. delete T;
  23. T=NULL;
  24. return 0;
  25. }
  26. if (parent->R==p) { parent->R=NULL; }
  27. else { parent->L=NULL; }
  28. }
  29. if (p->R==NULL)
  30. {
  31. if (parent->R==p) { parent->R=p->L; }
  32. else { parent->L=p->L; }
  33. delete p;
  34. return 0;
  35. }
  36. if (p->L==NULL)
  37. {
  38. if (parent->R==p) { parent->R=p->R; }
  39. else { parent->L=p->R; }
  40. delete p;
  41. return 0;
  42. }
  43.  
  44. elem *preparent=p;
  45. elem *child=p->L;
  46.  
  47. while(child->R!=NULL)
  48. {
  49. preparent=child;
  50. child=child->R;
  51. }
  52.  
  53. elem *foo=child->L;
  54. if (foo!=NULL) { while(foo!=NULL) { foo=foo->L; } }
  55. foo=preparent;
  56.  
  57. preparent->R=NULL;
  58. p=child;
  59.  
  60. return 0;
  61. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement