二叉树遍历的非递归算法
来源:互联网 发布:java下载doc文件 编辑:程序博客网 时间:2024/05/16 05:17
1.#define maxsize 100
typedef struct
{
Bitree Elem[maxsize];
int top;
}SqStack;
void PreOrderUnrec(Bitree t)
{
SqStack s;
StackInit(s);
p=t;
while (p!=null || !StackEmpty(s))
{
while (p!=null) //{
visite(p->data);
push(s,p);
p=p->lchild;
}//endwhile
if (!StackEmpty(s)) //while{
p=pop(s);
p=p->rchild;
}//endif
}//endwhile
}//PreOrderUnrec
2.#define maxsize 100
typedef struct
{
Bitree Elem[maxsize];
int top;
}SqStack;
void InOrderUnrec(Bitree t)
{
SqStack s;
StackInit(s);
p=t;
while (p!=null || !StackEmpty(s))
{
while (p!=null) //{
push(s,p);
p=p->lchild;
}//endwhile
if (!StackEmpty(s))
{
p=pop(s);
visite(p->data); //p=p->rchild; //}//endif
}//endwhile
}//InOrderUnrec
3.#define maxsize 100
typedef enum{L,R} tagtype;
typedef struct
{
Bitree ptr;
tagtype tag;
}stacknode;
typedef struct
{
stacknode Elem[maxsize];
int top;
}SqStack;
void PostOrderUnrec(Bitree t)
{
SqStack s;
stacknode x;
StackInit(s);
p=t;
do
{
while (p!=null) //{
x.ptr = p;
x.tag = L; //push(s,x);
p=p->lchild;
}
while (!StackEmpty(s) && s.Elem[s.top].tag==R)
{
x = pop(s);
p = x.ptr;
visite(p->data); //tagR
}
if (!StackEmpty(s))
{
s.Elem[s.top].tag =R; //p=s.Elem[s.top].ptr->rchild;
}
}while (!StackEmpty(s));
}//PostOrderUnrec
typedef struct
{
Bitree Elem[maxsize];
int top;
}SqStack;
void PreOrderUnrec(Bitree t)
{
SqStack s;
StackInit(s);
p=t;
while (p!=null || !StackEmpty(s))
{
while (p!=null) //{
visite(p->data);
push(s,p);
p=p->lchild;
}//endwhile
if (!StackEmpty(s)) //while{
p=pop(s);
p=p->rchild;
}//endif
}//endwhile
}//PreOrderUnrec
2.#define maxsize 100
typedef struct
{
Bitree Elem[maxsize];
int top;
}SqStack;
void InOrderUnrec(Bitree t)
{
SqStack s;
StackInit(s);
p=t;
while (p!=null || !StackEmpty(s))
{
while (p!=null) //{
push(s,p);
p=p->lchild;
}//endwhile
if (!StackEmpty(s))
{
p=pop(s);
visite(p->data); //p=p->rchild; //}//endif
}//endwhile
}//InOrderUnrec
3.#define maxsize 100
typedef enum{L,R} tagtype;
typedef struct
{
Bitree ptr;
tagtype tag;
}stacknode;
typedef struct
{
stacknode Elem[maxsize];
int top;
}SqStack;
void PostOrderUnrec(Bitree t)
{
SqStack s;
stacknode x;
StackInit(s);
p=t;
do
{
while (p!=null) //{
x.ptr = p;
x.tag = L; //push(s,x);
p=p->lchild;
}
while (!StackEmpty(s) && s.Elem[s.top].tag==R)
{
x = pop(s);
p = x.ptr;
visite(p->data); //tagR
}
if (!StackEmpty(s))
{
s.Elem[s.top].tag =R; //p=s.Elem[s.top].ptr->rchild;
}
}while (!StackEmpty(s));
}//PostOrderUnrec
- 二叉树遍历的非递归算法
- 遍历二叉树的非递归算法
- 遍历二叉树的非递归算法
- 二叉树的遍历非递归算法
- 遍历二叉树的非递归算法
- 二叉树的遍历非递归算法
- 二叉树遍历的非递归算法
- 二叉树的非递归遍历算法
- 二叉树遍历的非递归算法
- 二叉树遍历的非递归算法
- 二叉树遍历的非递归算法
- 二叉树遍历的非递归算法
- 二叉树的非递归遍历算法
- 二叉树遍历的非递归算法
- 遍历二叉树的递归算法与非递归算法
- 二叉树的递归和非递归的遍历算法
- 遍历二叉树的递归和非递归算法
- 二叉树遍历的递归与非递归算法
- 基于IC卡技术的城市信息平台的建设
- struts整合spring
- 关于前台调用后台事件__doPostBack函数
- c和汇编实现 一些基本字符串处理函数
- 全面掌握硬盘技术与硬盘知识
- 二叉树遍历的非递归算法
- 二叉树遍历之递归算法
- 一篇关于web.xml配置的详细说明
- 手机Linux依然面临内忧外患
- 8 个开发更安全代码的简单规则
- 程序设计中的感悟
- 三天不练手生(数据结构)
- Struts 2.0初体验-参考Tutorials写的登录页面
- 用css+div如何实现表格显示