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.7 2001/02/27 16:50:29 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)
409 avl_node_t *closest, *new;
414 new = avl_alloc_node();
416 avl_insert_top(tree, new);
420 closest = avl_search_closest_node(tree, data, &result);
424 new = avl_alloc_node();
426 avl_insert_before(tree, closest, new);
429 new = avl_alloc_node();
431 avl_insert_after(tree, closest, new);
448 avl_node_t *avl_insert_node(avl_tree_t *tree, avl_node_t *node)
454 avl_insert_top(tree, node);
457 closest = avl_search_closest_node(tree, node->data, &result);
461 avl_insert_before(tree, closest, node);
464 avl_insert_after(tree, closest, node);
481 void avl_insert_top(avl_tree_t *tree, avl_node_t *node)
483 node->prev = node->next = node->parent = NULL;
484 tree->head = tree->tail = tree->root = node;
487 void avl_insert_before(avl_tree_t *tree, avl_node_t *before, avl_node_t *node)
490 return tree->tail ? avl_insert_after(tree, tree->tail, node) : avl_insert_top(tree, node);
493 node->parent = before;
494 node->prev = before->prev;
497 return avl_insert_after(tree, before->prev, node);
500 before->prev->next = node;
507 avl_rebalance(tree, before->parent);
510 void avl_insert_after(avl_tree_t *tree, avl_node_t *after, avl_node_t *node)
513 return tree->head ? avl_insert_before(tree, tree->head, node) : avl_insert_top(tree, node);
516 return avl_insert_before(tree, after->next, node);
519 node->parent = after;
520 node->next = after->next;
523 after->next->prev = node;
530 avl_rebalance(tree, after->parent);
533 avl_node_t *avl_unlink(avl_tree_t *tree, void *data)
537 node = avl_search_node(tree, data);
540 avl_unlink_node(tree, node);
545 void avl_unlink_node(avl_tree_t *tree, avl_node_t *node)
548 avl_node_t **superparent;
549 avl_node_t *subst, *left, *right;
553 node->prev->next = node->next;
555 tree->head = node->next;
557 node->next->prev = node->prev;
559 tree->tail = node->prev;
561 parent = node->parent;
563 superparent = parent ? node == parent->left ? &parent->left : &parent->right : &tree->root;
569 *superparent = right;
571 right->parent = parent;
576 left->parent = parent;
586 balnode = subst->parent;
587 balnode->right = subst->left;
589 balnode->right->parent = balnode;
591 left->parent = subst;
593 subst->right = right;
594 subst->parent = parent;
595 right->parent = subst;
596 *superparent = subst;
599 avl_rebalance(tree, balnode);
602 void avl_delete_node(avl_tree_t *tree, avl_node_t *node)
604 avl_unlink_node(tree, node);
605 avl_free_node(tree, node);
608 void avl_delete(avl_tree_t *tree, void *data)
612 node = avl_search_node(tree, data);
615 avl_delete_node(tree, node);
618 /* Fast tree cleanup */
620 void avl_delete_tree(avl_tree_t *tree)
622 avl_node_t *node, *next;
624 for(node = tree->root; node; node = next)
627 avl_free_node(tree, node);
635 void avl_foreach(avl_tree_t *tree, avl_action_t action)
637 avl_node_t *node, *next;
639 for(node = tree->head; node; node = next)
646 void avl_foreach_node(avl_tree_t *tree, avl_action_t action)
648 avl_node_t *node, *next;
650 for(node = tree->head; node; node = next)
660 unsigned int avl_count(avl_tree_t *tree)
662 return AVL_NODE_COUNT(tree->root);
665 avl_node_t *avl_get_node(const avl_tree_t *tree, unsigned int index)
674 c = AVL_L_COUNT(node);
679 } else if (index > c)
692 unsigned int avl_index(const avl_node_t *node)
697 index = AVL_L_COUNT(node);
699 while ((next = node->parent))
701 if (node == next->right)
702 index += AVL_L_COUNT(next) + 1;
710 unsigned int avl_depth(avl_tree_t *tree)
712 return AVL_NODE_DEPTH(tree->root);