mirror of
https://github.com/DragonOS-Community/DragonOS.git
synced 2025-06-09 23:46:48 +00:00
fix: 修正未暴露delete接口的问题
This commit is contained in:
parent
870ae59646
commit
a704fe5cfe
@ -12,9 +12,10 @@
|
|||||||
*
|
*
|
||||||
* @param node 根节点
|
* @param node 根节点
|
||||||
* @param cmp 比较函数
|
* @param cmp 比较函数
|
||||||
|
* @param release 用来释放结点的value的函数
|
||||||
* @return struct bt_root_t* 树根结构体
|
* @return struct bt_root_t* 树根结构体
|
||||||
*/
|
*/
|
||||||
struct bt_root_t *bt_create_tree(struct bt_node_t *node, int (*cmp)(struct bt_node_t *a, struct bt_node_t *b))
|
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))
|
||||||
{
|
{
|
||||||
if (node == NULL || cmp == NULL)
|
if (node == NULL || cmp == NULL)
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
@ -23,6 +24,8 @@ struct bt_root_t *bt_create_tree(struct bt_node_t *node, int (*cmp)(struct bt_no
|
|||||||
memset((void *)root, 0, sizeof(struct bt_root_t));
|
memset((void *)root, 0, sizeof(struct bt_root_t));
|
||||||
root->bt_node = node;
|
root->bt_node = node;
|
||||||
root->cmp = cmp;
|
root->cmp = cmp;
|
||||||
|
root->release = release;
|
||||||
|
root->size = (node == NULL) ? 0 : 1;
|
||||||
|
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
@ -86,7 +89,7 @@ int bt_insert(struct bt_root_t *root, void *value)
|
|||||||
else
|
else
|
||||||
last_node->right = insert_node;
|
last_node->right = insert_node;
|
||||||
}
|
}
|
||||||
|
++root->size;
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
failed:;
|
failed:;
|
||||||
@ -181,6 +184,19 @@ int bt_delete(struct bt_root_t *root, void *value)
|
|||||||
to_delete->parent->right = to_delete_son;
|
to_delete->parent->right = to_delete_son;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
--root->size;
|
||||||
// 释放最终要删除的结点的对象
|
// 释放最终要删除的结点的对象
|
||||||
kfree(to_delete);
|
kfree(to_delete);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 释放整个二叉搜索树
|
||||||
|
*
|
||||||
|
* @param root 树的根节点
|
||||||
|
* @return int 错误码
|
||||||
|
*/
|
||||||
|
int bt_destroy_tree(struct bt_root_t *root)
|
||||||
|
{
|
||||||
|
// todo: 待kfifo完成后,使用kfifo队列来辅助destroy
|
||||||
|
return -1;
|
||||||
|
}
|
@ -13,6 +13,7 @@ struct bt_node_t
|
|||||||
struct bt_root_t
|
struct bt_root_t
|
||||||
{
|
{
|
||||||
struct bt_node_t *bt_node;
|
struct bt_node_t *bt_node;
|
||||||
|
int32_t size; // 树中的元素个数
|
||||||
int (*cmp)(struct bt_node_t *a, struct bt_node_t *b); // 比较函数 a>b 返回1, a==b返回0, a<b返回-1
|
int (*cmp)(struct bt_node_t *a, struct bt_node_t *b); // 比较函数 a>b 返回1, a==b返回0, a<b返回-1
|
||||||
/**
|
/**
|
||||||
* @brief 释放结点的value的函数
|
* @brief 释放结点的value的函数
|
||||||
@ -26,9 +27,10 @@ struct bt_root_t
|
|||||||
*
|
*
|
||||||
* @param node 根节点
|
* @param node 根节点
|
||||||
* @param cmp 比较函数
|
* @param cmp 比较函数
|
||||||
|
* @param release 用来释放结点的value的函数
|
||||||
* @return struct bt_root_t* 树根结构体
|
* @return struct bt_root_t* 树根结构体
|
||||||
*/
|
*/
|
||||||
struct bt_root_t *bt_create_tree(struct bt_node_t *node, int (*cmp)(struct bt_node_t *a, struct bt_node_t *b));
|
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));
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 创建结点
|
* @brief 创建结点
|
||||||
@ -58,3 +60,20 @@ int bt_insert(struct bt_root_t *root, void *value);
|
|||||||
* @return int 错误码
|
* @return int 错误码
|
||||||
*/
|
*/
|
||||||
int bt_query(struct bt_root_t *root, void *value, uint64_t *ret_addr);
|
int bt_query(struct bt_root_t *root, void *value, uint64_t *ret_addr);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 删除结点
|
||||||
|
*
|
||||||
|
* @param root 树根
|
||||||
|
* @param value 待删除结点的值
|
||||||
|
* @return int 返回码
|
||||||
|
*/
|
||||||
|
int bt_delete(struct bt_root_t *root, void *value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 释放整个二叉搜索树
|
||||||
|
*
|
||||||
|
* @param root
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
int bt_destroy_tree(struct bt_root_t *root);
|
Loading…
x
Reference in New Issue
Block a user