forked from patmorin/ods
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedBlackTree.java
More file actions
338 lines (313 loc) · 7.13 KB
/
RedBlackTree.java
File metadata and controls
338 lines (313 loc) · 7.13 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package ods;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Random;
import java.util.SortedSet;
import java.util.TreeSet;
public class RedBlackTree<T> extends BinarySearchTree<RedBlackTree.Node<T>, T>
implements SSet<T> {
protected static class Node<T> extends BinarySearchTree.BSTNode<Node<T>,T> {
byte colour;
}
static byte red = 0;
static byte black = 1;
public RedBlackTree(Comparator<T> c) {
super(new Node<T>(), new Node<T>(), c);
nil.colour = black;
}
public RedBlackTree() {
this(new DefaultComparator<T>());
}
/**
* Make u lighter and its children darker
* @param u
*/
protected void pushBlack(Node<T> u) {
u.colour--;
u.left.colour++;
u.right.colour++;
}
/**
* Make u darker and its children lighter
* @param u
*/
protected void pullBlack(Node<T> u) {
u.colour++;
u.left.colour--;
u.right.colour--;
}
protected void flipLeft(Node<T> u) {
swapColors(u, u.right);
rotateLeft(u);
}
protected void flipRight(Node<T> u) {
swapColors(u, u.left);
rotateRight(u);
}
/**
* Swap the color of u and w
* @param u
* @param w
*/
protected void swapColors(Node<T> u, Node<T> w) {
byte tmp = u.colour;
u.colour = w.colour;
w.colour = tmp;
}
public boolean add(T x) {
Node<T> u = newNode(x);
u.colour = red;
boolean added = add(u);
if (added)
addFixup(u);
return added;
}
/**
* Fixup the newly added node u. u is a red node. Each iteration ensures
* that (1) u is red, (2) the only red-red edge [if any] is between u and
* u.parent (3) the only right-leaning node [if any] is u.parent.
*
* @param u
*/
protected void addFixup(Node<T> u) {
while (u.colour == red) {
if (u == r) { // u is the root - done
u.colour = black;
return;
}
Node<T> w = u.parent;
if (w.left.colour == black) { // ensure left-leaning
flipLeft(w);
u = w;
w = u.parent;
}
if (w.colour == black)
return; // no red-red edge = done
Node<T> g = w.parent; // grandparent of u
if (g.right.colour == black) {
flipRight(g);
return;
} else {
pushBlack(g);
u = g;
}
}
}
public boolean remove(T x) {
Node<T> u = findLast(x);
if (u == nil || c.compare(u.x, x) != 0)
return false;
Node<T> w = u.right;
if (w == nil) {
w = u;
u = w.left;
} else {
while (w.left != nil)
w = w.left;
u.x = w.x;
u = w.right;
}
splice(w);
u.colour += w.colour;
u.parent = w.parent;
removeFixup(u);
return true;
}
/**
* Fixup u after the removal of u's parent. u is a node whose color is
* 1(black) or 2(double-black). In the latter case we do work to get rid of
* the double-black node.
*
* @param u
*/
protected void removeFixup(Node<T> u) {
while (u.colour > black) {
if (u == r) {
u.colour = black;
} else if (u.parent.left.colour == red) {
u = removeFixupCase1(u);
} else if (u == u.parent.left) {
u = removeFixupCase2(u);
} else {
u = removeFixupCase3(u);
}
}
if (u != r) { // restore left-leaning property if needed
Node<T> w = u.parent;
if (w.right.colour == red && w.left.colour == black) {
flipLeft(w);
}
}
}
/**
* This case gets applied when the tree looks like this
* 1
* / \
* 0 2(u)
* @param u
* @return the next node to fix up
*/
protected Node<T> removeFixupCase1(Node<T> u) {
flipRight(u.parent);
return u;
}
/**
* This case gets applied when the tree looks like this
* ?
* / \
* (u)2 1
* @param u
* @return the next node to fix up
*/
protected Node<T> removeFixupCase2(Node<T> u) {
Node<T> w = u.parent;
Node<T> v = w.right;
pullBlack(w); // w.left
flipLeft(w); // w is now red
Node<T> q = w.right;
if (q.colour == red) { // q-w is red-red
rotateLeft(w);
flipRight(v);
pushBlack(q);
if (v.right.colour == red)
flipLeft(v);
return q;
} else {
return v;
}
}
/**
* This case gets applied when the tree looks like this
* ?
* / \
* 1 2(u)
* @param u
* @return the next node to fix up
*/
protected Node<T> removeFixupCase3(Node<T> u) {
Node<T> w = u.parent;
Node<T> v = w.left;
pullBlack(w);
flipRight(w); // w is now red
Node<T> q = w.left;
if (q.colour == red) { // q-w is red-red
rotateRight(w);
flipLeft(v);
pushBlack(q);
return q;
} else {
if (v.left.colour == red) {
pushBlack(v); // both v's children are red
return v;
} else { // ensure left-leaning
flipLeft(v);
return w;
}
}
}
/**
* Debugging function that verifies the red-black tree properties
*/
protected void verify() {
if (size(r) != n)
throw new IllegalArgumentException("size is incorrect");
verify(r);
}
/**
* Debugging function that verifies the red-black tree properties
* for the subtree rooted at u
* @param u
* @return the black height of the node u
*/
protected int verify(Node<T> u) {
if (u == nil)
return u.colour;
if (u.colour < red || u.colour > black)
throw new AssertionError("Invalid color: " + u.colour);
if (u.colour == red)
if (u.left.colour == red || u.right.colour == red)
throw new AssertionError("red-red edge found");
if (u.right.colour == red && u.left.colour != red)
throw new AssertionError("non-left-leaning node found");
int dl = verify(u.left);
int dr = verify(u.right);
if (dl != dr)
throw new AssertionError("black-height property violated");
return dl + u.colour;
}
/**
* @param args
*/
public static void main(String[] args) {
RedBlackTree<Integer> s = new RedBlackTree<Integer>();
// sorted sequence
for (int i = 0; i < 100; i++) {
s.add(i);
}
for (Integer x : s) {
System.out.print(x + " ");
}
System.out.println();
s.clear();
// reverse sorted sequence
for (int i = 99; i >= 0; i--) {
s.add(i);
}
for (Integer x : s) {
System.out.print(x + " ");
}
System.out.println();
s.clear();
// pseudorandom sequence
for (int i = 1; i <= 101; i++) {
s.add((73*i)%101);
}
for (Integer x : s) {
System.out.print(x + " ");
}
System.out.println();
// pseudorandom sequence
for (int i = 1; i <= 50; i++) {
s.remove((i*89)%101);
}
for (Integer x : s) {
System.out.print(x + " ");
}
System.out.println();
System.out.print("Comparing to TreeSet...");
System.out.flush();
SortedSet<Integer> ts = new TreeSet<Integer>();
s.clear();
Random rand = new Random();
int n = 100000;
for (int i = 0; i < n; i++) {
Integer x = rand.nextInt();
ts.add(x);
s.add(x);
}
s.verify();
Utils.myassert(ts.size() == s.size());
Iterator<Integer> tsi = ts.iterator();
Iterator<Integer> si = s.iterator();
while (tsi.hasNext()) {
Utils.myassert(tsi.next().equals(si.next()));
}
Utils.myassert(s.size() == ts.size());
for (int i = 0; i < n/2; i++) {
Integer x = rand.nextInt();
Integer y = s.findGE(x);
if (y != null) {
s.remove(y);
ts.remove(y);
}
}
s.verify();
Utils.myassert(ts.size() == s.size());
tsi = ts.iterator();
si = s.iterator();
while (tsi.hasNext()) {
Utils.myassert(tsi.next().equals(si.next()));
}
System.out.println("done");
}
}