当前位置:   article > 正文

红黑树实现_#define rb_parent(r) ((struct rb_node *)((r)->rb_p

#define rb_parent(r) ((struct rb_node *)((r)->rb_parent_color & ~3))

红黑树概述

红黑树是一种自平衡二叉查找树,典型的用途是实现关联数组。它是复杂的,但它的操作有着良好的最坏情况运行时间,并且在实践中是高效的: 它可以在O(logn)时间内做查找,插入和删除,这里的n是树中元素的数目。红黑树是 2-3-4树的一种等同。换句话说,对于每个 2-3-4 树,都存在至少一个数据元素是同样次序的红黑树。详细的红黑树介绍参考我转载的一篇博文http://blog.csdn.net/pngynghay/article/details/8185351。本博文仅仅实现了rbtree以及如何使用rbtree。

红黑树实现

本博文红黑树的实现取自Linux内核对红黑树的实现,只是,我去掉了内核实现中对内核的依赖,使得我们可以在用户态应用程序中依然可以使用。

rbtree.h

  1. #ifndef RBTREE_H_
  2. #define RBTREE_H_
  3. #include <stdlib.h>
  4. #include <stddef.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. /*通过父结构体type中的成员member的已知地址ptr,来寻找当前ptr地址所属的父结构体type的地址*/
  8. #define container_of(ptr, type, member) ({ \
  9. const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  10. (type *)( (char *)__mptr - offsetof(type,member) );})
  11. struct rb_node {
  12. unsigned long rb_parent_color;
  13. #define RB_RED 0
  14. #define RB_BLACK 1
  15. struct rb_node *rb_right;
  16. struct rb_node *rb_left;
  17. }__attribute__((aligned(sizeof(long))));
  18. /* The alignment might seem pointless, but allegedly CRIS needs it */
  19. struct rb_root {
  20. struct rb_node *rb_node;
  21. };
  22. #define rb_parent(r) ((struct rb_node *)((r)->rb_parent_color & ~3))
  23. #define rb_color(r) ((r)->rb_parent_color & 1)
  24. #define rb_is_red(r) (!rb_color(r))
  25. #define rb_is_black(r) rb_color(r)
  26. #define rb_set_red(r) do { (r)->rb_parent_color &= ~1; } while (0)
  27. #define rb_set_black(r) do { (r)->rb_parent_color |= 1; } while (0)
  28. static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p) {
  29. rb->rb_parent_color = (rb->rb_parent_color & 3) | (unsigned long) p;
  30. }
  31. static inline void rb_set_color(struct rb_node *rb, int color) {
  32. rb->rb_parent_color = (rb->rb_parent_color & ~1) | color;
  33. }
  34. #define RB_ROOT (struct rb_root) { NULL, }
  35. #define rb_entry(ptr, type, member) container_of(ptr, type, member)
  36. #define RB_EMPTY_ROOT(root) ((root)->rb_node == NULL)
  37. #define RB_EMPTY_NODE(node) (rb_parent(node) == node)
  38. #define RB_CLEAR_NODE(node) (rb_set_parent(node, node))
  39. extern void rb_insert_color(struct rb_node *, struct rb_root *);
  40. extern void rb_erase(struct rb_node *, struct rb_root *);
  41. /* Find logical next and previous nodes in a tree */
  42. extern struct rb_node *rb_next(const struct rb_node *);
  43. extern struct rb_node *rb_prev(const struct rb_node *);
  44. extern struct rb_node *rb_first(const struct rb_root *);
  45. extern struct rb_node *rb_last(const struct rb_root *);
  46. /* Fast replacement of a single node without remove/rebalance/add/rebalance */
  47. extern void rb_replace_node(struct rb_node *victim, struct rb_node *new,
  48. struct rb_root *root);
  49. static inline void rb_link_node(struct rb_node * node, struct rb_node * parent,
  50. struct rb_node ** rb_link) {
  51. node->rb_parent_color = (unsigned long) parent;
  52. node->rb_left = node->rb_right = NULL;
  53. *rb_link = node;
  54. }
  55. #endif /* RBTREE_H_ */


rbtree.c

  1. static void __rb_rotate_left(struct rb_node *node, struct rb_root *root) {
  2. struct rb_node *right = node->rb_right;
  3. struct rb_node *parent = rb_parent(node);
  4. if ((node->rb_right = right->rb_left))
  5. rb_set_parent(right->rb_left, node);
  6. right->rb_left = node;
  7. rb_set_parent(right, parent);
  8. if (parent) {
  9. if (node == parent->rb_left)
  10. parent->rb_left = right;
  11. else
  12. parent->rb_right = right;
  13. } else
  14. root->rb_node = right;
  15. rb_set_parent(node, right);
  16. }
  17. static void __rb_rotate_right(struct rb_node *node, struct rb_root *root) {
  18. struct rb_node *left = node->rb_left;
  19. struct rb_node *parent = rb_parent(node);
  20. if ((node->rb_left = left->rb_right))
  21. rb_set_parent(left->rb_right, node);
  22. left->rb_right = node;
  23. rb_set_parent(left, parent);
  24. if (parent) {
  25. if (node == parent->rb_right)
  26. parent->rb_right = left;
  27. else
  28. parent->rb_left = left;
  29. } else
  30. root->rb_node = left;
  31. rb_set_parent(node, left);
  32. }
  33. void rb_insert_color(struct rb_node *node, struct rb_root *root) {
  34. struct rb_node *parent, *gparent;
  35. while ((parent = rb_parent(node)) && rb_is_red(parent)) {
  36. gparent = rb_parent(parent);
  37. if (parent == gparent->rb_left) {
  38. {
  39. register struct rb_node *uncle = gparent->rb_right;
  40. if (uncle && rb_is_red(uncle))
  41. {
  42. rb_set_black(uncle);
  43. rb_set_black(parent);
  44. rb_set_red(gparent);
  45. node = gparent;
  46. continue;
  47. }
  48. }
  49. if (parent->rb_right == node) {
  50. register struct rb_node *tmp;
  51. __rb_rotate_left(parent, root);
  52. tmp = parent;
  53. parent = node;
  54. node = tmp;
  55. }
  56. rb_set_black(parent);
  57. rb_set_red(gparent);
  58. __rb_rotate_right(gparent, root);
  59. } else {
  60. {
  61. register struct rb_node *uncle = gparent->rb_left;
  62. if (uncle && rb_is_red(uncle))
  63. {
  64. rb_set_black(uncle);
  65. rb_set_black(parent);
  66. rb_set_red(gparent);
  67. node = gparent;
  68. continue;
  69. }
  70. }
  71. if (parent->rb_left == node) {
  72. register struct rb_node *tmp;
  73. __rb_rotate_right(parent, root);
  74. tmp = parent;
  75. parent = node;
  76. node = tmp;
  77. }
  78. rb_set_black(parent);
  79. rb_set_red(gparent);
  80. __rb_rotate_left(gparent, root);
  81. }
  82. }
  83. rb_set_black(root->rb_node);
  84. }
  85. static void __rb_erase_color(struct rb_node *node, struct rb_node *parent,
  86. struct rb_root *root) {
  87. struct rb_node *other;
  88. while ((!node || rb_is_black(node)) && node != root->rb_node) {
  89. if (parent->rb_left == node) {
  90. other = parent->rb_right;
  91. if (rb_is_red(other)) {
  92. rb_set_black(other);
  93. rb_set_red(parent);
  94. __rb_rotate_left(parent, root);
  95. other = parent->rb_right;
  96. }
  97. if ((!other->rb_left || rb_is_black(other->rb_left))
  98. && (!other->rb_right || rb_is_black(other->rb_right))) {
  99. rb_set_red(other);
  100. node = parent;
  101. parent = rb_parent(node);
  102. } else {
  103. if (!other->rb_right || rb_is_black(other->rb_right))
  104. {
  105. rb_set_black(other->rb_left);
  106. rb_set_red(other);
  107. __rb_rotate_right(other, root);
  108. other = parent->rb_right;
  109. }
  110. rb_set_color(other, rb_color(parent));
  111. rb_set_black(parent);
  112. rb_set_black(other->rb_right);
  113. __rb_rotate_left(parent, root);
  114. node = root->rb_node;
  115. break;
  116. }
  117. } else {
  118. other = parent->rb_left;
  119. if (rb_is_red(other)) {
  120. rb_set_black(other);
  121. rb_set_red(parent);
  122. __rb_rotate_right(parent, root);
  123. other = parent->rb_left;
  124. }
  125. if ((!other->rb_left || rb_is_black(other->rb_left))
  126. && (!other->rb_right || rb_is_black(other->rb_right))) {
  127. rb_set_red(other);
  128. node = parent;
  129. parent = rb_parent(node);
  130. } else {
  131. if (!other->rb_left || rb_is_black(other->rb_left))
  132. {
  133. rb_set_black(other->rb_right);
  134. rb_set_red(other);
  135. __rb_rotate_left(other, root);
  136. other = parent->rb_left;
  137. }
  138. rb_set_color(other, rb_color(parent));
  139. rb_set_black(parent);
  140. rb_set_black(other->rb_left);
  141. __rb_rotate_right(parent, root);
  142. node = root->rb_node;
  143. break;
  144. }
  145. }
  146. }
  147. if (node)
  148. rb_set_black(node);
  149. }
  150. void rb_erase(struct rb_node *node, struct rb_root *root) {
  151. struct rb_node *child, *parent;
  152. int color;
  153. if (!node->rb_left)
  154. child = node->rb_right;
  155. else if (!node->rb_right)
  156. child = node->rb_left;
  157. else {
  158. struct rb_node *old = node, *left;
  159. node = node->rb_right;
  160. while ((left = node->rb_left) != NULL)
  161. node = left;
  162. if (rb_parent(old)) {
  163. if (rb_parent(old)->rb_left == old)
  164. rb_parent(old)->rb_left = node;
  165. else
  166. rb_parent(old)->rb_right = node;
  167. } else
  168. root->rb_node = node;
  169. child = node->rb_right;
  170. parent = rb_parent(node);
  171. color = rb_color(node);
  172. if (parent == old) {
  173. parent = node;
  174. } else {
  175. if (child)
  176. rb_set_parent(child, parent);
  177. parent->rb_left = child;
  178. node->rb_right = old->rb_right;
  179. rb_set_parent(old->rb_right, node);
  180. }
  181. node->rb_parent_color = old->rb_parent_color;
  182. node->rb_left = old->rb_left;
  183. rb_set_parent(old->rb_left, node);
  184. goto color;
  185. }
  186. parent = rb_parent(node);
  187. color = rb_color(node);
  188. if (child)
  189. rb_set_parent(child, parent);
  190. if (parent) {
  191. if (parent->rb_left == node)
  192. parent->rb_left = child;
  193. else
  194. parent->rb_right = child;
  195. } else
  196. root->rb_node = child;
  197. color: if (color == RB_BLACK
  198. )
  199. __rb_erase_color(child, parent, root);
  200. }
  201. /*
  202. * This function returns the first node (in sort order) of the tree.
  203. */
  204. struct rb_node *rb_first(const struct rb_root *root) {
  205. struct rb_node *n;
  206. n = root->rb_node;
  207. if (!n)
  208. return NULL;
  209. while (n->rb_left)
  210. n = n->rb_left;
  211. return n;
  212. }
  213. struct rb_node *rb_last(const struct rb_root *root) {
  214. struct rb_node *n;
  215. n = root->rb_node;
  216. if (!n)
  217. return NULL;
  218. while (n->rb_right)
  219. n = n->rb_right;
  220. return n;
  221. }
  222. struct rb_node *rb_next(const struct rb_node *node) {
  223. struct rb_node *parent;
  224. if (rb_parent(node) == node)
  225. return NULL;
  226. /* If we have a right-hand child, go down and then left as far
  227. as we can. */
  228. if (node->rb_right) {
  229. node = node->rb_right;
  230. while (node->rb_left)
  231. node = node->rb_left;
  232. return (struct rb_node *) node;
  233. }
  234. /* No right-hand children. Everything down and left is
  235. smaller than us, so any 'next' node must be in the general
  236. direction of our parent. Go up the tree; any time the
  237. ancestor is a right-hand child of its parent, keep going
  238. up. First time it's a left-hand child of its parent, said
  239. parent is our 'next' node. */
  240. while ((parent = rb_parent(node)) && node == parent->rb_right)
  241. node = parent;
  242. return parent;
  243. }
  244. struct rb_node *rb_prev(const struct rb_node *node) {
  245. struct rb_node *parent;
  246. if (rb_parent(node) == node)
  247. return NULL;
  248. /* If we have a left-hand child, go down and then right as far
  249. as we can. */
  250. if (node->rb_left) {
  251. node = node->rb_left;
  252. while (node->rb_right)
  253. node = node->rb_right;
  254. return (struct rb_node *) node;
  255. }
  256. /* No left-hand children. Go up till we find an ancestor which
  257. is a right-hand child of its parent */
  258. while ((parent = rb_parent(node)) && node == parent->rb_left)
  259. node = parent;
  260. return parent;
  261. }
  262. void rb_replace_node(struct rb_node *victim, struct rb_node *new,
  263. struct rb_root *root) {
  264. struct rb_node *parent = rb_parent(victim);
  265. /* Set the surrounding nodes to point to the replacement */
  266. if (parent) {
  267. if (victim == parent->rb_left)
  268. parent->rb_left = new;
  269. else
  270. parent->rb_right = new;
  271. } else {
  272. root->rb_node = new;
  273. }
  274. if (victim->rb_left)
  275. rb_set_parent(victim->rb_left, new);
  276. if (victim->rb_right)
  277. rb_set_parent(victim->rb_right, new);
  278. /* Copy the pointers/colour from the victim to the replacement */
  279. *new = *victim;
  280. }

rbtree实例

若要使用上面的rbtree,需要根据需要实现自己的rbtree插入和查询函数。本博文实现如下:

  1. //关联到红黑树的数据结构
  2. struct int_rbtree {
  3. struct rb_node rbnode;
  4. int i;
  5. };
  6. //红黑树最大节点数目
  7. #define MAX_NUM 20
  8. struct int_rbtree * int_search(struct rb_root *root, int key) {
  9. struct rb_node *node = root->rb_node;
  10. while (node) {
  11. struct int_rbtree *data = container_of(node, struct int_rbtree, rbnode);
  12. if (key < data->i)
  13. node = node->rb_left;
  14. else if (key > data->i)
  15. node = node->rb_right;
  16. else
  17. return data;
  18. }
  19. return NULL;
  20. }
  21. int int_insert(struct rb_root *root, struct int_rbtree *data) {
  22. struct rb_node **newnode = &(root->rb_node), *parent = NULL;
  23. /* Figure out where to put new node */
  24. while (*newnode) {
  25. struct int_rbtree *thisnode =
  26. container_of(*newnode, struct int_rbtree, rbnode);
  27. parent = *newnode;
  28. if (data->i < thisnode->i)
  29. newnode = &((*newnode)->rb_left);
  30. else if (data->i > thisnode->i)
  31. newnode = &((*newnode)->rb_right);
  32. else
  33. return 0;
  34. }
  35. /* Add new node and rebalance tree. */
  36. rb_link_node(&data->rbnode, parent, newnode);
  37. rb_insert_color(&data->rbnode, root);
  38. return 1;
  39. }

 测试主程序

  1. void testrbtree() {
  2. struct rb_node *node; // rb node
  3. struct rb_root root = RB_ROOT; //root node
  4. int i = 0;
  5. //insert
  6. for (i = 0; i < MAX_NUM; i = i + 2) {
  7. //分配节点,删除时需释放节点
  8. struct int_rbtree *inttree = malloc(sizeof(struct int_rbtree));
  9. memset(inttree, 0, sizeof(struct int_rbtree));
  10. inttree->i = i;
  11. int res = int_insert(&root, inttree);
  12. if (res) {
  13. printf("insert %d succeed\n", i);
  14. } else {
  15. printf("insert %d failed\n", i);
  16. }
  17. }
  18. for (i = 1; i < MAX_NUM; i = i + 2) {
  19. struct int_rbtree *inttree = malloc(sizeof(struct int_rbtree));
  20. memset(inttree, 0, sizeof(struct int_rbtree));
  21. inttree->i = i;
  22. int res = int_insert(&root, inttree);
  23. if (res) {
  24. printf("insert %d succeed\n", i);
  25. } else {
  26. printf("insert %d failed\n", i);
  27. }
  28. }
  29. //travel
  30. printf("begin to travel tree\n");
  31. for (node = rb_first(&root); node; node = rb_next(node)) {
  32. printf("key %d \n", rb_entry(node, struct int_rbtree, rbnode)->i);
  33. }
  34. printf("end to travel tree\n");
  35. //delete
  36. srand(time(NULL));
  37. int key = rand() % MAX_NUM;
  38. struct int_rbtree *data = int_search(&root, key);
  39. if (NULL != data) {
  40. rb_erase(&data->rbnode, &root);
  41. //删除时需释放节点
  42. free(data);
  43. data = NULL;
  44. printf("is going to delete key %d \n", key);
  45. } else {
  46. printf("key %d is not in the tree\n", key);
  47. return;
  48. }
  49. data = int_search(&root, key);
  50. if (NULL != data) {
  51. printf("delete key %d failed\n", key);
  52. } else {
  53. printf("delete key %d succeed\n", key);
  54. }
  55. return;
  56. }


只要在main函数中调用这个测试函数即可。

同时,有需要的朋友可以从http://download.csdn.net/detail/it_pcode/6632917下载本博文代码。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/246048
推荐阅读
相关标签
  

闽ICP备14008679号