avl_tree.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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. $Id$
  26. */
  27. #ifndef __AVL_TREE_H__
  28. #define __AVL_TREE_H__
  29. #ifndef AVL_DEPTH
  30. #ifndef AVL_COUNT
  31. #define AVL_DEPTH
  32. #endif
  33. #endif
  34. typedef struct avl_node_t {
  35. /* Linked list part */
  36. struct avl_node_t *next;
  37. struct avl_node_t *prev;
  38. /* Tree part */
  39. struct avl_node_t *parent;
  40. struct avl_node_t *left;
  41. struct avl_node_t *right;
  42. #ifdef AVL_COUNT
  43. unsigned int count;
  44. #endif
  45. #ifdef AVL_DEPTH
  46. unsigned char depth;
  47. #endif
  48. /* Payload */
  49. void *data;
  50. } avl_node_t;
  51. typedef int (*avl_compare_t)(const void *, const void *);
  52. typedef void (*avl_action_t)(const void *);
  53. typedef void (*avl_action_node_t)(const avl_node_t *);
  54. typedef struct avl_tree_t {
  55. /* Linked list part */
  56. avl_node_t *head;
  57. avl_node_t *tail;
  58. /* Tree part */
  59. avl_node_t *root;
  60. avl_compare_t compare;
  61. avl_action_t delete;
  62. } avl_tree_t;
  63. /* (De)constructors */
  64. extern avl_tree_t *avl_alloc_tree(avl_compare_t, avl_action_t);
  65. extern void avl_free_tree(avl_tree_t *);
  66. extern avl_node_t *avl_alloc_node(void);
  67. extern void avl_free_node(avl_tree_t *tree, avl_node_t *);
  68. /* Insertion and deletion */
  69. extern avl_node_t *avl_insert(avl_tree_t *, void *);
  70. extern avl_node_t *avl_insert_node(avl_tree_t *, avl_node_t *);
  71. extern void avl_insert_top(avl_tree_t *, avl_node_t *);
  72. extern void avl_insert_before(avl_tree_t *, avl_node_t *, avl_node_t *);
  73. extern void avl_insert_after(avl_tree_t *, avl_node_t *, avl_node_t *);
  74. extern avl_node_t *avl_unlink(avl_tree_t *, void *);
  75. extern void avl_unlink_node(avl_tree_t *tree, avl_node_t *);
  76. extern void avl_delete(avl_tree_t *, void *);
  77. extern void avl_delete_node(avl_tree_t *, avl_node_t *);
  78. /* Fast tree cleanup */
  79. extern void avl_delete_tree(avl_tree_t *);
  80. /* Searching */
  81. extern void *avl_search(const avl_tree_t *, const void *);
  82. extern void *avl_search_closest(const avl_tree_t *, const void *, int *);
  83. extern void *avl_search_closest_smaller(const avl_tree_t *, const void *);
  84. extern void *avl_search_closest_greater(const avl_tree_t *, const void *);
  85. extern avl_node_t *avl_search_node(const avl_tree_t *, const void *);
  86. extern avl_node_t *avl_search_closest_node(const avl_tree_t *, const void *, int *);
  87. extern avl_node_t *avl_search_closest_smaller_node(const avl_tree_t *, const void *);
  88. extern avl_node_t *avl_search_closest_greater_node(const avl_tree_t *, const void *);
  89. /* Tree walking */
  90. extern void avl_foreach(const avl_tree_t *, avl_action_t);
  91. extern void avl_foreach_node(const avl_tree_t *, avl_action_t);
  92. /* Indexing */
  93. #ifdef AVL_COUNT
  94. extern unsigned int avl_count(const avl_tree_t *);
  95. extern avl_node_t *avl_get_node(const avl_tree_t *, unsigned int);
  96. extern unsigned int avl_index(const avl_node_t *);
  97. #endif
  98. #ifdef AVL_DEPTH
  99. extern unsigned int avl_depth(const avl_tree_t *);
  100. #endif
  101. #endif /* __AVL_TREE_H__ */