用栈实现求高度,另外优化中序遍历代码。

This commit is contained in:
建峰 2024-06-25 09:47:43 +08:00
parent 6c7ce7f04e
commit 4fa3a06e8a
2 changed files with 63 additions and 11 deletions

4
mk.bat
View File

@ -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"
@REM cmake -B build -G "MinGW Makefiles" cmake -B build -G "MinGW Makefiles"
cmake -B build -G "Unix Makefiles" @REM cmake -B build -G "Unix Makefiles"
make -C build make -C build
make -C build install make -C build install

View File

@ -1521,14 +1521,50 @@ static struct _tree_node* tree_trun_right_then_left(struct _tree* self, struct _
int32_t tree_height(struct _tree* self, struct _tree_node* root) int32_t tree_height(struct _tree* self, struct _tree_node* root)
{ {
#if 0
assert(self != NULL); assert(self != NULL);
if(root == NULL) if(root == NULL)
{ {
return 0; return 0;
} }
uint32_t left_height = tree_height(self, root->left); int32_t left_height = tree_height(self, root->left);
uint32_t right_height = tree_height(self, root->right); int32_t right_height = tree_height(self, root->right);
return (left_height > right_height) ? (left_height + 1) : (right_height + 1); return (left_height > right_height) ? (left_height + 1) : (right_height + 1);
#else
assert(self != NULL);
if(root == NULL)
{
return 0;
}
struct _tree_node *node = root;
int32_t left_height = 0, right_height = 0;
int32_t left_height_max = 0, right_height_max = 0;
stack_t stack = stack_new();
stack_init(stack, sizeof(struct _tree_node*));
stack->push(stack, &root);
while(!stack->empty(stack))
{
stack->pop(stack, &node);
left_height = stack->size(stack);
if(left_height > left_height_max)
{
left_height_max = left_height;
}
if(node->right != NULL)
{
stack->push(stack, &node->right);
}
if(node->left != NULL)
{
stack->push(stack, &node->left);
}
}
stack_free(stack);
return left_height_max > right_height_max ? left_height_max : right_height_max;
#endif
} }
/** /**
@ -2069,7 +2105,7 @@ void tree_avl_inorder(struct _tree* self, struct _tree_node* root)
if(!self->_right_priority) if(!self->_right_priority)
{ {
while(node != NULL || !stack->empty(stack)) while(!stack->empty(stack) || node != NULL)
{ {
while(node != NULL) while(node != NULL)
{ {
@ -2077,14 +2113,22 @@ void tree_avl_inorder(struct _tree* self, struct _tree_node* root)
node = node->left; node = node->left;
} }
do // do
// {
// stack->pop(stack, &node);
// self->print_obj(node->obj);
// }while(node->right == NULL && !stack->empty(stack));
// node = node->right;
// optimize code
if(!stack->empty(stack))
{ {
stack->pop(stack, &node); stack->pop(stack, &node);
self->print_obj(node->obj); self->print_obj(node->obj);
}while(node->right == NULL && !stack->empty(stack));
node = node->right; node = node->right;
} }
} }
}
else else
{ {
while(node != NULL || !stack->empty(stack)) while(node != NULL || !stack->empty(stack))
@ -2095,14 +2139,22 @@ void tree_avl_inorder(struct _tree* self, struct _tree_node* root)
node = node->right; node = node->right;
} }
do // do
// {
// stack->pop(stack, &node);
// self->print_obj(node->obj);
// }while(node->left == NULL && !stack->empty(stack));
// node = node->left;
// optimize code
if(!stack->empty(stack))
{ {
stack->pop(stack, &node); stack->pop(stack, &node);
self->print_obj(node->obj); self->print_obj(node->obj);
}while(node->left == NULL && !stack->empty(stack));
node = node->left; node = node->left;
} }
} }
}
stack_free(stack); stack_free(stack);
#endif #endif
} }