#include <stdio.h>
#include "link_stack.h"
/*
*二叉树非递归实现:链式栈的应用
*/
#define BT_TRUE 1
#define BT_FALSE 0
//每个节点:存放数据,有左右孩子(没有孩子,为NULL)
typedef struct BinTree{
char ch;
struct BinTree* lchild;
struct BinTree* rchild;
}STBinTree_def;
//自定义数据,每个节点需要有个标识,true(输出)或false(入栈)
typedef struct BTStackNode{
STNode_def node;
STBinTree_def* root;
int flg;
}BTStackNode_def;
//栈中节点创建
BTStackNode_def* bintree_stacknode_create(STBinTree_def* node, int flg)
{
BTStackNode_def* stack_node = (BTStackNode_def*)malloc(sizeof(BTStackNode_def));
stack_node->root = node;
stack_node->flg = flg;
return stack_node;
}
//二叉树递归遍历
void bintree_recursion(STBinTree_def* root)
{
if (root == NULL)
{
return;
}
#if 0
//先序遍历
printf("%c", root->ch);
bintree_recursion(root->lchild);
bintree_recursion(root->rchild);
#endif
#if 0
//中序遍历
bintree_recursion(root->lchild);
printf("%c", root->ch);
bintree_recursion(root->rchild);
#endif
#if 1
//后序遍历
bintree_recursion(root->lchild);
bintree_recursion(root->rchild);
printf("%c", root->ch);
#endif
}
//二叉树非递归遍历
void bintree_norecursion(STBinTree_def* root)
{
//栈初始化
STLinkStack_def *stack = stack_init();
//将根节点先入栈,并初始化为BT_FALSE
stack_push(stack, (STNode_def*)bintree_stacknode_create(root, BT_FALSE));
while (stack_size(stack) > 0)
{
//获取栈顶元素
BTStackNode_def *top = (BTStackNode_def*)stack_top(stack);
//弹出栈顶元素
stack_pop(stack);
if (top->root == NULL)
{
continue;
}
if (top->flg == BT_TRUE)
{
printf("%c", top->root->ch);
}
else
{
#if 0
//先序遍历,入栈是逆操作
//当前节点右节点入栈
stack_push(stack, (STNode_def*)bintree_stacknode_create(top->root->rchild, BT_FALSE));
//当前节点左节点入栈
stack_push(stack, (STNode_def*)bintree_stacknode_create(top->root->lchild, BT_FALSE));
//当前节点入栈
top->flg = BT_TRUE;
stack_push(stack, (STNode_def*)top);
#endif
#if 0
//中序遍历,入栈是逆操作
//当前节点右节点入栈
stack_push(stack, (STNode_def*)bintree_stacknode_create(top->root->rchild, BT_FALSE));
//当前节点入栈
top->flg = BT_TRUE;
stack_push(stack, (STNode_def*)top);
//当前节点左节点入栈
stack_push(stack, (STNode_def*)bintree_stacknode_create(top->root->lchild, BT_FALSE));
#endif
#if 1
//后序遍历,入栈是逆操作
//当前节点入栈
top->flg = BT_TRUE;
stack_push(stack, (STNode_def*)top);
//当前节点右节点入栈
stack_push(stack, (STNode_def*)bintree_stacknode_create(top->root->rchild, BT_FALSE));
//当前节点左节点入栈
stack_push(stack, (STNode_def*)bintree_stacknode_create(top->root->lchild, BT_FALSE));
#endif
}
}
}
void bintree_create()
{
STBinTree_def node1={'A', NULL, NULL};
STBinTree_def node2={'B', NULL, NULL};
STBinTree_def node3={'C', NULL, NULL};
STBinTree_def node4={'D', NULL, NULL};
STBinTree_def node5={'E', NULL, NULL};
STBinTree_def node6={'F', NULL, NULL};
STBinTree_def node7={'G', NULL, NULL};
STBinTree_def node8={'H', NULL, NULL};
node1.lchild = &node2;
node1.rchild = &node6;
node2.rchild = &node3;
node3.lchild = &node4;
node3.rchild = &node5;
node6.rchild = &node7;
node7.lchild = &node8;
bintree_recursion(&node1);
printf("\n");
bintree_norecursion(&node1);
printf("\n");
}
int main()
{
bintree_create();
return 0;
}
数据结构 二叉树遍历
二叉树非递归遍历
最新推荐文章于 2024-07-15 17:15:24 发布
936

被折叠的 条评论
为什么被折叠?



