bitree.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #include <common/bitree.h>
  2. #include <mm/slab.h>
  3. #include <common/errno.h>
  4. #include <common/kfifo.h>
  5. #include <common/string.h>
  6. #include <debug/bug.h>
  7. #define smaller(root, a, b) (root->cmp((a)->value, (b)->value) == -1)
  8. #define equal(root, a, b) (root->cmp((a)->value, (b)->value) == 0)
  9. #define greater(root, a, b) (root->cmp((a)->value, (b)->value) == 1)
  10. /**
  11. * @brief 创建二叉搜索树
  12. *
  13. * @param node 根节点
  14. * @param cmp 比较函数
  15. * @param release 用来释放结点的value的函数
  16. * @return struct bt_root_t* 树根结构体
  17. */
  18. struct bt_root_t *bt_create_tree(struct bt_node_t *node, int (*cmp)(void *a, void *b), int (*release)(void *value))
  19. {
  20. if (node == NULL || cmp == NULL)
  21. return (void*)-EINVAL;
  22. struct bt_root_t *root = (struct bt_root_t *)kmalloc(sizeof(struct bt_root_t), 0);
  23. memset((void *)root, 0, sizeof(struct bt_root_t));
  24. root->bt_node = node;
  25. root->cmp = cmp;
  26. root->release = release;
  27. root->size = (node == NULL) ? 0 : 1;
  28. return root;
  29. }
  30. /**
  31. * @brief 创建结点
  32. *
  33. * @param left 左子节点
  34. * @param right 右子节点
  35. * @param value 当前节点的值
  36. * @return struct bt_node_t*
  37. */
  38. struct bt_node_t *bt_create_node(struct bt_node_t *left, struct bt_node_t *right, struct bt_node_t *parent, void *value)
  39. {
  40. struct bt_node_t *node = (struct bt_node_t *)kmalloc(sizeof(struct bt_node_t), 0);
  41. FAIL_ON_TO(node == NULL, nomem);
  42. memset((void *)node, 0, sizeof(struct bt_node_t));
  43. node->left = left;
  44. node->right = right;
  45. node->value = value;
  46. node->parent = parent;
  47. return node;
  48. nomem:;
  49. return (void*)-ENOMEM;
  50. }
  51. /**
  52. * @brief 插入结点
  53. *
  54. * @param root 树根结点
  55. * @param value 待插入结点的值
  56. * @return int 返回码
  57. */
  58. int bt_insert(struct bt_root_t *root, void *value)
  59. {
  60. if (root == NULL)
  61. return -EINVAL;
  62. struct bt_node_t *this_node = root->bt_node;
  63. struct bt_node_t *last_node = NULL;
  64. struct bt_node_t *insert_node = bt_create_node(NULL, NULL, NULL, value);
  65. FAIL_ON_TO((uint64_t)insert_node == (uint64_t)(-ENOMEM), failed);
  66. while (this_node != NULL)
  67. {
  68. last_node = this_node;
  69. if (smaller(root, insert_node, this_node))
  70. this_node = this_node->left;
  71. else
  72. this_node = this_node->right;
  73. }
  74. insert_node->parent = last_node;
  75. if (unlikely(last_node == NULL))
  76. root->bt_node = insert_node;
  77. else
  78. {
  79. if (smaller(root, insert_node, last_node))
  80. last_node->left = insert_node;
  81. else
  82. last_node->right = insert_node;
  83. }
  84. ++root->size;
  85. return 0;
  86. failed:;
  87. return -ENOMEM;
  88. }
  89. /**
  90. * @brief 搜索值为value的结点
  91. *
  92. * @param value 值
  93. * @param ret_addr 返回的结点基地址
  94. * @return int 错误码
  95. */
  96. int bt_query(struct bt_root_t *root, void *value, uint64_t *ret_addr)
  97. {
  98. struct bt_node_t *this_node = root->bt_node;
  99. struct bt_node_t tmp_node = {0};
  100. tmp_node.value = value;
  101. // 如果返回地址为0
  102. if (ret_addr == NULL)
  103. return -EINVAL;
  104. while (this_node != NULL && !equal(root, this_node, &tmp_node))
  105. {
  106. if (smaller(root, &tmp_node, this_node))
  107. this_node = this_node->left;
  108. else
  109. this_node = this_node->right;
  110. }
  111. if (this_node != NULL && equal(root, this_node, &tmp_node))
  112. {
  113. *ret_addr = (uint64_t)this_node;
  114. return 0;
  115. }
  116. else
  117. {
  118. // 找不到则返回-1,且addr设为0
  119. *ret_addr = NULL;
  120. return -1;
  121. }
  122. }
  123. static struct bt_node_t *bt_get_minimum(struct bt_node_t *this_node)
  124. {
  125. while (this_node->left != NULL)
  126. this_node = this_node->left;
  127. return this_node;
  128. }
  129. /**
  130. * @brief 删除结点
  131. *
  132. * @param root 树根
  133. * @param value 待删除结点的值
  134. * @return int 返回码
  135. */
  136. int bt_delete(struct bt_root_t *root, void *value)
  137. {
  138. uint64_t tmp_addr;
  139. int retval;
  140. // 寻找待删除结点
  141. retval = bt_query(root, value, &tmp_addr);
  142. if (retval != 0 || tmp_addr == NULL)
  143. return retval;
  144. struct bt_node_t *this_node = (struct bt_node_t *)tmp_addr;
  145. struct bt_node_t *to_delete = NULL, *to_delete_son = NULL;
  146. if (this_node->left == NULL || this_node->right == NULL)
  147. to_delete = this_node;
  148. else
  149. {
  150. to_delete = bt_get_minimum(this_node->right);
  151. // 释放要被删除的值,并把下一个结点的值替换上来
  152. root->release(this_node->value);
  153. this_node->value = to_delete->value;
  154. }
  155. if (to_delete->left != NULL)
  156. to_delete_son = to_delete->left;
  157. else
  158. to_delete_son = to_delete->right;
  159. if (to_delete_son != NULL)
  160. to_delete_son->parent = to_delete->parent;
  161. if (to_delete->parent == NULL)
  162. root->bt_node = to_delete_son;
  163. else
  164. {
  165. if (to_delete->parent->left == to_delete)
  166. to_delete->parent->left = to_delete_son;
  167. else
  168. to_delete->parent->right = to_delete_son;
  169. }
  170. --root->size;
  171. // 释放最终要删除的结点的对象
  172. kfree(to_delete);
  173. }
  174. /**
  175. * @brief 释放整个二叉搜索树
  176. *
  177. * @param root 树的根节点
  178. * @return int 错误码
  179. */
  180. int bt_destroy_tree(struct bt_root_t *root)
  181. {
  182. // 新建一个kfifo缓冲区,将指向结点的指针存入fifo队列
  183. // 注:为了将指针指向的地址存入队列,我们需要对指针取地址
  184. struct kfifo_t fifo;
  185. kfifo_alloc(&fifo, ((root->size + 1) / 2) * sizeof(struct bt_node_t *), 0);
  186. kfifo_in(&fifo, (void *)&(root->bt_node), sizeof(struct bt_node_t *));
  187. // bfs
  188. while (!kfifo_empty(&fifo))
  189. {
  190. // 取出队列头部的结点指针
  191. struct bt_node_t *nd;
  192. int count = kfifo_out(&fifo, &nd, sizeof(uint64_t));
  193. // 将子节点加入队列
  194. if (nd->left != NULL)
  195. kfifo_in(&fifo, (void *)&(nd->left), sizeof(struct bt_node_t *));
  196. if (nd->right != NULL)
  197. kfifo_in(&fifo, (void *)&(nd->right), sizeof(struct bt_node_t *));
  198. // 销毁当前节点
  199. root->release(nd->value);
  200. kfree(nd);
  201. }
  202. kfifo_free_alloc(&fifo);
  203. return 0;
  204. }