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
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@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>.
36 #ifndef __AVL_TREE_H__
37 #define __AVL_TREE_H__
45 typedef struct avl_node_t {
47 /* Linked list part */
49 struct avl_node_t *next;
50 struct avl_node_t *prev;
54 struct avl_node_t *parent;
55 struct avl_node_t *left;
56 struct avl_node_t *right;
71 typedef int (*avl_compare_t)(const void *, const void *);
72 typedef void (*avl_action_t)(const void *);
73 typedef void (*avl_action_node_t)(const avl_node_t *);
75 typedef struct avl_tree_t {
77 /* Linked list part */
86 avl_compare_t compare;
91 /* (De)constructors */
93 extern avl_tree_t *avl_alloc_tree(avl_compare_t, avl_action_t);
94 extern void avl_free_tree(avl_tree_t *);
96 extern avl_node_t *avl_alloc_node(void);
97 extern void avl_free_node(avl_tree_t *tree, avl_node_t *);
99 /* Insertion and deletion */
101 extern avl_node_t *avl_insert(avl_tree_t *, void *);
102 extern avl_node_t *avl_insert_node(avl_tree_t *, avl_node_t *);
104 extern void avl_insert_top(avl_tree_t *, avl_node_t *);
105 extern void avl_insert_before(avl_tree_t *, avl_node_t *, avl_node_t *);
106 extern void avl_insert_after(avl_tree_t *, avl_node_t *, avl_node_t *);
108 extern avl_node_t *avl_unlink(avl_tree_t *, void *);
109 extern void avl_unlink_node(avl_tree_t *tree, avl_node_t *);
110 extern void avl_delete(avl_tree_t *, void *);
111 extern void avl_delete_node(avl_tree_t *, avl_node_t *);
113 /* Fast tree cleanup */
115 extern void avl_delete_tree(avl_tree_t *);
119 extern void *avl_search(const avl_tree_t *, const void *);
120 extern void *avl_search_closest(const avl_tree_t *, const void *, int *);
121 extern void *avl_search_closest_smaller(const avl_tree_t *, const void *);
122 extern void *avl_search_closest_greater(const avl_tree_t *, const void *);
124 extern avl_node_t *avl_search_node(const avl_tree_t *, const void *);
125 extern avl_node_t *avl_search_closest_node(const avl_tree_t *, const void *, int *);
126 extern avl_node_t *avl_search_closest_smaller_node(const avl_tree_t *, const void *);
127 extern avl_node_t *avl_search_closest_greater_node(const avl_tree_t *, const void *);
131 extern void avl_foreach(const avl_tree_t *, avl_action_t);
132 extern void avl_foreach_node(const avl_tree_t *, avl_action_t);
137 extern unsigned int avl_count(const avl_tree_t *);
138 extern avl_node_t *avl_get_node(const avl_tree_t *, unsigned int);
139 extern unsigned int avl_index(const avl_node_t *);
142 extern unsigned int avl_depth(const avl_tree_t *);
145 #endif /* __AVL_TREE_H__ */