将对外用不到的接口放在tree.c作为局部函数调用

This commit is contained in:
建峰 2025-04-25 01:09:37 +08:00
parent 3ed9fb8437
commit ecc568bb90
2 changed files with 95 additions and 113 deletions

View File

@ -71,21 +71,15 @@ struct _tree
struct _iterator _iter;
void (*destory)(struct _tree* self);
// -------------------- public --------------------
// kernel
bool (*insert)(struct _tree* self, void* obj);
bool (*delete)(struct _tree* self, void* obj);
struct _tree_node* (*find)(struct _tree* self, void* obj);
struct _tree_node* (*find_min)(struct _tree* self, struct _tree_node* root);
struct _tree_node* (*find_max)(struct _tree* self, struct _tree_node* root);
bool (*rebalance)(struct _tree* self, struct _tree_node* root);
int32_t (*height)(struct _tree* self, struct _tree_node* root);
bool (*min)(struct _tree* self, void* obj);
bool (*max)(struct _tree* self, void* obj);
// base
bool (*clear)(struct _tree* self);
bool (*empty)(struct _tree* self);
@ -94,18 +88,12 @@ struct _tree
// iter
iterator_t (*iter)(struct _tree* self, enum _order);
/**
* @brief obj compare with obj2
*
* @return
* obj < obj2 return -1
* obj == obj2 return 0
* obj > obj2 return 1
*/
int (*compare)(void* obj, void* obj2);
// others
bool (*min)(struct _tree* self, void* obj);
bool (*max)(struct _tree* self, void* obj);
// free
void (*destory)(struct _tree* self);
// config
compare_fun_t compare; // !!! you have to implement this function
// -------------------- debug --------------------
void (*print_obj)(void* obj);

View File

@ -297,6 +297,86 @@ static bool tree_avl_rebalance(struct _tree* self, struct _tree_node* root)
#endif
}
static struct _tree_node* tree_find(struct _tree* self, void* obj)
{
assert(self != NULL);
struct _tree_node* root = self->_root;
while (root != NULL)
{
if (self->compare(obj, root->obj) == 0)
{
return root;
}
else if (self->compare(obj, root->obj) < 0)
{
root = root->left;
}
else
{
root = root->right;
}
}
return NULL;
}
static struct _tree_node* tree_find_min(struct _tree* self, struct _tree_node* root)
{
assert(self != NULL);
#if 0
if (root == NULL)
{
return NULL;
}
if (root->left == NULL)
{
return root;
}
return tree_find_min(self, root->left);
#else
while (root != NULL)
{
if (root->left != NULL)
{
root = root->left;
}
else
{
return root;
}
}
return root;
#endif
}
static struct _tree_node* tree_find_max(struct _tree* self, struct _tree_node* root)
{
#if 0
assert(self != NULL);
if (root == NULL)
{
return NULL;
}
if (root->right == NULL)
{
return root;
}
return tree_find_max(self, root->right);
#else
while (root != NULL)
{
if (root->right != NULL)
{
root = root->right;
}
else
{
return root;
}
}
return root;
#endif
}
static struct _tree_node* tree_node_new(struct _tree* self, void* obj)
{
assert(self != NULL);
@ -497,7 +577,7 @@ static bool tree_avl_delete_double_child(struct _tree* self, struct _tree_node*
{
assert(self != NULL);
assert(node != NULL);
struct _tree_node* tmp = self->find_min(self, node->right);
struct _tree_node* tmp = tree_find_min(self, node->right);
if (tmp != NULL)
{
memmove(node->obj, tmp->obj, self->_obj_size);
@ -517,7 +597,7 @@ static bool tree_avl_delete(struct _tree* self, void* obj)
return false;
}
struct _tree_node* node = self->find(self, obj);
struct _tree_node* node = tree_find(self, obj);
if (node == NULL)
{
return false;
@ -538,28 +618,6 @@ static bool tree_avl_delete(struct _tree* self, void* obj)
return true;
}
static struct _tree_node* tree_find(struct _tree* self, void* obj)
{
assert(self != NULL);
struct _tree_node* root = self->_root;
while (root != NULL)
{
if (self->compare(obj, root->obj) == 0)
{
return root;
}
else if (self->compare(obj, root->obj) < 0)
{
root = root->left;
}
else
{
root = root->right;
}
}
return NULL;
}
static bool tree_clear(struct _tree* self)
{
assert(self != NULL);
@ -621,64 +679,6 @@ static void tree_destory(struct _tree* self)
}
}
static struct _tree_node* tree_find_min(struct _tree* self, struct _tree_node* root)
{
assert(self != NULL);
#if 0
if (root == NULL)
{
return NULL;
}
if (root->left == NULL)
{
return root;
}
return tree_find_min(self, root->left);
#else
while (root != NULL)
{
if (root->left != NULL)
{
root = root->left;
}
else
{
return root;
}
}
return root;
#endif
}
static struct _tree_node* tree_find_max(struct _tree* self, struct _tree_node* root)
{
#if 0
assert(self != NULL);
if (root == NULL)
{
return NULL;
}
if (root->right == NULL)
{
return root;
}
return tree_find_max(self, root->right);
#else
while (root != NULL)
{
if (root->right != NULL)
{
root = root->right;
}
else
{
return root;
}
}
return root;
#endif
}
static bool tree_min(struct _tree* self, void* obj)
{
assert(self != NULL);
@ -1086,7 +1086,7 @@ static bool tree_rb_delete(struct _tree* self, void* obj)
return false;
}
struct _tree_node* node = self->find(self, obj);
struct _tree_node* node = tree_find(self, obj);
if (node == NULL)
{
return false;
@ -1114,7 +1114,7 @@ static bool tree_rb_delete(struct _tree* self, void* obj)
// 1. find the min node in right subtree
// 2. replace the node with min node
// 3. delete the min node
tmp = self->find_min(self, node->right);
tmp = tree_find_min(self, node->right);
memmove(node->obj, tmp->obj, self->_obj_size);
if (tmp->right != NULL)
{
@ -1150,7 +1150,7 @@ static bool tree_rb_delete(struct _tree* self, void* obj)
return true;
}
iterator_t tree_iter(struct _tree* self, enum _order order)
static iterator_t tree_iter(struct _tree* self, enum _order order)
{
assert(self != NULL);
iterator_t iter = &self->_iter;
@ -1234,7 +1234,7 @@ iterator_t tree_iter(struct _tree* self, enum _order order)
return iter;
}
bool tree_iter_hasnext(struct _iterator* iter)
static bool tree_iter_hasnext(struct _iterator* iter)
{
assert(iter != NULL);
assert(iter->parent != NULL);
@ -1247,7 +1247,7 @@ bool tree_iter_hasnext(struct _iterator* iter)
return false;
}
const void* tree_iter_next(struct _iterator* iter)
static const void* tree_iter_next(struct _iterator* iter)
{
assert(iter != NULL);
assert(iter->parent != NULL);
@ -1481,9 +1481,6 @@ static bool tree_avl_init(struct _tree* self, uint32_t obj_size)
self->iter = tree_iter;
// others
self->find = tree_find;
self->find_max = tree_find_max;
self->find_min = tree_find_min;
self->max = tree_max;
self->min = tree_min;
@ -1538,9 +1535,6 @@ static bool tree_rb_init(struct _tree* self, uint32_t obj_size)
self->iter = tree_iter;
// others
self->find = tree_find;
self->find_max = tree_find_max;
self->find_min = tree_find_min;
self->max = tree_max;
self->min = tree_min;