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
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
|
/* Ordered {set,map} data type implemented by a binary tree.
Copyright (C) 2006-2007, 2009-2020 Free Software Foundation, Inc.
Written by Bruno Haible <bruno@clisp.org>, 2006.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
/* A red-black tree is a binary tree where every node is colored black or
red such that
1. The root is black.
2. No red node has a red parent.
Or equivalently: No red node has a red child.
3. All paths from the root down to any NULL endpoint contain the same
number of black nodes.
Let's call this the "black-height" bh of the tree. It follows that every
such path contains exactly bh black and between 0 and bh red nodes. (The
extreme cases are a path containing only black nodes, and a path colored
alternately black-red-black-red-...-black-red.) The height of the tree
therefore is >= bh, <= 2*bh.
*/
/* Color of a node. */
typedef enum color { BLACK, RED } color_t;
/* Tree node implementation, valid for this file only. */
struct NODE_IMPL
{
struct NODE_IMPL *left; /* left branch, or NULL */
struct NODE_IMPL *right; /* right branch, or NULL */
/* Parent pointer, or NULL. The parent pointer is not needed for most
operations. It is needed so that a NODE_T can be returned without
memory allocation, on which the functions <container>_remove_node,
<container>_add_before, <container>_add_after can be implemented. */
struct NODE_IMPL *parent;
color_t color; /* node's color */
NODE_PAYLOAD_FIELDS
};
typedef struct NODE_IMPL * NODE_T;
/* Concrete CONTAINER_IMPL type, valid for this file only. */
struct CONTAINER_IMPL
{
struct CONTAINER_IMPL_BASE base;
struct NODE_IMPL *root; /* root node or NULL */
size_t count; /* number of nodes */
};
/* A red-black tree of height h has a black-height bh >= ceil(h/2) and
therefore at least 2^ceil(h/2) - 1 elements. So, h <= 116 (because a tree
of height h >= 117 would have at least 2^59 - 1 elements, and because even
on 64-bit machines,
sizeof (NODE_IMPL) * (2^59 - 1) > 2^64
this would exceed the address space of the machine. */
#define MAXHEIGHT 116
/* Rotates left a subtree.
B D
/ \ / \
A D --> B E
/ \ / \
C E A C
Changes the tree structure, updates the branch sizes.
The caller must update the colors and register D as child of its parent. */
static NODE_T
rotate_left (NODE_T b_node, NODE_T d_node)
{
NODE_T c_node = d_node->left;
b_node->right = c_node;
d_node->left = b_node;
d_node->parent = b_node->parent;
b_node->parent = d_node;
if (c_node != NULL)
c_node->parent = b_node;
return d_node;
}
/* Rotates right a subtree.
D B
/ \ / \
B E --> A D
/ \ / \
A C C E
Changes the tree structure, updates the branch sizes.
The caller must update the colors and register B as child of its parent. */
static NODE_T
rotate_right (NODE_T b_node, NODE_T d_node)
{
NODE_T c_node = b_node->right;
d_node->left = c_node;
b_node->right = d_node;
b_node->parent = d_node->parent;
d_node->parent = b_node;
if (c_node != NULL)
c_node->parent = d_node;
return b_node;
}
/* Ensures the tree is balanced, after an insertion operation.
Also assigns node->color.
parent is the given node's parent, known to be non-NULL. */
static void
rebalance_after_add (CONTAINER_T container, NODE_T node, NODE_T parent)
{
for (;;)
{
/* At this point, parent = node->parent != NULL.
Think of node->color being RED (although node->color is not yet
assigned.) */
NODE_T grandparent;
NODE_T uncle;
if (parent->color == BLACK)
{
/* A RED color for node is acceptable. */
node->color = RED;
return;
}
grandparent = parent->parent;
/* Since parent is RED, we know that
grandparent is != NULL and colored BLACK. */
if (grandparent->left == parent)
uncle = grandparent->right;
else if (grandparent->right == parent)
uncle = grandparent->left;
else
abort ();
if (uncle != NULL && uncle->color == RED)
{
/* Change grandparent from BLACK to RED, and
change parent and uncle from RED to BLACK.
This makes it acceptable for node to be RED. */
node->color = RED;
parent->color = uncle->color = BLACK;
node = grandparent;
}
else
{
/* grandparent and uncle are BLACK. parent is RED. node wants
to be RED too.
In this case, recoloring is not sufficient. Need to perform
one or two rotations. */
NODE_T *grandparentp;
if (grandparent->parent == NULL)
grandparentp = &container->root;
else if (grandparent->parent->left == grandparent)
grandparentp = &grandparent->parent->left;
else if (grandparent->parent->right == grandparent)
grandparentp = &grandparent->parent->right;
else
abort ();
if (grandparent->left == parent)
{
if (parent->right == node)
{
/* Rotation between node and parent. */
grandparent->left = rotate_left (parent, node);
node = parent;
parent = grandparent->left;
}
/* grandparent and uncle are BLACK. parent and node want to be
RED. parent = grandparent->left. node = parent->left.
grandparent parent
bh+1 bh+1
/ \ / \
parent uncle --> node grandparent
bh bh bh bh
/ \ / \
node C C uncle
bh bh bh bh
*/
*grandparentp = rotate_right (parent, grandparent);
parent->color = BLACK;
node->color = grandparent->color = RED;
}
else /* grandparent->right == parent */
{
if (parent->left == node)
{
/* Rotation between node and parent. */
grandparent->right = rotate_right (node, parent);
node = parent;
parent = grandparent->right;
}
/* grandparent and uncle are BLACK. parent and node want to be
RED. parent = grandparent->right. node = parent->right.
grandparent parent
bh+1 bh+1
/ \ / \
uncle parent --> grandparent node
bh bh bh bh
/ \ / \
C node uncle C
bh bh bh bh
*/
*grandparentp = rotate_left (grandparent, parent);
parent->color = BLACK;
node->color = grandparent->color = RED;
}
return;
}
/* Start again with a new (node, parent) pair. */
parent = node->parent;
if (parent == NULL)
{
/* Change node's color from RED to BLACK. This increases the
tree's black-height. */
node->color = BLACK;
return;
}
}
}
/* Ensures the tree is balanced, after a deletion operation.
CHILD was a grandchild of PARENT and is now its child. Between them,
a black node was removed. CHILD is also black, or NULL.
(CHILD can also be NULL. But PARENT is non-NULL.) */
static void
rebalance_after_remove (CONTAINER_T container, NODE_T child, NODE_T parent)
{
for (;;)
{
/* At this point, we reduced the black-height of the CHILD subtree by 1.
To make up, either look for a possibility to turn a RED to a BLACK
node, or try to reduce the black-height tree of CHILD's sibling
subtree as well. */
NODE_T *parentp;
if (parent->parent == NULL)
parentp = &container->root;
else if (parent->parent->left == parent)
parentp = &parent->parent->left;
else if (parent->parent->right == parent)
parentp = &parent->parent->right;
else
abort ();
if (parent->left == child)
{
NODE_T sibling = parent->right;
/* sibling's black-height is >= 1. In particular,
sibling != NULL.
parent
/ \
child sibling
bh bh+1
*/
if (sibling->color == RED)
{
/* sibling is RED, hence parent is BLACK and sibling's children
are non-NULL and BLACK.
parent sibling
bh+2 bh+2
/ \ / \
child sibling --> parent SR
bh bh+1 bh+1 bh+1
/ \ / \
SL SR child SL
bh+1 bh+1 bh bh+1
*/
*parentp = rotate_left (parent, sibling);
parent->color = RED;
sibling->color = BLACK;
/* Concentrate on the subtree of parent. The new sibling is
one of the old sibling's children, and known to be BLACK. */
parentp = &sibling->left;
sibling = parent->right;
}
/* Now we know that sibling is BLACK.
parent
/ \
child sibling
bh bh+1
*/
if (sibling->right != NULL && sibling->right->color == RED)
{
/*
parent sibling
bh+1|bh+2 bh+1|bh+2
/ \ / \
child sibling --> parent SR
bh bh+1 bh+1 bh+1
/ \ / \
SL SR child SL
bh bh bh bh
*/
*parentp = rotate_left (parent, sibling);
sibling->color = parent->color;
parent->color = BLACK;
sibling->right->color = BLACK;
return;
}
else if (sibling->left != NULL && sibling->left->color == RED)
{
/*
parent parent
bh+1|bh+2 bh+1|bh+2
/ \ / \
child sibling --> child SL
bh bh+1 bh bh+1
/ \ / \
SL SR SLL sibling
bh bh bh bh
/ \ / \
SLL SLR SLR SR
bh bh bh bh
where SLL, SLR, SR are all black.
*/
parent->right = rotate_right (sibling->left, sibling);
/* Change sibling from BLACK to RED and SL from RED to BLACK. */
sibling->color = RED;
sibling = parent->right;
sibling->color = BLACK;
/* Now do as in the previous case. */
*parentp = rotate_left (parent, sibling);
sibling->color = parent->color;
parent->color = BLACK;
sibling->right->color = BLACK;
return;
}
else
{
if (parent->color == BLACK)
{
/* Change sibling from BLACK to RED. Then the entire
subtree at parent has decreased its black-height.
parent parent
bh+2 bh+1
/ \ / \
child sibling --> child sibling
bh bh+1 bh bh
*/
sibling->color = RED;
child = parent;
}
else
{
/* Change parent from RED to BLACK, but compensate by
changing sibling from BLACK to RED.
parent parent
bh+1 bh+1
/ \ / \
child sibling --> child sibling
bh bh+1 bh bh
*/
parent->color = BLACK;
sibling->color = RED;
return;
}
}
}
else if (parent->right == child)
{
NODE_T sibling = parent->left;
/* sibling's black-height is >= 1. In particular,
sibling != NULL.
parent
/ \
sibling child
bh+1 bh
*/
if (sibling->color == RED)
{
/* sibling is RED, hence parent is BLACK and sibling's children
are non-NULL and BLACK.
parent sibling
bh+2 bh+2
/ \ / \
sibling child --> SR parent
bh+1 ch bh+1 bh+1
/ \ / \
SL SR SL child
bh+1 bh+1 bh+1 bh
*/
*parentp = rotate_right (sibling, parent);
parent->color = RED;
sibling->color = BLACK;
/* Concentrate on the subtree of parent. The new sibling is
one of the old sibling's children, and known to be BLACK. */
parentp = &sibling->right;
sibling = parent->left;
}
/* Now we know that sibling is BLACK.
parent
/ \
sibling child
bh+1 bh
*/
if (sibling->left != NULL && sibling->left->color == RED)
{
/*
parent sibling
bh+1|bh+2 bh+1|bh+2
/ \ / \
sibling child --> SL parent
bh+1 bh bh+1 bh+1
/ \ / \
SL SR SR child
bh bh bh bh
*/
*parentp = rotate_right (sibling, parent);
sibling->color = parent->color;
parent->color = BLACK;
sibling->left->color = BLACK;
return;
}
else if (sibling->right != NULL && sibling->right->color == RED)
{
/*
parent parent
bh+1|bh+2 bh+1|bh+2
/ \ / \
sibling child --> SR child
bh+1 bh bh+1 bh
/ \ / \
SL SR sibling SRR
bh bh bh bh
/ \ / \
SRL SRR SL SRL
bh bh bh bh
where SL, SRL, SRR are all black.
*/
parent->left = rotate_left (sibling, sibling->right);
/* Change sibling from BLACK to RED and SL from RED to BLACK. */
sibling->color = RED;
sibling = parent->left;
sibling->color = BLACK;
/* Now do as in the previous case. */
*parentp = rotate_right (sibling, parent);
sibling->color = parent->color;
parent->color = BLACK;
sibling->left->color = BLACK;
return;
}
else
{
if (parent->color == BLACK)
{
/* Change sibling from BLACK to RED. Then the entire
subtree at parent has decreased its black-height.
parent parent
bh+2 bh+1
/ \ / \
sibling child --> sibling child
bh+1 bh bh bh
*/
sibling->color = RED;
child = parent;
}
else
{
/* Change parent from RED to BLACK, but compensate by
changing sibling from BLACK to RED.
parent parent
bh+1 bh+1
/ \ / \
sibling child --> sibling child
bh+1 bh bh bh
*/
parent->color = BLACK;
sibling->color = RED;
return;
}
}
}
else
abort ();
/* Start again with a new (child, parent) pair. */
parent = child->parent;
#if 0 /* Already handled. */
if (child != NULL && child->color == RED)
{
child->color = BLACK;
return;
}
#endif
if (parent == NULL)
return;
}
}
static NODE_T
gl_tree_nx_add_first (CONTAINER_T container, NODE_PAYLOAD_PARAMS)
{
/* Create new node. */
NODE_T new_node =
(struct NODE_IMPL *) malloc (sizeof (struct NODE_IMPL));
if (new_node == NULL)
return NULL;
new_node->left = NULL;
new_node->right = NULL;
NODE_PAYLOAD_ASSIGN(new_node)
/* Add it to the tree. */
if (container->root == NULL)
{
new_node->color = BLACK;
container->root = new_node;
new_node->parent = NULL;
}
else
{
NODE_T node;
for (node = container->root; node->left != NULL; )
node = node->left;
node->left = new_node;
new_node->parent = node;
/* Color and rebalance. */
rebalance_after_add (container, new_node, node);
}
container->count++;
return new_node;
}
/* Adds the already allocated NEW_NODE to the tree, right before NODE. */
static void
gl_tree_add_node_before (CONTAINER_T container, NODE_T node, NODE_T new_node)
{
new_node->left = NULL;
new_node->right = NULL;
/* Add it to the tree. */
if (node->left == NULL)
node->left = new_node;
else
{
for (node = node->left; node->right != NULL; )
node = node->right;
node->right = new_node;
}
new_node->parent = node;
/* Color and rebalance. */
rebalance_after_add (container, new_node, node);
container->count++;
}
static NODE_T
gl_tree_nx_add_before (CONTAINER_T container, NODE_T node, NODE_PAYLOAD_PARAMS)
{
/* Create new node. */
NODE_T new_node =
(struct NODE_IMPL *) malloc (sizeof (struct NODE_IMPL));
if (new_node == NULL)
return NULL;
NODE_PAYLOAD_ASSIGN(new_node)
gl_tree_add_node_before (container, node, new_node);
return new_node;
}
/* Adds the already allocated NEW_NODE to the tree, right after NODE. */
static void
gl_tree_add_node_after (CONTAINER_T container, NODE_T node, NODE_T new_node)
{
new_node->left = NULL;
new_node->right = NULL;
/* Add it to the tree. */
if (node->right == NULL)
node->right = new_node;
else
{
for (node = node->right; node->left != NULL; )
node = node->left;
node->left = new_node;
}
new_node->parent = node;
/* Color and rebalance. */
rebalance_after_add (container, new_node, node);
container->count++;
}
static NODE_T
gl_tree_nx_add_after (CONTAINER_T container, NODE_T node, NODE_PAYLOAD_PARAMS)
{
/* Create new node. */
NODE_T new_node =
(struct NODE_IMPL *) malloc (sizeof (struct NODE_IMPL));
if (new_node == NULL)
return NULL;
NODE_PAYLOAD_ASSIGN(new_node)
gl_tree_add_node_after (container, node, new_node);
return new_node;
}
static void
gl_tree_remove_node_no_free (CONTAINER_T container, NODE_T node)
{
NODE_T parent = node->parent;
if (node->left == NULL)
{
/* Replace node with node->right. */
NODE_T child = node->right;
if (child != NULL)
{
child->parent = parent;
/* Since node->left == NULL, child must be RED and of height 1,
hence node must have been BLACK. Recolor the child. */
child->color = BLACK;
}
if (parent == NULL)
container->root = child;
else
{
if (parent->left == node)
parent->left = child;
else /* parent->right == node */
parent->right = child;
if (child == NULL && node->color == BLACK)
rebalance_after_remove (container, child, parent);
}
}
else if (node->right == NULL)
{
/* It is not absolutely necessary to treat this case. But the more
general case below is more complicated, hence slower. */
/* Replace node with node->left. */
NODE_T child = node->left;
child->parent = parent;
/* Since node->right == NULL, child must be RED and of height 1,
hence node must have been BLACK. Recolor the child. */
child->color = BLACK;
if (parent == NULL)
container->root = child;
else
{
if (parent->left == node)
parent->left = child;
else /* parent->right == node */
parent->right = child;
}
}
else
{
/* Replace node with the rightmost element of the node->left subtree. */
NODE_T subst;
NODE_T subst_parent;
NODE_T child;
color_t removed_color;
for (subst = node->left; subst->right != NULL; )
subst = subst->right;
subst_parent = subst->parent;
child = subst->left;
removed_color = subst->color;
/* The case subst_parent == node is special: If we do nothing special,
we get confusion about node->left, subst->left and child->parent.
subst_parent == node
<==> The 'for' loop above terminated immediately.
<==> subst == subst_parent->left
[otherwise subst == subst_parent->right]
In this case, we would need to first set
child->parent = node; node->left = child;
and later - when we copy subst into node's position - again
child->parent = subst; subst->left = child;
Altogether a no-op. */
if (subst_parent != node)
{
if (child != NULL)
child->parent = subst_parent;
subst_parent->right = child;
}
/* Copy subst into node's position.
(This is safer than to copy subst's value into node, keep node in
place, and free subst.) */
if (subst_parent != node)
{
subst->left = node->left;
subst->left->parent = subst;
}
subst->right = node->right;
subst->right->parent = subst;
subst->color = node->color;
subst->parent = parent;
if (parent == NULL)
container->root = subst;
else if (parent->left == node)
parent->left = subst;
else /* parent->right == node */
parent->right = subst;
if (removed_color == BLACK)
{
if (child != NULL && child->color == RED)
/* Recolor the child. */
child->color = BLACK;
else
/* Rebalancing starts at child's parent, that is subst_parent -
except when subst_parent == node. In this case, we need to use
its replacement, subst. */
rebalance_after_remove (container, child,
subst_parent != node ? subst_parent : subst);
}
}
container->count--;
}
static bool
gl_tree_remove_node (CONTAINER_T container, NODE_T node)
{
gl_tree_remove_node_no_free (container, node);
NODE_PAYLOAD_DISPOSE (container, node)
free (node);
return true;
}
/* For debugging. */
static unsigned int
check_invariants (NODE_T node, NODE_T parent, size_t *counterp)
{
unsigned int left_blackheight =
(node->left != NULL ? check_invariants (node->left, node, counterp) : 0);
unsigned int right_blackheight =
(node->right != NULL ? check_invariants (node->right, node, counterp) : 0);
if (!(node->parent == parent))
abort ();
if (!(node->color == BLACK || node->color == RED))
abort ();
if (parent == NULL && !(node->color == BLACK))
abort ();
if (!(left_blackheight == right_blackheight))
abort ();
(*counterp)++;
return left_blackheight + (node->color == BLACK ? 1 : 0);
}
|