第七天 树的遍历

         最近真是各种事情缠绕在一起,一直不敢挥霍时间刷题,惭愧惭愧。

         像链表一样,在练习树的算法之前先开一章,打好基础,主要是遍历的方法:前中后序遍历,非递归版本,广度(层次)遍历。

PS:深度遍历暂缺,另外后序的非递归版本copy别人的方法需要巩固。

         偷个懒,一股脑全部贴上来:

#include <vector> 
#include <iostream> 
#include <stdlib.h>
#include <stack>
#include <queue>
using namespace std;

struct TreeNode
{
	TreeNode *left;
	TreeNode *right;
	char val;
	TreeNode(char x);
	~TreeNode();
};

TreeNode::TreeNode(char x)
{
	val = x;
	left = 0;
	right = 0;
}

TreeNode::~TreeNode(){}

void visit(TreeNode *t)
{
	if (t)
		cout << t->val;
}

void preorder(TreeNode *root)
{
	if(root)
	{
		visit(root);
		preorder(root->left);
		preorder(root->right);
	}
}

void inorder(TreeNode *root)
{
	if(root)
	{
		inorder(root->left);
		visit(root);
		inorder(root->right);
	}
}

void postorder(TreeNode *root)
{
	if(root)
	{
		postorder(root->left);
		postorder(root->right);
		visit(root);
	}
}

void preorder2(TreeNode *root)
{
	stack<TreeNode *> s;
	while (root || !s.empty())
	{
		if (root)
		{
			s.push(root);
			cout << root->val;
			root = root->left;
		}
		else
		{
			root = s.top();
			s.pop();
			root = root->right;
		}
	}
}

void inorder2(TreeNode *root)
{
	stack<TreeNode *> s;
	while (root || !s.empty())
	{
		if (root)
		{
			s.push(root);
			root = root->left;
		}
		else
		{
			root = s.top();
			s.pop();
			cout << root->val;
			root = root->right;
		}
	}
}

typedef struct BiTNodePost{  
    TreeNode * biTree;  
    char tag;  
}BiTNodePost,*BiTreePost;  

void postorder2(TreeNode *root)
{
	stack<BiTreePost> stack;  
	//p是遍历指针  
	TreeNode * p = root;  
	BiTreePost BT;  
	//栈不空或者p不空时循环  
	while(p != NULL || !stack.empty())
	{  
		//遍历左子树  
		while(p != NULL)
		{  
			BT = (BiTreePost)malloc(sizeof(BiTNodePost));  
			BT->biTree = p;  
			//访问过左子树  
			BT->tag = 'L';  
			stack.push(BT);  
			p = p->left;  
		}  
		//左右子树访问完毕访问根节点  
		while(!stack.empty() && (stack.top())->tag == 'R'){  
			BT = stack.top();  
			//退栈  
			stack.pop();  
			printf("%c",BT->biTree->val);  
		}  
		//遍历右子树  
		if(!stack.empty())
		{  
			BT = stack.top();  
			//访问过右子树  
			BT->tag = 'R';  
			p = BT->biTree;  
			p = p->right;  
		}  
	}//while  
}

void levelorder(TreeNode *root)
{
	queue<TreeNode *> q;
	do
	{
		if (root && q.empty())
		{
			q.push(root);
		}
		
		if (!q.empty())
		{
			root = q.front();			
			q.pop();
			cout << root->val;
			if (root->left)
				q.push(root->left);
			if (root->right)
				q.push(root->right);						
		}
	}while(!q.empty());
}

int main()
{
	// 初始化树结构
	//  A
	// / \
	// B C
	//  \ / \
	//  D E F
	//   \   \
	//    G   H
	//       / \
	//       I J
	//      / \
	//      K L
	TreeNode nA('A');
	TreeNode nB('B');
	TreeNode nC('C');
	TreeNode nD('D');
	TreeNode nE('E');
	TreeNode nF('F');
	TreeNode nG('G');
	TreeNode nH('H');
	TreeNode nI('I');
	TreeNode nJ('J');
	TreeNode nK('K');
	TreeNode nL('L');

	nA.left = &nB;
	nA.right = &nC;
	nB.right = &nD;
	nD.right = &nG;
	nC.left = &nE;
	nC.right = &nF;
	nF.right = &nH;
	nH.left = &nI;
	nH.right = &nJ;
	nI.left = &nK;
	nI.right = &nL;

	cout << "前序遍历:" << endl;
	preorder(&nA);
	cout << endl;
	cout << "前序遍历(非递归):" << endl;
	preorder2(&nA);
	cout << endl;

	cout << "中序遍历:" << endl;
	inorder(&nA);
	cout << endl;
	cout << "中序遍历(非递归):" << endl;
	inorder2(&nA);
	cout << endl;

	cout << "后序遍历:" << endl;
	postorder(&nA);
	cout << endl;
	cout << "后序遍历(非递归):" << endl;
	postorder2(&nA);
	cout << endl;

	cout << "广度(层次)遍历:" << endl;
	levelorder(&nA);
	cout << endl;
	return 0;
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值