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 along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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>.
39 #define AVL_NODE_COUNT(n) ((n) ? (n)->count : 0)
40 #define AVL_L_COUNT(n) (AVL_NODE_COUNT((n)->left))
41 #define AVL_R_COUNT(n) (AVL_NODE_COUNT((n)->right))
42 #define AVL_CALC_COUNT(n) (AVL_L_COUNT(n) + AVL_R_COUNT(n) + 1)
46 #define AVL_NODE_DEPTH(n) ((n) ? (n)->depth : 0)
47 #define L_AVL_DEPTH(n) (AVL_NODE_DEPTH((n)->left))
48 #define R_AVL_DEPTH(n) (AVL_NODE_DEPTH((n)->right))
49 #define AVL_CALC_DEPTH(n) ((L_AVL_DEPTH(n)>R_AVL_DEPTH(n)?L_AVL_DEPTH(n):R_AVL_DEPTH(n)) + 1)
53 static int lg(unsigned int u) __attribute__ ((__const__));
55 static int lg(unsigned int u) {
88 /* Internal helper functions */
90 static int avl_check_balance(const avl_node_t *node) {
94 d = R_AVL_DEPTH(node) - L_AVL_DEPTH(node);
96 return d < -1 ? -1 : d > 1 ? 1 : 0;
99 * d = lg(AVL_R_COUNT(node)) - lg(AVL_L_COUNT(node));
100 * d = d<-1?-1:d>1?1:0;
104 pl = lg(AVL_L_COUNT(node));
105 r = AVL_R_COUNT(node);
110 if(pl < 2 || r >> pl - 2)
117 static void avl_rebalance(avl_tree_t *tree, avl_node_t *node) {
121 avl_node_t **superparent;
126 parent = node->parent;
130 parent->left ? &parent->left : &parent->right : &tree->root;
132 switch (avl_check_balance(node)) {
136 if(L_AVL_DEPTH(child) >= R_AVL_DEPTH(child)) {
138 if(AVL_L_COUNT(child) >= AVL_R_COUNT(child)) {
140 node->left = child->right;
142 node->left->parent = node;
145 node->parent = child;
146 *superparent = child;
147 child->parent = parent;
149 node->count = AVL_CALC_COUNT(node);
150 child->count = AVL_CALC_COUNT(child);
153 node->depth = AVL_CALC_DEPTH(node);
154 child->depth = AVL_CALC_DEPTH(child);
157 gchild = child->right;
158 node->left = gchild->right;
161 node->left->parent = node;
162 child->right = gchild->left;
165 child->right->parent = child;
166 gchild->right = node;
169 gchild->right->parent = gchild;
170 gchild->left = child;
173 gchild->left->parent = gchild;
174 *superparent = gchild;
176 gchild->parent = parent;
178 node->count = AVL_CALC_COUNT(node);
179 child->count = AVL_CALC_COUNT(child);
180 gchild->count = AVL_CALC_COUNT(gchild);
183 node->depth = AVL_CALC_DEPTH(node);
184 child->depth = AVL_CALC_DEPTH(child);
185 gchild->depth = AVL_CALC_DEPTH(gchild);
193 if(R_AVL_DEPTH(child) >= L_AVL_DEPTH(child)) {
195 if(AVL_R_COUNT(child) >= AVL_L_COUNT(child)) {
197 node->right = child->left;
199 node->right->parent = node;
201 node->parent = child;
202 *superparent = child;
203 child->parent = parent;
205 node->count = AVL_CALC_COUNT(node);
206 child->count = AVL_CALC_COUNT(child);
209 node->depth = AVL_CALC_DEPTH(node);
210 child->depth = AVL_CALC_DEPTH(child);
213 gchild = child->left;
214 node->right = gchild->left;
217 node->right->parent = node;
218 child->left = gchild->right;
221 child->left->parent = child;
225 gchild->left->parent = gchild;
226 gchild->right = child;
229 gchild->right->parent = gchild;
231 *superparent = gchild;
232 gchild->parent = parent;
234 node->count = AVL_CALC_COUNT(node);
235 child->count = AVL_CALC_COUNT(child);
236 gchild->count = AVL_CALC_COUNT(gchild);
239 node->depth = AVL_CALC_DEPTH(node);
240 child->depth = AVL_CALC_DEPTH(child);
241 gchild->depth = AVL_CALC_DEPTH(gchild);
248 node->count = AVL_CALC_COUNT(node);
251 node->depth = AVL_CALC_DEPTH(node);
258 /* (De)constructors */
260 avl_tree_t *avl_alloc_tree(avl_compare_t compare, avl_action_t delete) {
263 tree = xmalloc_and_zero(sizeof(avl_tree_t));
264 tree->compare = compare;
265 tree->delete = delete;
270 void avl_free_tree(avl_tree_t *tree) {
274 avl_node_t *avl_alloc_node(void) {
275 return xmalloc_and_zero(sizeof(avl_node_t));
278 void avl_free_node(avl_tree_t *tree, avl_node_t *node) {
279 if(node->data && tree->delete)
280 tree->delete(node->data);
287 void *avl_search(const avl_tree_t *tree, const void *data) {
290 node = avl_search_node(tree, data);
292 return node ? node->data : NULL;
295 void *avl_search_closest(const avl_tree_t *tree, const void *data, int *result) {
298 node = avl_search_closest_node(tree, data, result);
300 return node ? node->data : NULL;
303 void *avl_search_closest_smaller(const avl_tree_t *tree, const void *data) {
306 node = avl_search_closest_smaller_node(tree, data);
308 return node ? node->data : NULL;
311 void *avl_search_closest_greater(const avl_tree_t *tree, const void *data) {
314 node = avl_search_closest_greater_node(tree, data);
316 return node ? node->data : NULL;
319 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,
342 c = tree->compare(data, node->data);
370 avl_node_t *avl_search_closest_smaller_node(const avl_tree_t *tree,
375 node = avl_search_closest_node(tree, data, &result);
383 avl_node_t *avl_search_closest_greater_node(const avl_tree_t *tree,
388 node = avl_search_closest_node(tree, data, &result);
396 /* Insertion and deletion */
398 avl_node_t *avl_insert(avl_tree_t *tree, void *data) {
399 avl_node_t *closest, *new;
403 new = avl_alloc_node();
405 avl_insert_top(tree, new);
407 closest = avl_search_closest_node(tree, data, &result);
411 new = avl_alloc_node();
413 avl_insert_before(tree, closest, new);
417 new = avl_alloc_node();
419 avl_insert_after(tree, closest, new);
437 avl_node_t *avl_insert_node(avl_tree_t *tree, avl_node_t *node) {
442 avl_insert_top(tree, node);
444 closest = avl_search_closest_node(tree, node->data, &result);
448 avl_insert_before(tree, closest, node);
452 avl_insert_after(tree, closest, node);
470 void avl_insert_top(avl_tree_t *tree, avl_node_t *node) {
471 node->prev = node->next = node->parent = NULL;
472 tree->head = tree->tail = tree->root = node;
475 void avl_insert_before(avl_tree_t *tree, avl_node_t *before,
479 avl_insert_after(tree, tree->tail, node);
481 avl_insert_top(tree, node);
486 node->parent = before;
487 node->prev = before->prev;
490 avl_insert_after(tree, before->prev, node);
495 before->prev->next = node;
502 avl_rebalance(tree, before);
505 void avl_insert_after(avl_tree_t *tree, avl_node_t *after, avl_node_t *node) {
508 avl_insert_before(tree, tree->head, node);
510 avl_insert_top(tree, node);
515 avl_insert_before(tree, after->next, node);
520 node->parent = after;
521 node->next = after->next;
524 after->next->prev = node;
531 avl_rebalance(tree, after);
534 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) {
547 avl_node_t **superparent;
548 avl_node_t *subst, *left, *right;
552 node->prev->next = node->next;
554 tree->head = node->next;
556 node->next->prev = node->prev;
558 tree->tail = node->prev;
560 parent = node->parent;
564 parent->left ? &parent->left : &parent->right : &tree->root;
569 *superparent = right;
572 right->parent = parent;
577 left->parent = parent;
585 balnode = subst->parent;
586 balnode->right = subst->left;
589 balnode->right->parent = balnode;
592 left->parent = subst;
595 subst->right = right;
596 subst->parent = parent;
597 right->parent = subst;
598 *superparent = subst;
601 avl_rebalance(tree, balnode);
603 node->next = node->prev = node->parent = node->left = node->right = NULL;
613 void avl_delete_node(avl_tree_t *tree, avl_node_t *node) {
614 avl_unlink_node(tree, node);
615 avl_free_node(tree, node);
618 void avl_delete(avl_tree_t *tree, void *data) {
621 node = avl_search_node(tree, data);
624 avl_delete_node(tree, node);
627 /* Fast tree cleanup */
629 void avl_delete_tree(avl_tree_t *tree) {
630 avl_node_t *node, *next;
632 for(node = tree->head; node; node = next) {
634 avl_free_node(tree, node);
642 void avl_foreach(const avl_tree_t *tree, avl_action_t action) {
643 avl_node_t *node, *next;
645 for(node = tree->head; node; node = next) {
651 void avl_foreach_node(const avl_tree_t *tree, avl_action_t action) {
652 avl_node_t *node, *next;
654 for(node = tree->head; node; node = next) {
663 unsigned int avl_count(const avl_tree_t *tree) {
664 return AVL_NODE_COUNT(tree->root);
667 avl_node_t *avl_get_node(const avl_tree_t *tree, unsigned int index) {
674 c = AVL_L_COUNT(node);
678 } else if(index > c) {
689 unsigned int avl_index(const avl_node_t *node) {
693 index = AVL_L_COUNT(node);
695 while((next = node->parent)) {
696 if(node == next->right)
697 index += AVL_L_COUNT(next) + 1;
705 unsigned int avl_depth(const avl_tree_t *tree) {
706 return AVL_NODE_DEPTH(tree->root);