Advertisement
Guest User

Untitled

a guest
May 28th, 2015
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. TTree* delNode(TTree *&root,int key)
  2. {
  3. if(!root)
  4. return ptr;
  5. if(root->data < key)
  6. root->right = del_node(root->right,key);
  7. if(root->data > key)
  8. root->left = del_node(root->left,key);
  9. if(root->data == key)
  10. {
  11. if(!root->right && !root->left)
  12. {
  13. ptr = NULL;
  14. delete root;
  15. return ptr;
  16. }
  17. if(!root->left && root->right)
  18. {
  19. ptr = root->right;
  20. delete root;
  21. return ptr;
  22. }
  23. if(!root->right && root->left)
  24. {
  25. ptr = root->left;
  26. delete root;
  27. return ptr;
  28. }
  29. if(root->right && root->left)
  30. {
  31. int i = 0;
  32. TTree *ptr1 = NULL;
  33. ptr = Root->right;
  34. ptr1 = ptr;
  35. while(ptr->left)
  36. {
  37. ptr1 = ptr;
  38. ptr = ptr->left;
  39. i++;
  40. }
  41. if(i!=0)
  42. {
  43. ptr1->left = ptr->right;
  44. ptr->right = Root->right;
  45. }
  46. ptr->left = Root->left;
  47. delete Root;
  48. Root = ptr;
  49. return ptr;
  50. }
  51. }
  52. return Root;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement