bitree.c 5.0 KB

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