| 1 | /***************************************************************************** |
| 2 | |
| 3 | avl.h - Source code for the AVL-tree library. |
| 4 | |
| 5 | Copyright (C) 1998 Michael H. Buselli <[email protected]> |
| 6 | Copyright (C) 2000-2002 Wessel Dankers <[email protected]> |
| 7 | |
| 8 | This library is free software; you can redistribute it and/or |
| 9 | modify it under the terms of the GNU Lesser General Public |
| 10 | License as published by the Free Software Foundation; either |
| 11 | version 2.1 of the License, or (at your option) any later version. |
| 12 | |
| 13 | This library 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 GNU |
| 16 | Lesser General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU Lesser General Public |
| 19 | License along with this library; if not, write to the Free Software |
| 20 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | |
| 22 | Augmented AVL-tree. Original by Michael H. Buselli <[email protected]>. |
| 23 | |
| 24 | Modified by Wessel Dankers <[email protected]> to add a bunch of bloat to |
| 25 | the sourcecode, change the interface and squash a few bugs. |
| 26 | Mail him if you find new bugs. |
| 27 | |
| 28 | *****************************************************************************/ |
| 29 | |
| 30 | #ifndef _AVL_H |
| 31 | #define _AVL_H |
| 32 | |
| 33 | /* We need either depths, counts or both (the latter being the default) */ |
| 34 | #if !defined(AVL_DEPTH) && !defined(AVL_COUNT) |
| 35 | #define AVL_DEPTH |
| 36 | #define AVL_COUNT |
| 37 | #endif |
| 38 | |
| 39 | /* User supplied function to compare two items like strcmp() does. |
| 40 | * For example: cmp(a,b) will return: |
| 41 | * -1 if a < b |
| 42 | * 0 if a = b |
| 43 | * 1 if a > b |
| 44 | */ |
| 45 | typedef int (*avl_compare_t)(const void *, const void *); |
| 46 | |
| 47 | /* User supplied function to delete an item when a node is free()d. |
| 48 | * If NULL, the item is not free()d. |
| 49 | */ |
| 50 | typedef void (*avl_freeitem_t)(void *); |
| 51 | |
| 52 | typedef struct avl_node_t { |
| 53 | struct avl_node_t *next; |
| 54 | struct avl_node_t *prev; |
| 55 | struct avl_node_t *parent; |
| 56 | struct avl_node_t *left; |
| 57 | struct avl_node_t *right; |
| 58 | void *item; |
| 59 | #ifdef AVL_COUNT |
| 60 | unsigned int count; |
| 61 | #endif |
| 62 | #ifdef AVL_DEPTH |
| 63 | unsigned char depth; |
| 64 | #endif |
| 65 | } avl_node_t; |
| 66 | |
| 67 | typedef struct avl_tree_t { |
| 68 | avl_node_t *head; |
| 69 | avl_node_t *tail; |
| 70 | avl_node_t *top; |
| 71 | avl_compare_t cmp; |
| 72 | avl_freeitem_t freeitem; |
| 73 | } avl_tree_t; |
| 74 | |
| 75 | /* Initializes a new tree for elements that will be ordered using |
| 76 | * the supplied strcmp()-like function. |
| 77 | * Returns the value of avltree (even if it's NULL). |
| 78 | * O(1) */ |
| 79 | extern avl_tree_t *avl_init_tree(avl_tree_t *avltree, avl_compare_t, avl_freeitem_t); |
| 80 | |
| 81 | /* Allocates and initializes a new tree for elements that will be |
| 82 | * ordered using the supplied strcmp()-like function. |
| 83 | * Returns NULL if memory could not be allocated. |
| 84 | * O(1) */ |
| 85 | extern avl_tree_t *avl_alloc_tree(avl_compare_t, avl_freeitem_t); |
| 86 | |
| 87 | /* Frees the entire tree efficiently. Nodes will be free()d. |
| 88 | * If the tree's freeitem is not NULL it will be invoked on every item. |
| 89 | * O(n) */ |
| 90 | extern void avl_free_tree(avl_tree_t *); |
| 91 | |
| 92 | /* Reinitializes the tree structure for reuse. Nothing is free()d. |
| 93 | * Compare and freeitem functions are left alone. |
| 94 | * O(1) */ |
| 95 | extern void avl_clear_tree(avl_tree_t *); |
| 96 | |
| 97 | /* Free()s all nodes in the tree but leaves the tree itself. |
| 98 | * If the tree's freeitem is not NULL it will be invoked on every item. |
| 99 | * O(n) */ |
| 100 | extern void avl_free_nodes(avl_tree_t *); |
| 101 | |
| 102 | /* Initializes memory for use as a node. Returns NULL if avlnode is NULL. |
| 103 | * O(1) */ |
| 104 | extern avl_node_t *avl_init_node(avl_node_t *avlnode, void *item); |
| 105 | |
| 106 | /* Insert an item into the tree and return the new node. |
| 107 | * Returns NULL and sets errno if memory for the new node could not be |
| 108 | * allocated or if the node is already in the tree (EEXIST). |
| 109 | * O(lg n) */ |
| 110 | extern avl_node_t *avl_insert(avl_tree_t *, void *item); |
| 111 | |
| 112 | /* Insert a node into the tree and return it. |
| 113 | * Returns NULL if the node is already in the tree. |
| 114 | * O(lg n) */ |
| 115 | extern avl_node_t *avl_insert_node(avl_tree_t *, avl_node_t *); |
| 116 | |
| 117 | /* Insert a node in an empty tree. If avlnode is NULL, the tree will be |
| 118 | * cleared and ready for re-use. |
| 119 | * If the tree is not empty, the old nodes are left dangling. |
| 120 | * O(1) */ |
| 121 | extern avl_node_t *avl_insert_top(avl_tree_t *, avl_node_t *avlnode); |
| 122 | |
| 123 | /* Insert a node before another node. Returns the new node. |
| 124 | * If old is NULL, the item is appended to the tree. |
| 125 | * O(lg n) */ |
| 126 | extern avl_node_t *avl_insert_before(avl_tree_t *, avl_node_t *old, avl_node_t *new); |
| 127 | |
| 128 | /* Insert a node after another node. Returns the new node. |
| 129 | * If old is NULL, the item is prepended to the tree. |
| 130 | * O(lg n) */ |
| 131 | extern avl_node_t *avl_insert_after(avl_tree_t *, avl_node_t *old, avl_node_t *new); |
| 132 | |
| 133 | /* Deletes a node from the tree. Returns immediately if the node is NULL. |
| 134 | * The item will not be free()d regardless of the tree's freeitem handler. |
| 135 | * This function comes in handy if you need to update the search key. |
| 136 | * O(lg n) */ |
| 137 | extern void avl_unlink_node(avl_tree_t *, avl_node_t *); |
| 138 | |
| 139 | /* Deletes a node from the tree. Returns immediately if the node is NULL. |
| 140 | * If the tree's freeitem is not NULL, it is invoked on the item. |
| 141 | * If it is, returns the item. |
| 142 | * O(lg n) */ |
| 143 | extern void *avl_delete_node(avl_tree_t *, avl_node_t *); |
| 144 | |
| 145 | /* Searches for an item in the tree and deletes it if found. |
| 146 | * If the tree's freeitem is not NULL, it is invoked on the item. |
| 147 | * If it is, returns the item. |
| 148 | * O(lg n) */ |
| 149 | extern void *avl_delete(avl_tree_t *, const void *item); |
| 150 | |
| 151 | /* If exactly one node is moved in memory, this will fix the pointers |
| 152 | * in the tree that refer to it. It must be an exact shallow copy. |
| 153 | * Returns the pointer to the old position. |
| 154 | * O(1) */ |
| 155 | extern avl_node_t *avl_fixup_node(avl_tree_t *, avl_node_t *new); |
| 156 | |
| 157 | /* Searches for a node with the key closest (or equal) to the given item. |
| 158 | * If avlnode is not NULL, *avlnode will be set to the node found or NULL |
| 159 | * if the tree is empty. Return values: |
| 160 | * -1 if the returned node is smaller |
| 161 | * 0 if the returned node is equal or if the tree is empty |
| 162 | * 1 if the returned node is greater |
| 163 | * O(lg n) */ |
| 164 | extern int avl_search_closest(const avl_tree_t *, const void *item, avl_node_t **avlnode); |
| 165 | |
| 166 | /* Searches for the item in the tree and returns a matching node if found |
| 167 | * or NULL if not. |
| 168 | * O(lg n) */ |
| 169 | extern avl_node_t *avl_search(const avl_tree_t *, const void *item); |
| 170 | |
| 171 | #ifdef AVL_COUNT |
| 172 | /* Returns the number of nodes in the tree. |
| 173 | * O(1) */ |
| 174 | extern unsigned int avl_count(const avl_tree_t *); |
| 175 | |
| 176 | /* Searches a node by its rank in the list. Counting starts at 0. |
| 177 | * Returns NULL if the index exceeds the number of nodes in the tree. |
| 178 | * O(lg n) */ |
| 179 | extern avl_node_t *avl_at(const avl_tree_t *, unsigned int); |
| 180 | |
| 181 | /* Returns the rank of a node in the list. Counting starts at 0. |
| 182 | * O(lg n) */ |
| 183 | extern unsigned int avl_index(const avl_node_t *); |
| 184 | #endif |
| 185 | |
| 186 | #endif |