Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. public boolean delete(Node node, Delegate d){
  2. Node current = root;
  3. Node parent = root;
  4. boolean leftChild = false;
  5. while(current.delegate != delegate){
  6. parent = current;
  7. if(d < current.d){
  8. // Move to the left if searched value is less
  9. current = current.left;
  10. leftChild = true;
  11. }
  12. else{
  13. // Move to the right if searched value is >=
  14. current = current.right;
  15. leftChild = false;
  16. }
  17. if(current == null){
  18. return false;
  19. }
  20. }
  21. // if reached here means node to be deleted is found
  22.  
  23. // Leaf node deletion case
  24. if(current.left == null && current.right == null){
  25. System.out.println("Leaf node deletion case");
  26. // if root node is to be deleted
  27. if(current == root){
  28. root = null;
  29. }
  30. // left child
  31. else if(leftChild){
  32. parent.left = null;
  33. }
  34. // right child
  35. else{
  36. parent.right = null;
  37. }
  38. }
  39. // Node to be deleted has one child case
  40. // Node to be deleted has right child
  41. else if(current.left == null){
  42. System.out.println("One right child deletion case");
  43. // if root node is to be deleted
  44. if(current == root){
  45. root = current.right;
  46. }
  47. // if deleted node is left child
  48. else if(leftChild){
  49. parent.left = current.right;
  50. }
  51. // if deleted node is right child
  52. else{
  53. parent.right = current.right;
  54. }
  55. }
  56. // Node to be deleted has left child
  57. else if(current.right == null){
  58. System.out.println("One left child deletion case");
  59. if(current == root){
  60. root = current.left;
  61. }
  62. // if deleted node is left child
  63. else if(leftChild){
  64. parent.left = current.left;
  65. }
  66. // if deleted node is right child
  67. else{
  68. parent.right = current.left;
  69. }
  70. }
  71. // Node to be deleted has two children case
  72. else{
  73. System.out.println("Two children deletion case");
  74. // find in-order heir of the node to be deleted
  75. Node heir = findHeir(current);
  76. if(current == root){
  77. root = heir;
  78. }
  79. // if deleted node is left child
  80. else if(leftChild){
  81. parent.left = heir;
  82. }
  83. // if deleted node is right child
  84. else{
  85. parent.right = heir;
  86. }
  87. heir.left = current.left;
  88. }
  89. return true;
  90. }
  91. // Method to find the in-order heir of the deleted node
  92. private Node findHeir(Node node){
  93. Node successor = node;
  94. Node successorParent = node;
  95. // Start from the right child of the node to be deleted
  96. Node current = node.right;
  97. while(current != null){
  98. successorParent = successor;
  99. successor = current;
  100. current = current.left;
  101. }
  102. // When In-order heir is in the left subtree
  103. // perform two ref changes here as we have
  104. // access to successorParent
  105. if(successor != node.right){
  106. successorParent.left = successor.right;
  107. // applicable only when heir is not right child
  108. // so doing here
  109. successor.right = node.right;
  110. }
  111. return successor;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement