mirror of
https://github.com/DragonOS-Community/DragonOS.git
synced 2025-06-20 18:26:32 +00:00
二叉搜索树: create、 insert
This commit is contained in:
54
kernel/common/bitree.h
Normal file
54
kernel/common/bitree.h
Normal 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);
|
Reference in New Issue
Block a user