2 avl_tree.c -- avl_ tree and linked list convenience
3 Copyright (C) 1998 Michael H. Buselli
4 2000,2001 Ivo Timmermans <itimmermans@bigfoot.com>,
5 2000,2001 Guus Sliepen <guus@sliepen.warande.net>
6 2000,2001 Wessel Dankers <wsl@nl.linux.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@nl.linux.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://tinc.nl.linux.org) by
30 Guus Sliepen <guus@sliepen.warande.net>.
32 $Id: avl_tree.c,v 1.1.2.4 2001/01/08 21:32:00 guus Exp $
42 #define AVL_NODE_COUNT(n) ((n) ? (n)->count : 0)
43 #define AVL_L_COUNT(n) (AVL_NODE_COUNT((n)->left))
44 #define AVL_R_COUNT(n) (AVL_NODE_COUNT((n)->right))
45 #define AVL_CALC_COUNT(n) (AVL_L_COUNT(n) + AVL_R_COUNT(n) + 1)
49 #define AVL_NODE_DEPTH(n) ((n) ? (n)->depth : 0)
50 #define L_AVL_DEPTH(n) (AVL_NODE_DEPTH((n)->left))
51 #define R_AVL_DEPTH(n) (AVL_NODE_DEPTH((n)->right))
52 #define AVL_CALC_DEPTH(n) ((L_AVL_DEPTH(n)>R_AVL_DEPTH(n)?L_AVL_DEPTH(n):R_AVL_DEPTH(n)) + 1)
56 int lg(unsigned int u)
87 /* Internal helper functions */
89 int avl_check_balance(avl_node_t *node)
93 d = R_AVL_DEPTH(node) - L_AVL_DEPTH(node);
94 return d < -1 ? -1 : d > 1 ? 1 : 0;
97 * d = lg(AVL_R_COUNT(node)) - lg(AVL_L_COUNT(node));
98 * d = d<-1?-1:d>1?1:0;
102 pl = lg(AVL_L_COUNT(node));
103 r = AVL_R_COUNT(node);
107 if (pl < 2 || r >> pl - 2)
113 void avl_rebalance(avl_tree_t *tree, avl_node_t *node)
118 avl_node_t **superparent;
124 parent = node->parent;
126 superparent = parent ? node == parent->left ? &parent->left : &parent->right : &tree->root;
128 switch (avl_check_balance(node))
133 if(L_AVL_DEPTH(child) >= R_AVL_DEPTH(child)) {
135 if (AVL_L_COUNT(child) >= AVL_R_COUNT(child))
138 node->left = child->right;
140 node->left->parent = node;
142 node->parent = child;
143 *superparent = child;
144 child->parent = parent;
146 node->count = AVL_CALC_COUNT(node);
147 child->count = AVL_CALC_COUNT(child);
150 node->depth = AVL_CALC_DEPTH(node);
151 child->depth = AVL_CALC_DEPTH(child);
155 gchild = child->right;
156 node->left = gchild->right;
158 node->left->parent = node;
159 child->right = gchild->left;
161 child->right->parent = child;
162 gchild->right = node;
164 gchild->right->parent = gchild;
165 gchild->left = child;
167 gchild->left->parent = gchild;
168 *superparent = gchild;
169 gchild->parent = parent;
171 node->count = AVL_CALC_COUNT(node);
172 child->count = AVL_CALC_COUNT(child);
173 gchild->count = AVL_CALC_COUNT(gchild);
176 node->depth = AVL_CALC_DEPTH(node);
177 child->depth = AVL_CALC_DEPTH(child);
178 gchild->depth = AVL_CALC_DEPTH(gchild);
185 if(R_AVL_DEPTH(child) >= L_AVL_DEPTH(child)) {
187 if (AVL_R_COUNT(child) >= AVL_L_COUNT(child))
190 node->right = child->left;
192 node->right->parent = node;
194 node->parent = child;
195 *superparent = child;
196 child->parent = parent;
198 node->count = AVL_CALC_COUNT(node);
199 child->count = AVL_CALC_COUNT(child);
202 node->depth = AVL_CALC_DEPTH(node);
203 child->depth = AVL_CALC_DEPTH(child);
207 gchild = child->left;
208 node->right = gchild->left;
210 node->right->parent = node;
211 child->left = gchild->right;
213 child->left->parent = child;
216 gchild->left->parent = gchild;
217 gchild->right = child;
219 gchild->right->parent = gchild;
220 *superparent = gchild;
221 gchild->parent = parent;
223 node->count = AVL_CALC_COUNT(node);
224 child->count = AVL_CALC_COUNT(child);
225 gchild->count = AVL_CALC_COUNT(gchild);
228 node->depth = AVL_CALC_DEPTH(node);
229 child->depth = AVL_CALC_DEPTH(child);
230 gchild->depth = AVL_CALC_DEPTH(gchild);
236 node->count = AVL_CALC_COUNT(node);
239 node->depth = AVL_CALC_DEPTH(node);
246 /* (De)constructors */
248 avl_tree_t *avl_alloc_tree(avl_compare_t compare, avl_action_t delete)
252 tree = xmalloc_and_zero(sizeof(avl_tree_t));
253 tree->compare = compare;
254 tree->delete = delete;
259 void avl_free_tree(avl_tree_t *tree)
264 avl_node_t *avl_alloc_node(void)
268 node = xmalloc_and_zero(sizeof(avl_node_t));
273 void avl_free_node(avl_tree_t *tree, avl_node_t *node)
275 if(node->data && tree->delete)
276 tree->delete(node->data);
282 void *avl_search(const avl_tree_t *tree, const void *data)
286 node = avl_search_node(tree, data);
288 return node?node->data:NULL;
291 void *avl_search_closest(const avl_tree_t *tree, const void *data, int *result)
295 node = avl_search_closest_node(tree, data, result);
297 return node?node->data:NULL;
300 void *avl_search_closest_smaller(const avl_tree_t *tree, const void *data)
304 node = avl_search_closest_smaller_node(tree, data);
306 return node?node->data:NULL;
309 void *avl_search_closest_greater(const avl_tree_t *tree, const void *data)
313 node = avl_search_closest_greater_node(tree, data);
315 return node?node->data:NULL;
318 avl_node_t *avl_search_node(const avl_tree_t *tree, const void *data)
323 node = avl_search_closest_node(tree, data, &result);
325 return result?NULL:node;
328 avl_node_t *avl_search_closest_node(const avl_tree_t *tree, const void *data, int *result)
344 c = tree->compare(data, node->data);
379 avl_node_t *avl_search_closest_smaller_node(const avl_tree_t *tree, const void *data)
384 node = avl_search_closest_node(tree, data, &result);
392 avl_node_t *avl_search_closest_greater_node(const avl_tree_t *tree, const void *data)
397 node = avl_search_closest_node(tree, data, &result);
405 /* Insertion and deletion */
407 avl_node_t *avl_insert(avl_tree_t *tree, void *data)
411 node = avl_alloc_node();
414 return avl_insert_node(tree, node);
417 avl_node_t *avl_insert_node(avl_tree_t *tree, avl_node_t *node)
423 avl_insert_top(tree, node);
426 closest = avl_search_closest_node(tree, node->data, &result);
430 avl_insert_before(tree, closest, node);
433 avl_insert_after(tree, closest, node);
450 void avl_insert_top(avl_tree_t *tree, avl_node_t *node)
452 node->prev = node->next = node->parent = NULL;
453 tree->head = tree->tail = tree->root = node;
456 void avl_insert_before(avl_tree_t *tree, avl_node_t *before, avl_node_t *node)
459 return tree->tail ? avl_insert_after(tree, tree->tail, node) : avl_insert_top(tree, node);
462 node->parent = before;
463 node->prev = before->prev;
466 before->prev->next = node;
473 avl_rebalance(tree, before->parent);
476 void avl_insert_after(avl_tree_t *tree, avl_node_t *after, avl_node_t *node)
479 return tree->head ? avl_insert_before(tree, tree->head, node) : avl_insert_top(tree, node);
482 node->parent = after;
483 node->next = after->next;
486 after->next->prev = node;
493 avl_rebalance(tree, after->parent);
496 avl_node_t *avl_unlink(avl_tree_t *tree, void *data)
500 node = avl_search_node(tree, data);
503 avl_unlink_node(tree, node);
508 void avl_unlink_node(avl_tree_t *tree, avl_node_t *node)
511 avl_node_t **superparent;
512 avl_node_t *subst, *left, *right;
516 node->prev->next = node->next;
518 tree->head = node->next;
520 node->next->prev = node->prev;
522 tree->tail = node->prev;
524 parent = node->parent;
526 superparent = parent ? node == parent->left ? &parent->left : &parent->right : &tree->root;
532 *superparent = right;
534 right->parent = parent;
539 left->parent = parent;
549 balnode = subst->parent;
550 balnode->right = subst->left;
552 balnode->right->parent = balnode;
554 left->parent = subst;
556 subst->right = right;
557 subst->parent = parent;
558 right->parent = subst;
559 *superparent = subst;
562 avl_rebalance(tree, balnode);
565 void avl_delete_node(avl_tree_t *tree, avl_node_t *node)
567 avl_unlink_node(tree, node);
568 avl_free_node(tree, node);
571 void avl_delete(avl_tree_t *tree, void *data)
575 node = avl_search_node(tree, data);
578 avl_delete_node(tree, node);
581 /* Fast tree cleanup */
583 void avl_delete_tree(avl_tree_t *tree)
585 avl_node_t *node, *next;
587 for(node = tree->root; node; node = next)
590 avl_free_node(tree, node);
598 void avl_foreach(avl_tree_t *tree, avl_action_t action)
600 avl_node_t *node, *next;
602 for(node = tree->head; node; node = next)
609 void avl_foreach_node(avl_tree_t *tree, avl_action_t action)
611 avl_node_t *node, *next;
613 for(node = tree->head; node; node = next)
623 unsigned int avl_count(avl_tree_t *tree)
625 return AVL_NODE_COUNT(tree->root);
628 avl_node_t *avl_get_node(const avl_tree_t *tree, unsigned int index)
637 c = AVL_L_COUNT(node);
642 } else if (index > c)
655 unsigned int avl_index(const avl_node_t *node)
660 index = AVL_L_COUNT(node);
662 while ((next = node->parent))
664 if (node == next->right)
665 index += AVL_L_COUNT(next) + 1;
673 unsigned int avl_depth(avl_tree_t *tree)
675 return AVL_NODE_DEPTH(tree->root);