-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
red-black-tree.spice
418 lines (385 loc) · 11.4 KB
/
red-black-tree.spice
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import "std/type/error";
// Add generic type definitions
type K dyn;
type V dyn;
// Enums
type NodeColor enum { RED, BLACK }
/**
* Node of a Red-Black Tree
*/
type Node<K, V> struct {
K key
V value
NodeColor color
heap Node<K, V>* parent
heap Node<K, V>* childLeft
heap Node<K, V>* childRight
}
inline f<bool> Node.isRoot() {
return this.parent == nil<heap Node<K, V>*>;
}
inline f<bool> Node.hasLeftChild() {
return this.childLeft != nil<heap Node<K, V>*>;
}
inline f<bool> Node.hasRightChild() {
return this.childRight != nil<heap Node<K, V>*>;
}
inline f<bool> Node.isRed() {
return this.color == NodeColor::RED;
}
inline f<bool> Node.isBlack() {
return this.color == NodeColor::BLACK;
}
/**
* A Red-Black Tree is a self-balancing search tree, which is used e.g. in the implementation of maps.
*
* Time complexity:
* Insert: O(log n)
* Delete: O(log n)
* Lookup: O(log n)
*/
public type RedBlackTree<K, V> struct {
heap Node<K, V>* rootNode = nil<heap Node<K, V>*>
unsigned long size = 0l
}
/**
* Insert a new key-value pair into the tree.
*
* @param key The key of the new element
* @param value The value of the new element
*/
public p RedBlackTree.insert(const K& key, const V& value) {
// Create the new node
heap Node<K, V>* newNode = sNew<Node<K, V>>(Node<K, V>{
key,
value,
NodeColor::RED,
nil<heap Node<K, V>*>,
nil<heap Node<K, V>*>,
nil<heap Node<K, V>*>
});
// Search for the correct position
heap Node<K, V>* y = nil<heap Node<K, V>*>;
heap Node<K, V>* x = this.rootNode;
while x != nil<heap Node<K, V>*> {
y = x;
if newNode.key < x.key {
x = x.childLeft;
} else {
x = x.childRight;
}
}
// Insert the new node at the correct position
newNode.parent = y;
if y == nil<heap Node<K, V>*> {
this.rootNode = newNode;
} else if newNode.key < y.key {
y.childLeft = newNode;
} else {
y.childRight = newNode;
}
// Fixup the tree
this.insertFixup(newNode);
this.size++;
}
/**
* Remove an element from the tree.
*
* @param key The key of the element to remove
*/
public p RedBlackTree.remove(const K& key) {
// Search for the node to remove
heap Node<K, V>* z = this.search(key);
if z == nil<heap Node<K, V>*> {
return;
}
heap Node<K, V>* y = z;
heap Node<K, V>* x;
bool wasYBlack = y.isBlack();
if !z.hasLeftChild() {
x = z.childRight;
this.transplant(z, z.childRight);
} else if !z.hasRightChild() {
x = z.childLeft;
this.transplant(z, z.childLeft);
} else {
y = this.minimum(z.childRight);
wasYBlack = y.isBlack();
x = y.childRight;
if y.parent == z {
x.parent = y;
} else {
this.transplant(y, y.childRight);
y.childRight = z.childRight;
y.childRight.parent = y;
}
this.transplant(z, y);
y.childLeft = z.childLeft;
y.childLeft.parent = y;
y.color = z.color;
}
// Use dealloc, because we don't want to call the destructor.
// The destructor would delete children and parent.
unsafe {
sDealloc((byte*) z);
}
// Do a fixup if required
if wasYBlack && x != nil<heap Node<K, V>*> {
this.deleteFixup(x);
}
this.size--;
}
/**
* Find the value for a given key.
* Note: If the key is not found in the tree, this function will panic. To avoid this, use findSafe instead.
*
* @param key The key to search for
* @return The value for the given key
*/
public f<V&> RedBlackTree.find(const K& key) {
heap Node<K, V>* node = this.search(key);
if node == nil<heap Node<K, V>*> {
panic(Error("The provided key was not found"));
}
return node.value;
}
/**
* Find the value for a given key.
*
* @param key The key to search for
* @return The value for the given key, or an error if the key was not found
*/
public f<Result<V>> RedBlackTree.findSafe(const K& key) {
heap Node<K, V>* node = this.search(key);
if node == nil<heap Node<K, V>*> {
return err<V>(Error("The provided key was not found"));
}
return ok<V>(node.value);
}
/**
* Check if the tree contains a given key.
*
* @param key The key to search for
* @return True if the key was found, false otherwise
*/
public f<bool> RedBlackTree.contains(const K& key) {
return this.search(key) != nil<heap Node<K, V>*>;
}
/**
* Get the number of elements in the tree.
*
* @return The number of elements in the tree
*/
public f<unsigned long> RedBlackTree.getSize() {
return this.size;
}
public p RedBlackTree.clear() {
this.clearRecursive(this.rootNode);
this.rootNode = nil<heap Node<K, V>*>;
this.size = 0l;
}
/**
* Rotate the tree left around the given node.
*
* @param x The node to rotate around
*/
p RedBlackTree.rotateLeft(heap Node<K, V>* x) {
heap Node<K, V>* y = x.childRight;
x.childRight = y.childLeft;
if y.hasLeftChild() {
y.childLeft.parent = x;
}
y.parent = x.parent;
if x.isRoot() {
this.rootNode = y;
} else if x == x.parent.childLeft {
x.parent.childLeft = y;
} else {
x.parent.childRight = y;
}
y.childLeft = x;
x.parent = y;
}
/**
* Rotate the tree right around the given node.
*
* @param y The node to rotate around
*/
p RedBlackTree.rotateRight(heap Node<K, V>* y) {
heap Node<K, V>* x = y.childLeft;
y.childLeft = x.childRight;
if x.hasRightChild() {
x.childRight.parent = y;
}
x.parent = y.parent;
if y.isRoot() {
this.rootNode = x;
} else if y == y.parent.childRight {
y.parent.childRight = x;
} else {
y.parent.childLeft = x;
}
x.childRight = y;
y.parent = x;
}
/**
* Replace the subtree rooted at node u with the subtree rooted at node v.
*
* @param u The node to replace
* @param v The node to replace with
*/
p RedBlackTree.transplant(heap Node<K, V>* u, heap Node<K, V>* v) {
// Set v to the correct pointer
if u.isRoot() {
this.rootNode = v;
} else if u == u.parent.childLeft {
u.parent.childLeft = v;
} else {
u.parent.childRight = v;
}
// Update the parent
if v != nil<heap Node<K, V>*> {
v.parent = u.parent;
}
}
p RedBlackTree.insertFixup(heap Node<K, V>* z) {
while !z.isRoot() && z.parent.isRed() {
if z.parent == z.parent.parent.childLeft {
heap Node<K, V>* y = z.parent.parent.childRight;
if y != nil<heap Node<K, V>*> && y.isRed() {
z.parent.color = NodeColor::BLACK;
y.color = NodeColor::BLACK;
z.parent.parent.color = NodeColor::RED;
z = z.parent.parent;
} else {
if z == z.parent.childRight {
z = z.parent;
this.rotateLeft(z);
}
z.parent.color = NodeColor::BLACK;
z.parent.parent.color = NodeColor::RED;
this.rotateRight(z.parent.parent);
}
} else {
heap Node<K, V>* y = z.parent.parent.childLeft;
if y != nil<heap Node<K, V>*> && y.isRed() {
z.parent.color = NodeColor::BLACK;
y.color = NodeColor::BLACK;
z.parent.parent.color = NodeColor::RED;
z = z.parent.parent;
} else {
if z == z.parent.childLeft {
z = z.parent;
this.rotateRight(z);
}
z.parent.color = NodeColor::BLACK;
z.parent.parent.color = NodeColor::RED;
this.rotateLeft(z.parent.parent);
}
}
}
this.rootNode.color = NodeColor::BLACK;
}
p RedBlackTree.deleteFixup(heap Node<K, V>* x) {
while x != this.rootNode && (x == nil<Node<K, V>*> || x.isBlack()) {
if x == x.parent.childLeft {
heap Node<K, V>* w = x.parent.childRight;
if w != nil<Node<K, V>*> && w.isRed() {
w.color = NodeColor::BLACK;
x.parent.color = NodeColor::RED;
this.rotateLeft(x.parent);
w = x.parent.childRight;
}
if (!w.hasLeftChild() || w.childLeft.isBlack()) && (!w.hasRightChild() || w.childRight.isBlack()) {
w.color = NodeColor::RED;
x = x.parent;
} else {
if !w.hasRightChild() || w.childRight.isBlack() {
if w.hasLeftChild() {
w.childLeft.color = NodeColor::BLACK;
}
w.color = NodeColor::RED;
this.rotateRight(w);
w = x.parent.childRight;
}
w.color = x.parent.color;
x.parent.color = NodeColor::BLACK;
if w.hasRightChild() {
w.childRight.color = NodeColor::BLACK;
}
this.rotateLeft(x.parent);
x = this.rootNode;
}
} else {
heap Node<K, V>* w = x.parent.childLeft;
if w != nil<Node<K, V>*> && w.isRed() {
w.color = NodeColor::BLACK;
x.parent.color = NodeColor::RED;
this.rotateRight(x.parent);
w = x.parent.childLeft;
}
if (!w.hasRightChild() || w.childRight.isBlack()) && (!w.hasLeftChild() || w.childLeft.isBlack()) {
w.color = NodeColor::RED;
x = x.parent;
} else {
if !w.hasLeftChild() || w.childLeft.isBlack() {
if w.hasRightChild() {
w.childRight.color = NodeColor::BLACK;
}
w.color = NodeColor::RED;
this.rotateLeft(w);
w = x.parent.childLeft;
}
w.color = x.parent.color;
x.parent.color = NodeColor::BLACK;
if w.hasLeftChild() {
w.childLeft.color = NodeColor::BLACK;
}
this.rotateRight(x.parent);
x = this.rootNode;
}
}
}
if x != nil<Node<K, V>*> {
x.color = NodeColor::BLACK;
}
}
/**
* Find the node with the given key.
*
* @param key The key to search for
* @return The node with the given key, or nil if the key was not found
*/
f<heap Node<K, V>*> RedBlackTree.search(const K& key) {
heap Node<K, V>* currentNode = this.rootNode;
while currentNode != nil<heap Node<K, V>*> {
if key == currentNode.key {
return currentNode;
} else if key < currentNode.key {
currentNode = currentNode.childLeft;
} else {
currentNode = currentNode.childRight;
}
}
return nil<heap Node<K, V>*>;
}
/**
* Find the node with the minimum key in the subtree rooted at x.
*/
f<heap Node<K, V>*> RedBlackTree.minimum(heap Node<K, V>* x) {
while x.hasLeftChild() {
x = x.childLeft;
}
return x;
}
/**
* Clear the subtree rooted at the input node recursively.
*/
p RedBlackTree.clearRecursive(heap Node<K, V>* node) {
// Skip if node is nil
if node == nil<heap Node<K, V>*> { return; }
// Otherwise, clear children and delete node
this.clearRecursive(node.childLeft);
this.clearRecursive(node.childRight);
sDelete(node);
}