diff --git a/include/tree.h b/include/tree.h index d9eefbf..4adfc91 100644 --- a/include/tree.h +++ b/include/tree.h @@ -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); diff --git a/src/tree.c b/src/tree.c index 1db0b22..df815a5 100644 --- a/src/tree.c +++ b/src/tree.c @@ -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); @@ -1428,7 +1428,7 @@ const void* tree_iter_next(struct _iterator* iter) { }break; } - + return obj; } @@ -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;