From f1dd1c0812c4d0ca124e86caaaaeacb60f7c8448 Mon Sep 17 00:00:00 2001 From: fslongjin Date: Sat, 30 Jul 2022 12:16:21 +0800 Subject: [PATCH] new: bitree_destroy --- kernel/common/bitree.c | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/kernel/common/bitree.c b/kernel/common/bitree.c index 224c6b51..3a632951 100644 --- a/kernel/common/bitree.c +++ b/kernel/common/bitree.c @@ -1,6 +1,7 @@ #include "bitree.h" #include #include +#include #include #define smaller(root, a, b) (root->cmp((a)->value, (b)->value) == -1) @@ -201,6 +202,34 @@ int bt_delete(struct bt_root_t *root, void *value) */ int bt_destroy_tree(struct bt_root_t *root) { - // todo: 待kfifo完成后,使用kfifo队列来辅助destroy - return -1; + // 新建一个kfifo缓冲区,将指向结点的指针存入fifo队列 + // 注:为了将指针指向的地址存入队列,我们需要对指针取地址 + struct kfifo_t fifo; + kfifo_alloc(&fifo, ((root->size + 1) / 2) * sizeof(struct bt_node_t *), 0); + kfifo_in(&fifo, (void *)&(root->bt_node), sizeof(struct bt_node_t *)); + + // bfs + while (!kfifo_empty(&fifo)) + { + // 取出队列头部的结点指针 + struct bt_node_t *nd; + uint64_t res; + int count = kfifo_out(&fifo, &res, sizeof(uint64_t)); + nd = (struct bt_node_t *)res; + + // 将子节点加入队列 + if (nd->left != NULL) + kfifo_in(&fifo, (void *)&(nd->left), sizeof(struct bt_node_t *)); + + if (nd->right != NULL) + kfifo_in(&fifo, (void *)&(nd->right), sizeof(struct bt_node_t *)); + + // 销毁当前节点 + root->release(nd->value); + kfree(nd); + } + + kfifo_free_alloc(&fifo); + + return 0; } \ No newline at end of file