首页 > 代码库 > 二叉树三种遍历非递归算法
二叉树三种遍历非递归算法
http://blog.csdn.net/pipisorry/article/details/37353037
c实现:
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); //tag为R,表示右子树访问完毕,故访问根结点
}
if (!StackEmpty(s)) {
s.Elem[s.top].tag =R; //遍历右子树
p=s.Elem[s.top].ptr->rchild;
}
} while (!StackEmpty(s));
}//PostOrderUnrec
#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); //tag为R,表示右子树访问完毕,故访问根结点
}
if (!StackEmpty(s)) {
s.Elem[s.top].tag =R; //遍历右子树
p=s.Elem[s.top].ptr->rchild;
}
} while (!StackEmpty(s));
}//PostOrderUnrec
c++实现:
ref:二叉树遍历之非递归算法http://blog.csdn.net/sgbfblog/article/details/7773103
ref:http://siwei1987.blog.51cto.com/430256/118551
from:http://blog.csdn.net/pipisorry/article/details/37353037
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。