2 avl_tree.h -- header file for avl_tree.c
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 (https://www.tinc-vpn.org/) by
30 Guus Sliepen <guus@tinc-vpn.org>.
34 #ifndef __AVL_TREE_H__
35 #define __AVL_TREE_H__
43 typedef struct avl_node_t {
45 /* Linked list part */
47 struct avl_node_t *next;
48 struct avl_node_t *prev;
52 struct avl_node_t *parent;
53 struct avl_node_t *left;
54 struct avl_node_t *right;
69 typedef int (*avl_compare_t)(const void *, const void *);
70 typedef void (*avl_action_t)(const void *);
71 typedef void (*avl_action_node_t)(const avl_node_t *);
73 typedef struct avl_tree_t {
75 /* Linked list part */
84 avl_compare_t compare;
89 /* (De)constructors */
91 extern avl_tree_t *avl_alloc_tree(avl_compare_t, avl_action_t);
92 extern void avl_free_tree(avl_tree_t *);
94 extern avl_node_t *avl_alloc_node(void);
95 extern void avl_free_node(avl_tree_t *tree, avl_node_t *);
97 /* Insertion and deletion */
99 extern avl_node_t *avl_insert(avl_tree_t *, void *);
100 extern avl_node_t *avl_insert_node(avl_tree_t *, avl_node_t *);
102 extern void avl_insert_top(avl_tree_t *, avl_node_t *);
103 extern void avl_insert_before(avl_tree_t *, avl_node_t *, avl_node_t *);
104 extern void avl_insert_after(avl_tree_t *, avl_node_t *, avl_node_t *);
106 extern avl_node_t *avl_unlink(avl_tree_t *, void *);
107 extern void avl_unlink_node(avl_tree_t *tree, avl_node_t *);
108 extern void avl_delete(avl_tree_t *, void *);
109 extern void avl_delete_node(avl_tree_t *, avl_node_t *);
111 /* Fast tree cleanup */
113 extern void avl_delete_tree(avl_tree_t *);
117 extern void *avl_search(const avl_tree_t *, const void *);
118 extern void *avl_search_closest(const avl_tree_t *, const void *, int *);
119 extern void *avl_search_closest_smaller(const avl_tree_t *, const void *);
120 extern void *avl_search_closest_greater(const avl_tree_t *, const void *);
122 extern avl_node_t *avl_search_node(const avl_tree_t *, const void *);
123 extern avl_node_t *avl_search_closest_node(const avl_tree_t *, const void *, int *);
124 extern avl_node_t *avl_search_closest_smaller_node(const avl_tree_t *, const void *);
125 extern avl_node_t *avl_search_closest_greater_node(const avl_tree_t *, const void *);
129 extern void avl_foreach(const avl_tree_t *, avl_action_t);
130 extern void avl_foreach_node(const avl_tree_t *, avl_action_t);
135 extern unsigned int avl_count(const avl_tree_t *);
136 extern avl_node_t *avl_get_node(const avl_tree_t *, unsigned int);
137 extern unsigned int avl_index(const avl_node_t *);
140 extern unsigned int avl_depth(const avl_tree_t *);
143 #endif /* __AVL_TREE_H__ */