mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-07-05 00:16:53 +08:00
添加tree示例
This commit is contained in:
parent
a36bfd7507
commit
595e46c375
@ -120,10 +120,9 @@ struct _tree
|
|||||||
// free
|
// free
|
||||||
void (*destory)(struct _tree* self);
|
void (*destory)(struct _tree* self);
|
||||||
|
|
||||||
|
|
||||||
// ----- print -----
|
// ----- print -----
|
||||||
// traversal depth
|
// traversal depth
|
||||||
void (*preorder)(struct _tree* self);
|
void (*preorder)(struct _tree* self, struct _tree_node* root);
|
||||||
void (*inorder)(struct _tree* self);
|
void (*inorder)(struct _tree* self);
|
||||||
void (*postorder)(struct _tree* self);
|
void (*postorder)(struct _tree* self);
|
||||||
// traversal breadth
|
// traversal breadth
|
||||||
@ -131,11 +130,14 @@ struct _tree
|
|||||||
|
|
||||||
void (*print_obj)(void* obj);
|
void (*print_obj)(void* obj);
|
||||||
};
|
};
|
||||||
|
typedef struct _tree* tree_t;
|
||||||
|
|
||||||
// bst_tree
|
// bst_tree
|
||||||
bool tree_avl_init(struct _tree *self, uint32_t obj_size);
|
bool tree_avl_init(struct _tree *self, uint32_t obj_size);
|
||||||
bool tree_rb_init(struct _tree *self, uint32_t obj_size);
|
bool tree_rb_init(struct _tree *self, uint32_t obj_size);
|
||||||
|
|
||||||
|
tree_t tree_new(void);
|
||||||
|
void tree_free(tree_t tree);
|
||||||
|
|
||||||
#endif // _TREE_H_
|
#endif // _TREE_H_
|
||||||
|
|
||||||
|
37
src/tree.c
37
src/tree.c
@ -1404,6 +1404,8 @@ static bool tree_rebalance(ptree_t head, ptree_node_t tree)
|
|||||||
|
|
||||||
static tree_node_t tree_node_new(struct _tree* self, void* obj)
|
static tree_node_t tree_node_new(struct _tree* self, void* obj)
|
||||||
{
|
{
|
||||||
|
assert(self != NULL);
|
||||||
|
|
||||||
void * obj_new = malloc(self->_obj_size);
|
void * obj_new = malloc(self->_obj_size);
|
||||||
if (obj_new == NULL)
|
if (obj_new == NULL)
|
||||||
{
|
{
|
||||||
@ -1437,12 +1439,14 @@ done:
|
|||||||
|
|
||||||
static bool tree_node_free(struct _tree_node* node)
|
static bool tree_node_free(struct _tree_node* node)
|
||||||
{
|
{
|
||||||
assert(node != NULL);
|
if(node != NULL)
|
||||||
|
{
|
||||||
if(node->obj != NULL)
|
if(node->obj != NULL)
|
||||||
{
|
{
|
||||||
free(node->obj);
|
free(node->obj);
|
||||||
}
|
}
|
||||||
free(node);
|
free(node);
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1470,10 +1474,12 @@ bool tree_avl_insert(struct _tree* self, void* obj)
|
|||||||
if(self->compare(obj, root->obj) < 0)
|
if(self->compare(obj, root->obj) < 0)
|
||||||
{
|
{
|
||||||
root->left = node;
|
root->left = node;
|
||||||
|
node->parent = root;
|
||||||
}
|
}
|
||||||
else if(self->compare(obj, root->obj) > 0)
|
else if(self->compare(obj, root->obj) > 0)
|
||||||
{
|
{
|
||||||
root->right = node;
|
root->right = node;
|
||||||
|
node->parent = root;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -1517,14 +1523,25 @@ void tree_destory(struct _tree* self)
|
|||||||
self->clear(self);
|
self->clear(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
void tree_avl_preorder(struct _tree* self)
|
void tree_avl_preorder(struct _tree* self, struct _tree_node* root)
|
||||||
{
|
{
|
||||||
|
assert(self != NULL);
|
||||||
|
if(root->left != NULL)
|
||||||
|
{
|
||||||
|
tree_avl_preorder(self, root->left);
|
||||||
}
|
}
|
||||||
|
self->print_obj(root->obj);
|
||||||
|
if(root->right != NULL)
|
||||||
|
{
|
||||||
|
tree_avl_preorder(self, root->right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void tree_avl_inorder(struct _tree* self)
|
void tree_avl_inorder(struct _tree* self)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void tree_avl_postorder(struct _tree* self)
|
void tree_avl_postorder(struct _tree* self)
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -1563,3 +1580,17 @@ bool tree_rb_init(struct _tree *self, uint32_t obj_size)
|
|||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tree_t tree_new(void)
|
||||||
|
{
|
||||||
|
return (struct _tree*)malloc(sizeof(struct _tree));
|
||||||
|
}
|
||||||
|
|
||||||
|
void tree_free(tree_t tree)
|
||||||
|
{
|
||||||
|
if(tree != NULL)
|
||||||
|
{
|
||||||
|
tree->destory(tree);
|
||||||
|
free(tree);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
18
test/test.c
18
test/test.c
@ -6,6 +6,14 @@ void print_num(void* obj)
|
|||||||
printf("(%2d ) ", *(int*)obj);
|
printf("(%2d ) ", *(int*)obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int compare_num(void *obj, void *obj2)
|
||||||
|
{
|
||||||
|
int num1 = *(int*)obj;
|
||||||
|
int num2 = *(int*)obj2;
|
||||||
|
return num1 - num2 > 0 ? 1 : -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void print_struct(void* obj)
|
void print_struct(void* obj)
|
||||||
{
|
{
|
||||||
struct _student* student = (struct _student*)obj;
|
struct _student* student = (struct _student*)obj;
|
||||||
@ -27,12 +35,12 @@ int main()
|
|||||||
printf("----- unicstl test -----\n");
|
printf("----- unicstl test -----\n");
|
||||||
// while (1)
|
// while (1)
|
||||||
{
|
{
|
||||||
test_list();
|
// test_list();
|
||||||
test_stack();
|
// test_stack();
|
||||||
test_deque();
|
// test_deque();
|
||||||
test_queue();
|
// test_queue();
|
||||||
|
test_tree();
|
||||||
|
|
||||||
// tree_test();
|
|
||||||
// rbtree_test();
|
// rbtree_test();
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -17,6 +17,8 @@ struct _student
|
|||||||
};
|
};
|
||||||
|
|
||||||
void print_num(void* obj);
|
void print_num(void* obj);
|
||||||
|
int compare_num(void *obj, void *obj2);
|
||||||
|
|
||||||
void print_char(void* obj);
|
void print_char(void* obj);
|
||||||
void print_struct(void* obj);
|
void print_struct(void* obj);
|
||||||
void print_str(void* obj);
|
void print_str(void* obj);
|
||||||
@ -29,10 +31,8 @@ void print_str(void* obj);
|
|||||||
void test_list(void);
|
void test_list(void);
|
||||||
void test_stack(void);
|
void test_stack(void);
|
||||||
void test_deque(void);
|
void test_deque(void);
|
||||||
|
|
||||||
void test_queue(void);
|
void test_queue(void);
|
||||||
void tree_test(void);
|
void test_tree(void);
|
||||||
void rbtree_test(void);
|
|
||||||
|
|
||||||
#endif // _TEST_H_
|
#endif // _TEST_H_
|
||||||
|
|
||||||
|
@ -265,7 +265,35 @@ void tree_test(void)
|
|||||||
printf("----------------------------------------\n");
|
printf("----------------------------------------\n");
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
void test_tree_num(void)
|
||||||
|
{
|
||||||
|
uint32_t i = 0;
|
||||||
|
int data[] = { 1,2,3,4,5,6,7,8,9,10 };
|
||||||
|
int temp = 0;
|
||||||
|
uint32_t len = sizeof(data) / sizeof(data[0]);
|
||||||
|
|
||||||
|
tree_t tree = tree_new();
|
||||||
|
tree_avl_init(tree, sizeof(int));
|
||||||
|
tree->print_obj = print_num;
|
||||||
|
tree->compare = compare_num;
|
||||||
|
|
||||||
|
printf("\n\n----- test_queue_num -----\n");
|
||||||
|
|
||||||
|
printf("----- after push-----\n");
|
||||||
|
for (i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
tree->insert(tree, &data[i]);
|
||||||
|
}
|
||||||
|
printf("----- preorder -----\n");
|
||||||
|
tree->preorder(tree, tree->_root);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
tree_free(tree);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_tree(void)
|
||||||
|
{
|
||||||
|
test_tree_num();
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user