//非递归先序遍历
void PreOrderTraverse(BiTree b) {
InitStack(S);
BiTree p = b;
while (p || !IsEmpty(S)) {
while (p) {
printf("%c", p->data); //先序先遍历结点
Push(S, p); //进栈保存
p = p->lchild;
}
if (!IsEmpty(S)) {
p = Pop(S);
p = p->rchild;
}
}
}
//非递归中序遍历
void PreOrderTraverse(BiTree b) {
InitStack(S);
BiTree p = b; //工作指针p
while (p || !IsEmpty(S)) {
while (p) { //中序先将结点进栈保存
Push(S, p);
p = p->lchild;
}
//遍历到左下角尽头再出栈访问
p = Pop(S);
printf("%c", p->data);
p = p->rchild; //遍历右孩子
}
}
//非递归后序遍历
void PostOrderTraverse(BiTree b) {
InitStack(S);
BiTree p = b, r = NULL; //工作指针p 辅助指针r
while (p || !IsEmpty(S)) {
//从根结点到最左下角的左子树都入栈
if (p) {
Push(S, p);
二叉树的遍历代码(非递归)
最新推荐文章于 2024-05-18 10:53:04 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)