Advertisement
Guest User

Untitled

a guest
Nov 16th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. public void delete(String name) {
  2. if(root==null) {return;}
  3. reader=root;
  4. Node branch=null;
  5. int swish,path=0;
  6. while(true) {
  7. swish=name.compareTo(reader.name);
  8. if(swish>0) {
  9. if(reader.rightChild!=null) {
  10. branch=reader;
  11. path=1;
  12. reader=reader.rightChild;
  13. } else {
  14. return;
  15. }
  16. } else if(swish<0) {
  17. if(reader.leftChild!=null) {
  18. path=-1;
  19. branch=reader;
  20. reader=reader.leftChild;
  21. } else {
  22. return;
  23. }
  24. } else {
  25. //thing was found, delete it
  26. Node left=reader.leftChild;
  27. Node right=reader.rightChild;
  28. Node put=null;
  29. if(left==null&&right==null) {
  30. put=null;
  31. } else if(right==null) {
  32. put=left;
  33. } else if(left==null) {
  34. put=right;
  35. } else {
  36. //find inorder successor
  37. Node successor=right;
  38. Node presuccessor=reader;
  39. while(successor.leftChild!=null) {
  40. presuccessor=successor;
  41. successor=successor.leftChild;
  42. }
  43. successor.leftChild=reader.leftChild;
  44. presuccessor.leftChild=null;
  45. if(successor!=right) {
  46. Node place=successor;
  47. while(place.rightChild!=null) {
  48. place=place.rightChild;
  49. }
  50. place.rightChild=right;
  51. }
  52. put=successor;
  53. }
  54. if(path==1) {
  55. branch.rightChild=put;
  56. } else if(path==-1) {
  57. branch.leftChild=put;
  58. } else {
  59. root=put;
  60. }
  61. return;
  62. }
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement