-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBSTRotation.java
More file actions
97 lines (78 loc) · 3.43 KB
/
BSTRotation.java
File metadata and controls
97 lines (78 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
public class BSTRotation<T extends Comparable<T>> extends BinarySearchTree<T> {
/**
* Performs the rotation operation on the provided nodes within this tree.
* When the provided child is a left child of the provided parent, this
* method will perform a right rotation. When the provided child is a right
* child of the provided parent, this method will perform a left rotation.
* When the provided nodes are not related in one of these ways, this
* method will either throw a NullPointerException: when either reference is
* null, or otherwise will throw an IllegalArgumentException.
*
* @param child is the node being rotated from child to parent position
* @param parent is the node being rotated from parent to child position
* @throws NullPointerException when either passed argument is null
* @throws IllegalArgumentException when the provided child and parent
* nodes are not initially (pre-rotation) related that way
*/
protected void rotate(BSTNode<T> child, BSTNode<T> parent)
throws NullPointerException, IllegalArgumentException {
if(child == null || parent == null) {
throw new NullPointerException("Child or Parent is null");
}
//LEFT ROTATION
if(parent.getRight() == child) {
//The child's left child becomes the parent's right child
parent.setRight(child.getLeft());
if(child.getLeft() != null) {
child.getLeft().setUp(parent);
}
//The child becomes the new parent as the parent in the subtree
child.setLeft(parent);
BSTNode<T> grandParentNode = parent.getUp();
//Update the grandparent node if there is one
if(grandParentNode != null) {
if(grandParentNode.getLeft() == parent) {
grandParentNode.setLeft(child);
} else {
grandParentNode.setRight(child);
}
}
//Updating the root
if(parent == root) {
root = child;
}
//Adjust the child and parent references to their parents
child.setUp(grandParentNode);
parent.setUp(child);
}
//RIGHT ROTATION
else if(parent.getLeft() == child) {
//The child's right child becomes the parent's left child
parent.setLeft(child.getRight());
if(child.getRight() != null) {
child.getRight().setUp(parent);
}
//The child becomes the new parent as the parent in the subtree
child.setRight(parent);
BSTNode<T> grandParentNode = parent.getUp();
////Update the grandparent node if there is one
if(grandParentNode != null) {
if(grandParentNode.getRight() == parent) {
grandParentNode.setRight(child);
} else {
grandParentNode.setLeft(child);
}
}
//Update the root
if (parent == root) {
root = child;
}
//Adjust the child and parent reference to their parents
child.setUp(grandParentNode);
parent.setUp(child);
} else {
throw new IllegalArgumentException("Nodes are not child or parent and " +
"can't be rotated");
}
}
}