2 * rbtree.c Red-black balanced binary trees.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2004,2006 The FreeRADIUS server project
23 #include <freeradius-devel/ident.h>
26 #include <freeradius-devel/libradius.h>
28 /* red-black tree description */
29 typedef enum { Black, Red } NodeColor;
32 rbnode_t *Left; /* left child */
33 rbnode_t *Right; /* right child */
34 rbnode_t *Parent; /* parent */
35 NodeColor Color; /* node color (black, red) */
36 void *Data; /* data stored in node */
39 #define NIL &Sentinel /* all leafs are sentinels */
40 static rbnode_t Sentinel = { NIL, NIL, NULL, Black, NULL};
48 int (*Compare)(const void *, const void *);
50 void (*freeNode)(void *);
52 #define RBTREE_MAGIC (0x5ad09c42)
55 * Walks the tree to delete all nodes.
56 * Does NOT re-balance it!
58 static void FreeWalker(rbtree_t *tree, rbnode_t *X)
60 if (X->Left != NIL) FreeWalker(tree, X->Left);
61 if (X->Right != NIL) FreeWalker(tree, X->Right);
63 if (tree->freeNode) tree->freeNode(X->Data);
67 void rbtree_free(rbtree_t *tree)
72 * Walk the tree, deleting the nodes...
74 if (tree->Root != NIL) FreeWalker(tree, tree->Root);
84 * Create a new red-black tree.
86 rbtree_t *rbtree_create(int (*Compare)(const void *, const void *),
87 void (*freeNode)(void *),
92 if (!Compare) return NULL;
94 tree = malloc(sizeof(*tree));
95 if (!tree) return NULL;
97 memset(tree, 0, sizeof(*tree));
99 tree->magic = RBTREE_MAGIC;
102 tree->Compare = Compare;
103 tree->replace_flag = replace_flag;
104 tree->freeNode = freeNode;
110 static void RotateLeft(rbtree_t *tree, rbnode_t *X)
112 /**************************
113 * rotate Node X to left *
114 **************************/
116 rbnode_t *Y = X->Right;
118 /* establish X->Right link */
120 if (Y->Left != NIL) Y->Left->Parent = X;
122 /* establish Y->Parent link */
123 if (Y != NIL) Y->Parent = X->Parent;
125 if (X == X->Parent->Left)
128 X->Parent->Right = Y;
135 if (X != NIL) X->Parent = Y;
138 static void RotateRight(rbtree_t *tree, rbnode_t *X)
140 /****************************
141 * rotate Node X to right *
142 ****************************/
144 rbnode_t *Y = X->Left;
146 /* establish X->Left link */
148 if (Y->Right != NIL) Y->Right->Parent = X;
150 /* establish Y->Parent link */
151 if (Y != NIL) Y->Parent = X->Parent;
153 if (X == X->Parent->Right)
154 X->Parent->Right = Y;
163 if (X != NIL) X->Parent = Y;
166 static void InsertFixup(rbtree_t *tree, rbnode_t *X)
168 /*************************************
169 * maintain red-black tree balance *
170 * after inserting node X *
171 *************************************/
173 /* check red-black properties */
174 while (X != tree->Root && X->Parent->Color == Red) {
175 /* we have a violation */
176 if (X->Parent == X->Parent->Parent->Left) {
177 rbnode_t *Y = X->Parent->Parent->Right;
178 if (Y->Color == Red) {
181 X->Parent->Color = Black;
183 X->Parent->Parent->Color = Red;
184 X = X->Parent->Parent;
188 if (X == X->Parent->Right) {
189 /* make X a left child */
194 /* recolor and rotate */
195 X->Parent->Color = Black;
196 X->Parent->Parent->Color = Red;
197 RotateRight(tree, X->Parent->Parent);
201 /* mirror image of above code */
202 rbnode_t *Y = X->Parent->Parent->Left;
203 if (Y->Color == Red) {
206 X->Parent->Color = Black;
208 X->Parent->Parent->Color = Red;
209 X = X->Parent->Parent;
213 if (X == X->Parent->Left) {
215 RotateRight(tree, X);
217 X->Parent->Color = Black;
218 X->Parent->Parent->Color = Red;
219 RotateLeft(tree, X->Parent->Parent);
224 tree->Root->Color = Black;
229 * Insert an element into the tree.
231 rbnode_t *rbtree_insertnode(rbtree_t *tree, void *Data)
233 rbnode_t *Current, *Parent, *X;
235 /***********************************************
236 * allocate node for Data and insert in tree *
237 ***********************************************/
239 /* find where node belongs */
240 Current = tree->Root;
242 while (Current != NIL) {
246 * See if two entries are identical.
248 result = tree->Compare(Data, Current->Data);
251 * Don't replace the entry.
253 if (tree->replace_flag == 0) {
258 * Do replace the entry.
260 if (tree->freeNode) tree->freeNode(Current->Data);
261 Current->Data = Data;
266 Current = (result < 0) ? Current->Left : Current->Right;
270 if ((X = malloc (sizeof(*X))) == NULL) {
271 exit(1); /* FIXME! */
280 /* insert node in tree */
282 if (tree->Compare(Data, Parent->Data) <= 0)
290 InsertFixup(tree, X);
292 tree->num_elements++;
297 int rbtree_insert(rbtree_t *tree, void *Data)
299 if (rbtree_insertnode(tree, Data)) return 1;
303 static void DeleteFixup(rbtree_t *tree, rbnode_t *X, rbnode_t *Parent)
305 /*************************************
306 * maintain red-black tree balance *
307 * after deleting node X *
308 *************************************/
310 while (X != tree->Root && X->Color == Black) {
311 if (X == Parent->Left) {
312 rbnode_t *W = Parent->Right;
313 if (W->Color == Red) {
315 Parent->Color = Red; /* Parent != NIL? */
316 RotateLeft(tree, Parent);
319 if (W->Left->Color == Black && W->Right->Color == Black) {
320 if (W != NIL) W->Color = Red;
324 if (W->Right->Color == Black) {
325 if (W->Left != NIL) W->Left->Color = Black;
327 RotateRight(tree, W);
330 W->Color = Parent->Color;
331 if (Parent != NIL) Parent->Color = Black;
332 if (W->Right->Color != Black) {
333 W->Right->Color = Black;
335 RotateLeft(tree, Parent);
339 rbnode_t *W = Parent->Left;
340 if (W->Color == Red) {
342 Parent->Color = Red; /* Parent != NIL? */
343 RotateRight(tree, Parent);
346 if (W->Right->Color == Black && W->Left->Color == Black) {
347 if (W != NIL) W->Color = Red;
351 if (W->Left->Color == Black) {
352 if (W->Right != NIL) W->Right->Color = Black;
357 W->Color = Parent->Color;
358 if (Parent != NIL) Parent->Color = Black;
359 if (W->Left->Color != Black) {
360 W->Left->Color = Black;
362 RotateRight(tree, Parent);
371 * Delete an element from the tree.
373 void rbtree_delete(rbtree_t *tree, rbnode_t *Z)
378 /*****************************
379 * delete node Z from tree *
380 *****************************/
382 if (!Z || Z == NIL) return;
384 if (Z->Left == NIL || Z->Right == NIL) {
385 /* Y has a NIL node as a child */
388 /* find tree successor with a NIL node as a child */
390 while (Y->Left != NIL) Y = Y->Left;
393 /* X is Y's only child */
397 X = Y->Right; /* may be NIL! */
399 /* remove Y from the parent chain */
401 if (X != NIL) X->Parent = Parent;
404 if (Y == Parent->Left)
412 if (tree->freeNode) tree->freeNode(Z->Data);
416 if (Y->Color == Black && X != NIL)
417 DeleteFixup(tree, X, Parent);
420 * The user structure in Y->Data MAY include a
421 * pointer to Y. In that case, we CANNOT delete
422 * Y. Instead, we copy Z (which is now in the
423 * tree) to Y, and fix up the parent/child
426 memcpy(Y, Z, sizeof(*Y));
431 if (Y->Parent->Left == Z) Y->Parent->Left = Y;
432 if (Y->Parent->Right == Z) Y->Parent->Right = Y;
434 if (Y->Left->Parent == Z) Y->Left->Parent = Y;
435 if (Y->Right->Parent == Z) Y->Right->Parent = Y;
440 if (tree->freeNode) tree->freeNode(Y->Data);
442 if (Y->Color == Black && X != NIL)
443 DeleteFixup(tree, X, Parent);
448 tree->num_elements--;
452 * Delete a node from the tree, based on given data, which MUST
453 * have come from rbtree_finddata().
455 int rbtree_deletebydata(rbtree_t *tree, const void *data)
457 rbnode_t *node = rbtree_find(tree, data);
459 if (!node) return 0; /* false */
461 rbtree_delete(tree, node);
468 * Find an element in the tree, returning the data, not the node.
470 rbnode_t *rbtree_find(rbtree_t *tree, const void *Data)
472 /*******************************
473 * find node containing Data *
474 *******************************/
476 rbnode_t *Current = tree->Root;
478 while (Current != NIL) {
479 int result = tree->Compare(Data, Current->Data);
484 Current = (result < 0) ?
485 Current->Left : Current->Right;
492 * Find the user data.
494 void *rbtree_finddata(rbtree_t *tree, const void *Data)
498 X = rbtree_find(tree, Data);
505 * Walk the tree, Pre-order
507 * We call ourselves recursively for each function, but that's OK,
508 * as the stack is only log(N) deep, which is ~12 entries deep.
510 static int WalkNodePreOrder(rbnode_t *X,
511 int (*callback)(void *, void *), void *context)
515 rcode = callback(context, X->Data);
516 if (rcode != 0) return rcode;
518 if (X->Left != NIL) {
519 rcode = WalkNodePreOrder(X->Left, callback, context);
520 if (rcode != 0) return rcode;
523 if (X->Right != NIL) {
524 rcode = WalkNodePreOrder(X->Right, callback, context);
525 if (rcode != 0) return rcode;
528 return 0; /* we know everything returned zero */
534 static int WalkNodeInOrder(rbnode_t *X,
535 int (*callback)(void *, void *), void *context)
539 if (X->Left != NIL) {
540 rcode = WalkNodeInOrder(X->Left, callback, context);
541 if (rcode != 0) return rcode;
544 rcode = callback(context, X->Data);
545 if (rcode != 0) return rcode;
547 if (X->Right != NIL) {
548 rcode = WalkNodeInOrder(X->Right, callback, context);
549 if (rcode != 0) return rcode;
552 return 0; /* we know everything returned zero */
559 static int WalkNodePostOrder(rbnode_t *X,
560 int (*callback)(void *, void*), void *context)
564 if (X->Left != NIL) {
565 rcode = WalkNodeInOrder(X->Left, callback, context);
566 if (rcode != 0) return rcode;
569 if (X->Right != NIL) {
570 rcode = WalkNodeInOrder(X->Right, callback, context);
571 if (rcode != 0) return rcode;
574 rcode = callback(context, X->Data);
575 if (rcode != 0) return rcode;
577 return 0; /* we know everything returned zero */
581 * Walk the entire tree. The callback function CANNOT modify
584 * The callback function should return 0 to continue walking.
585 * Any other value stops the walk, and is returned.
587 int rbtree_walk(rbtree_t *tree, RBTREE_ORDER order,
588 int (*callback)(void *, void *), void *context)
590 if (tree->Root == NIL) return 0;
594 return WalkNodePreOrder(tree->Root, callback, context);
596 return WalkNodeInOrder(tree->Root, callback, context);
598 return WalkNodePostOrder(tree->Root, callback, context);
607 int rbtree_num_elements(rbtree_t *tree)
611 return tree->num_elements;
616 * Given a Node, return the data.
618 void *rbtree_node2data(rbtree_t *tree, rbnode_t *node)
620 tree = tree; /* -Wunused */
622 if (!node) return NULL;
628 * Return left-most child.
630 void *rbtree_min(rbtree_t *tree)
634 if (!tree || !tree->Root) return NULL;
636 Current = tree->Root;
637 while (Current->Left != NIL) Current = Current->Left;
639 return Current->Data;