b_tree.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*!
  2. Temelia - B-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 B_TREE_H_
  18. #define B_TREE_H_
  19. #include "platform.h"
  20. #include "common.h"
  21. struct _b_tree_t;
  22. typedef struct _b_tree_t *b_tree_t;
  23. struct _b_tree_node_t;
  24. typedef struct _b_tree_node_t *b_tree_node_t;
  25. /*!
  26. * @brief Constructor - returns an empty b-tree with given degree
  27. * Complexity O(1)
  28. *
  29. * @param Degree
  30. */
  31. DECLSPEC b_tree_t b_tree_new(int degree);
  32. /*!
  33. * @brief Destructor - frees the memory occupied by this b-tree
  34. * @param B-tree
  35. */
  36. DECLSPEC void b_tree_delete(b_tree_t b_tree);
  37. /*!
  38. * @brief Returns the root of current b-tree.
  39. * Complexity O(1)
  40. *
  41. * @param B-tree
  42. */
  43. DECLSPEC b_tree_node_t b_tree_get_root(b_tree_t b_tree);
  44. /*!
  45. * @brief Returns the degree of current b-tree.
  46. * Complexity O(1)
  47. *
  48. * @param B-tree
  49. */
  50. DECLSPEC int b_tree_get_degree(b_tree_t b_tree);
  51. /*!
  52. * @brief Returns the height of current b-tree.
  53. * Complexity O(log(n))
  54. *
  55. * @param B-tree
  56. */
  57. DECLSPEC int b_tree_get_height(b_tree_t b_tree);
  58. /*!
  59. * @brief Returns the depth of current node in it's b-tree.
  60. * Complexity O(log(n))
  61. *
  62. * @param B-tree node
  63. */
  64. DECLSPEC int b_tree_node_get_depth(b_tree_node_t node);
  65. /*!
  66. * @brief Returns the node's parent.
  67. * Complexity O(1)
  68. *
  69. * @param B-tree node
  70. */
  71. DECLSPEC b_tree_node_t b_tree_node_get_parent(b_tree_node_t node);
  72. /*!
  73. * @brief Returns the node's children number.
  74. * Complexity O(1)
  75. *
  76. * @param B-tree node
  77. */
  78. DECLSPEC int b_tree_node_get_children_number(b_tree_node_t node);
  79. /*!
  80. * @brief Returns node's child given by it's index.
  81. * Complexity O(1)
  82. *
  83. * @param B-tree node
  84. * @param Child index in children list.
  85. */
  86. DECLSPEC b_tree_node_t b_tree_node_get_child(b_tree_node_t node, int index);
  87. /*!
  88. * @brief Returns node's keys number.
  89. * Complexity O(1)
  90. *
  91. * @param B-tree node
  92. */
  93. DECLSPEC int b_tree_node_get_keys_number(b_tree_node_t node);
  94. /*!
  95. * @brief Returns node's key given by it's index.
  96. * Complexity O(1)
  97. *
  98. * @param B-tree node
  99. * @param Child index
  100. */
  101. DECLSPEC void *b_tree_node_get_key(b_tree_node_t node, int index);
  102. /*!
  103. * @brief Returns b-tree's size.
  104. * Complexity O(1)
  105. *
  106. * @param B-tree
  107. */
  108. DECLSPEC int b_tree_get_size(b_tree_t b_tree);
  109. /*!
  110. * @brief Searches given key in current b-tree; returns a reference
  111. * to the node storing the key and NULL otherwise.
  112. * Complexity O(log(n))
  113. *
  114. * @param B-tree
  115. * @param Key
  116. * @param Pointer to comparison function
  117. * @param Comparison context
  118. */
  119. DECLSPEC b_tree_node_t b_tree_search(b_tree_t b_tree, void *key, int compare(void *x,
  120. void *y, void *context), void *context);
  121. /*!
  122. * @brief Inserts key in current b-tree; returns a reference to new added
  123. * node.
  124. * Complexity O(log(n))
  125. *
  126. * @param Key reference
  127. * @param Pointer to comparison function
  128. * @param Add if the key already exists in b-tree : 0 if you don't want this to happen
  129. * and not 0 if you want to add an equal key
  130. */
  131. DECLSPEC b_tree_node_t b_tree_insert(b_tree_t b_tree, void *key, int compare(void *x,
  132. void *y, void *context), void *context, char add_if_exists);
  133. /*!
  134. * @brief Removes key from current b-tree. Returns 1 if it successfully
  135. * deletes key else it returns false.
  136. * Complexity O(log(n))
  137. *
  138. * @param Key
  139. * @param Pointer to comparison function
  140. * @param Comparison context
  141. */
  142. DECLSPEC int b_tree_remove(b_tree_t b_tree, void *key, int compare(void *x, void *y,
  143. void *context), void *context);
  144. /*!
  145. * @brief Returns the minimum node from current b-tree.
  146. * Complexity O(log(n))
  147. *
  148. * @param B-tree
  149. */
  150. DECLSPEC b_tree_node_t b_tree_get_min(b_tree_t b_tree);
  151. /*!
  152. * @brief Returns the minimum key in current b-tree.
  153. * Complexity O(log(n))
  154. *
  155. * @param B-tree
  156. */
  157. DECLSPEC void *b_tree_get_min_key(b_tree_t b_tree);
  158. /*!
  159. * @brief Returns the maximum node from current b-tree.
  160. * Complexity O(log(n))
  161. *
  162. * @param B-tree
  163. */
  164. DECLSPEC b_tree_node_t b_tree_get_max(b_tree_t b_tree);
  165. /*!
  166. * @brief Returns the maximum key in current b-tree.
  167. * Complexity O(log(n))
  168. *
  169. * @param B-tree
  170. */
  171. DECLSPEC void *b_tree_get_max_key(b_tree_t b_tree);
  172. /*!
  173. * @brief Prints the b-tree keys in level order. It's used in debugging.
  174. * If you want to iterate over b-tree's keys, use the functions that gives
  175. * you access to keys, parent and children of b-tree's root.
  176. *
  177. * Also, you can force the pointer to stream (FILE *) to be a generic pointer;
  178. * the library is not using that pointer so it's up to you what it reference.
  179. *
  180. * Complexity O(size)
  181. *
  182. * @param B-tree
  183. * @param Pointer to key printing function
  184. * @param Context
  185. */
  186. DECLSPEC void b_tree_level_order(b_tree_t b_tree, void key_handler(void *x,
  187. void *context), void *context);
  188. /*!
  189. * @brief Prints indented the b-tree keys.
  190. * Complexity O(size)
  191. *
  192. * @see b_tree_level_order
  193. */
  194. DECLSPEC void b_tree_show_indented(b_tree_t b_tree, void key_handler(void *key,
  195. int level, void *context), void *context);
  196. #endif /* B_TREE_H_ */