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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
|
/*
* Copyright © 2017 Faith Ekstrand
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "rb_tree.h"
/** \file rb_tree.c
*
* An implementation of a red-black tree
*
* This file implements the guts of a red-black tree. The implementation
* is mostly based on the one in "Introduction to Algorithms", third
* edition, by Cormen, Leiserson, Rivest, and Stein. The primary
* divergence in our algorithms from those presented in CLRS is that we use
* NULL for the leaves instead of a sentinel. This means we have to do a
* tiny bit more tracking in our implementation of delete but it makes the
* algorithms far more explicit than stashing stuff in the sentinel.
*/
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "macros.h"
static bool
rb_node_is_black(struct rb_node *n)
{
/* NULL nodes are leaves and therefore black */
return (n == NULL) || (n->parent & 1);
}
static bool
rb_node_is_red(struct rb_node *n)
{
return !rb_node_is_black(n);
}
static void
rb_node_set_black(struct rb_node *n)
{
n->parent |= 1;
}
static void
rb_node_set_red(struct rb_node *n)
{
n->parent &= ~1ull;
}
static void
rb_node_copy_color(struct rb_node *dst, struct rb_node *src)
{
dst->parent = (dst->parent & ~1ull) | (src->parent & 1);
}
static void
rb_node_set_parent(struct rb_node *n, struct rb_node *p)
{
n->parent = (n->parent & 1) | (uintptr_t)p;
}
static struct rb_node *
rb_node_minimum(struct rb_node *node)
{
while (node->left)
node = node->left;
return node;
}
static struct rb_node *
rb_node_maximum(struct rb_node *node)
{
while (node->right)
node = node->right;
return node;
}
/**
* Replace the subtree of T rooted at u with the subtree rooted at v
*
* This is called RB-transplant in CLRS.
*
* The node to be replaced is assumed to be a non-leaf.
*/
static void
rb_tree_splice(struct rb_tree *T, struct rb_node *u, struct rb_node *v)
{
assert(u);
struct rb_node *p = rb_node_parent(u);
if (p == NULL) {
assert(T->root == u);
T->root = v;
} else if (u == p->left) {
p->left = v;
} else {
assert(u == p->right);
p->right = v;
}
if (v)
rb_node_set_parent(v, p);
}
static void
rb_tree_rotate_left(struct rb_tree *T, struct rb_node *x,
void (*update)(struct rb_node *))
{
assert(x && x->right);
struct rb_node *y = x->right;
x->right = y->left;
if (y->left)
rb_node_set_parent(y->left, x);
rb_tree_splice(T, x, y);
y->left = x;
rb_node_set_parent(x, y);
if (update) {
update(x);
update(y);
}
}
static void
rb_tree_rotate_right(struct rb_tree *T, struct rb_node *y,
void (*update)(struct rb_node *))
{
assert(y && y->left);
struct rb_node *x = y->left;
y->left = x->right;
if (x->right)
rb_node_set_parent(x->right, y);
rb_tree_splice(T, y, x);
x->right = y;
rb_node_set_parent(y, x);
if (update) {
update(y);
update(x);
}
}
void
rb_augmented_tree_insert_at(struct rb_tree *T, struct rb_node *parent,
struct rb_node *node, bool insert_left,
void (*update)(struct rb_node *node))
{
/* This sets null children, parent, and a color of red */
memset(node, 0, sizeof(*node));
if (update)
update(node);
if (parent == NULL) {
assert(T->root == NULL);
T->root = node;
rb_node_set_black(node);
return;
}
if (insert_left) {
assert(parent->left == NULL);
parent->left = node;
} else {
assert(parent->right == NULL);
parent->right = node;
}
rb_node_set_parent(node, parent);
if (update) {
struct rb_node *p = parent;
while (p) {
update(p);
p = rb_node_parent(p);
}
}
/* Now we do the insertion fixup */
struct rb_node *z = node;
while (rb_node_is_red(rb_node_parent(z))) {
struct rb_node *z_p = rb_node_parent(z);
assert(z == z_p->left || z == z_p->right);
struct rb_node *z_p_p = rb_node_parent(z_p);
assert(z_p_p != NULL);
if (z_p == z_p_p->left) {
struct rb_node *y = z_p_p->right;
if (rb_node_is_red(y)) {
rb_node_set_black(z_p);
rb_node_set_black(y);
rb_node_set_red(z_p_p);
z = z_p_p;
} else {
if (z == z_p->right) {
z = z_p;
rb_tree_rotate_left(T, z, update);
/* We changed z */
z_p = rb_node_parent(z);
assert(z == z_p->left || z == z_p->right);
z_p_p = rb_node_parent(z_p);
}
rb_node_set_black(z_p);
rb_node_set_red(z_p_p);
rb_tree_rotate_right(T, z_p_p, update);
}
} else {
struct rb_node *y = z_p_p->left;
if (rb_node_is_red(y)) {
rb_node_set_black(z_p);
rb_node_set_black(y);
rb_node_set_red(z_p_p);
z = z_p_p;
} else {
if (z == z_p->left) {
z = z_p;
rb_tree_rotate_right(T, z, update);
/* We changed z */
z_p = rb_node_parent(z);
assert(z == z_p->left || z == z_p->right);
z_p_p = rb_node_parent(z_p);
}
rb_node_set_black(z_p);
rb_node_set_red(z_p_p);
rb_tree_rotate_left(T, z_p_p, update);
}
}
}
rb_node_set_black(T->root);
}
void
rb_augmented_tree_remove(struct rb_tree *T, struct rb_node *z,
void (*update)(struct rb_node *))
{
/* x_p is always the parent node of X. We have to track this
* separately because x may be NULL.
*/
struct rb_node *x, *x_p;
struct rb_node *y = z;
bool y_was_black = rb_node_is_black(y);
if (z->left == NULL) {
x = z->right;
x_p = rb_node_parent(z);
rb_tree_splice(T, z, x);
} else if (z->right == NULL) {
x = z->left;
x_p = rb_node_parent(z);
rb_tree_splice(T, z, x);
} else {
/* Find the minimum sub-node of z->right */
y = rb_node_minimum(z->right);
y_was_black = rb_node_is_black(y);
x = y->right;
if (rb_node_parent(y) == z) {
x_p = y;
} else {
x_p = rb_node_parent(y);
rb_tree_splice(T, y, x);
y->right = z->right;
rb_node_set_parent(y->right, y);
}
assert(y->left == NULL);
rb_tree_splice(T, z, y);
y->left = z->left;
rb_node_set_parent(y->left, y);
rb_node_copy_color(y, z);
}
assert(x_p == NULL || x == x_p->left || x == x_p->right);
if (update) {
struct rb_node *p = x_p;
while (p) {
update(p);
p = rb_node_parent(p);
}
}
if (!y_was_black)
return;
/* Fixup RB tree after the delete */
while (x != T->root && rb_node_is_black(x)) {
if (x == x_p->left) {
struct rb_node *w = x_p->right;
if (rb_node_is_red(w)) {
rb_node_set_black(w);
rb_node_set_red(x_p);
rb_tree_rotate_left(T, x_p, update);
assert(x == x_p->left);
w = x_p->right;
}
if (rb_node_is_black(w->left) && rb_node_is_black(w->right)) {
rb_node_set_red(w);
x = x_p;
} else {
if (rb_node_is_black(w->right)) {
rb_node_set_black(w->left);
rb_node_set_red(w);
rb_tree_rotate_right(T, w, update);
w = x_p->right;
}
rb_node_copy_color(w, x_p);
rb_node_set_black(x_p);
rb_node_set_black(w->right);
rb_tree_rotate_left(T, x_p, update);
x = T->root;
}
} else {
struct rb_node *w = x_p->left;
if (rb_node_is_red(w)) {
rb_node_set_black(w);
rb_node_set_red(x_p);
rb_tree_rotate_right(T, x_p, update);
assert(x == x_p->right);
w = x_p->left;
}
if (rb_node_is_black(w->right) && rb_node_is_black(w->left)) {
rb_node_set_red(w);
x = x_p;
} else {
if (rb_node_is_black(w->left)) {
rb_node_set_black(w->right);
rb_node_set_red(w);
rb_tree_rotate_left(T, w, update);
w = x_p->left;
}
rb_node_copy_color(w, x_p);
rb_node_set_black(x_p);
rb_node_set_black(w->left);
rb_tree_rotate_right(T, x_p, update);
x = T->root;
}
}
x_p = rb_node_parent(x);
}
if (x)
rb_node_set_black(x);
}
struct rb_node *
rb_tree_first(struct rb_tree *T)
{
return T->root ? rb_node_minimum(T->root) : NULL;
}
struct rb_node *
rb_tree_last(struct rb_tree *T)
{
return T->root ? rb_node_maximum(T->root) : NULL;
}
struct rb_node *
rb_node_next(struct rb_node *node)
{
if (node->right) {
/* If we have a right child, then the next thing (compared to this
* node) is the left-most child of our right child.
*/
return rb_node_minimum(node->right);
} else {
/* If node doesn't have a right child, crawl back up the to the
* left until we hit a parent to the right.
*/
struct rb_node *p = rb_node_parent(node);
while (p && node == p->right) {
node = p;
p = rb_node_parent(node);
}
assert(p == NULL || node == p->left);
return p;
}
}
struct rb_node *
rb_node_prev(struct rb_node *node)
{
if (node->left) {
/* If we have a left child, then the previous thing (compared to
* this node) is the right-most child of our left child.
*/
return rb_node_maximum(node->left);
} else {
/* If node doesn't have a left child, crawl back up the to the
* right until we hit a parent to the left.
*/
struct rb_node *p = rb_node_parent(node);
while (p && node == p->left) {
node = p;
p = rb_node_parent(node);
}
assert(p == NULL || node == p->right);
return p;
}
}
/* Return the first node in an interval tree that intersects a given interval
* or point. The tests against the interval and the max field are abstracted
* via function pointers, so that this works for any type of interval.
*/
static struct rb_node *
rb_node_min_intersecting(struct rb_node *node, void *interval,
int (*cmp_interval)(const struct rb_node *node,
const void *interval),
bool (*cmp_max)(const struct rb_node *node,
const void *interval))
{
if (!cmp_max(node, interval))
return NULL;
while (node) {
int cmp = cmp_interval(node, interval);
/* If the node's interval is entirely to the right of the interval
* we're searching for, all of its right descendants are also to the
* right and don't intersect so we have to search to the left.
*/
if (cmp > 0) {
node = node->left;
continue;
}
/* The interval overlaps or is to the left. This must also be true for
* its left descendants because their start points are to the left of
* node's. We can use the max to tell if there is an interval in its
* left descendants which overlaps our interval, in which case we
* should descend to the left.
*/
if (node->left && cmp_max(node->left, interval)) {
node = node->left;
continue;
}
/* Now the only possibilities are the node's interval intersects the
* interval or one of its right descendants does.
*/
if (cmp == 0)
return node;
node = node->right;
if (node && !cmp_max(node, interval))
return NULL;
}
return NULL;
}
/* Return the next node after "node" that intersects a given interval.
*
* Because rb_node_min_intersecting() takes O(log n) time and may be called up
* to O(log n) times, in addition to the O(log n) crawl up the tree, a naive
* runtime analysis would show that this takes O((log n)^2) time, but actually
* it's O(log n). Proving this is tricky:
*
* Call the rightmost node in the tree whose start is before the end of the
* interval we're searching for N. All nodes after N in the tree are to the
* right of the interval. We'll divide the search into two phases: in phase 1,
* "node" is *not* an ancestor of N, and in phase 2 it is. Because we always
* crawl up the tree, phase 2 cannot turn back into phase 1, but phase 1 may
* be followed by phase 2. We'll prove that the calls to
* rb_node_min_intersecting() take O(log n) time in both phases.
*
* Phase 1: Because "node" is to the left of N and N isn't a descendant of
* "node", the start of every interval in "node"'s subtree must be less than
* or equal to N's start which is less than or equal to the end of the search
* interval. Furthermore, either "node"'s max_end is less than the start of
* the interval, in which case rb_node_min_intersecting() immediately returns
* NULL, or some descendant has an end equal to "node"'s max_end which is
* greater than or equal to the search interval's start, and therefore it
* intersects the search interval and rb_node_min_intersecting() must return
* non-NULL which causes us to terminate. rb_node_min_intersecting() is called
* O(log n) times, with all but the last call taking constant time and the
* last call taking O(log n), so the overall runtime is O(log n).
*
* Phase 2: After the first call to rb_node_min_intersecting, we may crawl up
* the tree until we get to a node p where "node", and therefore N, is in p's
* left subtree. However this means that p is to the right of N in the tree
* and is therefore to the right of the search interval, and the search
* terminates on the first iteration of the loop so that
* rb_node_min_intersecting() is only called once.
*/
static struct rb_node *
rb_node_next_intersecting(struct rb_node *node,
void *interval,
int (*cmp_interval)(const struct rb_node *node,
const void *interval),
bool (*cmp_max)(const struct rb_node *node,
const void *interval))
{
while (true) {
/* The first place to search is the node's right subtree. */
if (node->right) {
struct rb_node *next =
rb_node_min_intersecting(node->right, interval, cmp_interval, cmp_max);
if (next)
return next;
}
/* If we don't find a matching interval there, crawl up the tree until
* we find an ancestor to the right. This is the next node after the
* right subtree which we determined didn't match.
*/
struct rb_node *p = rb_node_parent(node);
while (p && node == p->right) {
node = p;
p = rb_node_parent(node);
}
assert(p == NULL || node == p->left);
/* Check if we've searched everything in the tree. */
if (!p)
return NULL;
int cmp = cmp_interval(p, interval);
/* If it intersects, return it. */
if (cmp == 0)
return p;
/* If it's to the right of the interval, all following nodes will be
* to the right and we can bail early.
*/
if (cmp > 0)
return NULL;
node = p;
}
}
static int
uinterval_cmp(struct uinterval a, struct uinterval b)
{
if (a.end < b.start)
return -1;
else if (b.end < a.start)
return 1;
else
return 0;
}
static int
uinterval_node_cmp(const struct rb_node *_a, const struct rb_node *_b)
{
const struct uinterval_node *a = rb_node_data(struct uinterval_node, _a, node);
const struct uinterval_node *b = rb_node_data(struct uinterval_node, _b, node);
return (int) (b->interval.start - a->interval.start);
}
static int
uinterval_search_cmp(const struct rb_node *_node, const void *_interval)
{
const struct uinterval_node *node = rb_node_data(struct uinterval_node, _node, node);
const struct uinterval *interval = _interval;
return uinterval_cmp(node->interval, *interval);
}
static bool
uinterval_max_cmp(const struct rb_node *_node, const void *data)
{
const struct uinterval_node *node = rb_node_data(struct uinterval_node, _node, node);
const struct uinterval *interval = data;
return node->max_end >= interval->start;
}
static void
uinterval_update_max(struct rb_node *_node)
{
struct uinterval_node *node = rb_node_data(struct uinterval_node, _node, node);
node->max_end = node->interval.end;
if (node->node.left) {
struct uinterval_node *left = rb_node_data(struct uinterval_node, node->node.left, node);
node->max_end = MAX2(node->max_end, left->max_end);
}
if (node->node.right) {
struct uinterval_node *right = rb_node_data(struct uinterval_node, node->node.right, node);
node->max_end = MAX2(node->max_end, right->max_end);
}
}
void
uinterval_tree_insert(struct rb_tree *tree, struct uinterval_node *node)
{
rb_augmented_tree_insert(tree, &node->node, uinterval_node_cmp,
uinterval_update_max);
}
void
uinterval_tree_remove(struct rb_tree *tree, struct uinterval_node *node)
{
rb_augmented_tree_remove(tree, &node->node, uinterval_update_max);
}
struct uinterval_node *
uinterval_tree_first(struct rb_tree *tree, struct uinterval interval)
{
if (!tree->root)
return NULL;
struct rb_node *node =
rb_node_min_intersecting(tree->root, &interval, uinterval_search_cmp,
uinterval_max_cmp);
return node ? rb_node_data(struct uinterval_node, node, node) : NULL;
}
struct uinterval_node *
uinterval_node_next(struct uinterval_node *node,
struct uinterval interval)
{
struct rb_node *next =
rb_node_next_intersecting(&node->node, &interval, uinterval_search_cmp,
uinterval_max_cmp);
return next ? rb_node_data(struct uinterval_node, next, node) : NULL;
}
static void
validate_rb_node(struct rb_node *n, int black_depth)
{
if (n == NULL) {
assert(black_depth == 0);
return;
}
if (rb_node_is_black(n)) {
black_depth--;
} else {
assert(rb_node_is_black(n->left));
assert(rb_node_is_black(n->right));
}
validate_rb_node(n->left, black_depth);
validate_rb_node(n->right, black_depth);
}
void
rb_tree_validate(struct rb_tree *T)
{
if (T->root == NULL)
return;
assert(rb_node_is_black(T->root));
unsigned black_depth = 0;
for (struct rb_node *n = T->root; n; n = n->left) {
if (rb_node_is_black(n))
black_depth++;
}
validate_rb_node(T->root, black_depth);
}
|