获取树高的接口重新定义

This commit is contained in:
建峰 2025-04-27 11:05:12 +08:00
parent 932b078778
commit 59e5c9be71
2 changed files with 61 additions and 56 deletions

View File

@ -79,14 +79,14 @@ struct _tree
struct _iterator _iter; struct _iterator _iter;
bool (*rebalance)(struct _tree* self, struct _tree_node* root);
void (*_destory)(struct _tree* self); void (*_destory)(struct _tree* self);
// -------------------- public -------------------- // -------------------- public --------------------
// kernel // kernel
bool (*insert)(struct _tree* self, void* obj); bool (*insert)(struct _tree* self, void* obj);
bool (*delete)(struct _tree* self, void* obj); bool (*delete)(struct _tree* self, void* obj);
bool (*rebalance)(struct _tree* self, struct _tree_node* root); uint32_t (*height)(struct _tree* self);
int32_t (*height)(struct _tree* self, struct _tree_node* root);
// base // base
bool (*clear)(struct _tree* self); bool (*clear)(struct _tree* self);

View File

@ -12,6 +12,60 @@
#include "queue.h" #include "queue.h"
#include "stack.h" #include "stack.h"
static uint32_t tree_height_node(struct _tree* self, struct _tree_node* root)
{
#if 0
assert(self != NULL);
if (root == NULL)
{
return 0;
}
uint32_t left_height = tree_height_node(self, root->left);
uint32_t right_height = tree_height_node(self, root->right);
return (left_height > right_height) ? (left_height + 1) : (right_height + 1);
#else
assert(self != NULL);
if (root == NULL)
{
return 0;
}
uint32_t height = 0;
int32_t count_cur_level = 0;
int32_t count_next_level = 0;
struct _tree_node* node = root;
queue_t queue = queue_new(sizeof(struct _tree_node*));
queue->push(queue, &node);
while (!queue->empty(queue))
{
queue->pop(queue, &node);
if (node->left != NULL)
{
queue->push(queue, &node->left);
}
if (node->right != NULL)
{
queue->push(queue, &node->right);
}
if (count_cur_level == count_next_level)
{
count_next_level = queue->size(queue);
height++;
count_cur_level = 1;
}
else
{
count_cur_level++;
}
}
queue_free(&queue);
return height;
#endif
}
static void tree_set_balance(struct _tree* self, struct _tree_node* node) static void tree_set_balance(struct _tree* self, struct _tree_node* node)
{ {
assert(self != NULL); assert(self != NULL);
@ -19,7 +73,7 @@ static void tree_set_balance(struct _tree* self, struct _tree_node* node)
{ {
return; return;
} }
node->balance = self->height(self, node->right) - self->height(self, node->left); node->balance = (int32_t)(tree_height_node(self, node->right) - tree_height_node(self, node->left));
} }
static struct _tree_node* tree_turn_left(struct _tree* self, struct _tree_node* root) static struct _tree_node* tree_turn_left(struct _tree* self, struct _tree_node* root)
@ -128,59 +182,6 @@ static struct _tree_node* tree_turn_right_then_left(struct _tree* self, struct _
return node; return node;
} }
static int32_t tree_height(struct _tree* self, struct _tree_node* root)
{
#if 0
assert(self != NULL);
if (root == NULL)
{
return 0;
}
int32_t left_height = tree_height(self, root->left);
int32_t right_height = tree_height(self, root->right);
return (left_height > right_height) ? (left_height + 1) : (right_height + 1);
#else
assert(self != NULL);
if (root == NULL)
{
return 0;
}
int32_t height = 0;
int32_t count_cur_level = 0;
int32_t count_next_level = 0;
struct _tree_node* node = root;
queue_t queue = queue_new(sizeof(struct _tree_node*));
queue->push(queue, &node);
while (!queue->empty(queue))
{
queue->pop(queue, &node);
if (node->left != NULL)
{
queue->push(queue, &node->left);
}
if (node->right != NULL)
{
queue->push(queue, &node->right);
}
if (count_cur_level == count_next_level)
{
count_next_level = queue->size(queue);
height++;
count_cur_level = 1;
}
else
{
count_cur_level++;
}
}
queue_free(&queue);
return height;
#endif
}
/** /**
* @brief * @brief
* *
@ -1432,6 +1433,10 @@ static const void* tree_iter_next(struct _iterator* iter)
return obj; return obj;
} }
static uint32_t tree_height(struct _tree* self)
{
return tree_height_node(self, self->_root);
}
static bool tree_avl_init(struct _tree* self, uint32_t obj_size) static bool tree_avl_init(struct _tree* self, uint32_t obj_size)
{ {