aboutsummaryrefslogtreecommitdiff
path: root/src/rbt.c
blob: 53bfbd416546d84853bb82cce15d7430ab0365ed (plain)
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
/* Red-black tree support for Ping903
   Copyright (C) 2020-2023 Sergey Poznyakoff
  
   Ping903 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, or (at your option)
   any later version.
  
   Ping903 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 Ping903.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <stdlib.h>
#include <string.h>
#include "ping903.h"

typedef struct rbt_node RBT_NODE;

typedef enum { RED, BLACK } RBT_COLOR;

struct rbt_node {
    RBT_NODE *left, *right, *parent;
    RBT_COLOR color;
    HOSTPING *host;
};

struct rbt_tree {
    int (*cmp)(HOSTPING*, HOSTPING*);
    RBT_NODE *root;	/* Root of the tree */
};

RBT_TREE *
rbt_tree_create(int (*cmp)(HOSTPING*, HOSTPING*))
{
    RBT_TREE *tree = malloc(sizeof(*tree));
    if (tree) {
	tree->cmp = cmp;
	tree->root = NULL;
    }
    return tree;
}

/*
 * Red-black tree properties:
 *  1. Each node is either red or black.
 *  2. The root node is black.
 *  3. All leaves are black and contain no data.
 *  4. Every red node has two children, and both are black. IOW, the
 *     parent of every red node is black.
 *  5. All paths from any given node to its leaf nodes contain the same
 *     number of black nodes. 
 */

/*
 * Auxiliary functions for accessing nodes. 
 */

/*
 * Return the grandparent node of N. Prerequisite: N may not be root. 
 */
static inline RBT_NODE *
grandparent(RBT_NODE *n)
{
    return n->parent->parent;
}

/*
 * Return the sibling node of N. Prerequisite: N may not be root. 
 */
static inline RBT_NODE *
sibling(RBT_NODE *n)
{
    return (n == n->parent->left) ? n->parent->right : n->parent->left;
}

/*
 * Return the uncle node of N. Prerequisite: N must be at least 2 nodes
 * away from root. 
 */
static inline RBT_NODE *
uncle(RBT_NODE *n)
{
    return sibling(n->parent);
}

/*
 * Returns the color of the node N. Empty leaves are represented by NULL,
 * therefore NULL is assumed to be black (see property 3). 
 */
static inline RBT_COLOR
node_color(RBT_NODE *n)
{
    return n == NULL ? BLACK : n->color;
}

/*
 * Replace the OLDN with NEWN. Does not modify OLDN. 
 */
static void
replace_node(RBT_TREE *tree, RBT_NODE *oldn, RBT_NODE *newn)
{
    if (oldn->parent == NULL)
        tree->root = newn;
    else if (oldn == oldn->parent->left)
        oldn->parent->left = newn;
    else
        oldn->parent->right = newn;

    if (newn != NULL)
        newn->parent = oldn->parent;
}

/*
 * Rotate the TREE left over the node N. 
 */
static void
rotate_left(RBT_TREE *tree, RBT_NODE *n)
{
    RBT_NODE *right = n->right;
    replace_node(tree, n, right);
    n->right = right->left;
    if (right->left != NULL)
        right->left->parent = n;
    right->left = n;
    n->parent = right;
}

/*
 * Rotate the TREE right over the node N. 
 */
static void
rotate_right(RBT_TREE *tree, RBT_NODE *n)
{
    RBT_NODE *left = n->left;
    replace_node(tree, n, left);
    n->left = left->right;
    if (left->right != NULL)
        left->right->parent = n;
    left->right = n;
    n->parent = left;
}

static void
rbt_delete_fixup(RBT_TREE *tree, RBT_NODE *n)
{
    while (1) {
        if (n->parent == NULL) {
            /*
             * If N has become the root node, deletion resulted in
             * removing one black node (prior root) from every path, so
             * all properties still hold. 
             */
            return;
        } else {
            /*
             * If N has a red sibling, change the colors of the parent and 
             * sibling and rotate about the parent. Thus, the sibling
             * becomes grandparent and we can proceed to the next case. 
             */
            if (node_color(sibling(n)) == RED) {
                n->parent->color = RED;
                sibling(n)->color = BLACK;
                if (n == n->parent->left)
                    rotate_left(tree, n->parent);
                else
                    rotate_right(tree, n->parent);
            }

            /*
             * If the parent, sibling and nephews are all black, paint the 
             * sibling red.  This means one black node was removed from
             * all paths passing through the parent, so we recurse to the
             * beginning of the loop with parent as the argument to
             * restore the properties.  This is the only branch that
             * loops. 
             */
            if (node_color(n->parent) == BLACK
                && node_color(sibling(n)) == BLACK
                && node_color(sibling(n)->left) == BLACK
                && node_color(sibling(n)->right) == BLACK) {
                sibling(n)->color = RED;
                n = n->parent;
                continue;
            } else {
                /*
                 * If the sibling and nephews are black but the parent is
                 * red, swap the colors of the sibling and parent.  The
                 * properties are then restored. 
                 */
                if (node_color(n->parent) == RED
                    && node_color(sibling(n)) == BLACK
                    && node_color(sibling(n)->left) == BLACK
		    && node_color(sibling(n)->right) == BLACK) {
                    sibling(n)->color = RED;
                    n->parent->color = BLACK;
                } else {
                    /*
                     * N is the left child of its parent, its sibling is
                     * black, and the sibling's right child is black. Swap 
                     * the colors of the sibling and its left sibling and
                     * rotate right over the sibling. 
                     */
                    if (n == n->parent->left
                        && node_color(sibling(n)) == BLACK
                        && node_color(sibling(n)->left) == RED
                        && node_color(sibling(n)->right) == BLACK) {
                        sibling(n)->color = RED;
                        sibling(n)->left->color = BLACK;
                        rotate_right(tree, sibling(n));
                    } else if (n == n->parent->right
                               && node_color(sibling(n)) == BLACK
                               && node_color(sibling(n)->right) == RED
                               && node_color(sibling(n)->left) == BLACK) {
                        /*
                         * The mirror case is handled similarly. 
                         */
                        sibling(n)->color = RED;
                        sibling(n)->right->color = BLACK;
                        rotate_left(tree, sibling(n));
                    }
                    /*
                     * N is the left child of its parent, its sibling is
                     * black and the sibling's right child is red.  Swap
                     * the colors of the parent and sibling, paint the
                     * sibling's right child black and rotate left at the
                     * parent.  Similarly for the mirror case.  This
                     * achieves the following:
                     * 
                     *  . A black node is added to all paths passing
                     *    through N;
		     *  . A black node is removed from all paths 
                     *    through the sibling's red child.
		     *  . The latter is painted black which restores the
		     *    missing black node in all paths through the
		     *    sibling's red child.
                     * 
                     * Another sibling's child becomes a child of N's
                     * parent during the rotation and is therefore not
                     * affected. 
                     */
                    sibling(n)->color = node_color(n->parent);
                    n->parent->color = BLACK;
                    if (n == n->parent->left) {
                        sibling(n)->right->color = BLACK;
                        rotate_left(tree, n->parent);
                    } else {
                        sibling(n)->left->color = BLACK;
                        rotate_right(tree, n->parent);
                    }
                }
            }
        }
        break;
    }
}

/*
 * Remove N from the TREE. 
 */
void
rbt_delete_node(RBT_TREE *tree, RBT_NODE *n)
{
    RBT_NODE *child;

    /*
     * If N has both left and right children, reduce the problem to the
     * node with only one child.  To do so, find the in-order predecessor
     * of N, copy its value (elem) to N and then delete the predecessor. 
     */
    if (n->left != NULL && n->right != NULL) {
        RBT_NODE *p;
        for (p = n->left; p->right; p = p->right)
	    ;
        n->host = p->host;
        n = p;
    }

    /*
     * N has only one child. Select it. 
     */
    child = n->left ? n->left : n->right;
    if (node_color(n) == BLACK) {
        n->color = node_color(child);
        rbt_delete_fixup(tree, n);
    }
    replace_node(tree, n, child);
    if (n->parent == NULL && child != NULL)  /* root should be black */
        child->color = BLACK;

    free(n);
}

static void
rbt_insert_fixup(RBT_TREE *tree, RBT_NODE *n)
{
    while (1) {
        if (n->parent == NULL) {
            /*
             * Node was inserted at the root of the tree. The root node
             * must be black (property 2).  Changing its color to black
             * would add one black node to every path, which means the
             * property 5 would remain satisfied.  So we simply paint the
             * node black. 
             */
            n->color = BLACK;
        } else if (node_color(n->parent) == BLACK) {
            /*
             * The node has black parent. All properties are satisfied.
             * There's no need to change anything. 
             */
            return;
        } else if (node_color(uncle(n)) == RED) {
            /*
             * The uncle node is red. Repaint the parent and uncle black
             * and the grandparent red.  This would satisfy 4.  However,
             * if the grandparent is root, this would violate the property 
             * 2.  So we repaint the grandparent by re-entering the fixup
             * loop with grandparent as the node. This is the only branch
             * that loops. 
             */
            n->parent->color = BLACK;
            uncle(n)->color = BLACK;
            n = grandparent(n);
            n->color = RED;
            continue;
        } else {
            /*
             * The new node is the right child of its parent and the
             * parent is the left child of the grandparent.  Rotate left
             * about the parent. Mirror case: The new node is the left
             * child of its parent and the parent is the right child of
             * the grandparent.  Rotate right about the parent.  This
             * fixes the properties for the rbt_insert_5. 
             */
            if (n == n->parent->right && n->parent == grandparent(n)->left) {
                rotate_left(tree, n->parent);
                n = n->left;
            } else if (n == n->parent->left
                       && n->parent == grandparent(n)->right) {
                rotate_right(tree, n->parent);
                n = n->right;
            }

            /*
             * The new node is the left child of its parent and the parent 
             * is the left child of the grandparent. Rotate right about
             * the grandparent. Mirror case: The new node is the right
             * child of its parent and the parent is the right child of
             * the grandparent. Rotate left. 
             */
            n->parent->color = BLACK;
            grandparent(n)->color = RED;
            if (n == n->parent->left && n->parent == grandparent(n)->left) {
                rotate_right(tree, grandparent(n));
            } else {
                rotate_left(tree, grandparent(n));
            }
        }
        break;
    }
}

RBT_LOOKUP_RESULT
rbt_lookup_or_insert_node(RBT_TREE *tree, HOSTPING *key, int insert,
			  RBT_NODE **retval)
{
    RBT_LOOKUP_RESULT res;
    RBT_NODE *node, *parent = NULL;
    RBT_NODE **nodeptr;

    nodeptr = &tree->root;
    while ((node = *nodeptr) != NULL) {
        int rc = tree->cmp(key, node->host);
        if (rc == 0)
            break;
        parent = node;
        if (rc < 0)
            nodeptr = &node->left;
        else
            nodeptr = &node->right;
    }

    if (node) {
        res = RBT_LOOKUP_SUCCESS;
        *retval = node;
    } else {
	res = RBT_LOOKUP_NOENT;
	if (insert) {
	    node = malloc(sizeof(*node));
	    if (!node)
		return RBT_LOOKUP_FAILURE;
	    memset(node, 0, sizeof(*node));
	    *nodeptr = node;
	    node->parent = parent;
	    rbt_insert_fixup(tree, node);
	    *retval = node;
	}
    }
    return res;
}

HOSTPING *
rbt_lookup(RBT_TREE *tree, HOSTPING *key)
{
    RBT_NODE *node;
    switch (rbt_lookup_or_insert_node(tree, key, 0, &node)) {
    case RBT_LOOKUP_SUCCESS:
	return node->host;

    case RBT_LOOKUP_NOENT:
	return NULL;

    default:
	/* Should not happen, since no allocation takes place. */
	abort();
    }
}

RBT_LOOKUP_RESULT
rbt_insert(RBT_TREE *tree, HOSTPING *host)
{
    RBT_NODE *node;
    RBT_LOOKUP_RESULT res;
    res = rbt_lookup_or_insert_node(tree, host, 1, &node);
    switch (res) {
    case RBT_LOOKUP_SUCCESS:
    case RBT_LOOKUP_FAILURE:
	break;

    case RBT_LOOKUP_NOENT:
	node->host = host;
	break;

    default:
	/* Should not happen */
	abort();
    }
    return res;
}

void
rbt_delete(RBT_TREE *tree, HOSTPING *host)
{
    RBT_NODE *node;
    if (rbt_lookup_or_insert_node(tree, host, 0, &node) == RBT_LOOKUP_SUCCESS)
	rbt_delete_node(tree, node);
}    

/* Local Variables: */
/* mode: c */
/* c-basic-offset: 4 */
/* End: */

Return to:

Send suggestions and report system problems to the System administrator.