-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtree.c
1557 lines (1313 loc) · 32.5 KB
/
tree.c
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
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2013 Neil McGill
*
* See the LICENSE file for license.
*
* A red/black tree implementation. Rules:
*
* 1. Each node is either red or black.
* 2. The root node is black.
* 3. All leaf nodes are black.
* 4. Every red node has two black children.
* 5. Any paths from node to leaf has the same number of black nodes.
*/
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include "main.h"
int32_t gcc_no_warn;
static inline int32_t
tree_key_int32_compare_func (const tree_node *a, const tree_node *b)
{
tree_key_int *A = (typeof(A))a;
tree_key_int *B = (typeof(B))b;
if (A->key < B->key) {
return (-1);
}
if (A->key > B->key) {
return (1);
}
return (0);
}
static int32_t
tree_key_two_int32_compare_func (const tree_node *a, const tree_node *b)
{
tree_key_two_int *A = (typeof(A))a;
tree_key_two_int *B = (typeof(B))b;
if (A->key1 < B->key1) {
return (-1);
}
if (A->key1 > B->key1) {
return (1);
}
if (A->key2 < B->key2) {
return (-1);
}
if (A->key2 > B->key2) {
return (1);
}
return (0);
}
static int32_t tree_key_string_compare_func (const tree_node *a,
const tree_node *b)
{
tree_key_string *A = (typeof(A))a;
tree_key_string *B = (typeof(B))b;
return (strcmp(A->key, B->key));
}
static int32_t tree_key_pointer_compare_func (const tree_node *a,
const tree_node *b)
{
tree_key_pointer *A = (typeof(A))a;
tree_key_pointer *B = (typeof(B))b;
if (A->key < B->key) {
return (-1);
}
if (A->key > B->key) {
return (1);
}
return (0);
}
static inline int32_t tree_node_compare_func (const tree_root *root,
const tree_node *a,
const tree_node *b)
{
switch (root->type) {
case TREE_KEY_CUSTOM:
return (*(root->compare_func))(a, b);
case TREE_KEY_INTEGER:
return (tree_key_int32_compare_func(a, b));
case TREE_KEY_TWO_INTEGER:
return (tree_key_two_int32_compare_func(a, b));
case TREE_KEY_STRING:
return (tree_key_string_compare_func(a, b));
case TREE_KEY_POINTER:
return (tree_key_pointer_compare_func(a, b));
}
return (0);
}
tree_node *tree_root_top (tree_root *root)
{
if (!root) {
return (0);
}
return (root->node);
}
tree_node *tree_top (tree_node *node)
{
while (node) {
if (!node->parent) {
return (node);
}
}
return (node);
}
static tree_node *parent (const tree_node *node)
{
ASSERT(node);
return (node->parent);
}
static tree_node *grandparent (const tree_node *node)
{
ASSERT(node);
ASSERT(node->parent);
return (node->parent->parent);
}
static tree_node *sibling (const tree_node *node)
{
ASSERT(node);
ASSERT(node->parent);
if (node == node->parent->left) {
return (node->parent->right);
} else {
return (node->parent->left);
}
}
static tree_node *uncle (const tree_node * node)
{
ASSERT(node);
ASSERT(node->parent);
return (sibling(node->parent));
}
/*
* Leaf nodes that are nil and the nil root node are black.
*/
static node_color tree_node_color (const tree_node * node)
{
return (node ? node->color : RB_BLACK);
}
/*
* Tree has no contents?
*/
boolean tree_root_is_empty (tree_root *root)
{
tree_node *top;
if (!root) {
return (true);
}
top = root->node;
if (!top) {
return (true);
}
return (false);
}
/*
* Get the least node in the subtree.
*/
tree_node *tree_root_first (tree_root *root)
{
tree_node *top;
if (!root) {
return (0);
}
top = root->node;
if (!top) {
return (0);
}
while (top->left) {
top = top->left;
}
return (top);
}
/*
* Get the highest node in the subtree.
*/
tree_node *tree_root_last (tree_root *root)
{
tree_node *top;
if (!root) {
return (0);
}
top = root->node;
if (!top) {
return (0);
}
while (top->right) {
top = top->right;
}
return (top);
}
/*
* Get the least node in the subtree.
*/
tree_node *tree_first (tree_node *top)
{
if (!top) {
return (0);
}
while (top->left) {
top = top->left;
}
return (top);
}
/*
* Get the highest node in the subtree.
*/
tree_node *tree_last (tree_node *top)
{
if (!top) {
return (0);
}
while (top->right) {
top = top->right;
}
return (top);
}
/*
* Find the next highest node.
*/
tree_node *tree_get_next (tree_root *root,
tree_node *top,
tree_node *node)
{
tree_node *subtree;
int32_t compare;
if (!top) {
return (0);
}
compare = tree_node_compare_func(root, top, node);
if (compare == 0) {
/*
* top == node
*
* Dive into the right tree and return the least node.
*
* 4 (top 4, node 4, look at 7)
* / \
* 3 8
* / / \
* 1 7 9
*/
if (!top->right) {
return (0);
}
return (tree_first(top->right));
}
if (compare < 0) {
/*
* top < node
*
* Dive into the right tree.
*
* 4 (top 4, node 5, look at 8)
* / \
* 3 8
* / / \
* 1 7 9
*/
return (tree_get_next(root, top->right, node));
}
/*
* top > node
*
* Dive into the left tree.
*
* 4 (top 4, node 3, look at 1)
* / \
* 3 8
* / / \
* 1 7 9
*/
subtree = tree_get_next(root, top->left, node);
if (subtree) {
return (subtree);
}
/*
* top > node
*
* If there is no subtree.
*
* 4 (top 1, node 0, look at 1)
* / \
* 3 8
* / / \
* 1 7 9
*/
return (top);
}
/*
* Find the next lowest node.
*/
tree_node *tree_get_prev (tree_root *root,
tree_node *top,
tree_node *node)
{
tree_node *subtree;
int32_t compare;
if (!top) {
return (0);
}
compare = tree_node_compare_func(root, top, node);
if (compare == 0) {
/*
* top == node
*
* Dive into the left tree and return the greatest node.
*
* 4 (top 4, node 4, look at 3)
* / \
* 3 8
* / / \
* 1 7 9
*/
if (!top->left) {
return (0);
}
return (tree_last(top->left));
}
if (compare < 0) {
/*
* top < node
*
* Dive into the right tree.
*
* 4 (top 4, node 5, look at 8)
* / \
* 3 8
* / / \
* 1 7 9
*/
subtree = tree_get_prev(root, top->right, node);
if (subtree) {
return (subtree);
}
/*
* top > node
*
* If there is no subtree.
*
* 4 (top 4, node 5, return 4)
* /
* 3
* /
* 1
*/
return (top);
}
/*
* top > node
*
* Dive into the left tree.
*
* 4 (top 4, node 3, look at 1)
* / \
* 3 8
* / / \
* 1 7 9
*/
return (tree_get_prev(root, top->left, node));
}
/*
* How many nodes.
*/
uint32_t tree_size (const tree_node *top)
{
if (!top) {
return (0);
}
return (1 + tree_size(top->left) + tree_size(top->right));
}
/*
* Find a node.
*/
tree_node *tree_find (tree_root *root, const tree_node *target)
{
tree_node *top;
int32_t compare;
if (!root) {
return (0);
}
top = root->node;
while (top) {
compare = tree_node_compare_func(root, top, target);
if (compare == 0) {
return (top);
}
if (compare < 0) {
top = top->right;
continue;
}
top = top->left;
}
return (0);
}
#ifdef OLD_TREE_WALK_MUCH_SLOWER
/*
* Walk all nodes. Not safe if next node is destroyed. Use TREE_WALK instead.
*/
int tree_walk (tree_root *root, tree_walker_func walker_func, void *arg)
{
tree_node *top;
tree_node *node;
tree_node *next;
top = root->node;
if (!top) {
return (1);
}
node = tree_first(top);
while (node) {
next = tree_get_next(root, top, node);
if (!(*walker_func)(node, arg)) {
return (0);
}
node = next;
}
return (1);
}
#endif
/*
* Walk all nodes. Not safe if next node is destroyed. Use TREE_WALK instead.
*/
tree_node *tree_next (tree_root *root, tree_node *node)
{
tree_node *next;
int32_t compare;
if (!node) {
return (0);
}
if (node->right) {
next = tree_first(node->right);
} else if (node->parent) {
next = node;
do {
next = next->parent;
if (!next) {
break;
}
compare = tree_node_compare_func(root, next, node);
} while (compare <= 0);
} else {
next = 0;
}
return (next);
}
tree_node *tree_prev (tree_root *root, tree_node *node)
{
tree_node *next;
int32_t compare;
if (!node) {
return (0);
}
if (node->left) {
next = tree_last(node->left);
} else if (node->parent) {
next = node;
do {
next = next->parent;
if (!next) {
break;
}
compare = tree_node_compare_func(root, next, node);
} while (compare > 0);
} else {
next = 0;
}
return (next);
}
/*
* Walk all nodes. Not safe if next node is destroyed. Use TREE_WALK instead.
*/
boolean tree_walk (tree_root *root, tree_walker_func walker_func, void *arg)
{
tree_node *node;
tree_node *next;
next = node = tree_root_first(root);
while (next) {
node = next;
next = tree_next(root, node);
if (!(*walker_func)(node, arg)) {
return (0);
}
}
return (1);
}
/*
* Walk all nodes. Not safe if next node is destroyed. Use TREE_WALK instead.
*/
boolean tree_walk_reverse (tree_root *root,
tree_walker_func walker_func, void *arg)
{
tree_node *node;
tree_node *prev;
prev = node = tree_root_last(root);
while (prev) {
node = prev;
prev = tree_prev(root, node);
if (!(*walker_func)(node, arg)) {
return (0);
}
}
return (1);
}
/*
* Find the nth node. Slow.
*/
tree_node *tree_root_get_nth (tree_root *root, uint32_t n)
{
tree_node *node;
tree_node *next;
tree_node *top;
uint32_t count;
int32_t compare;
top = root->node;
if (!top) {
return (0);
}
node = tree_first(top);
count = 0;
while (node) {
if (count++ == n) {
return (node);
}
if (node->right) {
next = tree_first(node->right);
} else if (node->parent) {
next = node;
do {
next = next->parent;
if (!next) {
break;
}
compare = tree_node_compare_func(root, next, node);
} while (compare <= 0);
} else {
next = 0;
}
node = next;
}
return (0);
}
uint32_t tree_root_size (tree_root *root)
{
tree_node *top;
top = root->node;
if (!top) {
return (0);
}
return (tree_size(top));
}
tree_node *tree_root_get_random (tree_root *root)
{
tree_node *top;
uint32_t size;
uint32_t r;
top = root->node;
if (!top) {
return (0);
}
size = tree_size(top);
if (!size) {
return (0);
}
r = rand() % size;
return (tree_root_get_nth(root, r));
}
#ifdef ENABLE_TREE_SANITY
/*
* A node must be red or black.
*/
static void tree_verify_node_is_red_or_black (tree_node *node)
{
ASSERT((tree_node_color(node) == RB_RED) ||
(tree_node_color(node) == RB_BLACK));
if (!node) {
return;
}
ASSERT(node->left != node);
ASSERT(node->right != node);
ASSERT(node->parent != node);
tree_verify_node_is_red_or_black(node->left);
tree_verify_node_is_red_or_black(node->right);
}
/*
* The root node must be black.
*/
static void tree_verify_root_is_black (tree_node *node)
{
if (!node) {
return;
}
ASSERT(tree_node_color(node) == RB_BLACK);
}
/*
* Every red node has two black children and a black parent.
*/
static void
tree_verify_every_red_node_has_two_black_children (tree_node *node)
{
if (tree_node_color(node) == RB_RED) {
ASSERT(tree_node_color(node->left) == RB_BLACK);
ASSERT(tree_node_color(node->right) == RB_BLACK);
ASSERT(tree_node_color(node->parent) == RB_BLACK);
}
if (!node) {
return;
}
ASSERT(node->left != node);
ASSERT(node->right != node);
ASSERT(node->parent != node);
if (node->parent) {
ASSERT((node->parent->left == node) ||
(node->parent->right == node));
}
tree_verify_every_red_node_has_two_black_children(node->left);
tree_verify_every_red_node_has_two_black_children(node->right);
}
/*
* Count every black node path and make sure they are the same.
*/
static void
tree_verify_count_black_path (const tree_node * node,
int this_black_path_count,
int *first_saved_black_path_count)
{
/*
* Remember if this is a leaf node, it is black.
*/
if (tree_node_color(node) == RB_BLACK) {
this_black_path_count++;
}
/*
* If we read a leaf node...
*/
if (!node) {
/*
* And if we have no saved path count...
*/
if (*first_saved_black_path_count == -1) {
/*
* Save it.
*/
*first_saved_black_path_count = this_black_path_count;
} else {
/*
* Else check this new path is the same.
*/
ASSERT(this_black_path_count == *first_saved_black_path_count);
}
return;
}
/*
* Check the children.
*/
tree_verify_count_black_path(node->left, this_black_path_count,
first_saved_black_path_count);
tree_verify_count_black_path(node->right, this_black_path_count,
first_saved_black_path_count);
}
/*
* Count every black node path and make sure they are the same.
*/
static void
tree_verify_all_paths_have_same_black_node_count (const tree_node *root)
{
int black_path_count;
black_path_count = -1;
tree_verify_count_black_path(root, 0, &black_path_count);
}
#endif
/*
* Perform checks that the red black tree is valid.
*/
static void tree_verify (const tree_root *root)
{
#ifdef ENABLE_TREE_SANITY
ASSERT(root);
tree_verify_root_is_black(root->node);
tree_verify_node_is_red_or_black(root->node);
tree_verify_every_red_node_has_two_black_children(root->node);
tree_verify_all_paths_have_same_black_node_count(root->node);
#endif
}
/*
* Move the new node to the same position as the old node.
*/
static void tree_replace_node (tree_root *root,
tree_node *old_node,
tree_node *new_node)
{
if (!old_node->parent) {
/*
* Old node was the root.
*/
root->node = new_node;
} else {
/*
* Make the old nodes parent see only the new node.
*/
if (old_node == old_node->parent->left) {
old_node->parent->left = new_node;
} else {
old_node->parent->right = new_node;
}
}
/*
* And make the new node only see the old node's parent.
*/
if (new_node) {
new_node->parent = old_node->parent;
}
}
/*
* Swap two nodes in the tree
*/
static void tree_swap_nodes (tree_root *root, tree_node *a, tree_node *b)
{
tree_node A;
tree_node B;
tree_node PA;
tree_node PB;
A = *a;
B = *b;
/*
* Handle root node change.
*/
if (A.parent) {
PA = *(A.parent);
} else {
root->node = b;
PA.left = 0;
PA.right = 0;
}
if (B.parent) {
PB = *(B.parent);
} else {
root->node = a;
PB.left = 0;
PB.right = 0;
}
/*
* Make the children point to new parents.
*/
if (A.left) {
A.left->parent = b;
}
if (A.right) {
A.right->parent = b;
}
if (B.left) {
B.left->parent = a;
}
if (B.right) {
B.right->parent = a;
}
/*
* Make the parents point to new children.
*/
if (PA.left == a) {
A.parent->left = b;
} else if (PA.right == a) {
A.parent->right = b;
}
if (PB.left == b) {
B.parent->left = a;
} else if (PB.right == b) {
B.parent->right = a;
}
/*
* Swap the nodes.
*/
*a = B;
*b = A;
/*
* Fixup for the case where nodes are immediately adjacent and the above
* pointer swap causes loops.
*/
if (a->left == a) {
a->left = b;
} else if (a->right == a) {
a->right = b;
} else if (a->parent == a) {
a->parent = b;
}
if (b->left == b) {
b->left = a;
} else if (b->right == b) {
b->right = a;
} else if (b->parent == b) {
b->parent = a;
}
}
/*
* Rotate a tree right.
*
* A <---- node B
* / \ / \
* / \ / \
* / \ / \
* succ-> B C ----> D A
* / \ / \ / \ / \
* / \ / \ / \ / \
* D 3 4 5 1 2 3 C
* / \ / \
* / \ / \
* 1 2 4 5
*/
static void tree_rotate_right (tree_root *root, tree_node *node)
{
tree_node *successor;
successor = node->left;
tree_replace_node(root, node, successor);
node->left = successor->right;
if (successor->right) {
successor->right->parent = node;
}
successor->right = node;
node->parent = successor;
}
/*
* Rotate a tree left.
*
* A B <---- node
* / \ / \
* / \ / \
* / \ / \
* B C <---- D A <---- successor
* / \ / \ / \ / \
* / \ / \ / \ / \
* D 3 4 5 1 2 3 C
* / \ / \
* / \ / \
* 1 2 4 5
*/
static void tree_rotate_left (tree_root *root, tree_node *node)
{
tree_node *successor;