二叉树的遍历方式-递归与非递归

//============================================================================
//
// >     File        :    
// >     Author      :    flowertree
// >     Time        :    2016年4月24日
// >     Algorithm   :    二叉树的三种递归遍历和非递归
//
//============================================================================

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;

//树节点
struct treenode
{
	int value;
	treenode * left;
	treenode * right;
	treenode()
	{
		this -> value = 0;
		this -> left = NULL;
		this -> right = NULL;
	}
};

treenode n[10];
int nodepool = 1;

//获得树节点地址
treenode * getTreeNode()
{
	return &n[nodepool++];
}

//创建树
treenode * initTree(treenode * head, int number)
{
	queue<treenode *> q;
	q.push(head);
	treenode * temp;
	while(number < 7)
	{
		temp = q.front();
		q.pop();
		treenode * a = getTreeNode();
		a -> value = number++;
		temp -> left = a;
		q.push(a);
		treenode * b = getTreeNode();
		b ->value = number++;
		temp -> right = b;
		q.push(b);
	}
	return head;
}

//输出树
void printTree(treenode * head, int blank)
{
	if(head == NULL)
		return ;
	for(int i = 0; i < blank; i++)
		cout << ' ';
	cout << head -> value << endl;
	blank += 4;
	printTree(head -> left, blank);
	printTree(head -> right, blank);
}

//递归先序遍历
void prePrintTree(treenode * head)
{
	if(head == NULL)
		return ;
	cout << head -> value << " ";
	prePrintTree(head -> left);
	prePrintTree(head -> right);
}
//递归中序遍历
void inPrintTree(treenode * head)
{
	if(head == NULL)
		return ;
	inPrintTree(head -> left);
	cout << head -> value << " ";
	inPrintTree(head -> right);
}

//递归后序遍历
void posPrintTree(treenode * head)
{
	if(head == NULL)
		return ;
	posPrintTree(head -> left);
	posPrintTree(head -> right);
	cout << head -> value << " ";
}

//非递归先序遍历(用栈,不能用队列)
void noRePrePrintTree(treenode * head)
{
	stack<treenode *> p;
	p.push(head);
	treenode * temp;
	while(!p.empty())
	{
		temp = p.top();
		p.pop();
		cout << temp -> value << " ";
		if(temp -> right != NULL)
			p.push(temp -> right);
		if(temp -> left != NULL)
			p.push(temp -> left);
	}
}

//非递归中序遍历
void noReInPrintTree(treenode * head)
{
	if(head == NULL)
		return ;
	stack<treenode *> p;
	p.push(head);
	treenode * temp = head;
	while(!p.empty() || (temp != NULL))
	{
		if(temp != NULL)
		{
			p.push(temp -> left);
			temp = temp -> left;
		}
		else
		{
			p.pop();
			if(p.empty())
				continue;
			temp = p.top();
			cout << temp -> value << " ";
			p.pop();
			p.push(temp -> right);
			temp = temp -> right;
		}
	}
}

//非递归后序遍历(方式1:用两个栈)
void noRePosPrintTree1(treenode * head)
{
	if(head == NULL)
		return ;
	stack<treenode *> p1;
	stack<treenode *> p2;
	p1.push(head);
	while(!p1.empty())
	{
		treenode * temp = p1.top();
		p1.pop();
		p2.push(temp);
		if(temp -> left != NULL)
		{
			p1.push(temp -> left);
		}
		if(temp -> right != NULL)
		{
			p1.push(temp -> right);
		}
	}
	while(!p2.empty())
	{
		treenode * temp = p2.top();
		p2.pop();
		cout << temp -> value << " ";
	}
}

//非递归后序遍历(方式2:用1个栈)
void noRePosPrintTree2(treenode * head)
{
	if(head == NULL)
		return ;
	stack<treenode * > p;
	treenode * last;
	last = NULL;
	treenode * now;
	p.push(head);
	while(!p.empty())
	{
		now = p.top();
		if(now -> left != NULL && now -> left != last && now -> right != last)
		{
			p.push(now -> left);
		}
		else if(now -> right != NULL && now -> right != last)
		{
			p.push(now -> right);
		}
		else
		{
			cout << now -> value << " ";
			last = now;
			p.pop();
		}
	}
}

int main()
{
	treenode head;
	int number = 1;
	//创建一个3层的满二叉树
	treenode * h = initTree(&head, number);
	int blank = 0;
	//输出树
	printTree(h, blank);
	//递归先序遍历
	cout << "递归先序遍历" << endl;
	prePrintTree(h);
	cout << endl;
	//递归中序遍历
	cout << "递归中序遍历" << endl;
	inPrintTree(h);
	cout << endl;
	//递归后序遍历
	cout << "递归后序遍历" << endl;
	posPrintTree(h);
	cout << endl;
	//非递归先序遍历
	cout << "非递归先序遍历" << endl;
	noRePrePrintTree(h);
	cout << endl;
	//非递归中序遍历
	cout << "非递归中序遍历" << endl;
	noReInPrintTree(h);
	cout << endl;
	//非递归后序遍历方式1:用2个栈
	cout << "非递归后序遍历1" << endl;
	noRePosPrintTree1(h);
	cout << endl;
	//非递归后序遍历方式2:用1个栈
	cout << "非递归后序遍历2" << endl;
	noRePosPrintTree2(h);
	cout << endl;
	system("PAUSE");
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值