您的位置:首页 > 其它

二叉树遍历,递归和非递归方法

2016-02-25 11:37 260 查看
// BinTree.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <string>
#include <stack>

using namespace std;

typedef struct BinTree
{
BinTree* lChild;
BinTree* rChild;
string strValue;
}BinTree;

void preOrder(BinTree* root);
void preOrderNoneRecursion(BinTree* root);

void inOrder(BinTree* root);
void inOrderrNoneRecursion(BinTree* root);

void postOrder(BinTree* root);
void postOrderNoneRecursion(BinTree* root);

BinTree* formTestBinTree();

int _tmain(int argc, _TCHAR* argv[])
{
BinTree* root = formTestBinTree();

printf("preOrder recursion:");
preOrder(root);
printf("\n");

printf("preOrder none recursion:");
preOrderNoneRecursion(root);
printf("\n");

printf("inOrder recursion:");
inOrder(root);
printf("\n");

printf("inOrder none recursion:");
inOrderrNoneRecursion(root);
printf("\n");

printf("postOrder recursion:");
postOrder(root);
printf("\n");

printf("postOrder none recursion:");
postOrderNoneRecursion(root);
printf("\n");

return 0;
}

BinTree* formTestBinTree()
{
//layer3
BinTree* layer3_1 = new BinTree();
layer3_1->strValue = 'D';
layer3_1->lChild = NULL;
layer3_1->rChild = NULL;

BinTree* layer3_2 = new BinTree();
layer3_2->strValue = 'E';
layer3_2->lChild = NULL;
layer3_2->rChild = NULL;

BinTree* layer3_3 = new BinTree();
layer3_3->strValue = 'F';
layer3_3->lChild = NULL;
layer3_3->rChild = NULL;

//layer2
BinTree* layer2_1 = new BinTree();
layer2_1->strValue = 'B';
layer2_1->lChild = layer3_1;
layer2_1->rChild = layer3_2;

BinTree* layer2_2 = new BinTree();
layer2_2->strValue = 'C';
layer2_2->lChild = layer3_3;
layer2_2->rChild = NULL;

BinTree* root = new BinTree();
root->strValue = 'A';
root->lChild = layer2_1;
root->rChild = layer2_2;

return root;
}

void preOrder(BinTree* root)
{
if(!root)
{
return;
}

//_tprintf(TEXT("%s"), root->strValue);
printf("%s", root->strValue.c_str());
preOrder(root->lChild);
preOrder(root->rChild);
}

void preOrderNoneRecursion(BinTree* root)
{
stack<BinTree*> s;
BinTree *p=root;
while(p!=NULL||!s.empty())
{
while(p!=NULL)
{
printf("%s", p->strValue.c_str());
s.push(p);
p=p->lChild;
}
if(!s.empty())
{
p=s.top();
s.pop();
p=p->rChild;
}
}
}

void inOrder(BinTree* root)
{
if(!root)
{
return;
}

inOrder(root->lChild);
printf("%s", root->strValue.c_str());
inOrder(root->rChild);
}

void inOrderrNoneRecursion(BinTree* root)
{
stack<BinTree*> s;
BinTree *p=root;
while(p!=NULL||!s.empty())
{
while(p!=NULL)
{
s.push(p);
p=p->lChild;
}
if(!s.empty())
{
p=s.top();
printf("%s", p->strValue.c_str());
s.pop();
p=p->rChild;
}
}
}

void postOrder(BinTree* root)
{
if(!root)
{
return;
}

postOrder(root->lChild);
postOrder(root->rChild);
printf("%s", root->strValue.c_str());
}

void postOrderNoneRecursion(BinTree* root)
{
stack<BinTree*> s;
BinTree *cur;                      //当前结点
BinTree *pre=NULL;                 //前一次访问的结点
s.push(root);
while(!s.empty())
{
cur=s.top();
if((cur->lChild==NULL && cur->rChild==NULL)
|| (pre!=NULL && (pre==cur->lChild||pre==cur->rChild)))
{
printf("%s", cur->strValue.c_str());  //如果当前结点没有孩子结点或者孩子节点都已被访问过
s.pop();
pre=cur;
}
else
{
if(cur->rChild != NULL)
s.push(cur->rChild);
if(cur->lChild != NULL)
s.push(cur->lChild);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  二叉树 非递归