avl_tree.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  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>
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License along
  16. with this program; if not, write to the Free Software Foundation, Inc.,
  17. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. Original AVL tree library by Michael H. Buselli <cosine@cosine.org>.
  19. Modified 2000-11-28 by Wessel Dankers <wsl@tinc-vpn.org> to use counts
  20. instead of depths, to add the ->next and ->prev and to generally obfuscate
  21. the code. Mail me if you found a bug.
  22. Cleaned up and incorporated some of the ideas from the red-black tree
  23. library for inclusion into tinc (http://www.tinc-vpn.org/) by
  24. Guus Sliepen <guus@tinc-vpn.org>.
  25. */
  26. #ifndef __AVL_TREE_H__
  27. #define __AVL_TREE_H__
  28. #ifndef AVL_DEPTH
  29. #ifndef AVL_COUNT
  30. #define AVL_DEPTH
  31. #endif
  32. #endif
  33. typedef struct avl_node_t {
  34. /* Linked list part */
  35. struct avl_node_t *next;
  36. struct avl_node_t *prev;
  37. /* Tree part */
  38. struct avl_node_t *parent;
  39. struct avl_node_t *left;
  40. struct avl_node_t *right;
  41. #ifdef AVL_COUNT
  42. unsigned int count;
  43. #endif
  44. #ifdef AVL_DEPTH
  45. unsigned char depth;
  46. #endif
  47. /* Payload */
  48. void *data;
  49. } avl_node_t;
  50. typedef int (*avl_compare_t)(const void *, const void *);
  51. typedef void (*avl_action_t)(const void *);
  52. typedef void (*avl_action_node_t)(const avl_node_t *);
  53. typedef struct avl_tree_t {
  54. /* Linked list part */
  55. avl_node_t *head;
  56. avl_node_t *tail;
  57. /* Tree part */
  58. avl_node_t *root;
  59. avl_compare_t compare;
  60. avl_action_t delete;
  61. } avl_tree_t;
  62. /* (De)constructors */
  63. extern avl_tree_t *avl_alloc_tree(avl_compare_t, avl_action_t);
  64. extern void avl_free_tree(avl_tree_t *);
  65. extern avl_node_t *avl_alloc_node(void);
  66. extern void avl_free_node(avl_tree_t *tree, avl_node_t *);
  67. /* Insertion and deletion */
  68. extern avl_node_t *avl_insert(avl_tree_t *, void *);
  69. extern avl_node_t *avl_insert_node(avl_tree_t *, avl_node_t *);
  70. extern void avl_insert_top(avl_tree_t *, avl_node_t *);
  71. extern void avl_insert_before(avl_tree_t *, avl_node_t *, avl_node_t *);
  72. extern void avl_insert_after(avl_tree_t *, avl_node_t *, avl_node_t *);
  73. extern avl_node_t *avl_unlink(avl_tree_t *, void *);
  74. extern void avl_unlink_node(avl_tree_t *tree, avl_node_t *);
  75. extern void avl_delete(avl_tree_t *, void *);
  76. extern void avl_delete_node(avl_tree_t *, avl_node_t *);
  77. /* Fast tree cleanup */
  78. extern void avl_delete_tree(avl_tree_t *);
  79. /* Searching */
  80. extern void *avl_search(const avl_tree_t *, const void *);
  81. extern void *avl_search_closest(const avl_tree_t *, const void *, int *);
  82. extern void *avl_search_closest_smaller(const avl_tree_t *, const void *);
  83. extern void *avl_search_closest_greater(const avl_tree_t *, const void *);
  84. extern avl_node_t *avl_search_node(const avl_tree_t *, const void *);
  85. extern avl_node_t *avl_search_closest_node(const avl_tree_t *, const void *, int *);
  86. extern avl_node_t *avl_search_closest_smaller_node(const avl_tree_t *, const void *);
  87. extern avl_node_t *avl_search_closest_greater_node(const avl_tree_t *, const void *);
  88. /* Tree walking */
  89. extern void avl_foreach(const avl_tree_t *, avl_action_t);
  90. extern void avl_foreach_node(const avl_tree_t *, avl_action_t);
  91. /* Indexing */
  92. #ifdef AVL_COUNT
  93. extern unsigned int avl_count(const avl_tree_t *);
  94. extern avl_node_t *avl_get_node(const avl_tree_t *, unsigned int);
  95. extern unsigned int avl_index(const avl_node_t *);
  96. #endif
  97. #ifdef AVL_DEPTH
  98. extern unsigned int avl_depth(const avl_tree_t *);
  99. #endif
  100. #endif /* __AVL_TREE_H__ */