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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
20 * Copyright 2004 The FreeRADIUS server project
23 static const char rcsid[] = "$Id$";
30 #include "libradius.h"
32 /* red-black tree description */
33 typedef enum { Black, Red } NodeColor;
36 rbnode_t *Left; /* left child */
37 rbnode_t *Right; /* right child */
38 rbnode_t *Parent; /* parent */
39 NodeColor Color; /* node color (black, red) */
40 void *Data; /* data stored in node */
43 #define NIL &Sentinel /* all leafs are sentinels */
44 static rbnode_t Sentinel = { NIL, NIL, NULL, Black, NULL};
52 int (*Compare)(const void *, const void *);
54 void (*freeNode)(void *);
56 #define RBTREE_MAGIC (0x5ad09c42)
59 * Walks the tree to delete all nodes.
60 * Does NOT re-balance it!
62 static void FreeWalker(rbtree_t *tree, rbnode_t *X)
64 if (X->Left != NIL) FreeWalker(tree, X->Left);
65 if (X->Right != NIL) FreeWalker(tree, X->Right);
67 if (tree->freeNode) tree->freeNode(X->Data);
71 void rbtree_free(rbtree_t *tree)
76 * Walk the tree, deleting the nodes...
78 if (tree->Root != NIL) FreeWalker(tree, tree->Root);
88 * Create a new red-black tree.
90 rbtree_t *rbtree_create(int (*Compare)(const void *, const void *),
91 void (*freeNode)(void *),
96 if (!Compare) return NULL;
98 tree = malloc(sizeof(*tree));
99 if (!tree) return NULL;
101 memset(tree, 0, sizeof(*tree));
103 tree->magic = RBTREE_MAGIC;
106 tree->Compare = Compare;
107 tree->replace_flag = replace_flag;
108 tree->freeNode = freeNode;
114 static void RotateLeft(rbtree_t *tree, rbnode_t *X)
116 /**************************
117 * rotate Node X to left *
118 **************************/
120 rbnode_t *Y = X->Right;
122 /* establish X->Right link */
124 if (Y->Left != NIL) Y->Left->Parent = X;
126 /* establish Y->Parent link */
127 if (Y != NIL) Y->Parent = X->Parent;
129 if (X == X->Parent->Left)
132 X->Parent->Right = Y;
139 if (X != NIL) X->Parent = Y;
142 static void RotateRight(rbtree_t *tree, rbnode_t *X)
144 /****************************
145 * rotate Node X to right *
146 ****************************/
148 rbnode_t *Y = X->Left;
150 /* establish X->Left link */
152 if (Y->Right != NIL) Y->Right->Parent = X;
154 /* establish Y->Parent link */
155 if (Y != NIL) Y->Parent = X->Parent;
157 if (X == X->Parent->Right)
158 X->Parent->Right = Y;
167 if (X != NIL) X->Parent = Y;
170 static void InsertFixup(rbtree_t *tree, rbnode_t *X)
172 /*************************************
173 * maintain red-black tree balance *
174 * after inserting node X *
175 *************************************/
177 /* check red-black properties */
178 while (X != tree->Root && X->Parent->Color == Red) {
179 /* we have a violation */
180 if (X->Parent == X->Parent->Parent->Left) {
181 rbnode_t *Y = X->Parent->Parent->Right;
182 if (Y->Color == Red) {
185 X->Parent->Color = Black;
187 X->Parent->Parent->Color = Red;
188 X = X->Parent->Parent;
192 if (X == X->Parent->Right) {
193 /* make X a left child */
198 /* recolor and rotate */
199 X->Parent->Color = Black;
200 X->Parent->Parent->Color = Red;
201 RotateRight(tree, X->Parent->Parent);
205 /* mirror image of above code */
206 rbnode_t *Y = X->Parent->Parent->Left;
207 if (Y->Color == Red) {
210 X->Parent->Color = Black;
212 X->Parent->Parent->Color = Red;
213 X = X->Parent->Parent;
217 if (X == X->Parent->Left) {
219 RotateRight(tree, X);
221 X->Parent->Color = Black;
222 X->Parent->Parent->Color = Red;
223 RotateLeft(tree, X->Parent->Parent);
228 tree->Root->Color = Black;
233 * Insert an element into the tree.
235 int rbtree_insert(rbtree_t *tree, void *Data)
237 rbnode_t *Current, *Parent, *X;
239 /***********************************************
240 * allocate node for Data and insert in tree *
241 ***********************************************/
243 /* find where node belongs */
244 Current = tree->Root;
246 while (Current != NIL) {
250 * See if two entries are identical.
252 result = tree->Compare(Data, Current->Data);
255 * Don't replace the entry.
257 if (tree->replace_flag == 0) {
262 * Do replace the entry.
264 Current->Data = Data;
269 Current = (result < 0) ? Current->Left : Current->Right;
273 if ((X = malloc (sizeof(*X))) == NULL) {
283 /* insert node in tree */
285 if (tree->Compare(Data, Parent->Data) <= 0)
293 InsertFixup(tree, X);
295 tree->num_elements++;
301 static void DeleteFixup(rbtree_t *tree, rbnode_t *X)
303 /*************************************
304 * maintain red-black tree balance *
305 * after deleting node X *
306 *************************************/
308 while (X != tree->Root && X->Color == Black) {
309 if (X == X->Parent->Left) {
310 rbnode_t *W = X->Parent->Right;
311 if (W->Color == Red) {
313 X->Parent->Color = Red;
314 RotateLeft(tree, X->Parent);
315 W = X->Parent->Right;
317 if (W->Left->Color == Black && W->Right->Color == Black) {
321 if (W->Right->Color == Black) {
322 W->Left->Color = Black;
324 RotateRight(tree, W);
325 W = X->Parent->Right;
327 W->Color = X->Parent->Color;
328 X->Parent->Color = Black;
329 W->Right->Color = Black;
330 RotateLeft(tree, X->Parent);
334 rbnode_t *W = X->Parent->Left;
335 if (W->Color == Red) {
337 X->Parent->Color = Red;
338 RotateRight(tree, X->Parent);
341 if (W->Right->Color == Black && W->Left->Color == Black) {
345 if (W->Left->Color == Black) {
346 W->Right->Color = Black;
351 W->Color = X->Parent->Color;
352 X->Parent->Color = Black;
353 W->Left->Color = Black;
354 RotateRight(tree, X->Parent);
363 * Delete an element from the tree.
365 void rbtree_delete(rbtree_t *tree, rbnode_t *Z)
369 /*****************************
370 * delete node Z from tree *
371 *****************************/
373 if (!Z || Z == NIL) return;
375 if (Z->Left == NIL || Z->Right == NIL) {
376 /* Y has a NIL node as a child */
379 /* find tree successor with a NIL node as a child */
381 while (Y->Left != NIL) Y = Y->Left;
384 /* X is Y's only child */
390 /* remove Y from the parent chain */
391 X->Parent = Y->Parent;
393 if (Y == Y->Parent->Left)
396 Y->Parent->Right = X;
402 * Move the child's data to here, and then
403 * re-balance the tree.
405 if (tree->freeNode) tree->freeNode(Z->Data);
408 } else if (tree->freeNode) {
409 tree->freeNode(Z->Data);
411 if (Y->Color == Black)
412 DeleteFixup(tree, X);
416 tree->num_elements--;
420 * Find an element in the tree, returning the data, not the node.
422 rbnode_t *rbtree_find(rbtree_t *tree, void *Data)
424 /*******************************
425 * find node containing Data *
426 *******************************/
428 rbnode_t *Current = tree->Root;
430 while (Current != NIL) {
431 int result = tree->Compare(Data, Current->Data);
436 Current = (result < 0) ?
437 Current->Left : Current->Right;
444 * Find the user data.
446 void *rbtree_finddata(rbtree_t *tree, void *Data)
450 X = rbtree_find(tree, Data);
457 * Walk the tree, Pre-order
459 * We call ourselves recursively for each function, but that's OK,
460 * as the stack is only log(N) deep, which is ~12 entries deep.
462 static int WalkNodePreOrder(rbnode_t *X, int (*callback)(void *))
466 rcode = callback(X->Data);
467 if (rcode != 0) return rcode;
469 if (X->Left != NIL) {
470 rcode = WalkNodePreOrder(X->Left, callback);
471 if (rcode != 0) return rcode;
474 if (X->Right != NIL) {
475 rcode = WalkNodePreOrder(X->Right, callback);
476 if (rcode != 0) return rcode;
479 return 0; /* we know everything returned zero */
485 static int WalkNodeInOrder(rbnode_t *X, int (*callback)(void *))
489 if (X->Left != NIL) {
490 rcode = WalkNodeInOrder(X->Left, callback);
491 if (rcode != 0) return rcode;
494 rcode = callback(X->Data);
495 if (rcode != 0) return rcode;
497 if (X->Right != NIL) {
498 rcode = WalkNodeInOrder(X->Right, callback);
499 if (rcode != 0) return rcode;
502 return 0; /* we know everything returned zero */
509 static int WalkNodePostOrder(rbnode_t *X, int (*callback)(void *))
513 if (X->Left != NIL) {
514 rcode = WalkNodeInOrder(X->Left, callback);
515 if (rcode != 0) return rcode;
518 if (X->Right != NIL) {
519 rcode = WalkNodeInOrder(X->Right, callback);
520 if (rcode != 0) return rcode;
523 rcode = callback(X->Data);
524 if (rcode != 0) return rcode;
526 return 0; /* we know everything returned zero */
530 * Walk the entire tree. The callback function CANNOT modify
533 * The callback function should return 0 to continue walking.
534 * Any other value stops the walk, and is returned.
536 int rbtree_walk(rbtree_t *tree, int (*callback)(void *), RBTREE_ORDER order)
540 return WalkNodePreOrder(tree->Root, callback);
542 return WalkNodeInOrder(tree->Root, callback);
544 return WalkNodePostOrder(tree->Root, callback);
553 int rbtree_num_elements(rbtree_t *tree)
557 return tree->num_elements;
562 * Given a Node, return the data.
564 void *rbtree_node2data(rbtree_t *tree, rbnode_t *node)
566 tree = tree; /* -Wunused */
568 if (!node) return NULL;