rbtree.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #ifndef __PJ_RBTREE_H__
  20. #define __PJ_RBTREE_H__
  21. /**
  22. * @file rbtree.h
  23. * @brief Red/Black Tree
  24. */
  25. #include <pj/types.h>
  26. PJ_BEGIN_DECL
  27. /**
  28. * @defgroup PJ_RBTREE Red/Black Balanced Tree
  29. * @ingroup PJ_DS
  30. * @brief
  31. * Red/Black tree is the variant of balanced tree, where the search, insert,
  32. * and delete operation is \b guaranteed to take at most \a O( lg(n) ).
  33. * @{
  34. */
  35. /**
  36. * Color type for Red-Black tree.
  37. */
  38. typedef enum pj_rbcolor_t
  39. {
  40. PJ_RBCOLOR_BLACK,
  41. PJ_RBCOLOR_RED
  42. } pj_rbcolor_t;
  43. /**
  44. * The type of the node of the R/B Tree.
  45. */
  46. typedef struct pj_rbtree_node
  47. {
  48. /** Pointers to the node's parent. */
  49. struct pj_rbtree_node *parent;
  50. /** Pointers to the node's left sibling. */
  51. struct pj_rbtree_node *left;
  52. /** Pointers to the node's right sibling. */
  53. struct pj_rbtree_node *right;
  54. /** Key associated with the node. */
  55. const void *key;
  56. /** User data associated with the node. */
  57. void *user_data;
  58. /** The R/B Tree node color. */
  59. pj_rbcolor_t color;
  60. } pj_rbtree_node;
  61. /**
  62. * The type of function use to compare key value of tree node.
  63. * @return
  64. * 0 if the keys are equal
  65. * <0 if key1 is lower than key2
  66. * >0 if key1 is greater than key2.
  67. */
  68. typedef int pj_rbtree_comp(const void *key1, const void *key2);
  69. /**
  70. * Declaration of a red-black tree. All elements in the tree must have UNIQUE
  71. * key.
  72. * A red black tree always maintains the balance of the tree, so that the
  73. * tree height will not be greater than lg(N). Insert, search, and delete
  74. * operation will take lg(N) on the worst case. But for insert and delete,
  75. * there is additional time needed to maintain the balance of the tree.
  76. */
  77. typedef struct pj_rbtree
  78. {
  79. pj_rbtree_node null_node; /**< Constant to indicate NULL node. */
  80. pj_rbtree_node *null; /**< Constant to indicate NULL node. */
  81. pj_rbtree_node *root; /**< Root tree node. */
  82. unsigned size; /**< Number of elements in the tree. */
  83. pj_rbtree_comp *comp; /**< Key comparison function. */
  84. } pj_rbtree;
  85. /**
  86. * Guidance on how much memory required for each of the node.
  87. */
  88. #define PJ_RBTREE_NODE_SIZE (sizeof(pj_rbtree_node))
  89. /**
  90. * Guidance on memory required for the tree.
  91. */
  92. #define PJ_RBTREE_SIZE (sizeof(pj_rbtree))
  93. /**
  94. * Initialize the tree.
  95. * @param tree the tree to be initialized.
  96. * @param comp key comparison function to be used for this tree.
  97. */
  98. PJ_DECL(void) pj_rbtree_init( pj_rbtree *tree, pj_rbtree_comp *comp);
  99. /**
  100. * Get the first element in the tree.
  101. * The first element always has the least value for the key, according to
  102. * the comparison function.
  103. * @param tree the tree.
  104. * @return the tree node, or NULL if the tree has no element.
  105. */
  106. PJ_DECL(pj_rbtree_node*) pj_rbtree_first( pj_rbtree *tree );
  107. /**
  108. * Get the last element in the tree.
  109. * The last element always has the greatest key value, according to the
  110. * comparison function defined for the tree.
  111. * @param tree the tree.
  112. * @return the tree node, or NULL if the tree has no element.
  113. */
  114. PJ_DECL(pj_rbtree_node*) pj_rbtree_last( pj_rbtree *tree );
  115. /**
  116. * Get the successive element for the specified node.
  117. * The successive element is an element with greater key value.
  118. * @param tree the tree.
  119. * @param node the node.
  120. * @return the successive node, or NULL if the node has no successor.
  121. */
  122. PJ_DECL(pj_rbtree_node*) pj_rbtree_next( pj_rbtree *tree,
  123. pj_rbtree_node *node );
  124. /**
  125. * The the previous node for the specified node.
  126. * The previous node is an element with less key value.
  127. * @param tree the tree.
  128. * @param node the node.
  129. * @return the previous node, or NULL if the node has no previous node.
  130. */
  131. PJ_DECL(pj_rbtree_node*) pj_rbtree_prev( pj_rbtree *tree,
  132. pj_rbtree_node *node );
  133. /**
  134. * Insert a new node.
  135. * The node will be inserted at sorted location. The key of the node must
  136. * be UNIQUE, i.e. it hasn't existed in the tree.
  137. * @param tree the tree.
  138. * @param node the node to be inserted.
  139. * @return zero on success, or -1 if the key already exist.
  140. */
  141. PJ_DECL(int) pj_rbtree_insert( pj_rbtree *tree,
  142. pj_rbtree_node *node );
  143. /**
  144. * Find a node which has the specified key.
  145. * @param tree the tree.
  146. * @param key the key to search.
  147. * @return the tree node with the specified key, or NULL if the key can not
  148. * be found.
  149. */
  150. PJ_DECL(pj_rbtree_node*) pj_rbtree_find( pj_rbtree *tree,
  151. const void *key );
  152. /**
  153. * Erase a node from the tree.
  154. * @param tree the tree.
  155. * @param node the node to be erased.
  156. * @return the tree node itself.
  157. */
  158. PJ_DECL(pj_rbtree_node*) pj_rbtree_erase( pj_rbtree *tree,
  159. pj_rbtree_node *node );
  160. /**
  161. * Get the maximum tree height from the specified node.
  162. * @param tree the tree.
  163. * @param node the node, or NULL to get the root of the tree.
  164. * @return the maximum height, which should be at most lg(N)
  165. */
  166. PJ_DECL(unsigned) pj_rbtree_max_height( pj_rbtree *tree,
  167. pj_rbtree_node *node );
  168. /**
  169. * Get the minumum tree height from the specified node.
  170. * @param tree the tree.
  171. * @param node the node, or NULL to get the root of the tree.
  172. * @return the height
  173. */
  174. PJ_DECL(unsigned) pj_rbtree_min_height( pj_rbtree *tree,
  175. pj_rbtree_node *node );
  176. /**
  177. * @}
  178. */
  179. PJ_END_DECL
  180. #endif /* __PJ_RBTREE_H__ */