binary_tree.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*!
  2. Temelia - Binary tree interface.
  3. Copyright (C) 2008, 2009 Ceata
  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 BINARYTREE_H_
  18. #define BINARYTREE_H_
  19. #include "platform.h"
  20. #include "binary_tree_common.h"
  21. /*!
  22. * @brief Constructor - creates a empty tree. Call with NULL if you want an empty tree.
  23. * Complexity O(1)
  24. *
  25. * @param Key
  26. */
  27. DECLSPEC binary_tree_t binary_tree_new(void *key);
  28. /*!
  29. * @brief Destructor - frees the memory occupied by the root node. It's useful when
  30. * the tree contains only one node.
  31. * Complexity O(1)
  32. *
  33. * @param Binary tree
  34. */
  35. DECLSPEC void binary_tree_delete_node(binary_tree_node_t node);
  36. /*!
  37. * @brief Destructor - frees the memory occupied by a tree.
  38. * Complexity O(size)
  39. *
  40. * @param Binary tree
  41. */
  42. DECLSPEC void binary_tree_delete(binary_tree_t tree);
  43. /*!
  44. * @brief Compares two binary trees. It return 0 if they are equal and another value if they aren't,
  45. * depending on the comparison function.
  46. * Complexity O(max(size1, size2))
  47. *
  48. * @param Pointer to first binary tree
  49. * @param Pointer to second binary tree
  50. * @param Pointer to comparison function
  51. */
  52. DECLSPEC int binary_tree_compare_trees(binary_tree_t bt1, binary_tree_t bt2,
  53. int compare(void *x, void *y, void *context), void *context);
  54. /*!
  55. * @brief Checks if tree is empty.
  56. * Complexity O(1)
  57. *
  58. * @param Binary tree
  59. */
  60. DECLSPEC int binary_tree_is_empty(binary_tree_t tree);
  61. /*!
  62. * @brief Checks if node is leaf.
  63. * Complexity O(1)
  64. *
  65. * @param Binary tree node
  66. */
  67. DECLSPEC int binary_tree_is_leaf(binary_tree_t node);
  68. /*!
  69. * @brief Returns the nodes number of tree.
  70. * Complexity O(size)
  71. *
  72. * @param Binary tree
  73. */
  74. DECLSPEC int binary_tree_get_size(binary_tree_t tree);
  75. /*!
  76. * @brief Returns root of a tree which contains node.
  77. * Complexity O(size)
  78. *
  79. * @param Binary tree
  80. */
  81. DECLSPEC binary_tree_t binary_tree_get_root(binary_tree_t node);
  82. /*!
  83. * @brief Returns the key contained by node.
  84. * Complexity O(1)
  85. *
  86. * @param Binary tree node
  87. */
  88. DECLSPEC void *binary_tree_get_key(binary_tree_t node);
  89. /*!
  90. * @brief Sets the key of node.
  91. * Complexity O(1)
  92. *
  93. * @param Binary tree node
  94. * @param Key
  95. */
  96. DECLSPEC void binary_tree_set_key(binary_tree_t node, void *key);
  97. /*!
  98. * @brief Returns the parent of node.
  99. * Complexity O(1)
  100. *
  101. * @param Binary tree node
  102. */
  103. DECLSPEC binary_tree_t binary_tree_get_parent(binary_tree_t node);
  104. /*!
  105. * @brief Sets parent_node to be the parent of node.
  106. * If dir=-1 then node will be the left child of parent
  107. * else if dir=1 node will be the it's right child.
  108. * Complexity O(1)
  109. *
  110. * @param Child binary tree node.
  111. * @param Parent binary tree node
  112. * @param Direction
  113. */
  114. DECLSPEC void binary_tree_set_parent(binary_tree_t node, binary_tree_t parent, int dir);
  115. /*!
  116. * @brief Returns left child of node.
  117. * Complexity O(1)
  118. *
  119. * @param Binary tree node
  120. */
  121. DECLSPEC binary_tree_t binary_tree_get_left_child(binary_tree_t node);
  122. /*!
  123. * @brief Sets left child of node.
  124. * Complexity O(1)
  125. *
  126. * @param Binary tree node
  127. * @param Child binary tree node
  128. */
  129. DECLSPEC void binary_tree_set_left_child(binary_tree_t node, binary_tree_t child);
  130. /*!
  131. * @brief Returns right child of node.
  132. * Complexity O(1)
  133. *
  134. * @param Binary tree node
  135. */
  136. DECLSPEC binary_tree_t binary_tree_get_right_child(binary_tree_t node);
  137. /*!
  138. * @brief Sets right child of node.
  139. * Complexity O(1)
  140. *
  141. * @param Binary tree node
  142. * @param Child node
  143. */
  144. DECLSPEC void binary_tree_set_right_child(binary_tree_t node, binary_tree_t child);
  145. /*!
  146. * @brief Joins two trees called left and right into a single tree called parent.
  147. * Complexity O(1)
  148. *
  149. * @param Parent binary tree
  150. * @param Right binary tree
  151. * @param Left binary tree
  152. */
  153. DECLSPEC void binary_tree_join(binary_tree_t parent, binary_tree_t left,
  154. binary_tree_t right);
  155. /*!
  156. * @brief Splits a tree into two trees.
  157. * Complexity O(1)
  158. *
  159. * @param Current tree
  160. * @param Left binary tree
  161. * @param Right binary tree
  162. */
  163. DECLSPEC void binary_tree_split(binary_tree_t parent, binary_tree_t *left,
  164. binary_tree_t *right);
  165. /*!
  166. * @brief Creates a new node containing key, with children nodes left and right and with
  167. * parent node parent. If dir == -1 then the new node will be parent's left child, else
  168. * if dir == 1 the new node will be parent's right child.
  169. * Complexity O(1)
  170. *
  171. * @param Parent binary tree node
  172. * @param Left binary tree child
  173. * @param Right binary tree child
  174. * @param Key
  175. * @param Direction
  176. */
  177. DECLSPEC binary_tree_t binary_tree_create_node(binary_tree_t parent, binary_tree_t left,
  178. binary_tree_t right, void *key, int dir);
  179. /*!
  180. * @brief Returns the height of tree.
  181. * Complexity O(size)
  182. *
  183. * @param Binary tree
  184. */
  185. DECLSPEC int binary_tree_get_height(binary_tree_t tree);
  186. /*!
  187. * @brief Returns the depth of node, in a tree.
  188. * Complexity O(size)
  189. *
  190. * @param Binary tree node
  191. */
  192. DECLSPEC int binary_tree_get_depth(binary_tree_t node);
  193. /*!
  194. * @brief Preorder : Root Left Right.
  195. * Complexity O(size)
  196. *
  197. * @param Binary tree
  198. * @param Pointer to iterating function
  199. * @param Context
  200. */
  201. DECLSPEC void binary_tree_preorder(binary_tree_t tree, void key_handler(void *key,
  202. void *context), void *context);
  203. /*!
  204. * @brief Inorder : Left Root Right.
  205. * Complexity O(size)
  206. *
  207. * @param Binary tree
  208. * @param Pointer to printing function
  209. * @param Context
  210. */
  211. DECLSPEC void binary_tree_inorder(binary_tree_t tree, void key_handler(void *key,
  212. void *context), void *context);
  213. /*!
  214. * @brief Reverse inorder : Right Root Left.
  215. * Complexity O(size)
  216. *
  217. * @param Binary tree
  218. * @param Pointer to printing function
  219. * @param Context
  220. */
  221. DECLSPEC void binary_tree_reverse_inorder(binary_tree_t tree, void key_handler(
  222. void *key, void *context), void *context);
  223. /*!
  224. * @brief Postorder : Left Right Root.
  225. * Complexity O(size)
  226. *
  227. * @param Binary tree
  228. * @param Pointer to printing function
  229. * @param Context
  230. */
  231. DECLSPEC void binary_tree_postorder(binary_tree_t tree, void key_handler(void *key,
  232. void *context), void *context);
  233. /*!
  234. * @brief Level order.
  235. * Complexity O(size)
  236. *
  237. * @param Binary tree
  238. * @param Pointer to printing function
  239. * @param Context
  240. */
  241. DECLSPEC void binary_tree_level_order(binary_tree_t tree, void key_handler(void *key,
  242. void *context), void *context);
  243. /*!
  244. * @brief Indented show.
  245. * Complexity O(size)
  246. *
  247. * @param Binary tree
  248. * @param Pointer to printing function
  249. * @param Context
  250. */
  251. DECLSPEC void binary_tree_show_indented(binary_tree_t tree, void key_handler(void *key,
  252. int level, void *context), void *context);
  253. #endif /* BINARY_TREE_H */