-
Notifications
You must be signed in to change notification settings - Fork 5
/
rbtree.c
225 lines (178 loc) · 5.43 KB
/
rbtree.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
/**
*
* Copyright 2017 Mihai Serban <[email protected]>
*
* SPDX short identifier: MIT
*/
#include <assert.h>
#include <stdlib.h>
#include "rbtree.h"
//#define NDEBUG
static rb_node_t *__insert(rb_tree_t *root, rb_node_t *node, void *data)
{
int c;
rb_node_t **x = &(root->node);
rb_node_t *parent = NULL;
while (*x) {
c = root->insert_cmp(data, *x);
if (c == 0)
return NULL; // no duplicates
parent = *x;
if (c < 0)
x = &((*x)->left);
else
x = &((*x)->right);
}
node->left = node->right = NULL;
node->parent = parent;
node->color = RB_TREE_RED;
*x = node;
return *x; // node
}
static void __rotate_left(rb_tree_t *tree, rb_node_t *x)
{
rb_node_t *right = x->right;
assert(x->right); // rotate on left because we have a right child
x->right = right->left;
if (x->right)
x->right->parent = x;
right->left = x;
right->parent = x->parent;
if (x->parent) {
if (x == x->parent->left)
right->parent->left = right;
else
right->parent->right = right;
}
x->parent = right;
if (x == tree->node)
tree->node = right;
}
static void __rotate_right(rb_tree_t *tree, rb_node_t *x)
{
rb_node_t *left = x->left;
assert(x->left); // rotate on right because we have a left child
x->left = left->right;
if (x->left)
x->left->parent = x;
left->right = x;
left->parent = x->parent;
if (x->parent) {
if (x == x->parent->left)
left->parent->left = left;
else
left->parent->right = left;
}
x->parent = left;
if (x == tree->node)
tree->node = left;
}
int rb_tree_add(rb_tree_t *tree, rb_node_t *node, void *data)
{
rb_node_t *x = __insert(tree, node, data);
rb_node_t *parent, *gparent, *uncle;
if (!x)
return -1;
while (x != tree->node && rb_color(x) == RB_TREE_RED) {
parent = x->parent;
if (!parent)
break;
gparent = parent->parent;
if (!gparent)
break;
// on left
if (parent == gparent->left) {
uncle = gparent->right;
if (uncle && rb_color(uncle) == RB_TREE_RED) {
// case 1: recolor, recurse on gparent
gparent->color = RB_TREE_RED;
parent->color = RB_TREE_BLACK;
uncle->color = RB_TREE_BLACK;
x = gparent;
} else { // uncle is black
if (x == parent->right) { // left rotate on parent, zig-zag --> zig-zig, new x becomes parent
// case 2
__rotate_left(tree, parent);
// prepare for case 3 below
x = parent;
parent = x->parent;
gparent = parent->parent;
}
// case 3: black->red->red on left, fix with right rotation on
// gparent and recolor
__rotate_right(tree, gparent);
parent->color = RB_TREE_BLACK;
x->color = RB_TREE_RED; // already red but does not matter
gparent->color = RB_TREE_RED;
x = parent; // this will end because color(parent)=black
}
} else { // on right, mirror the left side cases
uncle = gparent->left;
if (uncle && rb_color(uncle) == RB_TREE_RED) {
// case 1: recolor, recurse on gparent
gparent->color = RB_TREE_RED;
parent->color = RB_TREE_BLACK;
uncle->color = RB_TREE_BLACK;
x = gparent;
} else { // uncle is black
if (x == parent->left) { // right rotate on parent, zig-zag --> zig-zig, new x becomes parent
// case 2
__rotate_right(tree, parent);
// prepare for case 3 below
x = parent;
parent = x->parent;
gparent = parent->parent;
}
// case 3: black->red->red on right, fix with left rotation on
// gparent and recolor
__rotate_left(tree, gparent);
parent->color = RB_TREE_BLACK;
x->color = RB_TREE_RED; // already red but does not matter
gparent->color = RB_TREE_RED;
x = parent; // this will end because color(parent)=black
}
}
}
// root is black
tree->node->color = RB_TREE_BLACK;
return 0;
}
rb_node_t *rb_tree_find(rb_tree_t *tree, void *data)
{
rb_node_t *x = tree->node;
int c;
while (x) {
c = tree->find_cmp(data, x);
if (c == 0)
return x;
if (c < 1)
x = x->left;
else
x = x->right;
}
return NULL;
}
rb_node_t *rb_first(rb_tree_t *t)
{
rb_node_t *node = t->node;
if (!node)
return NULL;
while (node->left)
node = node->left;
return node;
}
rb_node_t *rb_next(rb_node_t *node)
{
rb_node_t *x;
// get left-most node from right child
if (node->right) {
x = node->right;
while (x->left)
x = x->left;
return x;
}
// go up to the first parent that has me on its left leaf
while ((x = node->parent) && x->right == node)
node = x;
return x;
}