mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-05-18 03:51:35 +08:00
中序遍历用栈实现了一下,虽然判断条件有点多。以后再考虑优化
This commit is contained in:
parent
00ae16c777
commit
3316ce981a
@ -1,6 +1,6 @@
|
|||||||
|
|
||||||
# 0. cmake 最低版本号要求
|
# 0. cmake 最低版本号要求
|
||||||
cmake_minimum_required(VERSION 3.27)
|
cmake_minimum_required(VERSION 3.29)
|
||||||
|
|
||||||
# 0. 项目信息
|
# 0. 项目信息
|
||||||
project(demo)
|
project(demo)
|
||||||
|
4
mk.bat
4
mk.bat
@ -1,8 +1,8 @@
|
|||||||
@REM D:\Lang\cmake-3.27.5-windows-x86_64\bin\cmake.EXE --no-warn-unused-cli -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_C_COMPILER:FILEPATH=D:\Software\mingw64\bin\gcc.exe -DCMAKE_CXX_COMPILER:FILEPATH=D:\Software\mingw64\bin\g++.exe -SF:/OpenDemo/1_vsc_cmake -Bf:/OpenDemo/1_vsc_cmake/build -G "MinGW Makefiles"
|
@REM D:\Lang\cmake-3.27.5-windows-x86_64\bin\cmake.EXE --no-warn-unused-cli -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_C_COMPILER:FILEPATH=D:\Software\mingw64\bin\gcc.exe -DCMAKE_CXX_COMPILER:FILEPATH=D:\Software\mingw64\bin\g++.exe -SF:/OpenDemo/1_vsc_cmake -Bf:/OpenDemo/1_vsc_cmake/build -G "MinGW Makefiles"
|
||||||
@REM cmake -Bbuild -G "Visual Studio 17 2022"
|
@REM cmake -Bbuild -G "Visual Studio 17 2022"
|
||||||
|
|
||||||
cmake -B build -G "MinGW Makefiles"
|
@REM cmake -B build -G "MinGW Makefiles"
|
||||||
@REM cmake -B build -G "Unix Makefiles"
|
cmake -B build -G "Unix Makefiles"
|
||||||
|
|
||||||
make -C build
|
make -C build
|
||||||
make -C build install
|
make -C build install
|
||||||
|
54
src/tree.c
54
src/tree.c
@ -10,6 +10,7 @@
|
|||||||
*/
|
*/
|
||||||
#include "tree.h"
|
#include "tree.h"
|
||||||
#include "queue.h"
|
#include "queue.h"
|
||||||
|
#include "stack.h"
|
||||||
|
|
||||||
#if RAVLTREE == 1
|
#if RAVLTREE == 1
|
||||||
// function declare
|
// function declare
|
||||||
@ -1981,6 +1982,7 @@ void tree_avl_preorder(struct _tree* self, struct _tree_node* root)
|
|||||||
|
|
||||||
void tree_avl_inorder(struct _tree* self, struct _tree_node* root)
|
void tree_avl_inorder(struct _tree* self, struct _tree_node* root)
|
||||||
{
|
{
|
||||||
|
#if 0
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
if(root == NULL)
|
if(root == NULL)
|
||||||
{
|
{
|
||||||
@ -1996,7 +1998,7 @@ void tree_avl_inorder(struct _tree* self, struct _tree_node* root)
|
|||||||
self->print_obj(root->obj);
|
self->print_obj(root->obj);
|
||||||
if(root->right != NULL)
|
if(root->right != NULL)
|
||||||
{
|
{
|
||||||
tree_avl_inorder(self, root->right);
|
tree_avl_inorder(self, root->right);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -2011,6 +2013,56 @@ void tree_avl_inorder(struct _tree* self, struct _tree_node* root)
|
|||||||
tree_avl_inorder(self, root->left);
|
tree_avl_inorder(self, root->left);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
assert(self != NULL);
|
||||||
|
if(root == NULL)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct _tree_node *node = root;
|
||||||
|
|
||||||
|
stack_t stack = stack_new();
|
||||||
|
stack_init(stack, sizeof(struct _tree_node*));
|
||||||
|
|
||||||
|
if(!self->_right_priority)
|
||||||
|
{
|
||||||
|
while(node != NULL || !stack->empty(stack))
|
||||||
|
{
|
||||||
|
while(node != NULL)
|
||||||
|
{
|
||||||
|
stack->push(stack, &node);
|
||||||
|
node = node->left;
|
||||||
|
}
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
stack->pop(stack, &node);
|
||||||
|
self->print_obj(node->obj);
|
||||||
|
}while(node->right == NULL && !stack->empty(stack));
|
||||||
|
node = node->right;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
while(node != NULL || !stack->empty(stack))
|
||||||
|
{
|
||||||
|
while(node != NULL)
|
||||||
|
{
|
||||||
|
stack->push(stack, &node);
|
||||||
|
node = node->right;
|
||||||
|
}
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
stack->pop(stack, &node);
|
||||||
|
self->print_obj(node->obj);
|
||||||
|
}while(node->left == NULL && !stack->empty(stack));
|
||||||
|
node = node->left;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stack_free(stack);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void tree_avl_postorder(struct _tree* self, struct _tree_node* root)
|
void tree_avl_postorder(struct _tree* self, struct _tree_node* root)
|
||||||
|
Loading…
Reference in New Issue
Block a user