2 avl_tree.c -- avl_ tree and linked list convenience
3 Copyright (C) 1998 Michael H. Buselli
4 2000-2005 Ivo Timmermans,
5 2000-2006 Guus Sliepen <guus@tinc-vpn.org>
6 2000-2005 Wessel Dankers <wsl@tinc-vpn.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 Original AVL tree library by Michael H. Buselli <cosine@cosine.org>.
24 Modified 2000-11-28 by Wessel Dankers <wsl@tinc-vpn.org> to use counts
25 instead of depths, to add the ->next and ->prev and to generally obfuscate
26 the code. Mail me if you found a bug.
28 Cleaned up and incorporated some of the ideas from the red-black tree
29 library for inclusion into tinc (http://www.tinc-vpn.org/) by
30 Guus Sliepen <guus@tinc-vpn.org>.
41 #define AVL_NODE_COUNT(n) ((n) ? (n)->count : 0)
42 #define AVL_L_COUNT(n) (AVL_NODE_COUNT((n)->left))
43 #define AVL_R_COUNT(n) (AVL_NODE_COUNT((n)->right))
44 #define AVL_CALC_COUNT(n) (AVL_L_COUNT(n) + AVL_R_COUNT(n) + 1)
48 #define AVL_NODE_DEPTH(n) ((n) ? (n)->depth : 0)
49 #define L_AVL_DEPTH(n) (AVL_NODE_DEPTH((n)->left))
50 #define R_AVL_DEPTH(n) (AVL_NODE_DEPTH((n)->right))
51 #define AVL_CALC_DEPTH(n) ((L_AVL_DEPTH(n)>R_AVL_DEPTH(n)?L_AVL_DEPTH(n):R_AVL_DEPTH(n)) + 1)
55 static int lg(unsigned int u) __attribute__ ((__const__));
57 static int lg(unsigned int u) {
90 /* Internal helper functions */
92 static int avl_check_balance(const avl_node_t *node) {
96 d = R_AVL_DEPTH(node) - L_AVL_DEPTH(node);
98 return d < -1 ? -1 : d > 1 ? 1 : 0;
101 * d = lg(AVL_R_COUNT(node)) - lg(AVL_L_COUNT(node));
102 * d = d<-1?-1:d>1?1:0;
106 pl = lg(AVL_L_COUNT(node));
107 r = AVL_R_COUNT(node);
112 if(pl < 2 || r >> pl - 2)
119 static void avl_rebalance(avl_tree_t *tree, avl_node_t *node) {
123 avl_node_t **superparent;
128 parent = node->parent;
132 parent->left ? &parent->left : &parent->right : &tree->root;
134 switch (avl_check_balance(node)) {
138 if(L_AVL_DEPTH(child) >= R_AVL_DEPTH(child)) {
140 if(AVL_L_COUNT(child) >= AVL_R_COUNT(child)) {
142 node->left = child->right;
144 node->left->parent = node;
147 node->parent = child;
148 *superparent = child;
149 child->parent = parent;
151 node->count = AVL_CALC_COUNT(node);
152 child->count = AVL_CALC_COUNT(child);
155 node->depth = AVL_CALC_DEPTH(node);
156 child->depth = AVL_CALC_DEPTH(child);
159 gchild = child->right;
160 node->left = gchild->right;
163 node->left->parent = node;
164 child->right = gchild->left;
167 child->right->parent = child;
168 gchild->right = node;
171 gchild->right->parent = gchild;
172 gchild->left = child;
175 gchild->left->parent = gchild;
176 *superparent = gchild;
178 gchild->parent = parent;
180 node->count = AVL_CALC_COUNT(node);
181 child->count = AVL_CALC_COUNT(child);
182 gchild->count = AVL_CALC_COUNT(gchild);
185 node->depth = AVL_CALC_DEPTH(node);
186 child->depth = AVL_CALC_DEPTH(child);
187 gchild->depth = AVL_CALC_DEPTH(gchild);
195 if(R_AVL_DEPTH(child) >= L_AVL_DEPTH(child)) {
197 if(AVL_R_COUNT(child) >= AVL_L_COUNT(child)) {
199 node->right = child->left;
201 node->right->parent = node;
203 node->parent = child;
204 *superparent = child;
205 child->parent = parent;
207 node->count = AVL_CALC_COUNT(node);
208 child->count = AVL_CALC_COUNT(child);
211 node->depth = AVL_CALC_DEPTH(node);
212 child->depth = AVL_CALC_DEPTH(child);
215 gchild = child->left;
216 node->right = gchild->left;
219 node->right->parent = node;
220 child->left = gchild->right;
223 child->left->parent = child;
227 gchild->left->parent = gchild;
228 gchild->right = child;
231 gchild->right->parent = gchild;
233 *superparent = gchild;
234 gchild->parent = parent;
236 node->count = AVL_CALC_COUNT(node);
237 child->count = AVL_CALC_COUNT(child);
238 gchild->count = AVL_CALC_COUNT(gchild);
241 node->depth = AVL_CALC_DEPTH(node);
242 child->depth = AVL_CALC_DEPTH(child);
243 gchild->depth = AVL_CALC_DEPTH(gchild);
250 node->count = AVL_CALC_COUNT(node);
253 node->depth = AVL_CALC_DEPTH(node);
260 /* (De)constructors */
262 avl_tree_t *avl_alloc_tree(avl_compare_t compare, avl_action_t delete) {
265 tree = xmalloc_and_zero(sizeof(avl_tree_t));
266 tree->compare = compare;
267 tree->delete = delete;
272 void avl_free_tree(avl_tree_t *tree) {
276 avl_node_t *avl_alloc_node(void) {
277 return xmalloc_and_zero(sizeof(avl_node_t));
280 void avl_free_node(avl_tree_t *tree, avl_node_t *node) {
281 if(node->data && tree->delete)
282 tree->delete(node->data);
289 void *avl_search(const avl_tree_t *tree, const void *data) {
292 node = avl_search_node(tree, data);
294 return node ? node->data : NULL;
297 void *avl_search_closest(const avl_tree_t *tree, const void *data, int *result) {
300 node = avl_search_closest_node(tree, data, result);
302 return node ? node->data : NULL;
305 void *avl_search_closest_smaller(const avl_tree_t *tree, const void *data) {
308 node = avl_search_closest_smaller_node(tree, data);
310 return node ? node->data : NULL;
313 void *avl_search_closest_greater(const avl_tree_t *tree, const void *data) {
316 node = avl_search_closest_greater_node(tree, data);
318 return node ? node->data : NULL;
321 avl_node_t *avl_search_node(const avl_tree_t *tree, const void *data) {
325 node = avl_search_closest_node(tree, data, &result);
327 return result ? NULL : node;
330 avl_node_t *avl_search_closest_node(const avl_tree_t *tree, const void *data,
344 c = tree->compare(data, node->data);
372 avl_node_t *avl_search_closest_smaller_node(const avl_tree_t *tree,
377 node = avl_search_closest_node(tree, data, &result);
385 avl_node_t *avl_search_closest_greater_node(const avl_tree_t *tree,
390 node = avl_search_closest_node(tree, data, &result);
398 /* Insertion and deletion */
400 avl_node_t *avl_insert(avl_tree_t *tree, void *data) {
401 avl_node_t *closest, *new;
405 new = avl_alloc_node();
407 avl_insert_top(tree, new);
409 closest = avl_search_closest_node(tree, data, &result);
413 new = avl_alloc_node();
415 avl_insert_before(tree, closest, new);
419 new = avl_alloc_node();
421 avl_insert_after(tree, closest, new);
439 avl_node_t *avl_insert_node(avl_tree_t *tree, avl_node_t *node) {
444 avl_insert_top(tree, node);
446 closest = avl_search_closest_node(tree, node->data, &result);
450 avl_insert_before(tree, closest, node);
454 avl_insert_after(tree, closest, node);
472 void avl_insert_top(avl_tree_t *tree, avl_node_t *node) {
473 node->prev = node->next = node->parent = NULL;
474 tree->head = tree->tail = tree->root = node;
477 void avl_insert_before(avl_tree_t *tree, avl_node_t *before,
481 avl_insert_after(tree, tree->tail, node);
483 avl_insert_top(tree, node);
488 node->parent = before;
489 node->prev = before->prev;
492 avl_insert_after(tree, before->prev, node);
497 before->prev->next = node;
504 avl_rebalance(tree, before);
507 void avl_insert_after(avl_tree_t *tree, avl_node_t *after, avl_node_t *node) {
510 avl_insert_before(tree, tree->head, node);
512 avl_insert_top(tree, node);
517 avl_insert_before(tree, after->next, node);
522 node->parent = after;
523 node->next = after->next;
526 after->next->prev = node;
533 avl_rebalance(tree, after);
536 avl_node_t *avl_unlink(avl_tree_t *tree, void *data) {
539 node = avl_search_node(tree, data);
542 avl_unlink_node(tree, node);
547 void avl_unlink_node(avl_tree_t *tree, avl_node_t *node) {
549 avl_node_t **superparent;
550 avl_node_t *subst, *left, *right;
554 node->prev->next = node->next;
556 tree->head = node->next;
558 node->next->prev = node->prev;
560 tree->tail = node->prev;
562 parent = node->parent;
566 parent->left ? &parent->left : &parent->right : &tree->root;
571 *superparent = right;
574 right->parent = parent;
579 left->parent = parent;
587 balnode = subst->parent;
588 balnode->right = subst->left;
591 balnode->right->parent = balnode;
594 left->parent = subst;
597 subst->right = right;
598 subst->parent = parent;
599 right->parent = subst;
600 *superparent = subst;
603 avl_rebalance(tree, balnode);
605 node->next = node->prev = node->parent = node->left = node->right = NULL;
615 void avl_delete_node(avl_tree_t *tree, avl_node_t *node) {
616 avl_unlink_node(tree, node);
617 avl_free_node(tree, node);
620 void avl_delete(avl_tree_t *tree, void *data) {
623 node = avl_search_node(tree, data);
626 avl_delete_node(tree, node);
629 /* Fast tree cleanup */
631 void avl_delete_tree(avl_tree_t *tree) {
632 avl_node_t *node, *next;
634 for(node = tree->head; node; node = next) {
636 avl_free_node(tree, node);
644 void avl_foreach(const avl_tree_t *tree, avl_action_t action) {
645 avl_node_t *node, *next;
647 for(node = tree->head; node; node = next) {
653 void avl_foreach_node(const avl_tree_t *tree, avl_action_t action) {
654 avl_node_t *node, *next;
656 for(node = tree->head; node; node = next) {
665 unsigned int avl_count(const avl_tree_t *tree) {
666 return AVL_NODE_COUNT(tree->root);
669 avl_node_t *avl_get_node(const avl_tree_t *tree, unsigned int index) {
676 c = AVL_L_COUNT(node);
680 } else if(index > c) {
691 unsigned int avl_index(const avl_node_t *node) {
695 index = AVL_L_COUNT(node);
697 while((next = node->parent)) {
698 if(node == next->right)
699 index += AVL_L_COUNT(next) + 1;
707 unsigned int avl_depth(const avl_tree_t *tree) {
708 return AVL_NODE_DEPTH(tree->root);