迭代器:前序遍历right优先测试通过

This commit is contained in:
建峰 2024-09-02 11:38:19 +08:00
parent c8f39bf53a
commit e6828c2cb0
3 changed files with 341 additions and 297 deletions

View File

@ -1,35 +1,35 @@
# unicstl
## 简介
全称: Universal C standard library
## 简介
全称: Universal C standard library
基于C语言实现的通用C库。包含常用数据结构和算法
基于C语言实现的通用C库。包含常用数据结构和算法
> 标准:--std=c99
> 标准:--std=c99
[数据结构详细说明](http://wenjianfeng.top)
[数据结构详细说明](http://wenjianfeng.top)
## 数据结构
|数据结构 | 原理 |说明 |
## 数据结构
|数据结构 | 原理 |说明 |
|---|---|---|
| **stack** | | **栈** |
| stack_init | 链表 | |
| stack_init2 | 动态数组 | |
| **list** | | **列表**
| list_init2 | 动态数组 | |
| **queue** | | **队列**
| queue_init | 单向链表 | |
| queue_init2 | 数组 | FIFO/空/满 |
| **deque** | |**双端队列** |
| deque_init | 双向循环链表 | |
| **tree** | |**树** |
| tree_avl_init | 二叉搜索树 | AVL树 |
| tree_rb_init | 二叉搜索树 | 红黑树 |
| **heap** | |**堆** |
| heap_init2 | 数组 | 最大堆/最小堆 |
| **stack** | | **栈** |
| stack_init | 链表 | |
| stack_init2 | 动态数组 | |
| **list** | | **列表**
| list_init2 | 动态数组 | |
| **queue** | | **队列**
| queue_init | 单向链表 | |
| queue_init2 | 数组 | FIFO/空/满 |
| **deque** | |**双端队列** |
| deque_init | 双向循环链表 | |
| **tree** | |**树** |
| tree_avl_init | 二叉搜索树 | AVL树 |
| tree_rb_init | 二叉搜索树 | 红黑树 |
| **heap** | |**堆** |
| heap_init2 | 数组 | 最大堆/最小堆 |
## 版本
| 版本 | 说明 |
## 版本
| 版本 | 说明 |
|:----:|:----:|
| 0.xx.xx | 测试版本 |
| 0.xx.xx | 测试版本 |

View File

@ -1482,11 +1482,14 @@ void* tree_begin(struct _tree* self)
switch (self->_order)
{
case ORDER_LEFT_PRE:
case ORDER_RIGHT_PRE:
{
struct _tree_node* node = NULL;
self->cur_node = self->_root;
self->stack->clear(self->stack);
if (self->_order == ORDER_LEFT_PRE)
{
while (!self->stack->empty(self->stack) || self->cur_node != NULL)
{
if (self->cur_node != NULL)
@ -1504,6 +1507,28 @@ void* tree_begin(struct _tree* self)
self->cur_node = self->cur_node->right;
}
}
}
else
{
while (!self->stack->empty(self->stack) || self->cur_node != NULL)
{
if (self->cur_node != NULL)
{
node = self->cur_node;
self->stack->push(self->stack, &self->cur_node);
self->cur_node = self->cur_node->right;
break;
}
else
{
self->stack->pop(self->stack, &self->cur_node);
self->cur_node = self->cur_node->left;
}
}
}
if (node == NULL)
{
return NULL;
@ -1615,10 +1640,6 @@ void* tree_begin(struct _tree* self)
}
}
return self->cur_node == NULL ? NULL : self->cur_node->obj;
}break;
case ORDER_RIGHT_PRE:
{
}break;
case ORDER_RIGHT_IN:
{
@ -1640,8 +1661,11 @@ void* tree_next(struct _tree* self)
switch (self->_order)
{
case ORDER_LEFT_PRE:
case ORDER_RIGHT_PRE:
{
struct _tree_node* node = NULL;
if (self->_order == ORDER_LEFT_PRE)
{
while (!self->stack->empty(self->stack) || self->cur_node != NULL)
{
if (self->cur_node != NULL)
@ -1659,6 +1683,28 @@ void* tree_next(struct _tree* self)
self->cur_node = self->cur_node->right;
}
}
}
else
{
while (!self->stack->empty(self->stack) || self->cur_node != NULL)
{
if (self->cur_node != NULL)
{
node = self->cur_node;
self->stack->push(self->stack, &self->cur_node);
self->cur_node = self->cur_node->right;
break;
}
else
{
self->stack->pop(self->stack, &self->cur_node);
self->cur_node = self->cur_node->left;
}
}
}
if (node == NULL)
{
return NULL;
@ -1739,10 +1785,6 @@ void* tree_next(struct _tree* self)
self->cur_node = NULL;
}
return self->cur_node == NULL ? NULL : self->cur_node->obj;
}break;
case ORDER_RIGHT_PRE:
{
}break;
case ORDER_RIGHT_IN:
{

View File

@ -558,8 +558,9 @@ static void test_tree_iter(void)
TEST_ASSERT_EQUAL_INT_ARRAY(expected_int_array[i], buff, count);
}
tree->order(tree, true);
printf("\n\nactual data = \n");
tree->breadth(tree, tree->_root);
tree->preorder(tree, tree->_root);
printf("\n");
// set order
@ -567,7 +568,8 @@ static void test_tree_iter(void)
// tree->set_order(tree, ORDER_LEFT_IN);
// tree->set_order(tree, ORDER_LEFT_POST);
// tree->set_order(tree, ORDER_LEFT_BREADTH);
tree->set_order(tree, ORDER_RIGHT_BREADTH);
tree->set_order(tree, ORDER_RIGHT_PRE);
// tree->set_order(tree, ORDER_RIGHT_BREADTH);
printf("\n ----- iter data -----\n");
for (count = 0, iter = tree->begin(tree); iter != tree->end(tree); iter = tree->next(tree))
{