-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedBlackTree.java
More file actions
223 lines (170 loc) · 6.98 KB
/
RedBlackTree.java
File metadata and controls
223 lines (170 loc) · 6.98 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
public class RedBlackTree <T extends Comparable<T>> extends BSTRotation<T> {
/**
Inserting a node into a Red Black Tree
*/
@Override
public void insert(T value) {
if (value == null) {
throw new NullPointerException("No data to insert");
}
RBTNode<T> newNode = new RBTNode<>(value);
newNode.isRed = true;
//If the tree is empty set the newNode as the root
if (root == null) {
root = newNode;
}
else {
//Use the insertHelper method to insert the node
insertHelper(newNode, root);
}
//If the new node is not the root, make sure the red property is satisfied
if (newNode.getUp() != null) {
ensureRedProperty(newNode);
}
//The root is black after all insertions
if (this.root != null) {
((RBTNode<T>) this.root).isRed = false;
}
}
/**
* Checks if a new red node in the RedBlackTree causes a red property violation
* by having a red parent. If this is not the case, the method terminates without
* making any changes to the tree. If a red property violation is detected, then
* the method repairs this violation and any additional red property violations
* that are generated as a result of the applied repair operation.
* @param newRedNode a newly inserted red node, or a node turned red by previous repair
*/
protected void ensureRedProperty(RBTNode<T> newRedNode) {
// Case 1 - newRedNode is inserted at the root, must change to black and exit
RBTNode<T> root = (RBTNode<T>)(this.root);
if(newRedNode.equals(root)) {
newRedNode.isRed = false;
return;
}
// Case 2 - if the parent is black exit
RBTNode<T> parent = newRedNode.getUp();
if(!parent.isRed){
return;
}
//Case 3 - Red Aunt violations, need to swap colors parent, aunt, and grandparent
RBTNode<T> grandparent = parent.getUp();
// Aunt depends on what side the parent is on
RBTNode<T> aunt;
if (parent.equals(grandparent.getLeft())) {
aunt = grandparent.getRight();
} else {
aunt = grandparent.getLeft();
}
if(aunt != null && aunt.isRed){
aunt.isRed = false;
parent.isRed = false;
grandparent.isRed = true;
ensureRedProperty(grandparent);
return;
}
//Case 4 - Black Aunt or null violations
// 0 for left, 1 for right
int childSide = (newRedNode.equals(parent.getLeft())) ? 0 : 1;
// 0 for left, 1 for right
int parentSide = (parent.equals(grandparent.getLeft())) ? 0 : 1;
if(childSide == parentSide) {
// Black Line Case - rotate grandparent and parent, swap colors
rotate(parent, grandparent);
grandparent.isRed = true;
parent.isRed = false;
}
else {
// Black Zig Case - double rotation involving parent, child, and grandparent
// rotate parent and child
rotate(newRedNode, parent);
// rotate grandparent and child
rotate(newRedNode, grandparent);
// fix the colors after rotation
newRedNode.isRed = false;
grandparent.isRed = true;
}
}
/**
* Checks basic functionality of methods to see if the root
* would be black and the children should be red
*/
@Test
public void RedBlackTreeTest1(){
RedBlackTree<Integer> RBTTester = new RedBlackTree<>();
//Insert root value in the tree which has to be colored black
RBTTester.insert(10);
RBTNode<Integer> rootRBT = (RBTNode<Integer>) (RBTTester.root);
Assertions.assertEquals("10(b)", rootRBT.toString());
//Insert child values in the tree which have to be colored red
RBTTester.insert(5);
RBTTester.insert(15);
//Tree should be
// 10(b)
// 5(r) 15(r)
RBTNode<Integer> rightChild = rootRBT.getRight();
RBTNode<Integer> leftChild = rootRBT.getLeft();
Assertions.assertEquals("5(r)", leftChild.toString());
Assertions.assertEquals("15(r)", rightChild.toString());
}
/**
* Test containing a Q03.RBTInsert quiz example, question 2
*/
@Test
public void RedBlackTreeTest2(){
RedBlackTree<String> RBTTester = new RedBlackTree<>();
//Insert values in the tree
RBTTester.insert("L");
RBTTester.insert("F");
RBTTester.insert("T");
RBTTester.insert("B");
RBTTester.insert("J");
RBTTester.insert("N");
RBTTester.insert("S");
//Assign names to each node
RBTNode<String> rootRBT = (RBTNode<String>) (RBTTester.root);
RBTNode<String> rightParent = rootRBT.getRight();
RBTNode<String> leftParent = rootRBT.getLeft();
RBTNode<String> leftParentLeftChild = leftParent.getLeft();
RBTNode<String> leftParentRightChild = leftParent.getRight();
RBTNode<String> rightParentLeftChild = rightParent.getLeft();
RBTNode<String> rightParentRightChild = rightParent.getRight();
//The order should be
// L(b)
// F(b) S(b)
// B(r) J(r) N(r) T(r)
Assertions.assertEquals("L(b)", rootRBT.toString());
Assertions.assertEquals("F(b)", leftParent.toString());
Assertions.assertEquals("S(b)", rightParent.toString());
Assertions.assertEquals("B(r)", leftParentLeftChild.toString());
Assertions.assertEquals("J(r)", leftParentRightChild.toString());
Assertions.assertEquals("N(r)", rightParentLeftChild.toString());
Assertions.assertEquals("T(r)", rightParentRightChild.toString());
}
/**
* Test to see what happens when there is a Red Aunt Violation
*/
@Test
public void RedBlackTreeTest3(){
RedBlackTree<Integer> RBTTester = new RedBlackTree<>();
//Insert values in the tree
RBTTester.insert(10);
RBTTester.insert(8);
RBTTester.insert(12);
RBTTester.insert(1);
//Assign names to each node
RBTNode<Integer> grandparent = (RBTNode<Integer>) (RBTTester.root);
RBTNode<Integer> aunt = grandparent.getRight();
RBTNode<Integer> parent = grandparent.getLeft();
RBTNode<Integer> child = parent.getLeft();
//Tree should be
// 10(b)
// 8(b) 12(b)
// 1(r)
Assertions.assertEquals("10(b)", grandparent.toString());
Assertions.assertEquals("8(b)", grandparent.getLeft().toString());
Assertions.assertEquals("12(b)", grandparent.getRight().toString());
Assertions.assertEquals("1(r)", parent.getLeft().toString());
}
}