
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
#define MAXSIZE 20
typedef struct bnode
{
char data;
struct bnode *lchild,*rchild;
}BNode,*bitree;
bitree pre_create()//创建二叉树
{
bitree bt;
char ch;
ch=getchar();
if(ch==' ')
return NULL;
else
{
bt=new BNode[sizeof(BNode)];
bt->data=ch;
bt->lchild=pre_create();
bt->rchild=pre_create();
}
}
void pre_order(bitree bt)//非递归先序遍历
{
stack<bitree> s;
bitree p=bt;
while(p||!s.empty())
{
while(p)
{
s.push(p);
cout<<p->data<<" ";
p=p->lchild;
}
if(!s.empty())
{
p=s.top()->rchild;
s.pop();

本文详细介绍了如何使用两个栈和一个带有标志位的栈实现二叉树的非递归先序、中序、后序遍历,并探讨了层次遍历的方法。通过实例解析,帮助读者深入理解这些遍历策略的实现过程。
最低0.47元/天 解锁文章
1204

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



