binary_search_tree.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*!
  2. Temelia - Binary search tree interface.
  3. Copyright (C) 2008, 2009 Ceata (http://cod.ceata.org/proiecte/temelia).
  4. @author Dascalu Laurentiu
  5. This program is free software; you can redistribute it and
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 3
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. */
  17. #ifndef BINARY_SEARCH_TREE_H_
  18. #define BINARY_SEARCH_TREE_H_
  19. #include "platform.h"
  20. #include "common.h"
  21. #include "binary_tree_common.h"
  22. /*!
  23. * @see binary_tree_new
  24. */
  25. DECLSPEC binary_search_tree_t binary_search_tree_new(void *key);
  26. /*!
  27. * @see binary_tree_delete_node
  28. */
  29. DECLSPEC void binary_search_tree_delete_node(binary_search_tree_node_t node);
  30. /*!
  31. * @see binary_tree_delete
  32. */
  33. DECLSPEC void binary_search_tree_delete(binary_search_tree_t bst);
  34. /*!
  35. * @see binary_tree_is_empty
  36. */
  37. DECLSPEC char binary_search_tree_empty(binary_search_tree_t bst);
  38. /*!
  39. * @see binary_tree_is_leaf
  40. */
  41. DECLSPEC char binary_search_tree_leaf(binary_search_tree_t bst);
  42. /*!
  43. * @see binary_tree_get_size
  44. */
  45. DECLSPEC int binary_search_tree_get_size(binary_search_tree_t bst);
  46. /*!
  47. * @see binary_tree_get_root
  48. */
  49. DECLSPEC binary_search_tree_t
  50. binary_search_tree_get_root(binary_search_tree_node_t node);
  51. /*!
  52. * @see binary_tree_get_key
  53. */
  54. DECLSPEC void *binary_search_tree_get_key(binary_search_tree_node_t node);
  55. /*!
  56. * @see binary_tree_get_parent
  57. */
  58. DECLSPEC binary_search_tree_node_t binary_search_tree_get_parent(
  59. binary_search_tree_node_t node);
  60. /*!
  61. * @see binary_tree_get_left_child
  62. */
  63. DECLSPEC binary_search_tree_node_t binary_search_tree_get_left_child(
  64. binary_search_tree_node_t node);
  65. /*!
  66. * @see binary_tree_get_right_child
  67. */
  68. DECLSPEC binary_search_tree_node_t binary_search_tree_get_right_child(
  69. binary_search_tree_node_t node);
  70. /*!
  71. * @see binary_tree_split
  72. */
  73. DECLSPEC void binary_search_tree_split(binary_search_tree_t parent,
  74. binary_search_tree_t *left, binary_search_tree_t *right);
  75. /*!
  76. * @see binary_tree_get_height
  77. */
  78. DECLSPEC int binary_search_tree_get_height(binary_search_tree_t tree);
  79. /*!
  80. * @see binary_tree_get_depth
  81. */
  82. DECLSPEC int binary_search_tree_get_depth(binary_search_tree_node_t node);
  83. /*!
  84. * @brief Searches key in binary search tree and returns the node reference.
  85. * Complexity O(log(n)); worst case O(n)
  86. *
  87. * @param Binary search tree
  88. * @param Key
  89. * @param Pointer to comparison function
  90. */
  91. DECLSPEC binary_search_tree_node_t binary_search_tree_search(binary_search_tree_t bst,
  92. void *key, int compare(void *x, void *y, void *context), void *context);
  93. /*!
  94. * @brief Inserts a key in binary search tree and returns a pointer to the added tree node.
  95. * Complexity O(log(n)); worst case O(n)
  96. *
  97. * @param Binary search tree
  98. * @param Key
  99. * @param Pointer to comparison function
  100. * @param Comparison context
  101. */
  102. DECLSPEC binary_search_tree_node_t binary_search_tree_insert(binary_search_tree_t bst,
  103. void *key, int compare(void *x, void *y, void *context), void *context);
  104. /*!
  105. * @brief Removes a key from binary search tree. Returns the replaced binary
  106. * search tree node. This is very useful for algorithm on advanced binary search
  107. * tree structures as AVL or red-black.
  108. *
  109. * In order to remove memory leaks, free the returned pointer
  110. *
  111. * Complexity O(log(n)); worst case O(n)
  112. *
  113. * @param Address of binary search tree
  114. * @param Key
  115. * @param Pointer to comparison function
  116. * @param Comparison context
  117. * @param Pointer to deleted node from tree
  118. */
  119. DECLSPEC binary_search_tree_node_t binary_search_tree_remove(binary_search_tree_t *bst,
  120. void *key, int compare(void *x, void *y, void *context), void *context,
  121. binary_search_tree_t *freed);
  122. /*!
  123. * @brief Returns the minimum node from binary search tree.
  124. * Complexity O(log(n)); worst case O(n)
  125. *
  126. * @param Binary search tree
  127. */
  128. DECLSPEC binary_search_tree_node_t binary_search_tree_get_min(binary_search_tree_t bst);
  129. /*!
  130. * @brief Returns the maximum node from binary search tree.
  131. * Complexity O(log(n)); worst case O(n)
  132. *
  133. * @param Binary search tree
  134. */
  135. DECLSPEC binary_search_tree_node_t binary_search_tree_get_max(binary_search_tree_t bst);
  136. /*!
  137. * @brief Returns the next node traversing in inorder.
  138. * Complexity O(log(n)); worst case O(n)
  139. *
  140. * @param Binary search tree
  141. * @param Binary search tree node
  142. */
  143. DECLSPEC binary_search_tree_node_t binary_search_tree_next(binary_search_tree_t bst,
  144. binary_search_tree_node_t node);
  145. /*!
  146. * @brief Returns the previous node traversing in inorder.
  147. * Complexity O(log(n)); worst case O(n)
  148. *
  149. * @param Binary search tree
  150. * @param Binary search tree node
  151. */
  152. DECLSPEC binary_search_tree_node_t binary_search_tree_prev(binary_search_tree_t bst,
  153. binary_search_tree_node_t node);
  154. /*!
  155. * @see binary_tree_preorder
  156. */
  157. DECLSPEC void binary_search_tree_preorder(binary_search_tree_t tree, void key_handler(
  158. void *x, void *context), void *context);
  159. /*!
  160. * @see binary_tree_inorder
  161. */
  162. DECLSPEC void binary_search_tree_inorder(binary_search_tree_t tree, void key_handler(
  163. void *x, void *context), void *context);
  164. /*!
  165. * @see binary_tree_reverse_inorder
  166. */
  167. DECLSPEC void binary_search_tree_reverse_inorder(binary_search_tree_t tree,
  168. void key_handler(void *x, void *context), void *context);
  169. /*!
  170. * @see binary_tree_postorder
  171. */
  172. DECLSPEC void binary_search_tree_postorder(binary_search_tree_t tree, void key_handler(
  173. void *x, void *context), void *context);
  174. /*!
  175. * @see binary_tree_level_order
  176. */
  177. DECLSPEC void binary_search_tree_level_order(binary_search_tree_t tree,
  178. void key_handler(void *x, void *context), void *context);
  179. /*!
  180. * @see binary_tree_show_indented
  181. */
  182. DECLSPEC void binary_search_tree_show_indented(binary_search_tree_t tree,
  183. void key_handler(void *key, int level, void *context), void *context);
  184. #endif /* BINARY_SEARCH_TREE_H_ */