mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-07-03 23:56:54 +08:00
rbt实测结构体也是ok的
This commit is contained in:
parent
590a50d9e7
commit
73fd6176ae
11
test/test.c
11
test/test.c
@ -23,6 +23,17 @@ void print_struct(void* obj)
|
||||
printf("(%4d:%-8s) ", student->id, student->name);
|
||||
}
|
||||
|
||||
int compare_struct(void *obj, void *obj2)
|
||||
{
|
||||
struct _student num1 = *(struct _student*)obj;
|
||||
struct _student num2 = *(struct _student*)obj2;
|
||||
if(num1.id == num2.id)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return num1.id > num2.id ? 1 : -1;
|
||||
}
|
||||
|
||||
void print_char(void* obj)
|
||||
{
|
||||
printf("(%2c ) ", *(char*)obj);
|
||||
|
@ -19,8 +19,10 @@ struct _student
|
||||
void print_num(void* obj);
|
||||
int compare_num(void *obj, void *obj2);
|
||||
|
||||
void print_char(void* obj);
|
||||
void print_struct(void* obj);
|
||||
int compare_struct(void *obj, void *obj2);
|
||||
|
||||
void print_char(void* obj);
|
||||
void print_str(void* obj);
|
||||
|
||||
|
||||
|
133
test/test_tree.c
133
test/test_tree.c
@ -292,8 +292,138 @@ void test_rbtree_num(void)
|
||||
// delete
|
||||
tree->delete(tree, &temp);
|
||||
|
||||
printf("size = %2d : ", tree->size(tree));
|
||||
tree->preorder(tree, tree->_root);
|
||||
printf("\n");
|
||||
|
||||
if(true != tree_rb_check(tree))
|
||||
{
|
||||
printf("----- rb_check_error -----\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if(tree->empty(tree))
|
||||
{
|
||||
printf("----- empty -----\n");
|
||||
}
|
||||
#endif
|
||||
tree_free(tree);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief
|
||||
*/
|
||||
void test_rbtree_struct(void)
|
||||
{
|
||||
uint32_t i = 0;
|
||||
struct _student data[] = {
|
||||
{"zhao", 1001},{"qian", 1002}, {"sun", 1003}, {"li", 1004},
|
||||
"zhou", 1005, "wu", 1006, "zheng", 1007, "wang", 1008,
|
||||
};
|
||||
struct _student temp = {0};
|
||||
uint32_t len = sizeof(data) / sizeof(data[0]);
|
||||
|
||||
tree_t tree = tree_new();
|
||||
tree_rb_init(tree, sizeof(struct _student));
|
||||
tree->print_obj = print_struct;
|
||||
tree->compare = compare_struct;
|
||||
|
||||
printf("\n\n----- test_rbtree_struct -----\n");
|
||||
|
||||
printf("----- insert -----\n");
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
temp = data[i];
|
||||
tree->insert(tree, &temp);
|
||||
|
||||
printf("insert = ");
|
||||
tree->print_obj(&temp);
|
||||
printf("size = %2d : ", tree->size(tree));
|
||||
tree->preorder(tree, tree->_root);
|
||||
printf("\n");
|
||||
|
||||
if(true != tree_rb_check(tree))
|
||||
{
|
||||
printf("----- rb_check_error -----\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
printf("----- max -----\n");
|
||||
tree->max(tree, &temp);
|
||||
tree->print_obj(&temp);
|
||||
printf("\n");
|
||||
|
||||
printf("----- min -----\n");
|
||||
tree->min(tree, &temp);
|
||||
tree->print_obj(&temp);
|
||||
printf("\n");
|
||||
|
||||
printf("----- tree -----\n");
|
||||
tree->clear(tree);
|
||||
if(tree->empty(tree))
|
||||
{
|
||||
printf("----- empty -----\n");
|
||||
}
|
||||
printf("----- insert -----\n");
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
temp = data[i];
|
||||
tree->insert(tree, &temp);
|
||||
}
|
||||
|
||||
printf("----- preorder -----\n");
|
||||
tree->preorder(tree, tree->_root);
|
||||
printf("\n");
|
||||
|
||||
printf("----- inorder -----\n");
|
||||
tree->inorder(tree, tree->_root);
|
||||
printf("\n");
|
||||
|
||||
printf("----- postorder -----\n");
|
||||
tree->postorder(tree, tree->_root);
|
||||
printf("\n");
|
||||
|
||||
printf("----- breadth -----\n");
|
||||
tree->breadth(tree, tree->_root);
|
||||
printf("\n");
|
||||
|
||||
printf("----- right priority -----\n");
|
||||
tree->order(tree, true);
|
||||
|
||||
printf("----- preorder(right) -----\n");
|
||||
tree->preorder(tree, tree->_root);
|
||||
printf("\n");
|
||||
|
||||
printf("----- inorder(right) -----\n");
|
||||
tree->inorder(tree, tree->_root);
|
||||
printf("\n");
|
||||
|
||||
printf("----- postorder(right) -----\n");
|
||||
tree->postorder(tree, tree->_root);
|
||||
printf("\n");
|
||||
|
||||
printf("----- breadth(right) -----\n");
|
||||
tree->breadth(tree, tree->_root);
|
||||
printf("\n");
|
||||
|
||||
#if 1
|
||||
printf("----- left priority -----\n");
|
||||
tree->order(tree, false);
|
||||
printf("----- preorder -----\n");
|
||||
tree->preorder(tree, tree->_root);
|
||||
printf("\n");
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
temp = data[i];
|
||||
printf("delete = ");
|
||||
tree->print_obj(&temp);
|
||||
|
||||
// delete
|
||||
tree->delete(tree, &temp);
|
||||
|
||||
printf("size = %2d : ", tree->size(tree));
|
||||
tree->preorder(tree, tree->_root);
|
||||
printf("\n");
|
||||
@ -316,5 +446,6 @@ void test_rbtree_num(void)
|
||||
void test_tree(void)
|
||||
{
|
||||
// test_avltree_num();
|
||||
test_rbtree_num();
|
||||
// test_rbtree_num();
|
||||
test_rbtree_struct();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user