Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void transplant(Node<T> toBeRemoved, Node<T> toBePlanted) {
- Node<T> toBeRemovedParent = (Node<T>) toBeRemoved.getParent();
- if (!toBeRemoved.hasParent()) {
- root = toBePlanted;
- } else if (toBeRemoved.equals(toBeRemovedParent.getLeftChild())) {
- toBeRemovedParent.setLeftChild(toBePlanted);
- } else {
- toBeRemovedParent.setRightChild(toBePlanted);
- }
- toBeRemoved.setParent(null);
- if (toBePlanted != null) {
- Node<T> toBePlantedParent = (Node<T>) toBePlanted.getParent();
- // change
- if (!toBeRemoved.equals(toBePlantedParent)) {
- if (toBePlantedParent.hasLeftChild() && toBePlantedParent.getLeftChild().equals(toBePlanted)) {
- toBePlantedParent.setLeftChild((Node) toBePlanted.getRightChild());
- if (toBePlanted.hasRightChild())
- ((Node) toBePlanted.getRightChild()).setParent(toBePlantedParent);
- } else if (toBePlantedParent.hasRightChild() && toBePlantedParent.getRightChild().equals(toBePlanted)) {
- toBePlantedParent.setRightChild(null);
- }
- toBePlanted.setParent(toBeRemovedParent);
- if (toBeRemoved.hasLeftChild()) {
- Node<T> leftOfToBeRemoved = ((Node<T>) toBeRemoved.getLeftChild());
- toBePlanted.setLeftChild(leftOfToBeRemoved);
- leftOfToBeRemoved.setParent(toBePlanted);
- toBeRemoved.setLeftChild(null);
- }
- if (toBeRemoved.hasRightChild()) {
- Node<T> rightOfToBeRemoved = ((Node<T>) toBeRemoved.getRightChild());
- toBePlanted.setRightChild(rightOfToBeRemoved);
- rightOfToBeRemoved.setParent(toBePlanted);
- toBeRemoved.setRightChild(null);
- }
- } else if (toBeRemoved.equals(toBePlantedParent)) {
- toBePlanted.setParent(toBeRemovedParent);
- //
- if (toBeRemoved.hasRightChild() && toBeRemoved.getRightChild().equals(toBePlanted)) {
- toBePlanted.setLeftChild((Node) toBeRemoved.getLeftChild());
- if (toBeRemoved.hasLeftChild())
- ((Node) toBePlanted.getLeftChild()).setParent(toBePlanted);
- } else if (toBeRemoved.hasLeftChild() && toBeRemoved.getLeftChild().equals(toBePlanted)) {
- toBePlanted.setRightChild((Node) toBeRemoved.getRightChild());
- if (toBeRemoved.hasRightChild())
- ((Node) toBePlanted.getRightChild()).setParent(toBePlanted);
- }
- }
- deleteRebalance(toBePlanted);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement