|
@@ -29,7 +29,7 @@
|
|
|
library for inclusion into tinc (http://tinc.nl.linux.org/) by
|
|
|
Guus Sliepen <guus@sliepen.eu.org>.
|
|
|
|
|
|
- $Id: avl_tree.c,v 1.1.2.10 2002/09/09 21:49:16 guus Exp $
|
|
|
+ $Id: avl_tree.c,v 1.1.2.11 2002/09/09 22:32:24 guus Exp $
|
|
|
*/
|
|
|
|
|
|
#include <stdio.h>
|
|
@@ -89,7 +89,7 @@ int lg(unsigned int u)
|
|
|
|
|
|
/* Internal helper functions */
|
|
|
|
|
|
-int avl_check_balance(avl_node_t * node)
|
|
|
+int avl_check_balance(avl_node_t *node)
|
|
|
{
|
|
|
#ifdef AVL_DEPTH
|
|
|
int d;
|
|
@@ -117,7 +117,7 @@ int avl_check_balance(avl_node_t * node)
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
-void avl_rebalance(avl_tree_t * tree, avl_node_t * node)
|
|
|
+void avl_rebalance(avl_tree_t *tree, avl_node_t *node)
|
|
|
{
|
|
|
avl_node_t *child;
|
|
|
avl_node_t *gchild;
|
|
@@ -272,7 +272,7 @@ avl_tree_t *avl_alloc_tree(avl_compare_t compare, avl_action_t delete)
|
|
|
return tree;
|
|
|
}
|
|
|
|
|
|
-void avl_free_tree(avl_tree_t * tree)
|
|
|
+void avl_free_tree(avl_tree_t *tree)
|
|
|
{
|
|
|
free(tree);
|
|
|
}
|
|
@@ -282,7 +282,7 @@ avl_node_t *avl_alloc_node(void)
|
|
|
return (avl_node_t *)xmalloc_and_zero(sizeof(avl_node_t));
|
|
|
}
|
|
|
|
|
|
-void avl_free_node(avl_tree_t * tree, avl_node_t * node)
|
|
|
+void avl_free_node(avl_tree_t *tree, avl_node_t *node)
|
|
|
{
|
|
|
if(node->data && tree->delete)
|
|
|
tree->delete(node->data);
|
|
@@ -292,7 +292,7 @@ void avl_free_node(avl_tree_t * tree, avl_node_t * node)
|
|
|
|
|
|
/* Searching */
|
|
|
|
|
|
-void *avl_search(const avl_tree_t * tree, const void *data)
|
|
|
+void *avl_search(const avl_tree_t *tree, const void *data)
|
|
|
{
|
|
|
avl_node_t *node;
|
|
|
|
|
@@ -301,7 +301,7 @@ void *avl_search(const avl_tree_t * tree, const void *data)
|
|
|
return node ? node->data : NULL;
|
|
|
}
|
|
|
|
|
|
-void *avl_search_closest(const avl_tree_t * tree, const void *data, int *result)
|
|
|
+void *avl_search_closest(const avl_tree_t *tree, const void *data, int *result)
|
|
|
{
|
|
|
avl_node_t *node;
|
|
|
|
|
@@ -310,7 +310,7 @@ void *avl_search_closest(const avl_tree_t * tree, const void *data, int *result)
|
|
|
return node ? node->data : NULL;
|
|
|
}
|
|
|
|
|
|
-void *avl_search_closest_smaller(const avl_tree_t * tree, const void *data)
|
|
|
+void *avl_search_closest_smaller(const avl_tree_t *tree, const void *data)
|
|
|
{
|
|
|
avl_node_t *node;
|
|
|
|
|
@@ -319,7 +319,7 @@ void *avl_search_closest_smaller(const avl_tree_t * tree, const void *data)
|
|
|
return node ? node->data : NULL;
|
|
|
}
|
|
|
|
|
|
-void *avl_search_closest_greater(const avl_tree_t * tree, const void *data)
|
|
|
+void *avl_search_closest_greater(const avl_tree_t *tree, const void *data)
|
|
|
{
|
|
|
avl_node_t *node;
|
|
|
|
|
@@ -328,7 +328,7 @@ void *avl_search_closest_greater(const avl_tree_t * tree, const void *data)
|
|
|
return node ? node->data : NULL;
|
|
|
}
|
|
|
|
|
|
-avl_node_t *avl_search_node(const avl_tree_t * tree, const void *data)
|
|
|
+avl_node_t *avl_search_node(const avl_tree_t *tree, const void *data)
|
|
|
{
|
|
|
avl_node_t *node;
|
|
|
int result;
|
|
@@ -338,7 +338,7 @@ avl_node_t *avl_search_node(const avl_tree_t * tree, const void *data)
|
|
|
return result ? NULL : node;
|
|
|
}
|
|
|
|
|
|
-avl_node_t *avl_search_closest_node(const avl_tree_t * tree, const void *data,
|
|
|
+avl_node_t *avl_search_closest_node(const avl_tree_t *tree, const void *data,
|
|
|
int *result)
|
|
|
{
|
|
|
avl_node_t *node;
|
|
@@ -381,7 +381,7 @@ avl_node_t *avl_search_closest_node(const avl_tree_t * tree, const void *data,
|
|
|
return node;
|
|
|
}
|
|
|
|
|
|
-avl_node_t *avl_search_closest_smaller_node(const avl_tree_t * tree,
|
|
|
+avl_node_t *avl_search_closest_smaller_node(const avl_tree_t *tree,
|
|
|
const void *data)
|
|
|
{
|
|
|
avl_node_t *node;
|
|
@@ -395,7 +395,7 @@ avl_node_t *avl_search_closest_smaller_node(const avl_tree_t * tree,
|
|
|
return node;
|
|
|
}
|
|
|
|
|
|
-avl_node_t *avl_search_closest_greater_node(const avl_tree_t * tree,
|
|
|
+avl_node_t *avl_search_closest_greater_node(const avl_tree_t *tree,
|
|
|
const void *data)
|
|
|
{
|
|
|
avl_node_t *node;
|
|
@@ -411,7 +411,7 @@ avl_node_t *avl_search_closest_greater_node(const avl_tree_t * tree,
|
|
|
|
|
|
/* Insertion and deletion */
|
|
|
|
|
|
-avl_node_t *avl_insert(avl_tree_t * tree, void *data)
|
|
|
+avl_node_t *avl_insert(avl_tree_t *tree, void *data)
|
|
|
{
|
|
|
avl_node_t *closest, *new;
|
|
|
int result;
|
|
@@ -451,7 +451,7 @@ avl_node_t *avl_insert(avl_tree_t * tree, void *data)
|
|
|
return new;
|
|
|
}
|
|
|
|
|
|
-avl_node_t *avl_insert_node(avl_tree_t * tree, avl_node_t * node)
|
|
|
+avl_node_t *avl_insert_node(avl_tree_t *tree, avl_node_t *node)
|
|
|
{
|
|
|
avl_node_t *closest;
|
|
|
int result;
|
|
@@ -485,14 +485,14 @@ avl_node_t *avl_insert_node(avl_tree_t * tree, avl_node_t * node)
|
|
|
return node;
|
|
|
}
|
|
|
|
|
|
-void avl_insert_top(avl_tree_t * tree, avl_node_t * node)
|
|
|
+void avl_insert_top(avl_tree_t *tree, avl_node_t *node)
|
|
|
{
|
|
|
node->prev = node->next = node->parent = NULL;
|
|
|
tree->head = tree->tail = tree->root = node;
|
|
|
}
|
|
|
|
|
|
-void avl_insert_before(avl_tree_t * tree, avl_node_t * before,
|
|
|
- avl_node_t * node)
|
|
|
+void avl_insert_before(avl_tree_t *tree, avl_node_t *before,
|
|
|
+ avl_node_t *node)
|
|
|
{
|
|
|
if(!before)
|
|
|
return tree->tail ? avl_insert_after(tree, tree->tail, node) : avl_insert_top(tree, node);
|
|
@@ -515,7 +515,7 @@ void avl_insert_before(avl_tree_t * tree, avl_node_t * before,
|
|
|
avl_rebalance(tree, before->parent);
|
|
|
}
|
|
|
|
|
|
-void avl_insert_after(avl_tree_t * tree, avl_node_t * after, avl_node_t * node)
|
|
|
+void avl_insert_after(avl_tree_t *tree, avl_node_t *after, avl_node_t *node)
|
|
|
{
|
|
|
if(!after)
|
|
|
return tree->head ? avl_insert_before(tree, tree->head,
|
|
@@ -540,7 +540,7 @@ void avl_insert_after(avl_tree_t * tree, avl_node_t * after, avl_node_t * node)
|
|
|
avl_rebalance(tree, after->parent);
|
|
|
}
|
|
|
|
|
|
-avl_node_t *avl_unlink(avl_tree_t * tree, void *data)
|
|
|
+avl_node_t *avl_unlink(avl_tree_t *tree, void *data)
|
|
|
{
|
|
|
avl_node_t *node;
|
|
|
|
|
@@ -552,7 +552,7 @@ avl_node_t *avl_unlink(avl_tree_t * tree, void *data)
|
|
|
return node;
|
|
|
}
|
|
|
|
|
|
-void avl_unlink_node(avl_tree_t * tree, avl_node_t * node)
|
|
|
+void avl_unlink_node(avl_tree_t *tree, avl_node_t *node)
|
|
|
{
|
|
|
avl_node_t *parent;
|
|
|
avl_node_t **superparent;
|
|
@@ -621,13 +621,13 @@ void avl_unlink_node(avl_tree_t * tree, avl_node_t * node)
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
-void avl_delete_node(avl_tree_t * tree, avl_node_t * node)
|
|
|
+void avl_delete_node(avl_tree_t *tree, avl_node_t *node)
|
|
|
{
|
|
|
avl_unlink_node(tree, node);
|
|
|
avl_free_node(tree, node);
|
|
|
}
|
|
|
|
|
|
-void avl_delete(avl_tree_t * tree, void *data)
|
|
|
+void avl_delete(avl_tree_t *tree, void *data)
|
|
|
{
|
|
|
avl_node_t *node;
|
|
|
|
|
@@ -639,7 +639,7 @@ void avl_delete(avl_tree_t * tree, void *data)
|
|
|
|
|
|
/* Fast tree cleanup */
|
|
|
|
|
|
-void avl_delete_tree(avl_tree_t * tree)
|
|
|
+void avl_delete_tree(avl_tree_t *tree)
|
|
|
{
|
|
|
avl_node_t *node, *next;
|
|
|
|
|
@@ -653,7 +653,7 @@ void avl_delete_tree(avl_tree_t * tree)
|
|
|
|
|
|
/* Tree walking */
|
|
|
|
|
|
-void avl_foreach(avl_tree_t * tree, avl_action_t action)
|
|
|
+void avl_foreach(avl_tree_t *tree, avl_action_t action)
|
|
|
{
|
|
|
avl_node_t *node, *next;
|
|
|
|
|
@@ -663,7 +663,7 @@ void avl_foreach(avl_tree_t * tree, avl_action_t action)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void avl_foreach_node(avl_tree_t * tree, avl_action_t action)
|
|
|
+void avl_foreach_node(avl_tree_t *tree, avl_action_t action)
|
|
|
{
|
|
|
avl_node_t *node, *next;
|
|
|
|
|
@@ -676,12 +676,12 @@ void avl_foreach_node(avl_tree_t * tree, avl_action_t action)
|
|
|
/* Indexing */
|
|
|
|
|
|
#ifdef AVL_COUNT
|
|
|
-unsigned int avl_count(avl_tree_t * tree)
|
|
|
+unsigned int avl_count(avl_tree_t *tree)
|
|
|
{
|
|
|
return AVL_NODE_COUNT(tree->root);
|
|
|
}
|
|
|
|
|
|
-avl_node_t *avl_get_node(const avl_tree_t * tree, unsigned int index)
|
|
|
+avl_node_t *avl_get_node(const avl_tree_t *tree, unsigned int index)
|
|
|
{
|
|
|
avl_node_t *node;
|
|
|
unsigned int c;
|
|
@@ -704,7 +704,7 @@ avl_node_t *avl_get_node(const avl_tree_t * tree, unsigned int index)
|
|
|
return NULL;
|
|
|
}
|
|
|
|
|
|
-unsigned int avl_index(const avl_node_t * node)
|
|
|
+unsigned int avl_index(const avl_node_t *node)
|
|
|
{
|
|
|
avl_node_t *next;
|
|
|
unsigned int index;
|
|
@@ -721,7 +721,7 @@ unsigned int avl_index(const avl_node_t * node)
|
|
|
}
|
|
|
#endif
|
|
|
#ifdef AVL_DEPTH
|
|
|
-unsigned int avl_depth(avl_tree_t * tree)
|
|
|
+unsigned int avl_depth(avl_tree_t *tree)
|
|
|
{
|
|
|
return AVL_NODE_DEPTH(tree->root);
|
|
|
}
|