tinc/lib/avl_tree.c

742 lines
14 KiB
C
Raw Normal View History

2019-08-26 11:44:36 +00:00
/*
avl_tree.c -- avl_ tree and linked list convenience
Copyright (C) 1998 Michael H. Buselli
2019-08-26 11:44:37 +00:00
2000-2005 Ivo Timmermans,
2000-2006 Guus Sliepen <guus@tinc-vpn.org>
2019-08-26 11:44:36 +00:00
2000-2005 Wessel Dankers <wsl@tinc-vpn.org>
2019-08-26 11:44:36 +00:00
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Original AVL tree library by Michael H. Buselli <cosine@cosine.org>.
2019-08-26 11:44:36 +00:00
Modified 2000-11-28 by Wessel Dankers <wsl@tinc-vpn.org> to use counts
2019-08-26 11:44:36 +00:00
instead of depths, to add the ->next and ->prev and to generally obfuscate
the code. Mail me if you found a bug.
Cleaned up and incorporated some of the ideas from the red-black tree
2019-08-26 11:44:36 +00:00
library for inclusion into tinc (http://www.tinc-vpn.org/) by
Guus Sliepen <guus@tinc-vpn.org>.
2019-08-26 11:44:36 +00:00
2019-08-26 11:44:37 +00:00
$Id: avl_tree.c 1470 2006-11-11 22:45:45Z guus $
2019-08-26 11:44:36 +00:00
*/
2019-08-26 11:44:36 +00:00
#include "system.h"
2019-08-26 11:44:36 +00:00
#include "avl_tree.h"
2019-08-26 11:44:36 +00:00
#include "xalloc.h"
2019-08-26 11:44:36 +00:00
#ifdef AVL_COUNT
#define AVL_NODE_COUNT(n) ((n) ? (n)->count : 0)
#define AVL_L_COUNT(n) (AVL_NODE_COUNT((n)->left))
#define AVL_R_COUNT(n) (AVL_NODE_COUNT((n)->right))
#define AVL_CALC_COUNT(n) (AVL_L_COUNT(n) + AVL_R_COUNT(n) + 1)
#endif
#ifdef AVL_DEPTH
#define AVL_NODE_DEPTH(n) ((n) ? (n)->depth : 0)
#define L_AVL_DEPTH(n) (AVL_NODE_DEPTH((n)->left))
#define R_AVL_DEPTH(n) (AVL_NODE_DEPTH((n)->right))
#define AVL_CALC_DEPTH(n) ((L_AVL_DEPTH(n)>R_AVL_DEPTH(n)?L_AVL_DEPTH(n):R_AVL_DEPTH(n)) + 1)
#endif
#ifndef AVL_DEPTH
2019-08-26 11:44:36 +00:00
static int lg(unsigned int u) __attribute__ ((__const__));
static int lg(unsigned int u)
2019-08-26 11:44:36 +00:00
{
2019-08-26 11:44:36 +00:00
int r = 1;
if(!u)
return 0;
if(u & 0xffff0000) {
u >>= 16;
r += 16;
}
if(u & 0x0000ff00) {
u >>= 8;
r += 8;
}
if(u & 0x000000f0) {
u >>= 4;
r += 4;
}
if(u & 0x0000000c) {
u >>= 2;
r += 2;
}
if(u & 0x00000002)
r++;
return r;
2019-08-26 11:44:36 +00:00
}
#endif
/* Internal helper functions */
2019-08-26 11:44:36 +00:00
static int avl_check_balance(const avl_node_t *node)
2019-08-26 11:44:36 +00:00
{
#ifdef AVL_DEPTH
2019-08-26 11:44:36 +00:00
int d;
d = R_AVL_DEPTH(node) - L_AVL_DEPTH(node);
return d < -1 ? -1 : d > 1 ? 1 : 0;
2019-08-26 11:44:36 +00:00
#else
/* int d;
* d = lg(AVL_R_COUNT(node)) - lg(AVL_L_COUNT(node));
* d = d<-1?-1:d>1?1:0;
*/
2019-08-26 11:44:36 +00:00
int pl, r;
pl = lg(AVL_L_COUNT(node));
r = AVL_R_COUNT(node);
if(r >> pl + 1)
return 1;
2019-08-26 11:44:36 +00:00
2019-08-26 11:44:36 +00:00
if(pl < 2 || r >> pl - 2)
return 0;
2019-08-26 11:44:36 +00:00
2019-08-26 11:44:36 +00:00
return -1;
2019-08-26 11:44:36 +00:00
#endif
}
2019-08-26 11:44:36 +00:00
static void avl_rebalance(avl_tree_t *tree, avl_node_t *node)
2019-08-26 11:44:36 +00:00
{
2019-08-26 11:44:36 +00:00
avl_node_t *child;
avl_node_t *gchild;
avl_node_t *parent;
avl_node_t **superparent;
2019-08-26 11:44:36 +00:00
2019-08-26 11:44:36 +00:00
parent = node;
2019-08-26 11:44:36 +00:00
2019-08-26 11:44:36 +00:00
while(node) {
parent = node->parent;
2019-08-26 11:44:36 +00:00
2019-08-26 11:44:36 +00:00
superparent =
parent ? node ==
parent->left ? &parent->left : &parent->right : &tree->root;
2019-08-26 11:44:36 +00:00
2019-08-26 11:44:36 +00:00
switch (avl_check_balance(node)) {
case -1:
child = node->left;
2019-08-26 11:44:36 +00:00
#ifdef AVL_DEPTH
2019-08-26 11:44:36 +00:00
if(L_AVL_DEPTH(child) >= R_AVL_DEPTH(child)) {
2019-08-26 11:44:36 +00:00
#else
2019-08-26 11:44:36 +00:00
if(AVL_L_COUNT(child) >= AVL_R_COUNT(child)) {
2019-08-26 11:44:36 +00:00
#endif
2019-08-26 11:44:36 +00:00
node->left = child->right;
if(node->left)
node->left->parent = node;
child->right = node;
node->parent = child;
*superparent = child;
child->parent = parent;
2019-08-26 11:44:36 +00:00
#ifdef AVL_COUNT
2019-08-26 11:44:36 +00:00
node->count = AVL_CALC_COUNT(node);
child->count = AVL_CALC_COUNT(child);
2019-08-26 11:44:36 +00:00
#endif
#ifdef AVL_DEPTH
2019-08-26 11:44:36 +00:00
node->depth = AVL_CALC_DEPTH(node);
child->depth = AVL_CALC_DEPTH(child);
2019-08-26 11:44:36 +00:00
#endif
2019-08-26 11:44:36 +00:00
} else {
gchild = child->right;
node->left = gchild->right;
if(node->left)
node->left->parent = node;
child->right = gchild->left;
if(child->right)
child->right->parent = child;
gchild->right = node;
if(gchild->right)
gchild->right->parent = gchild;
gchild->left = child;
if(gchild->left)
gchild->left->parent = gchild;
*superparent = gchild;
gchild->parent = parent;
2019-08-26 11:44:36 +00:00
#ifdef AVL_COUNT
2019-08-26 11:44:36 +00:00
node->count = AVL_CALC_COUNT(node);
child->count = AVL_CALC_COUNT(child);
gchild->count = AVL_CALC_COUNT(gchild);
2019-08-26 11:44:36 +00:00
#endif
#ifdef AVL_DEPTH
2019-08-26 11:44:36 +00:00
node->depth = AVL_CALC_DEPTH(node);
child->depth = AVL_CALC_DEPTH(child);
gchild->depth = AVL_CALC_DEPTH(gchild);
2019-08-26 11:44:36 +00:00
#endif
2019-08-26 11:44:36 +00:00
}
break;
case 1:
child = node->right;
2019-08-26 11:44:36 +00:00
#ifdef AVL_DEPTH
2019-08-26 11:44:36 +00:00
if(R_AVL_DEPTH(child) >= L_AVL_DEPTH(child)) {
2019-08-26 11:44:36 +00:00
#else
2019-08-26 11:44:36 +00:00
if(AVL_R_COUNT(child) >= AVL_L_COUNT(child)) {
2019-08-26 11:44:36 +00:00
#endif
2019-08-26 11:44:36 +00:00
node->right = child->left;
if(node->right)
node->right->parent = node;
child->left = node;
node->parent = child;
*superparent = child;
child->parent = parent;
2019-08-26 11:44:36 +00:00
#ifdef AVL_COUNT
2019-08-26 11:44:36 +00:00
node->count = AVL_CALC_COUNT(node);
child->count = AVL_CALC_COUNT(child);
2019-08-26 11:44:36 +00:00
#endif
#ifdef AVL_DEPTH
2019-08-26 11:44:36 +00:00
node->depth = AVL_CALC_DEPTH(node);
child->depth = AVL_CALC_DEPTH(child);
2019-08-26 11:44:36 +00:00
#endif
2019-08-26 11:44:36 +00:00
} else {
gchild = child->left;
node->right = gchild->left;
if(node->right)
node->right->parent = node;
child->left = gchild->right;
if(child->left)
child->left->parent = child;
gchild->left = node;
if(gchild->left)
gchild->left->parent = gchild;
gchild->right = child;
if(gchild->right)
gchild->right->parent = gchild;
*superparent = gchild;
gchild->parent = parent;
2019-08-26 11:44:36 +00:00
#ifdef AVL_COUNT
2019-08-26 11:44:36 +00:00
node->count = AVL_CALC_COUNT(node);
child->count = AVL_CALC_COUNT(child);
gchild->count = AVL_CALC_COUNT(gchild);
2019-08-26 11:44:36 +00:00
#endif
#ifdef AVL_DEPTH
2019-08-26 11:44:36 +00:00
node->depth = AVL_CALC_DEPTH(node);
child->depth = AVL_CALC_DEPTH(child);
gchild->depth = AVL_CALC_DEPTH(gchild);
2019-08-26 11:44:36 +00:00
#endif
2019-08-26 11:44:36 +00:00
}
break;
default:
2019-08-26 11:44:36 +00:00
#ifdef AVL_COUNT
2019-08-26 11:44:36 +00:00
node->count = AVL_CALC_COUNT(node);
2019-08-26 11:44:36 +00:00
#endif
#ifdef AVL_DEPTH
2019-08-26 11:44:36 +00:00
node->depth = AVL_CALC_DEPTH(node);
2019-08-26 11:44:36 +00:00
#endif
2019-08-26 11:44:36 +00:00
}
node = parent;
}
2019-08-26 11:44:36 +00:00
}
/* (De)constructors */
avl_tree_t *avl_alloc_tree(avl_compare_t compare, avl_action_t delete)
{
2019-08-26 11:44:36 +00:00
avl_tree_t *tree;
2019-08-26 11:44:36 +00:00
2019-08-26 11:44:36 +00:00
tree = xmalloc_and_zero(sizeof(avl_tree_t));
tree->compare = compare;
tree->delete = delete;
return tree;
2019-08-26 11:44:36 +00:00
}
void avl_free_tree(avl_tree_t *tree)
{
2019-08-26 11:44:36 +00:00
free(tree);
2019-08-26 11:44:36 +00:00
}
avl_node_t *avl_alloc_node(void)
{
2019-08-26 11:44:36 +00:00
return xmalloc_and_zero(sizeof(avl_node_t));
2019-08-26 11:44:36 +00:00
}
void avl_free_node(avl_tree_t *tree, avl_node_t *node)
{
2019-08-26 11:44:36 +00:00
if(node->data && tree->delete)
tree->delete(node->data);
free(node);
2019-08-26 11:44:36 +00:00
}
/* Searching */
void *avl_search(const avl_tree_t *tree, const void *data)
{
2019-08-26 11:44:36 +00:00
avl_node_t *node;
2019-08-26 11:44:36 +00:00
2019-08-26 11:44:36 +00:00
node = avl_search_node(tree, data);
return node ? node->data : NULL;
2019-08-26 11:44:36 +00:00
}
void *avl_search_closest(const avl_tree_t *tree, const void *data, int *result)
{
2019-08-26 11:44:36 +00:00
avl_node_t *node;
node = avl_search_closest_node(tree, data, result);
2019-08-26 11:44:36 +00:00
2019-08-26 11:44:36 +00:00
return node ? node->data : NULL;
2019-08-26 11:44:36 +00:00
}
void *avl_search_closest_smaller(const avl_tree_t *tree, const void *data)
{
2019-08-26 11:44:36 +00:00
avl_node_t *node;
2019-08-26 11:44:36 +00:00
2019-08-26 11:44:36 +00:00
node = avl_search_closest_smaller_node(tree, data);
return node ? node->data : NULL;
2019-08-26 11:44:36 +00:00
}
void *avl_search_closest_greater(const avl_tree_t *tree, const void *data)
{
2019-08-26 11:44:36 +00:00
avl_node_t *node;
node = avl_search_closest_greater_node(tree, data);
2019-08-26 11:44:36 +00:00
2019-08-26 11:44:36 +00:00
return node ? node->data : NULL;
2019-08-26 11:44:36 +00:00
}
avl_node_t *avl_search_node(const avl_tree_t *tree, const void *data)
{
2019-08-26 11:44:36 +00:00
avl_node_t *node;
int result;
node = avl_search_closest_node(tree, data, &result);
return result ? NULL : node;
2019-08-26 11:44:36 +00:00
}
2019-08-26 11:44:36 +00:00
avl_node_t *avl_search_closest_node(const avl_tree_t *tree, const void *data,
int *result)
2019-08-26 11:44:36 +00:00
{
2019-08-26 11:44:36 +00:00
avl_node_t *node;
int c;
node = tree->root;
if(!node) {
if(result)
*result = 0;
return NULL;
}
for(;;) {
c = tree->compare(data, node->data);
if(c < 0) {
if(node->left)
node = node->left;
else {
if(result)
*result = -1;
break;
}
} else if(c > 0) {
if(node->right)
node = node->right;
else {
if(result)
*result = 1;
break;
}
} else {
if(result)
*result = 0;
break;
}
}
return node;
2019-08-26 11:44:36 +00:00
}
2019-08-26 11:44:36 +00:00
avl_node_t *avl_search_closest_smaller_node(const avl_tree_t *tree,
const void *data)
2019-08-26 11:44:36 +00:00
{
2019-08-26 11:44:36 +00:00
avl_node_t *node;
int result;
node = avl_search_closest_node(tree, data, &result);
if(result < 0)
node = node->prev;
return node;
2019-08-26 11:44:36 +00:00
}
2019-08-26 11:44:36 +00:00
avl_node_t *avl_search_closest_greater_node(const avl_tree_t *tree,
const void *data)
2019-08-26 11:44:36 +00:00
{
2019-08-26 11:44:36 +00:00
avl_node_t *node;
int result;
node = avl_search_closest_node(tree, data, &result);
if(result > 0)
node = node->next;
return node;
2019-08-26 11:44:36 +00:00
}
/* Insertion and deletion */
avl_node_t *avl_insert(avl_tree_t *tree, void *data)
{
2019-08-26 11:44:36 +00:00
avl_node_t *closest, *new;
int result;
if(!tree->root) {
new = avl_alloc_node();
new->data = data;
avl_insert_top(tree, new);
} else {
closest = avl_search_closest_node(tree, data, &result);
switch (result) {
case -1:
new = avl_alloc_node();
new->data = data;
avl_insert_before(tree, closest, new);
break;
case 1:
new = avl_alloc_node();
new->data = data;
avl_insert_after(tree, closest, new);
break;
default:
return NULL;
}
}
2019-08-26 11:44:36 +00:00
#ifdef AVL_COUNT
2019-08-26 11:44:36 +00:00
new->count = 1;
2019-08-26 11:44:36 +00:00
#endif
#ifdef AVL_DEPTH
2019-08-26 11:44:36 +00:00
new->depth = 1;
2019-08-26 11:44:36 +00:00
#endif
2019-08-26 11:44:36 +00:00
return new;
2019-08-26 11:44:36 +00:00
}
avl_node_t *avl_insert_node(avl_tree_t *tree, avl_node_t *node)
{
2019-08-26 11:44:36 +00:00
avl_node_t *closest;
int result;
if(!tree->root)
avl_insert_top(tree, node);
else {
closest = avl_search_closest_node(tree, node->data, &result);
switch (result) {
case -1:
avl_insert_before(tree, closest, node);
break;
case 1:
avl_insert_after(tree, closest, node);
break;
case 0:
return NULL;
}
}
2019-08-26 11:44:36 +00:00
#ifdef AVL_COUNT
2019-08-26 11:44:36 +00:00
node->count = 1;
2019-08-26 11:44:36 +00:00
#endif
#ifdef AVL_DEPTH
2019-08-26 11:44:36 +00:00
node->depth = 1;
2019-08-26 11:44:36 +00:00
#endif
2019-08-26 11:44:36 +00:00
return node;
2019-08-26 11:44:36 +00:00
}
void avl_insert_top(avl_tree_t *tree, avl_node_t *node)
{
2019-08-26 11:44:36 +00:00
node->prev = node->next = node->parent = NULL;
tree->head = tree->tail = tree->root = node;
2019-08-26 11:44:36 +00:00
}
2019-08-26 11:44:36 +00:00
void avl_insert_before(avl_tree_t *tree, avl_node_t *before,
avl_node_t *node)
2019-08-26 11:44:36 +00:00
{
2019-08-26 11:44:36 +00:00
if(!before) {
if(tree->tail)
avl_insert_after(tree, tree->tail, node);
else
avl_insert_top(tree, node);
return;
}
node->next = before;
node->parent = before;
node->prev = before->prev;
if(before->left) {
avl_insert_after(tree, before->prev, node);
return;
}
if(before->prev)
before->prev->next = node;
else
tree->head = node;
before->prev = node;
before->left = node;
avl_rebalance(tree, before);
2019-08-26 11:44:36 +00:00
}
void avl_insert_after(avl_tree_t *tree, avl_node_t *after, avl_node_t *node)
{
2019-08-26 11:44:36 +00:00
if(!after) {
if(tree->head)
avl_insert_before(tree, tree->head, node);
else
avl_insert_top(tree, node);
return;
}
if(after->right) {
avl_insert_before(tree, after->next, node);
return;
}
node->prev = after;
node->parent = after;
node->next = after->next;
if(after->next)
after->next->prev = node;
else
tree->tail = node;
after->next = node;
after->right = node;
avl_rebalance(tree, after);
2019-08-26 11:44:36 +00:00
}
avl_node_t *avl_unlink(avl_tree_t *tree, void *data)
{
2019-08-26 11:44:36 +00:00
avl_node_t *node;
2019-08-26 11:44:36 +00:00
2019-08-26 11:44:36 +00:00
node = avl_search_node(tree, data);
2019-08-26 11:44:36 +00:00
2019-08-26 11:44:36 +00:00
if(node)
avl_unlink_node(tree, node);
return node;
2019-08-26 11:44:36 +00:00
}
void avl_unlink_node(avl_tree_t *tree, avl_node_t *node)
{
2019-08-26 11:44:36 +00:00
avl_node_t *parent;
avl_node_t **superparent;
avl_node_t *subst, *left, *right;
avl_node_t *balnode;
if(node->prev)
node->prev->next = node->next;
else
tree->head = node->next;
if(node->next)
node->next->prev = node->prev;
else
tree->tail = node->prev;
parent = node->parent;
superparent =
parent ? node ==
parent->left ? &parent->left : &parent->right : &tree->root;
left = node->left;
right = node->right;
if(!left) {
*superparent = right;
if(right)
right->parent = parent;
balnode = parent;
} else if(!right) {
*superparent = left;
left->parent = parent;
balnode = parent;
} else {
subst = node->prev;
if(subst == left) {
balnode = subst;
} else {
balnode = subst->parent;
balnode->right = subst->left;
if(balnode->right)
balnode->right->parent = balnode;
subst->left = left;
left->parent = subst;
}
subst->right = right;
subst->parent = parent;
right->parent = subst;
*superparent = subst;
}
avl_rebalance(tree, balnode);
node->next = node->prev = node->parent = node->left = node->right = NULL;
2019-08-26 11:44:36 +00:00
#ifdef AVL_COUNT
2019-08-26 11:44:36 +00:00
node->count = 0;
2019-08-26 11:44:36 +00:00
#endif
#ifdef AVL_DEPTH
2019-08-26 11:44:36 +00:00
node->depth = 0;
2019-08-26 11:44:36 +00:00
#endif
}
void avl_delete_node(avl_tree_t *tree, avl_node_t *node)
{
2019-08-26 11:44:36 +00:00
avl_unlink_node(tree, node);
avl_free_node(tree, node);
2019-08-26 11:44:36 +00:00
}
void avl_delete(avl_tree_t *tree, void *data)
{
2019-08-26 11:44:36 +00:00
avl_node_t *node;
2019-08-26 11:44:36 +00:00
2019-08-26 11:44:36 +00:00
node = avl_search_node(tree, data);
2019-08-26 11:44:36 +00:00
2019-08-26 11:44:36 +00:00
if(node)
avl_delete_node(tree, node);
2019-08-26 11:44:36 +00:00
}
/* Fast tree cleanup */
void avl_delete_tree(avl_tree_t *tree)
{
2019-08-26 11:44:36 +00:00
avl_node_t *node, *next;
2019-08-26 11:44:37 +00:00
for(node = tree->head; node; node = next) {
2019-08-26 11:44:36 +00:00
next = node->next;
avl_free_node(tree, node);
}
avl_free_tree(tree);
2019-08-26 11:44:36 +00:00
}
/* Tree walking */
2019-08-26 11:44:36 +00:00
void avl_foreach(const avl_tree_t *tree, avl_action_t action)
2019-08-26 11:44:36 +00:00
{
2019-08-26 11:44:36 +00:00
avl_node_t *node, *next;
for(node = tree->head; node; node = next) {
next = node->next;
action(node->data);
}
2019-08-26 11:44:36 +00:00
}
2019-08-26 11:44:36 +00:00
void avl_foreach_node(const avl_tree_t *tree, avl_action_t action)
2019-08-26 11:44:36 +00:00
{
2019-08-26 11:44:36 +00:00
avl_node_t *node, *next;
for(node = tree->head; node; node = next) {
next = node->next;
action(node);
}
2019-08-26 11:44:36 +00:00
}
/* Indexing */
#ifdef AVL_COUNT
2019-08-26 11:44:36 +00:00
unsigned int avl_count(const avl_tree_t *tree)
2019-08-26 11:44:36 +00:00
{
2019-08-26 11:44:36 +00:00
return AVL_NODE_COUNT(tree->root);
2019-08-26 11:44:36 +00:00
}
avl_node_t *avl_get_node(const avl_tree_t *tree, unsigned int index)
{
2019-08-26 11:44:36 +00:00
avl_node_t *node;
unsigned int c;
node = tree->root;
while(node) {
c = AVL_L_COUNT(node);
if(index < c) {
node = node->left;
} else if(index > c) {
node = node->right;
index -= c + 1;
} else {
return node;
}
}
return NULL;
2019-08-26 11:44:36 +00:00
}
unsigned int avl_index(const avl_node_t *node)
{
2019-08-26 11:44:36 +00:00
avl_node_t *next;
unsigned int index;
2019-08-26 11:44:36 +00:00
2019-08-26 11:44:36 +00:00
index = AVL_L_COUNT(node);
2019-08-26 11:44:36 +00:00
2019-08-26 11:44:36 +00:00
while((next = node->parent)) {
if(node == next->right)
index += AVL_L_COUNT(next) + 1;
node = next;
}
2019-08-26 11:44:36 +00:00
2019-08-26 11:44:36 +00:00
return index;
2019-08-26 11:44:36 +00:00
}
#endif
#ifdef AVL_DEPTH
2019-08-26 11:44:36 +00:00
unsigned int avl_depth(const avl_tree_t *tree)
2019-08-26 11:44:36 +00:00
{
2019-08-26 11:44:36 +00:00
return AVL_NODE_DEPTH(tree->root);
2019-08-26 11:44:36 +00:00
}
#endif