Advertisement
FNSY

Untitled

Mar 23rd, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. private void transplant(Node<T> toBeRemoved, Node<T> toBePlanted) {
  2.  
  3. Node<T> toBeRemovedParent = (Node<T>) toBeRemoved.getParent();
  4. if (!toBeRemoved.hasParent()) {
  5. root = toBePlanted;
  6. } else if (toBeRemoved.equals(toBeRemovedParent.getLeftChild())) {
  7. toBeRemovedParent.setLeftChild(toBePlanted);
  8. } else {
  9. toBeRemovedParent.setRightChild(toBePlanted);
  10. }
  11. toBeRemoved.setParent(null);
  12. if (toBePlanted != null) {
  13. Node<T> toBePlantedParent = (Node<T>) toBePlanted.getParent();
  14.  
  15. // change
  16. if (!toBeRemoved.equals(toBePlantedParent)) {
  17. if (toBePlantedParent.hasLeftChild() && toBePlantedParent.getLeftChild().equals(toBePlanted)) {
  18. toBePlantedParent.setLeftChild((Node) toBePlanted.getRightChild());
  19. if (toBePlanted.hasRightChild())
  20. ((Node) toBePlanted.getRightChild()).setParent(toBePlantedParent);
  21. } else if (toBePlantedParent.hasRightChild() && toBePlantedParent.getRightChild().equals(toBePlanted)) {
  22. toBePlantedParent.setRightChild(null);
  23. }
  24.  
  25. toBePlanted.setParent(toBeRemovedParent);
  26.  
  27. if (toBeRemoved.hasLeftChild()) {
  28. Node<T> leftOfToBeRemoved = ((Node<T>) toBeRemoved.getLeftChild());
  29. toBePlanted.setLeftChild(leftOfToBeRemoved);
  30. leftOfToBeRemoved.setParent(toBePlanted);
  31. toBeRemoved.setLeftChild(null);
  32.  
  33. }
  34.  
  35. if (toBeRemoved.hasRightChild()) {
  36. Node<T> rightOfToBeRemoved = ((Node<T>) toBeRemoved.getRightChild());
  37. toBePlanted.setRightChild(rightOfToBeRemoved);
  38. rightOfToBeRemoved.setParent(toBePlanted);
  39. toBeRemoved.setRightChild(null);
  40.  
  41. }
  42. } else if (toBeRemoved.equals(toBePlantedParent)) {
  43.  
  44. toBePlanted.setParent(toBeRemovedParent);
  45. //
  46. if (toBeRemoved.hasRightChild() && toBeRemoved.getRightChild().equals(toBePlanted)) {
  47. toBePlanted.setLeftChild((Node) toBeRemoved.getLeftChild());
  48. if (toBeRemoved.hasLeftChild())
  49. ((Node) toBePlanted.getLeftChild()).setParent(toBePlanted);
  50. } else if (toBeRemoved.hasLeftChild() && toBeRemoved.getLeftChild().equals(toBePlanted)) {
  51. toBePlanted.setRightChild((Node) toBeRemoved.getRightChild());
  52. if (toBeRemoved.hasRightChild())
  53. ((Node) toBePlanted.getRightChild()).setParent(toBePlanted);
  54. }
  55. }
  56. deleteRebalance(toBePlanted);
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement