您的位置:首页 > 其它

二叉树递归遍历和非递归遍历

2014-07-10 13:57 316 查看
// vs_demo.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <Windows.h>
#include <Shlobj.h>

typedef struct st_BiTreeNode
{
char data;
st_BiTreeNode* lchild;
st_BiTreeNode* rchild;
bool rchild_visited;
st_BiTreeNode(){
data = '#';
lchild = rchild = NULL;
rchild_visited = false;
}
}BiTreeNode, *BiTree;

const int kMaxBiTreeNodes = 100;

void VisitNode(BiTree t)
{
if (t)
{
printf("%c, ", t->data);
}
}

void PreVisit(BiTree t)
{
if (t)
{
VisitNode(t);
PreVisit(t->lchild);
PreVisit(t->rchild);
}
}

void PreVisitNoneRecursive(BiTree t)
{
if (t)
{
BiTree stack[kMaxBiTreeNodes];
int top = 0;
BiTree p = t;
while (p || top > 0)
{
while (p){
stack[top++] = p;
VisitNode(p);
p = p->lchild;
}

if (top > 0){
p = stack[--top];
p = p->rchild;
}

}
}
}

void InorderVisit(BiTree t)
{
if (t)
{
InorderVisit(t->lchild);
VisitNode(t);
InorderVisit(t->rchild);
}
}

void InorderVisitNoneRecursive(BiTree t)
{

if (t)
{
BiTree stack[kMaxBiTreeNodes];
int top = 0;
BiTree p = t;

while (p || top > 0)
{
while (p)
{
stack[top++] = p;
p = p->lchild;
}

if (top > 0)
{
p = stack[--top];
VisitNode(p);
p = p->rchild;
}
}

}
}
void PostVisit(BiTree t)
{
if (t)
{
BiTree l = t->lchild;
BiTree r = t->rchild;
PostVisit(l);
PostVisit(r);
VisitNode(t);
}

}

void PostVisitNoneRecursive(BiTree t)
{
if(t){
BiTree stack[kMaxBiTreeNodes];
int top = 0;
BiTree p = t;
if (p )
{
while (p)
{
stack[top++] = p;
p = p->lchild;
}

while (top > 0)
{
p = stack[--top];
if (!p->rchild || p->rchild_visited){
VisitNode(p);
}
else{
++top;
p->rchild_visited = true;
p = p->rchild;
while (p){
stack[top++] = p;
p = p->lchild;
}

}
}
}

}

}
<pre name="code" class="cpp">//LRD NoneRecursive
void PostOrderTraverse_NoneRecurisive(BiTree t)
{
if (t)
{
BiTree stack[MAX_BITREE_NODE];
int top = 0;
BiTree p = t;
BiTree q = NULL;//作为上一次访问的节点,

while (p || top > 0)
{
while (p)
{
stack[top++] = p;
p = p->lchild;

}
if (top>0)
{
p = stack[top-1];
if (p->rchild == NULL || p->rchild == q)
{
TraverseNode(p);
q = p;//备份用以判断是否为右子树根节点
p = NULL;
top--;
}
else{
p = p->rchild;
}
}

}
}
}



int _tmain(int argc, _TCHAR* argv[], _TCHAR* env[])
{
BiTree a = new BiTreeNode;a->data = 'a';
BiTree b = new BiTreeNode; b->data = 'b';
BiTree c= new BiTreeNode; c->data = 'c';
BiTree d = new BiTreeNode; d->data = 'd';
BiTree e = new BiTreeNode; e->data = 'e';
BiTree f = new BiTreeNode; f->data = 'f';
a->lchild = b; a->rchild = e; b->lchild = c; b->rchild = d; e->lchild = f;

printf("\nPreVisit\n");
PreVisit(a);
printf("\n");
PreVisitNoneRecursive(a);

printf("\nInorderVisit\n");
InorderVisit(a);
printf("\n");
InorderVisitNoneRecursive(a);

printf("\nPostVisit\n");
PostVisit(a);
printf("\n");
PostVisitNoneRecursive(a);

getchar();
delete a;delete b; delete c; delete d; delete e;delete f;
return 0;
}

输出:

PreVisit

a, b, c, d, e, f,

a, b, c, d, e, f,

InorderVisit

c, b, d, a, f, e,

c, b, d, a, f, e,

PostVisit

c, d, b, f, e, a,

c, d, b, f, e, a,
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: