Compare commits

..

3 Commits

Author SHA1 Message Date
feffd2b3c7 迭代器:后续遍历测试通过 2024-09-02 11:04:11 +08:00
36707a4b72 中序遍历测试通过 2024-09-02 10:36:56 +08:00
1bf06756b7 添加测试用的数据 2024-09-02 10:11:43 +08:00
2 changed files with 197 additions and 61 deletions

View File

@ -1511,11 +1511,62 @@ void* tree_begin(struct _tree* self)
}break; }break;
case ORDER_LEFT_IN: case ORDER_LEFT_IN:
{ {
struct _tree_node* node = NULL;
self->cur_node = self->_root;
while(!self->stack->empty(self->stack) || self->cur_node != NULL)
{
if(self->cur_node != NULL)
{
self->stack->push(self->stack, &self->cur_node);
self->cur_node = self->cur_node->left;
}
else
{
self->stack->pop(self->stack, &self->cur_node);
// self->print_obj(self->cur_node->obj);
node = self->cur_node;
self->cur_node = self->cur_node->right;
break;
}
}
if(node == NULL)
{
return NULL;
}
return node->obj;
}break; }break;
case ORDER_LEFT_POST: case ORDER_LEFT_POST:
{ {
struct _tree_node* node = self->_root;
stack_t stack = stack_new();
stack_init(stack, sizeof(struct _tree_node*));
while(!stack->empty(stack) || node != NULL)
{
if(node != NULL)
{
self->stack->push(self->stack, &node);
stack->push(stack, &node);
node = node->right;
}
else
{
stack->pop(stack, &node);
node = node->left;
}
}
stack_free(&stack);
if(!self->stack->empty(self->stack))
{
self->stack->pop(self->stack, &self->cur_node);
}
else
{
self->cur_node = NULL;
}
return self->cur_node == NULL ? NULL : self->cur_node->obj;
}break; }break;
case ORDER_LEFT_BREADTH: case ORDER_LEFT_BREADTH:
{ {
@ -1576,11 +1627,40 @@ void* tree_next(struct _tree* self)
}break; }break;
case ORDER_LEFT_IN: case ORDER_LEFT_IN:
{ {
struct _tree_node* node = NULL;
while(!self->stack->empty(self->stack) || self->cur_node != NULL)
{
if(self->cur_node != NULL)
{
self->stack->push(self->stack, &self->cur_node);
self->cur_node = self->cur_node->left;
}
else
{
self->stack->pop(self->stack, &self->cur_node);
// self->print_obj(self->cur_node->obj);
node = self->cur_node;
self->cur_node = self->cur_node->right;
break;
}
}
if(node == NULL)
{
return NULL;
}
return node->obj;
}break; }break;
case ORDER_LEFT_POST: case ORDER_LEFT_POST:
{ {
if(!self->stack->empty(self->stack))
{
self->stack->pop(self->stack, &self->cur_node);
}
else
{
self->cur_node = NULL;
}
return self->cur_node == NULL ? NULL : self->cur_node->obj;
}break; }break;
case ORDER_LEFT_BREADTH: case ORDER_LEFT_BREADTH:
{ {
@ -1619,31 +1699,31 @@ void* tree_end(struct _tree* self)
}break; }break;
case ORDER_LEFT_IN: case ORDER_LEFT_IN:
{ {
return NULL;
}break; }break;
case ORDER_LEFT_POST: case ORDER_LEFT_POST:
{ {
return NULL;
}break; }break;
case ORDER_LEFT_BREADTH: case ORDER_LEFT_BREADTH:
{ {
return NULL;
}break; }break;
case ORDER_RIGHT_PRE: case ORDER_RIGHT_PRE:
{ {
return NULL;
}break; }break;
case ORDER_RIGHT_IN: case ORDER_RIGHT_IN:
{ {
return NULL;
}break; }break;
case ORDER_RIGHT_POST: case ORDER_RIGHT_POST:
{ {
return NULL;
}break; }break;
case ORDER_RIGHT_BREADTH: case ORDER_RIGHT_BREADTH:
{ {
return NULL;
}break; }break;
default: default:
{ {

View File

@ -1,16 +1,16 @@
/** /**
* @file test_tree.c * @file test_tree.c
* @author wenjf (Orig5826@163.com) * @author wenjf (Orig5826@163.com)
* @brief * @brief
* @version 0.1 * @version 0.1
* @date 2024-09-02 * @date 2024-09-02
* *
* @copyright Copyright (c) 2024 * @copyright Copyright (c) 2024
* *
*/ */
#include "test.h" #include "test.h"
// if vs2022 has error: 'max': macro redefinition // if vs2022 has error: 'max': macro redefinition
#ifdef max #ifdef max
#undef max #undef max
#endif #endif
@ -21,7 +21,7 @@
#if 0 #if 0
/** /**
* @brief * @brief
* int data[] = { 5,2,3,1,7,8,6 }; * int data[] = { 5,2,3,1,7,8,6 };
* 5 * 5
* | | * | |
@ -39,7 +39,7 @@ void test_avltree_num(void)
int temp = 0; int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]); uint32_t len = sizeof(data) / sizeof(data[0]);
tree_t tree = tree_new,; tree_t tree = tree_new, ;
tree_avl_init(tree, sizeof(int)); tree_avl_init(tree, sizeof(int));
tree->print_obj = print_num; tree->print_obj = print_num;
tree->compare = compare_num; tree->compare = compare_num;
@ -71,7 +71,7 @@ void test_avltree_num(void)
printf("----- tree -----\n"); printf("----- tree -----\n");
tree->clear(tree); tree->clear(tree);
if(tree->empty(tree)) if (tree->empty(tree))
{ {
printf("----- empty -----\n"); printf("----- empty -----\n");
} }
@ -137,7 +137,7 @@ void test_avltree_num(void)
printf("\n"); printf("\n");
} }
if(tree->empty(tree)) if (tree->empty(tree))
{ {
printf("----- empty -----\n"); printf("----- empty -----\n");
} }
@ -145,11 +145,11 @@ void test_avltree_num(void)
tree_free(&tree); tree_free(&tree);
} }
static bool tree_rb_check_color(struct _tree *self, struct _tree_node* root, int black_num, int black_num_expected) static bool tree_rb_check_color(struct _tree* self, struct _tree_node* root, int black_num, int black_num_expected)
{ {
if(root == NULL) if (root == NULL)
{ {
if(black_num != black_num_expected) if (black_num != black_num_expected)
{ {
printf("black_num != black_num_expected\n"); printf("black_num != black_num_expected\n");
return false; return false;
@ -157,29 +157,29 @@ static bool tree_rb_check_color(struct _tree *self, struct _tree_node* root, int
return true; return true;
} }
if(root->color == RBT_BLACK) if (root->color == RBT_BLACK)
{ {
black_num++; black_num++;
} }
if(root->color == RBT_RED && root->parent && root->parent->color == RBT_RED) if (root->color == RBT_RED && root->parent && root->parent->color == RBT_RED)
{ {
printf("The red node is adjacent to the red node\n"); printf("The red node is adjacent to the red node\n");
return false; return false;
} }
return tree_rb_check_color(self, root->left, black_num, black_num_expected) && return tree_rb_check_color(self, root->left, black_num, black_num_expected) &&
tree_rb_check_color(self, root->right, black_num, black_num_expected); tree_rb_check_color(self, root->right, black_num, black_num_expected);
} }
static bool tree_rb_check(struct _tree* self) static bool tree_rb_check(struct _tree* self)
{ {
assert(self != NULL); assert(self != NULL);
if(self->_root == NULL) if (self->_root == NULL)
{ {
return true; return true;
} }
if(self->_root->color != RBT_BLACK) if (self->_root->color != RBT_BLACK)
{ {
printf("self->_root->color != RBT_BLACK\n"); printf("self->_root->color != RBT_BLACK\n");
return false; return false;
@ -187,9 +187,9 @@ static bool tree_rb_check(struct _tree* self)
int black_num_expected = 0; int black_num_expected = 0;
struct _tree_node* root = self->_root; struct _tree_node* root = self->_root;
while(root) while (root)
{ {
if(root->color == RBT_BLACK) if (root->color == RBT_BLACK)
{ {
black_num_expected++; black_num_expected++;
} }
@ -199,7 +199,7 @@ static bool tree_rb_check(struct _tree* self)
} }
/** /**
* @brief * @brief
*/ */
void test_rbtree_num(void) void test_rbtree_num(void)
{ {
@ -211,7 +211,7 @@ void test_rbtree_num(void)
int temp = 0; int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]); uint32_t len = sizeof(data) / sizeof(data[0]);
tree_t tree = tree_new,; tree_t tree = tree_new, ;
tree_rb_init(tree, sizeof(int)); tree_rb_init(tree, sizeof(int));
tree->print_obj = print_num; tree->print_obj = print_num;
tree->compare = compare_num; tree->compare = compare_num;
@ -230,7 +230,7 @@ void test_rbtree_num(void)
tree->preorder(tree, tree->_root); tree->preorder(tree, tree->_root);
printf("\n"); printf("\n");
if(true != tree_rb_check(tree)) if (true != tree_rb_check(tree))
{ {
printf("----- rb_check_error -----\n"); printf("----- rb_check_error -----\n");
return; return;
@ -249,7 +249,7 @@ void test_rbtree_num(void)
printf("----- tree -----\n"); printf("----- tree -----\n");
tree->clear(tree); tree->clear(tree);
if(tree->empty(tree)) if (tree->empty(tree))
{ {
printf("----- empty -----\n"); printf("----- empty -----\n");
} }
@ -315,14 +315,14 @@ void test_rbtree_num(void)
tree->preorder(tree, tree->_root); tree->preorder(tree, tree->_root);
printf("\n"); printf("\n");
if(true != tree_rb_check(tree)) if (true != tree_rb_check(tree))
{ {
printf("----- rb_check_error -----\n"); printf("----- rb_check_error -----\n");
return; return;
} }
} }
if(tree->empty(tree)) if (tree->empty(tree))
{ {
printf("----- empty -----\n"); printf("----- empty -----\n");
} }
@ -331,7 +331,7 @@ void test_rbtree_num(void)
} }
/** /**
* @brief * @brief
*/ */
void test_rbtree_struct(void) void test_rbtree_struct(void)
{ {
@ -340,10 +340,10 @@ void test_rbtree_struct(void)
{"zhao", 1001},{"qian", 1002}, {"sun", 1003}, {"li", 1004}, {"zhao", 1001},{"qian", 1002}, {"sun", 1003}, {"li", 1004},
"zhou", 1005, "wu", 1006, "zheng", 1007, "wang", 1008, "zhou", 1005, "wu", 1006, "zheng", 1007, "wang", 1008,
}; };
struct _student temp = {0}; struct _student temp = { 0 };
uint32_t len = sizeof(data) / sizeof(data[0]); uint32_t len = sizeof(data) / sizeof(data[0]);
tree_t tree = tree_new,; tree_t tree = tree_new, ;
tree_rb_init(tree, sizeof(struct _student)); tree_rb_init(tree, sizeof(struct _student));
tree->print_obj = print_struct; tree->print_obj = print_struct;
tree->compare = compare_struct; tree->compare = compare_struct;
@ -362,7 +362,7 @@ void test_rbtree_struct(void)
tree->preorder(tree, tree->_root); tree->preorder(tree, tree->_root);
printf("\n"); printf("\n");
if(true != tree_rb_check(tree)) if (true != tree_rb_check(tree))
{ {
printf("----- rb_check_error -----\n"); printf("----- rb_check_error -----\n");
return; return;
@ -381,7 +381,7 @@ void test_rbtree_struct(void)
printf("----- tree -----\n"); printf("----- tree -----\n");
tree->clear(tree); tree->clear(tree);
if(tree->empty(tree)) if (tree->empty(tree))
{ {
printf("----- empty -----\n"); printf("----- empty -----\n");
} }
@ -447,14 +447,14 @@ void test_rbtree_struct(void)
tree->preorder(tree, tree->_root); tree->preorder(tree, tree->_root);
printf("\n"); printf("\n");
if(true != tree_rb_check(tree)) if (true != tree_rb_check(tree))
{ {
printf("----- rb_check_error -----\n"); printf("----- rb_check_error -----\n");
return; return;
} }
} }
if(tree->empty(tree)) if (tree->empty(tree))
{ {
printf("----- empty -----\n"); printf("----- empty -----\n");
} }
@ -463,11 +463,53 @@ void test_rbtree_struct(void)
} }
#endif #endif
static const int const expected_int_array[9][15] = { static const int expected_int_array[9][15] = {
{ 5, 2, 3, 1, 7, 8, 6, 4, 9, 10, 12, 11, 15, 14, 13, }, // original data { 5, 2, 3, 1, 7, 8, 6, 4, 9, 10, 12, 11, 15, 14, 13}, // original data
{ 7, 3, 2, 1, 5, 4, 6, 11, 9, 8, 10, 14, 12, 13, 15, }, // order_left_pre { 7, 3, 2, 1, 5, 4, 6, 11, 9, 8, 10, 14, 12, 13, 15}, // order_left_pre
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, // order_left_in
{ 1, 2, 4, 6, 5, 3, 8, 10, 9, 13, 12, 15, 14, 11, 7}, // order_left_post
{ 7, 3, 11, 2, 5, 9, 14, 1, 4, 6, 8, 10, 12, 15, 13}, // order_left_breadth
{ 7, 11, 14, 15, 12, 13, 9, 10, 8, 3, 5, 6, 4, 2, 1}, // order_right_pre
{ 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, // order_right_in
{ 15, 13, 12, 14, 10, 8, 9, 11, 6, 4, 5, 1, 2, 3, 7}, // order_right_post
{ 7, 11, 3, 14, 9, 5, 2, 15, 12, 10, 8, 6, 4, 1, 13}, // order_right_breadth
}; };
static const int expected_int_array_orderpre_insert[15][15] = {
{ 5, },
{ 5, 2, },
{ 3, 2, 5, },
{ 3, 2, 1, 5, },
{ 3, 2, 1, 5, 7, },
{ 3, 2, 1, 7, 5, 8, },
{ 3, 2, 1, 7, 5, 6, 8, },
{ 3, 2, 1, 7, 5, 4, 6, 8, },
{ 3, 2, 1, 7, 5, 4, 6, 8, 9, },
{ 3, 2, 1, 7, 5, 4, 6, 9, 8, 10, },
{ 7, 3, 2, 1, 5, 4, 6, 9, 8, 10, 12, },
{ 7, 3, 2, 1, 5, 4, 6, 9, 8, 11, 10, 12, },
{ 7, 3, 2, 1, 5, 4, 6, 11, 9, 8, 10, 12, 15, },
{ 7, 3, 2, 1, 5, 4, 6, 11, 9, 8, 10, 14, 12, 15, },
{ 7, 3, 2, 1, 5, 4, 6, 11, 9, 8, 10, 14, 12, 13, 15, },
};
static const int expected_int_array_orderpre_delete[15][15] = {
{ 7, 3, 2, 1, 5, 4, 6, 11, 9, 8, 10, 14, 12, 13, 15, },
{ 7, 3, 2, 1, 6, 4, 11, 9, 8, 10, 14, 12, 13, 15, },
{ 7, 3, 1, 6, 4, 11, 9, 8, 10, 14, 12, 13, 15, },
{ 7, 4, 1, 6, 11, 9, 8, 10, 14, 12, 13, 15, },
{ 11, 7, 4, 6, 9, 8, 10, 14, 12, 13, 15, },
{ 11, 8, 4, 6, 9, 10, 14, 12, 13, 15, },
{ 11, 9, 4, 6, 10, 14, 12, 13, 15, },
{ 11, 9, 4, 10, 14, 12, 13, 15, },
{ 11, 9, 10, 14, 12, 13, 15, },
{ 12, 11, 10, 14, 13, 15, },
{ 12, 11, 14, 13, 15, },
{ 13, 11, 14, 15, },
{ 14, 13, 15, },
{ 14, 13, },
{ 13, },
};
static void test_tree_iter(void) static void test_tree_iter(void)
{ {
@ -478,11 +520,15 @@ static void test_tree_iter(void)
// int data[] = { 5,2,3,1,7,8,6,4,9,10,12,11,15,14,13 }; // int data[] = { 5,2,3,1,7,8,6,4,9,10,12,11,15,14,13 };
int data[15] = { 5, 2, 3, 1, 7, 8, 6, 4, 9, 10, 12, 11, 15, 14, 13, }; int data[15] = { 5, 2, 3, 1, 7, 8, 6, 4, 9, 10, 12, 11, 15, 14, 13, };
int buff[32]; int buff[32];
int count = 0;
int temp = 0; int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]); uint32_t len = sizeof(data) / sizeof(data[0]);
int * iter = NULL;
int count = 0;
tree_t tree = tree_new(); tree_t tree = tree_new();
TEST_ASSERT_NOT_NULL(tree);
tree_avl_init(tree, sizeof(int)); tree_avl_init(tree, sizeof(int));
tree->print_obj = print_num; tree->print_obj = print_num;
tree->compare = compare_num; tree->compare = compare_num;
@ -490,34 +536,44 @@ static void test_tree_iter(void)
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
{ {
temp = data[i]; temp = data[i];
tree->insert(tree, &temp); TEST_ASSERT_TRUE(tree->insert(tree, &temp));
// printf("insert = "); for (count = 0, iter = tree->begin(tree); iter != tree->end(tree); iter = tree->next(tree))
// tree->print_obj(&temp); {
// printf("size = %2d : ", tree->size(tree)); buff[count++] = *iter;
// tree->preorder(tree, tree->_root); }
// printf("\n"); TEST_ASSERT_EQUAL_INT_ARRAY(expected_int_array_orderpre_insert[i], buff, count);
} }
printf("insert = ");
tree->print_obj(&temp); for(i = 1; i < 3; i++)
printf("size = %2d : \n", tree->size(tree)); {
tree->preorder(tree, tree->_root); tree->set_order(tree, i); //ORDER_LEFT_IN
// printf("\n ----- iter test -----\n");
for (count = 0, iter = tree->begin(tree); iter != tree->end(tree); iter = tree->next(tree))
{
// printf("(%2d ) ", *iter);
buff[count++] = *iter;
}
// printf("\n");
TEST_ASSERT_EQUAL_INT_ARRAY(expected_int_array[i], buff, count);
}
printf("\n\nactual data = \n");
tree->postorder(tree, tree->_root);
printf("\n"); printf("\n");
// set order // set order
tree->order(tree, ORDER_LEFT_PRE); // tree->set_order(tree, ORDER_LEFT_PRE);
// tree->set_order(tree, ORDER_LEFT_IN); // tree->set_order(tree, ORDER_LEFT_IN);
tree->set_order(tree, ORDER_LEFT_POST);
printf("\niter test\n"); printf("\n ----- iter data -----\n");
int * iter = NULL; for (count = 0, iter = tree->begin(tree); iter != tree->end(tree); iter = tree->next(tree))
for(iter = tree->begin(tree); iter != tree->end(tree); iter = tree->next(tree))
{ {
printf("(%2d ) ", *iter); printf("(%2d ) ", *iter);
buff[count++] = *iter; buff[count++] = *iter;
} }
printf("\n"); printf("\n");
TEST_ASSERT_EQUAL_INT_ARRAY(expected_int_array[1], buff, count); TEST_ASSERT_EQUAL_INT_ARRAY(expected_int_array[3], buff, count);
TEST_ASSERT_FALSE(tree->empty(tree)); TEST_ASSERT_FALSE(tree->empty(tree));
TEST_ASSERT_TRUE(tree->clear(tree)); TEST_ASSERT_TRUE(tree->clear(tree));