二叉搜索树: create、 insert

This commit is contained in:
fslongjin
2022-07-25 15:46:32 +08:00
parent 2b0b727893
commit eead936244
5 changed files with 188 additions and 12 deletions

54
kernel/common/bitree.h Normal file
View File

@ -0,0 +1,54 @@
#pragma once
struct bt_node_t
{
struct bt_node_t *left;
struct bt_node_t *right;
struct bt_node_t *parent;
void *value; // 数据
} __attribute__((aligned(sizeof(long))));
struct bt_root_t
{
struct bt_node_t *bt_node;
int (*cmp)(struct bt_node_t *a, struct bt_node_t *b); // 比较函数 a>b 返回1 a==b返回0, a<b返回-1
};
/**
* @brief 创建二叉搜索树
*
* @param node 根节点
* @param cmp 比较函数
* @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));
/**
* @brief 创建结点
*
* @param left 左子节点
* @param right 右子节点
* @param value 当前节点的值
* @return struct bt_node_t*
*/
struct bt_node_t *bt_create_node(struct bt_node_t *left, struct bt_node_t *right, struct bt_node_t *parent, void *value);
/**
* @brief 插入结点
*
* @param root 树根结点
* @param value 待插入结点的值
* @return int 返回码
*/
int bt_insert(struct bt_root_t *root, void *value);
/**
* @brief 搜索值为value的结点
*
* @param root 树根结点
* @param value 值
* @param ret_addr 返回的结点基地址
* @return int 错误码
*/
int bt_query(struct bt_root_t *root, void *value, uint64_t *ret_addr);