数据结构与算法——树的构建和遍历

树的构建方法:通过前序和中序或者中序和后序可以重构二叉树,故也可以通过该方法构建一个二叉树
遍历:前序,中序,后序和层序,都采用迭代方式。

#include<iostream>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
struct TreeNode{
	int val;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int x): val(x),left(NULL),right(NULL){
	}
}; 
//通过前序和中序来构造二叉树 
TreeNode* construct(vector<int> pre,vector<int> in)
{
	if(pre.size() == 0 || pre.size() != in.size() )
		return NULL; 
	vector<int> lpre,rpre,lin,rin;
	TreeNode* root = new TreeNode(pre[0]);
	int i = 0;
	while(in[i]!=pre[0])
	{
		i++;
	}
	for(int j = 0; j < i; j++)
	{
		lpre.push_back(pre[j+1]);
		lin.push_back(in[j]);
	}
	for(int j = i+1; j < pre.size(); j++)
	{
		rpre.push_back(pre[j]);
		rin.push_back(in[j]); 
	}
	root->left = construct(lpre,lin);
	root->right = construct(rpre,rin);
	return root;
}
//前序遍历 
vector<int> preorder(TreeNode *root)
{
	
	if(root == NULL)
		return {};
//	res.push_back(root->val);
//	preorder(root->left);
//	preorder(root->right) ;
	stack<TreeNode*> s;
	vector<int> res;
	s.push(root);
	while(!s.empty())
	{
		root = s.top();
		s.pop();
		res.push_back(root->val);
		if(root->right!=NULL)
			s.push(root->right);
		if(root->left!=NULL)
			s.push(root->left) ;
	}
	return res;
}
//中序遍历 
vector<int> inorder(TreeNode* root)
{
	if(root == NULL)
		return {};
	stack<TreeNode*> s;
	vector<int> res;
	while(!s.empty() || root != NULL) 
	{
		if(root != NULL)
		{
			s.push(root);
			root = root -> left;
		}
		else
		{
			root = s.top();
			s.pop();
			res.push_back(root->val);
			root = root -> right;
		}
	}
	return res;
 } 
 //后序遍历 
vector<int> backorder(TreeNode* root)
{
	if(root == NULL)
		return {};
	stack<TreeNode*> s1,s2;
	vector<int> res;
	s1.push(root);
	while(!s1.empty())
	{
		root = s1.top();
		s1.pop();
		s2.push(root);
		if(root->left)
			s1.push(root->left);
		if(root->right)
			s1.push(root->right);
	} 
	while(!s2.empty())
	{
		res.push_back(s2.top()->val);
		s2.pop(); 
	}	
	return res;
}
//层序遍历 
vector< vector<int> > levelorder(TreeNode *root)
{
	if(root == NULL)
		return {};
	vector<vector<int> > res;
	queue<TreeNode*> q;
	q.push(root);
	while(!q.empty())
	{
		int sz = q.size();
		vector<int> temp;
		while(sz--)
		{
			root = q.front();
			temp.push_back(root->val);
			q.pop();
			if(root->left != NULL)
				q.push(root->left);
			if(root->right != NULL)
				q.push(root->right);
		}
		res.push_back(temp); 
	}
	return res;
}
void print(vector<int> nums)
{
	cout<<"当前数组中的值为:\n"; 
	for(int i = 0; i < nums.size(); i++)
		cout<<nums[i]<<" ";
	cout<<endl; 
}
void print2dim(vector< vector<int> > nums)
{ 
	for(int i = 0; i < nums.size(); i++)
	{
		for(int j = 0; j < nums[i].size(); j++)
		{
			cout<<nums[i][j]<<" ";
		}
		cout<<endl;
	}
	cout<<endl; 
}
vector<int> insert(int a[],int sz)
{
	vector<int> res;
	for(int i = 0; i < sz; ++i)
		res.push_back(a[i]) ;
	return res;
}

int main()
{
	int a[9] = {3,5,6,2,7,4,1,0,8};//前序遍历
	int b[9] = {6,5,7,2,4,3,0,1,8};//中序遍历
	int len = sizeof(a)/sizeof(int);
	vector<int> pre = insert(a,len);
	vector<int> in  = insert(b,len); 
	print(pre);
	print(in);
	TreeNode* proot = construct(pre,in);
	cout<<"前序遍历:\n"; 
	vector<int> result1 = preorder(proot);
	print(result1);
	cout<<"中序遍历:\n";
	vector<int> result2 = inorder(proot);
	print(result2);
	cout<<"后序遍历:\n"; 
	vector<int> result3 = backorder(proot);
	print(result3);
	cout<<"层序遍历:\n";
	vector<vector<int> > result4 = levelorder(proot);
	print2dim(result4);
}

二叉树结构
遍历结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值