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

// 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);
		}
	}    
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值