二叉树的遍历

#include <iostream>
#include <windows.h>
#include <stack>
#include <set>
using namespace std;


struct TreeNode {
	int value;
	TreeNode* lChild;
	TreeNode* rChild;
	TreeNode(int v) :value(v), lChild(NULL), rChild(NULL)
	{

	}
};

void PrintValue(int value)
{
	cout << value << " ";
}

void InOrder(TreeNode* root)
{
	cout << "*****************中序遍历*********************" << endl;
	if (NULL == root)
		return;
	stack<TreeNode*> treeStack;
	treeStack.push(root);
	TreeNode*  curLeftChild = root->lChild;
	while (curLeftChild)
	{
		treeStack.push(curLeftChild);
		curLeftChild = curLeftChild->lChild;
	}
	while (!treeStack.empty())
	{
		TreeNode* curNode = treeStack.top();
		treeStack.pop();
		PrintValue(curNode->value);
		if (curNode->rChild)
		{
			treeStack.push(curNode->rChild);
			curNode = curNode->rChild;
			while (curNode->lChild)
			{
				treeStack.push(curNode->lChild);
				curNode = curNode->lChild;
			}
		}
	}
	cout << endl;
	cout << "**********************************************" << endl;
}

void InOrderR(TreeNode* root)
{
	
	if (NULL == root)
		return;
	InOrderR(root->lChild);
	PrintValue(root->value);
	InOrderR(root->rChild);
}

void PreOrder(TreeNode* root)
{
	cout << "*****************前序遍历*********************" << endl;
	if (NULL == root)
		return;
	TreeNode* curNode = root;
	stack<TreeNode*> treeS;
	while (curNode)
	{
		PrintValue(curNode->value);
		treeS.push(curNode);
		curNode = curNode->lChild;
	}
	while (!treeS.empty())
	{
		curNode = treeS.top();
		treeS.pop();
		if (curNode->rChild)
		{
			curNode = curNode->rChild;
			PrintValue(curNode->value);
			treeS.push(curNode);
			while (curNode->lChild)
			{
				curNode = curNode->lChild;
				PrintValue(curNode->value);
				treeS.push(curNode);
			}
		}
	}
	cout << endl;
	cout << "**********************************************" << endl;
}
void PreOrderR(TreeNode* root)
{
	if (NULL == root)
		return;
	PrintValue(root->value);
	PreOrderR(root->lChild);
	PreOrderR(root->rChild);
}

void PostOrder(TreeNode* root)
{
	cout << "*****************后序遍历*********************" << endl;
	if (NULL == root)
		return;
	stack<TreeNode*> treeStack;
	set<TreeNode*> reachSet;
	treeStack.push(root);
	TreeNode*  curLeftChild = root->lChild;
	while (curLeftChild)
	{
		treeStack.push(curLeftChild);
		curLeftChild = curLeftChild->lChild;
	}
	while (!treeStack.empty())
	{
		TreeNode* curNode = treeStack.top();
		if (curNode->rChild&&(reachSet.find(curNode)== reachSet.end()))
		{
			reachSet.insert(curNode);
			curNode = curNode->rChild;
			treeStack.push(curNode);
			while (curNode->lChild)
			{
				treeStack.push(curNode->lChild);
				curNode = curNode->lChild;
			}
		}
		else
		{
			PrintValue(curNode->value);
			treeStack.pop();
		}
	}
	cout << endl;
	cout << "**********************************************" << endl;
}
void PostOrderR(TreeNode* root)
{
	if (NULL == root)
		return;
	PostOrderR(root->lChild);
	PostOrderR(root->rChild);
	PrintValue(root->value);
}

int main(int argc, char* argv[])
{
	TreeNode* root = new TreeNode(1);
	TreeNode* l1 = new TreeNode(2);
	TreeNode* r1 = new TreeNode(3);
	root->lChild = l1;
	l1->rChild = r1;
	TreeNode* l2 = new TreeNode(4);
	TreeNode* r2 = new TreeNode(5);
	r1->lChild = l2;
	l2->rChild = r2;
	TreeNode* l3 = new TreeNode(6);
	TreeNode* r3 = new TreeNode(7);
	r2->lChild = l3;
	l3->rChild = r3;
	InOrder(root);
	cout << "*****************中序遍历递归*********************" << endl;
	InOrderR(root);
	cout << endl;
	cout << "**********************************************" << endl;
	PreOrder(root);
	cout << "*****************前序遍历递归*********************" << endl;
	PreOrderR(root);
	cout << endl;
	cout << "**********************************************" << endl;
	PostOrder(root);
	cout << "*****************后序遍历递归*********************" << endl;
	PostOrderR(root);
	cout << endl;
	cout << "**********************************************" << endl;
	system("pause");
	return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值