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
|
/*
* The MIT License
*
* Copyright (C) 2015-2016 Alexander Saprykin <saprykin.spb@gmail.com>
* Illustrations have been taken from the Linux kernel rbtree.c
*
* 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 "pmem.h"
#include "ptree-rb.h"
typedef enum PTreeRBColor_ {
P_TREE_RB_COLOR_RED = 0x01,
P_TREE_RB_COLOR_BLACK = 0x02
} PTreeRBColor;
typedef struct PTreeRBNode_ {
struct PTreeBaseNode_ base;
struct PTreeRBNode_ *parent;
PTreeRBColor color;
} PTreeRBNode;
static pboolean pp_tree_rb_is_black (PTreeRBNode *node);
static pboolean pp_tree_rb_is_red (PTreeRBNode *node);
static PTreeRBNode * pp_tree_rb_get_gparent (PTreeRBNode *node);
static PTreeRBNode * pp_tree_rb_get_uncle (PTreeRBNode *node);
static PTreeRBNode * pp_tree_rb_get_sibling (PTreeRBNode *node);
static void pp_tree_rb_rotate_left (PTreeRBNode *node, PTreeBaseNode **root);
static void pp_tree_rb_rotate_right (PTreeRBNode *node, PTreeBaseNode **root);
static void pp_tree_rb_balance_insert (PTreeRBNode *node, PTreeBaseNode **root);
static void pp_tree_rb_balance_remove (PTreeRBNode *node, PTreeBaseNode **root);
static pboolean
pp_tree_rb_is_black (PTreeRBNode *node)
{
if (node == NULL)
return TRUE;
return ((node->color) & P_TREE_RB_COLOR_BLACK) > 0 ? TRUE : FALSE;
}
static pboolean
pp_tree_rb_is_red (PTreeRBNode *node)
{
return ((node->color) & P_TREE_RB_COLOR_RED) > 0 ? TRUE : FALSE;
}
static PTreeRBNode *
pp_tree_rb_get_gparent (PTreeRBNode *node)
{
return node->parent->parent;
}
static PTreeRBNode *
pp_tree_rb_get_uncle (PTreeRBNode *node)
{
PTreeRBNode *gparent = pp_tree_rb_get_gparent (node);
if ((PTreeRBNode *) gparent->base.left == node->parent)
return (PTreeRBNode *) gparent->base.right;
else
return (PTreeRBNode *) gparent->base.left;
}
static PTreeRBNode *
pp_tree_rb_get_sibling (PTreeRBNode *node)
{
if (node->parent->base.left == (PTreeBaseNode *) node)
return (PTreeRBNode *) node->parent->base.right;
else
return (PTreeRBNode *) node->parent->base.left;
}
static void
pp_tree_rb_rotate_left (PTreeRBNode *node, PTreeBaseNode **root)
{
PTreeBaseNode *tmp_node;
tmp_node = node->base.right;
if (P_LIKELY (node->parent != NULL)) {
if (node->parent->base.left == (PTreeBaseNode *) node)
node->parent->base.left = tmp_node;
else
node->parent->base.right = tmp_node;
}
node->base.right = tmp_node->left;
if (tmp_node->left != NULL)
((PTreeRBNode *) tmp_node->left)->parent = node;
tmp_node->left = (PTreeBaseNode *) node;
((PTreeRBNode *) tmp_node)->parent = node->parent;
node->parent = (PTreeRBNode *) tmp_node;
if (P_UNLIKELY (((PTreeRBNode *) tmp_node)->parent == NULL))
*root = tmp_node;
}
static void
pp_tree_rb_rotate_right (PTreeRBNode *node, PTreeBaseNode **root)
{
PTreeBaseNode *tmp_node;
tmp_node = node->base.left;
if (P_LIKELY (node->parent != NULL)) {
if (node->parent->base.left == (PTreeBaseNode *) node)
node->parent->base.left = tmp_node;
else
node->parent->base.right = tmp_node;
}
node->base.left = tmp_node->right;
if (tmp_node->right != NULL)
((PTreeRBNode *) tmp_node->right)->parent = node;
tmp_node->right = (PTreeBaseNode *) node;
((PTreeRBNode *) tmp_node)->parent = node->parent;
node->parent = (PTreeRBNode *) tmp_node;
if (P_UNLIKELY (((PTreeRBNode *) tmp_node)->parent == NULL))
*root = tmp_node;
}
static void
pp_tree_rb_balance_insert (PTreeRBNode *node, PTreeBaseNode **root)
{
PTreeRBNode *uncle;
PTreeRBNode *gparent;
while (TRUE) {
/* Case 1: We are at the root */
if (P_UNLIKELY (node->parent == NULL)) {
node->color = P_TREE_RB_COLOR_BLACK;
break;
}
/* Case 2: We have a black parent */
if (pp_tree_rb_is_black (node->parent) == TRUE)
break;
uncle = pp_tree_rb_get_uncle (node);
gparent = pp_tree_rb_get_gparent (node);
/* Case 3: Both parent and uncle are red, flip colors
*
* G g
* / \ / \
* p u --> P U
* / /
* n n
*/
if (uncle != NULL && pp_tree_rb_is_red (uncle) == TRUE) {
node->parent->color = P_TREE_RB_COLOR_BLACK;
uncle->color = P_TREE_RB_COLOR_BLACK;
gparent->color = P_TREE_RB_COLOR_RED;
/* Continue iteratively from gparent */
node = gparent;
continue;
}
if (node->parent == (PTreeRBNode *) gparent->base.left) {
if (node == (PTreeRBNode *) node->parent->base.right) {
/* Case 4a: Left rotate at parent
*
* G G
* / \ / \
* p U --> n U
* \ /
* n p
*/
pp_tree_rb_rotate_left (node->parent, root);
node = (PTreeRBNode *) node->base.left;
}
gparent->color = P_TREE_RB_COLOR_RED;
node->parent->color = P_TREE_RB_COLOR_BLACK;
/* Case 5a: Right rotate at gparent
*
* G P
* / \ / \
* p U --> n g
* / \
* n U
*/
pp_tree_rb_rotate_right (gparent, root);
break;
} else {
if (node == (PTreeRBNode *) node->parent->base.left) {
/* Case 4b: Right rotate at parent */
pp_tree_rb_rotate_right (node->parent, root);
node = (PTreeRBNode *) node->base.right;
}
gparent->color = P_TREE_RB_COLOR_RED;
node->parent->color = P_TREE_RB_COLOR_BLACK;
/* Case 5b: Left rotate at gparent*/
pp_tree_rb_rotate_left (gparent, root);
break;
}
}
}
static void
pp_tree_rb_balance_remove (PTreeRBNode *node, PTreeBaseNode **root)
{
PTreeRBNode *sibling;
while (TRUE) {
/* Case 1: We are at the root */
if (P_UNLIKELY (node->parent == NULL))
break;
sibling = pp_tree_rb_get_sibling (node);
if (pp_tree_rb_is_red (sibling) == TRUE) {
/*
* Case 2: Left (right) rotate at parent
*
* P S
* / \ / \
* N s --> p Sr
* / \ / \
* Sl Sr N Sl
*/
node->parent->color = P_TREE_RB_COLOR_RED;
sibling->color = P_TREE_RB_COLOR_BLACK;
if ((PTreeBaseNode *) node == node->parent->base.left)
pp_tree_rb_rotate_left (node->parent, root);
else
pp_tree_rb_rotate_right (node->parent, root);
sibling = pp_tree_rb_get_sibling (node);
}
/*
* Case 3: Sibling (parent) color flip
*
* (p) (p)
* / \ / \
* N S --> N s
* / \ / \
* Sl Sr Sl Sr
*/
if (pp_tree_rb_is_black ((PTreeRBNode *) sibling->base.left) == TRUE &&
pp_tree_rb_is_black ((PTreeRBNode *) sibling->base.right) == TRUE) {
sibling->color = P_TREE_RB_COLOR_RED;
if (pp_tree_rb_is_black (node->parent) == TRUE) {
node = node->parent;
continue;
} else {
node->parent->color = P_TREE_RB_COLOR_BLACK;
break;
}
}
/*
* Case 4: Right (left) rotate at sibling
*
* (p) (p)
* / \ / \
* N S --> N Sl
* / \ \
* sl Sr s
* \
* Sr
*/
if ((PTreeBaseNode *) node == node->parent->base.left &&
pp_tree_rb_is_black ((PTreeRBNode *) sibling->base.right) == TRUE) {
sibling->color = P_TREE_RB_COLOR_RED;
((PTreeRBNode *) sibling->base.left)->color = P_TREE_RB_COLOR_BLACK;
pp_tree_rb_rotate_right (sibling, root);
sibling = pp_tree_rb_get_sibling (node);
} else if ((PTreeBaseNode *) node == node->parent->base.right &&
pp_tree_rb_is_black ((PTreeRBNode *) sibling->base.left) == TRUE) {
sibling->color = P_TREE_RB_COLOR_RED;
((PTreeRBNode *) sibling->base.right)->color = P_TREE_RB_COLOR_BLACK;
pp_tree_rb_rotate_left (sibling, root);
sibling = pp_tree_rb_get_sibling (node);
}
/*
* Case 5: Left (right) rotate at parent and color flips
*
* (p) (s)
* / \ / \
* N S --> P Sr
* / \ / \
* (sl) sr N (sl)
*/
sibling->color = node->parent->color;
node->parent->color = P_TREE_RB_COLOR_BLACK;
if ((PTreeBaseNode *) node == node->parent->base.left) {
((PTreeRBNode *) sibling->base.right)->color = P_TREE_RB_COLOR_BLACK;
pp_tree_rb_rotate_left (node->parent, root);
} else {
((PTreeRBNode *) sibling->base.left)->color = P_TREE_RB_COLOR_BLACK;
pp_tree_rb_rotate_right (node->parent, root);
}
break;
}
}
pboolean
p_tree_rb_insert (PTreeBaseNode **root_node,
PCompareDataFunc compare_func,
ppointer data,
PDestroyFunc key_destroy_func,
PDestroyFunc value_destroy_func,
ppointer key,
ppointer value)
{
PTreeBaseNode **cur_node;
PTreeBaseNode *parent_node;
pint cmp_result;
cur_node = root_node;
parent_node = *root_node;
/* Find where to insert the node */
while (*cur_node != NULL) {
cmp_result = compare_func (key, (*cur_node)->key, data);
if (cmp_result < 0) {
parent_node = *cur_node;
cur_node = &(*cur_node)->left;
} else if (cmp_result > 0) {
parent_node = *cur_node;
cur_node = &(*cur_node)->right;
} else
break;
}
/* If we have existing one - replace a key-value pair */
if (*cur_node != NULL) {
if (key_destroy_func != NULL)
key_destroy_func ((*cur_node)->key);
if (value_destroy_func != NULL)
value_destroy_func ((*cur_node)->value);
(*cur_node)->key = key;
(*cur_node)->value = value;
return FALSE;
}
if (P_UNLIKELY ((*cur_node = p_malloc0 (sizeof (PTreeRBNode))) == NULL))
return FALSE;
(*cur_node)->key = key;
(*cur_node)->value = value;
((PTreeRBNode *) *cur_node)->color = P_TREE_RB_COLOR_RED;
((PTreeRBNode *) *cur_node)->parent = (PTreeRBNode *) parent_node;
/* Balance the tree */
pp_tree_rb_balance_insert ((PTreeRBNode *) *cur_node, root_node);
return TRUE;
}
pboolean
p_tree_rb_remove (PTreeBaseNode **root_node,
PCompareDataFunc compare_func,
ppointer data,
PDestroyFunc key_destroy_func,
PDestroyFunc value_destroy_func,
pconstpointer key)
{
PTreeBaseNode *cur_node;
PTreeBaseNode *prev_node;
PTreeBaseNode *child_node;
PTreeRBNode *child_parent;
pint cmp_result;
cur_node = *root_node;
while (cur_node != NULL) {
cmp_result = compare_func (key, cur_node->key, data);
if (cmp_result < 0)
cur_node = cur_node->left;
else if (cmp_result > 0)
cur_node = cur_node->right;
else
break;
}
if (P_UNLIKELY (cur_node == NULL))
return FALSE;
if (cur_node->left != NULL && cur_node->right != NULL) {
prev_node = cur_node->left;
while (prev_node->right != NULL)
prev_node = prev_node->right;
cur_node->key = prev_node->key;
cur_node->value = prev_node->value;
/* Mark node for removal */
cur_node = prev_node;
}
child_node = cur_node->left == NULL ? cur_node->right : cur_node->left;
if (child_node == NULL && pp_tree_rb_is_black ((PTreeRBNode *) cur_node) == TRUE)
pp_tree_rb_balance_remove ((PTreeRBNode *) cur_node, root_node);
/* Replace node with its child */
if (cur_node == *root_node) {
*root_node = child_node;
child_parent = NULL;
} else {
child_parent = ((PTreeRBNode *) cur_node)->parent;
if (child_parent->base.left == cur_node)
child_parent->base.left = child_node;
else
child_parent->base.right = child_node;
}
if (child_node != NULL) {
((PTreeRBNode *) child_node)->parent = child_parent;
/* Check if we need to repaint the node */
if (pp_tree_rb_is_black ((PTreeRBNode *) cur_node) == TRUE)
((PTreeRBNode *) child_node)->color = P_TREE_RB_COLOR_BLACK;
}
/* Free unused node */
if (key_destroy_func != NULL)
key_destroy_func (cur_node->key);
if (value_destroy_func != NULL)
value_destroy_func (cur_node->value);
p_free (cur_node);
return TRUE;
}
void
p_tree_rb_node_free (PTreeBaseNode *node)
{
p_free (node);
}
|