2 * Libavl is a library to manage AVL structure to store and organize
3 * everykind of data. You just need to implement function to compare,
4 * to desallocate and to print your structure.
6 * DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
7 * Version 2, December 2004
9 * Copyright (C) 2013 Adrien Oliva <adrien.oliva@yapbreak.fr>
11 * Everyone is permitted to copy and distribute verbatim or modified
12 * copies of this license document, and changing it is allowed as long
13 * as the name is changed.
15 * DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
16 * TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
18 * 0. You just DO WHAT THE FUCK YOU WANT TO.
23 * \author Adrien Oliva
24 * \date May, 24th, 2011
25 * \brief Generic AVL-tree library.
28 * Implementation of an AVL tree to store generic data.
29 * In computer science, an AVL tree is a self-balancing binary search tree,
30 * and it was the first such data structure to be invented. In an AVL tree,
31 * the heights of the two child subtrees of any node differ by at most one.
32 * Lookup, insertion, and deletion all take \f$\mathcal{O}(\log n)\f$
33 * time in both the average and worst cases, where \e n is the number of
34 * nodes in the tree prior to the operation. Insertions and deletions may
35 * require the tree to be rebalanced by one or more tree rotations.
37 * The AVL tree is named after its two Soviet inventors, G.M. Adelson-Velskii
38 * and E.M. Landis, who published it in their 1962 paper "An algorithm for the
39 * organization of information."
41 * The balance factor of a node is the height of its left subtree minus the
42 * height of its right subtree (sometimes opposite) and a node with balance
43 * factor 1, 0, or -1 is considered balanced. A node with any other balance
44 * factor is considered unbalanced and requires rebalancing the tree. The
45 * balance factor is either stored directly at each node or computed from the
46 * heights of the subtrees.
48 * \section Usage Use of library
50 * Here is an example of code that store structure in AVL tree:
57 * // Structure we want to store
58 * // key is used to order data
64 * // Function that compares two struct data
65 * int data_cmp(void *a, void *b)
67 * struct data *aa = (struct data *) a;
68 * struct data *bb = (struct data *) b;
70 * // Protect against NULL pointer
71 * // It could generally never happened
75 * return aa->key - bb->key;
78 * // Function that dumps data structure
79 * void data_print(void *d)
81 * struct data *dd = (struct data *) d;
84 * printf("{ %d => %d }\n", dd->key, dd->value);
87 * // Function that delete a data structure
88 * void data_delete(void *d)
90 * struct data *dd = (struct data *) d;
93 * // You can put here all additional needed
94 * // memory deallocation
99 * int main(int argc, char *argv)
101 * tree *avl_tree = NULL;
105 * // Initialize a new tree with our three previously defined
106 * // functions to store data structure.
107 * avl_tree = init_dictionnary(data_cmp, data_print, data_delete);
112 * // Add element {42, 4242} in our tree.
113 * result = insert_elmt(avl_tree, &tmp, sizeof(struct data));
114 * // Here result is equal to 1 since there is only 1 element in tree.
116 * // Dump tree to stdout with data_print function
117 * print_tree(avl_tree);
119 * // For all search function, the only value needed in tmp structure
124 * if (!is_present(avl_tree, &tmp))
125 * printf("Key 20 is not found.\n");
128 * if (is_present(avl_tree, &tmp))
129 * printf("Key 42 exist in tree.\n");
131 * if (get_data(avl_tree, &tmp, sizeof(struct data)))
132 * printf("Now, tmp.key is equal to 4242\n");
134 * delete_node(avl_tree, &tmp);
135 * if (!is_present(avl_tree, &tmp))
136 * printf("Key 42 does not exist anymore.\n");
139 * delete_tree(avl_tree);
145 * You can find this example in folder \b example.
147 * \subsection Tree initialisation
149 * To start a new tree, you need to init a new one with function
152 * \subsection Manage data
154 * The libavl provide all necessary function to store, retrieve and
155 * browse your data. The following set gives basic operation:
160 * * \b delete_node_min
162 * Moreover, libavl gives the availability to browse your entire data
163 * or a subset of your data with:
165 * * \b explore_restrain_tree
168 * Finally, libavl take care of your memory and deallocate all memory
169 * used in a tree when you want to destroy it with \b delete_tree.
180 * \brief Node of a tree
182 * Structure that contain all data usefull to organize the tree
185 /** Size of subtree */
191 /** Pointer to data stored in this node */
196 * \brief Pointer to the \c _node structure.
198 typedef struct _node *node;
201 * \brief Tree structure wich contains all necessary element.
203 typedef struct _tree {
204 /** Number of element in tree */
206 /** Pointer to the first node of tree */
208 /** \brief External function to compare data
210 * \param a Pointer to first element to compare
211 * \param b Pointer to second element to compare
213 * \return 0 if a = b, positive if a > b and negative if a < b.
215 * \note \e You must implement this function. It is necessary for the library
216 * to work and depends on your data you want to store.
218 int (* data_cmp) (void *, void *);
219 /** \brief External function to print data.
221 * \param d Pointer to data to print.
223 * This function is usefull for debuging program.
225 * \note \e You must implement this function. It is necessary for the library
226 * to work and depends on your data you want to store.
228 void (* data_print) (void *d);
230 /** \brief External function to delete data.
232 * \param d Pointer to data to delete.
234 * This function is usefull for when you want to delete data from tree to prevent
237 * \note \e You must implement this function. It is necessary for the library
238 * to work and depends on your data you want to store.
240 void (* data_delete) (void *d);
242 /** \brief External function to copy data.
244 * \param d Pointer to data to copy.
245 * \return New data allocation with all its fields filled.
247 * \note \e You must implement this function. It is necessary for the library
248 * to work and depends on your data you want to store.
250 void (* data_copy) (void *, void *);
255 /* ************************************************************************* *\
256 |* EXTERNAL FUNCTION *|
257 \* ************************************************************************* */
259 /** \fn tree *init_dictionnary(int (*data_cmp)(void *, void *),
260 * void (*data_print)(void *),
261 * void (*data_delete)(void *),
262 * void (*data_copy)(void *, void *));
263 * \brief Initialize dictionnary.
265 * \return Pointer to new tree.
267 * \param data_cmp Function to compare data.
268 * \param data_print Function to print data.
269 * \param data_delete Function to delete data.
270 * \param data_copy Function to copy data.
272 * This function return an initilized tree.
274 * \note By default, if you do not provide function usefull to the tree,
275 * the LibAVL library uses its own function. As LibAVL does not know exactly
276 * the structure and the definition of your data, result can not be as good
277 * as expected. Details on these functions are given in the following.
279 * \section LibAVL internal data function
281 * - Compare function: by default, the internal \c data_cmp function will
282 * return the difference between the two pointer given in arguments
284 * return (int) ((ptrdiff_t) a - (ptrdiff_t) b);
286 * The \c data_cmp function is really critical and guarantee efficiency of
288 * - Print function: by default, the internal \c data_print function will
289 * print the addresse contained in pointer in argument, in hexadecimal
291 * printf("0x%08x\n", d);
293 * Since \c data_print is mainly used as debug function to dump a complete
294 * tree, it is not the most important data function, but with the correct
295 * implementation, it could save lots of time and money when something
297 * - Delete function: by default, the internal \c data_delete function will
298 * call \c free on the argument pointer. If your data is only a static
299 * structure or a simple type, this function is enough. But for bigger
300 * object like a string array, it is necessary to provide a new
301 * \c data_delete function to avoid memory leak.
302 * - Copy function: by default, the internal \c data_copy function will
305 tree *init_dictionnary(int (*data_cmp)(void *, void *),
306 void (*data_print)(void *),
307 void (*data_delete)(void *),
308 void (*data_copy)(void *, void *));
310 /** \fn int insert_elmt(tree *t, void *data, size_t datasize);
311 * \brief Insert new element in tree.
313 * \return Number of element inserted in tree.
314 * \param t Pointer to tree.
315 * \param data Pointer to data to add.
316 * \param datasize Size of data to add.
318 * This function allocate a new memory space with the given size
319 * and copy object pointed by \c data to the newly created space.
321 unsigned int insert_elmt(tree *t, void *data, size_t datasize);
323 /** \fn void verif_tree(tree *t);
324 * \brief Deffensive check if tree is a real AVL tree.
326 * \param t Pointer to tree.
328 * If tree is not an AVL tree, this function end on an assert.
330 void verif_tree(tree *t);
332 /** \fn void delete_tree(tree *t);
333 * \brief Deallocate all memory used by tree.
335 * \param t Pointer to tree to delete.
337 void delete_tree(tree *t);
339 /** \fn void print_tree(tree *t);
340 * \brief Use for debug only. Print all element in tree with function
343 * \param t Pointer to tree.
345 void print_tree(tree *t);
347 /** \fn void explore_tree(tree *t, void (*treatement)(void *, void *),
349 * \brief Execute function \c treatement on every node in tree.
351 * \param t Pointer to tree.
352 * \param treatement Function to apply to each node.
353 * \param param Pointer to extra data to pass to \c treatement function.
355 * This function goes thought the entire tree and, if \c n is the pointer
356 * to the current node, call the function:
358 * treatement(n, param);
361 void explore_tree(tree *t, void (*treatement)(void *, void *), void *param);
363 /** \fn explore_restrain_tree(tree *t, int (*check)(void *, void *),
365 * void *data_min, void *data_max);
366 * \brief Execute function \c check on every node between \c data_min and
369 * \return Accumulation of all return value of \c check function.
370 * \param t Pointer to tree.
371 * \param check Function apply on every node between \c data_min and
373 * \param param Pointer to extra data to pass to \c check function.
374 * \param data_min Pointer to the minimum element.
375 * \param data_max Pointer to the maximum element.
377 * This function goes thought a part of tree bounded with \c data_min and
378 * \c data_max, and if \c n is the pointer to the current node, it calls
381 * accu += check(n, param);
383 * The value of \c accu is returned by \c explore_restrain_tree.
385 int explore_restrain_tree(tree *t, int (*check)(void *, void *), void *param,
386 void *data_min, void *data_max);
388 /** \fn int is_present(tree *t, void *d);
389 * \brief Function to check if a given data is present in tree.
391 * \return 1 if data is present, 0 if not.
392 * \param t Pointer to tree.
393 * \param d Pointer to data. Only field used in \c avl_data_cmp need
394 * to be filled in \c d.
396 int is_present(tree *t, void *d);
398 /** \fn void delete_node_min(tree *t);
399 * \brief Delete minimum element of a tree.
401 * \param t Tree where minimum element will be deleted.
403 void delete_node_min(tree *t);
405 /** \fn void delete_node(tree *t, void *data);
406 * \brief Delete node n of tree.
408 * \param t Pointer to tree.
409 * \param data Data to delete.
411 void delete_node(tree *t, void *data);
413 /** \fn int get_data(tree *t, void *data, size_t data_size);
414 * \brief Fill information pointed by data with the data stored in the tree.
416 * \return True if value pointed by data are relevant, false if not.
418 * \param t Pointer to tree.
419 * \param data Data to retrieve. At the begining of the function, only
420 * field used in \c avl_data_cmp must be filled.
421 * \param data_size Size of data structure pointed by data.
423 int get_data(tree *t, void *data, size_t data_size);