-
Notifications
You must be signed in to change notification settings - Fork 0
/
RedBlackTree.java
321 lines (279 loc) · 9.7 KB
/
RedBlackTree.java
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
// an implementation of a left-leaning Red/Black Binary Search Tree
import Queue;
import java.util.NoSuchElementException;
public class RedBlackTree<K extends Comparable<K>, V> {
private Node root;
private static final boolean RED = true;
private static final boolean BLACK = false;
private class Node {
private K key;
private V value;
private int count;
private boolean color;
private Node left, right;
private Node(K kee, V val, boolean col, int count) {
key = kee;
value = val;
color = col;
}
}
private boolean isRed(Node curr) {
if (curr == null) { return false; }
return curr.color;
}
// flip a right-leaning red link to the left
private Node rotateLeft(Node curr) {
Node next = curr.right;
assert isRed(next);
curr.right = next.left;
next.left = curr;
next.color = curr.color;
curr.color = RED;
next.count = curr.count;
curr.count = 1 + size(curr.left) + size(curr.right);
return next;
}
// flip a left-leaning red link to the right (for temporary use)
private Node rotateRight(Node curr) {
Node next = curr.left;
assert isRed(next);
curr.left = next.right;
next.right = curr;
next.color = curr.color;
curr.color = RED;
next.count = curr.count;
curr.count = 1 + size(curr.left) + size(curr.right);
return next;
}
// flips the color of a node and both its children
private void flipColors(Node head) {
head.color = !head.color;
head.left.color = !head.left.color;
head.right.color = !head.right.color;
}
private Node moveRedLeft(Node curr) {
flipColors(curr);
if (isRed(curr.right.left)) {
curr.right = rotateRight(curr.right);
curr = rotateLeft(curr);
flipColors(curr);
}
return curr;
}
private Node moveRedRight(Node curr) {
flipColors(curr);
if (isRed(curr.left.left)) {
curr = rotateRight(curr);
flipColors(curr);
}
return curr;
}
private Node balance(Node curr) {
if (isRed(curr.right)) { curr = rotateLeft(curr); }
if (isRed(curr.left) && isRed(curr.left.left)) {
curr = rotateRight(curr);
}
if (isRed(curr.right) && isRed(curr.right)) { flipColors(curr); }
curr.count = 1 + size(curr.left) + size(curr.right);
return curr;
}
public int size() {
return size(root);
}
private int size(Node curr) {
if (curr == null) { return 0; }
else { return curr.count; }
}
public void add(K kee, V val) {
if (kee == null) {
throw new IllegalArgumentException("first argument to put() is null");
}
if (val == null) {
delete(kee);
return;
}
root = add(root, kee, val);
root.color = BLACK;
}
private Node add(Node curr, K kee, V val) {
// base case 1: insert a new node in the proper spot
if (curr == null) { return new Node(kee, val, RED, 1); }
int cmp = kee.compareTo(curr.key);
if (cmp < 0) { curr.left = add(curr.left, kee, val); }
else if (cmp > 0) { curr.right = add(curr.right, kee, val); }
else { curr.value = val; } // base case 2: if key matches, update value
if (isRed(curr.right) && !isRed(curr.left)) {
// fix a right-leaning red link
curr = rotateLeft(curr);
}
if (isRed(curr.left) && isRed(curr.left.left)) {
// balance a 4-node
curr = rotateRight(curr);
}
if (isRed(curr.left) && isRed(curr.right)) {
// split a 4-node
curr = flipColors(curr);
}
curr.count = 1 + size(curr.left) + size(curr.right);
return curr;
}
public V get(K kee) {
if (key == null) {
throw new IllegalArgumentException("Argument to get() is null");
}
Node curr = root;
while (curr != null) {
int cmp = kee.compareTo(curr.key);
if (cmp < 0) { curr = curr.left; }
else if (cmp > 0) { curr = curr.right; }
else { return curr.value; }
}
// if not found, return null.
return null;
}
public boolean contains(K kee) {
return get(kee) != null;
}
public K min() {
if (root == null) { throw new NoSuchElementException("called min() with empty tree"); }
return min(root).key;
}
private Node min(Node curr) {
if (curr.left == null) { return curr; }
else { return min(curr.left); }
}
public K max() {
if (root == null) { throw new NoSuchElementException("called max() with empty tree"); }
return max(root).key;
}
private Node max(Node curr) {
if (curr.right == null) { return curr; }
else { return max(curr.right); }
}
public K floor(K kee) {
if (kee == null) {
throw new IllegalArgumentException("argument to floor() is null");
}
if (root == null) {
throw new NoSuchElementException("called floor() with empty tree");
}
Node curr = floor(root, kee);
if (curr == null) { return null; }
return curr.key;
}
private Node floor(Node curr, K kee) {
if (curr == null) { return null; }
int cmp = kee.compareTo(curr.key);
if (cmp < 0) { return floor(curr.left, kee); }
else if (cmp == 0) { return curr; }
Node rightfloor = floor(curr.right, kee);
if (rightfloor != null) { return rightfloor; }
else { return curr; }
}
public K ceiling(K kee) {
if (kee == null) {
throw new IllegalArgumentException("argument to ceiling() is null");
}
if (root == null) {
throw new NoSuchElementException("called ceiling() with empty tree");
}
Node curr = ceiling(root, kee);
if (curr == null) { return null; }
return curr.key;
}
private Node ceiling(Node curr, K kee) {
if (curr == null) { return null; }
int cmp = kee.compareTo(curr.key);
if (cmp > 0) { return ceiling(curr.right, kee); }
else if (cmp == 0) { return curr; }
Node leftceiling = ceiling(curr.left, kee);
if (leftceiling != null) { return leftceiling; }
else { return curr; }
}
public int rank(K kee) {
if (kee == null) throw new IllegalArgumentException("argument to rank() is null");
return rank(root, kee);
}
private int rank(Node curr, K kee) {
if (curr == null) { return 0; }
int cmp = kee.compareTo(curr.key);
if (cmp < 0) { return rank(curr.left, kee); }
else if (cmp > 0) {
return 1 + size(curr.left) + rank(curr.right, kee);
} else { return size(curr.left); }
}
public void deleteMin() {
if (root == null) { throw new NoSuchElementException("Tree underflow"); }
if (!isRed(root.left) && !isRed(root.right)) { root.color = RED; }
root = deleteMin(root);
if (root != null) { root.color = BLACK; }
}
private Node deleteMin(Node curr) {
if (curr.left == null) { return null; }
if (!isRed(curr.left) && !isRed(curr.left.left)) {
curr = moveRedLeft(curr);
}
curr.left = deleteMin(curr.left);
return balance(curr);
}
public void deleteMax() {
if (root == null) { throw new NoSuchElementException("Tree underflow"); }
if (!isRed(root.left) && !isRed(root.right)) { root.color = RED; }
root = deleteMax(root);
if (root != null) { root.color = BLACK; }
}
private Node deleteMax(Node curr) {
if (isRed(curr.left)) { curr = rotateRight(curr); }
if (curr.left == null) { return null; }
if (!isRed(curr.right) && !isRed(curr.right.left)) {
curr = moveRedRight(curr);
}
curr.right = deleteMax(curr.right);
return balance(curr);
}
public void delete(K kee) {
if (kee == null) {
throw new IllegalArgumentException("argument to delete() is null");
}
if (!contains(kee)) { return; }
if (!isRed(root.left) && !isRed(root.right)) { root.color = RED; }
root = delete(root, kee);
if (root != null) { root.color = BLACK; }
}
private Node delete(Node curr, K kee) {
if (kee.compareTo(curr.key) < 0) {
if (!isRed(curr.left) && !isRed(curr.left.left)) {
curr = moveRedLeft(curr);
}
curr.left = delete(curr.left, kee);
} else {
if (isRed(curr.left)) { curr = rotateRight(curr); }
if ((kee.compareTo(curr.key) == 0) && (curr.right == null)) {
return null;
}
if (!isRed(curr.right) && !isRed(curr.right.left)) {
curr = moveRedRight(curr);
}
if (kee.compareTo(curr.key) == 0) {
Node next = min(curr.right);
curr.key = next.key;
curr.value = next.value;
curr.right = deleteMin(curr.right);
} else { curr.right = delete(curr.right, key); }
}
return balance(curr);
}
// returns a queue of the keys in order
public Iterable<K> keys() {
Queue<K> q = new Queue<K>();
inorder(root, q);
return q;
}
// traverse in order, adding keys to a queue
private void inorder(Node curr, Queue<K> q) {
if (curr == null) { return; }
inorder(curr.left, q);
q.enqueue(curr.key);
inorder(curr.right, q);
}
}