数据结构考前背代码4:二叉树的七种遍历和三种构建

#include<iostream>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
typedef struct tree
{
	char data;
	tree* left;
	tree* right;
}tree;
tree* creat(string f, string i, int left1, int right1, int left2, int right2)
{
	if (left1 > right1 || left2 > right2)
	{
		return nullptr;
	}
	tree* root = new tree;
	 root->data = f[left1];
	int mid = 0;
	while (i[mid] != root->data)
	{
		mid++;
	}
	int leftsize = mid - left2;
	root->left = creat(f, i, left1 + 1, left1 + leftsize, left2, mid - 1);
	root->right = creat(f, i, left1 + leftsize + 1, right1, mid + 1, right2);
	return root;
}
tree* creat1(string i, string b, int left1, int right1, int left2, int right2)
{
	if (left1 > right1 || left2 > right2)
	{
		return nullptr;
	}
	tree* root = new tree;
	root->data = b[right2];
	int mid = 0;
	while (b[mid] != root->data)
	{
		mid++;
	}
	int leftsize = mid - left1;
	root->left = creat1(i, b, left1, mid-1, left2, left2+leftsize);
	root->right = creat1(i, b, mid+1, right1, left2+leftsize+1, right2-1);
}
tree* creat1(string f, string b, int left1, int right1, int left2, int right2)
{
	if (left1 > right1 || left2 > right2)
	{
		return nullptr;
	}
	tree* root = new tree;
	root->data = f[left1];
	int mid = left2;
	while (b[mid] != f[left1+1])
	{
		mid++;
	}
	int leftsize = mid - left1;
	root->left = creat1(f, b, left1+1, left1+leftsize+1, left2, mid);
	root->right = creat1(f, b, left1+leftsize+2, right1, mid+1, right2 - 1);
}
void pf(tree* root)
{
	if (root == nullptr)
	{
		return;
	}
	cout << root->data;
	pf(root->left);
	pf(root->right);
}
void pi(tree* root)
{
	if (root == nullptr)
	{
		return;
	}
	pi(root->left);
	cout << root->data;

	pi(root->right);
}
void pb(tree* root)
{
	if (root == nullptr)
	{
		return;
	}
	pb(root->left);


	pb(root->right);
	cout << root->data;
}
void setf(tree* root, vector<char>& s)
{
	stack<tree*>st;
	tree* cur = root;
	while (cur != nullptr ||!s.empty())
	{
		while (cur)
		{
			st.push(cur);
			s.push_back(cur->data);
			cur = cur->left;
		}
		tree* cur1 = st.top();
		st.pop();
		cur = cur1->right;
	}


}
void seti(tree* root, vector<char>& s)
{
	stack<tree*>st;
	tree* cur = root;
	while (cur != nullptr || !s.empty())
	{
		while (cur)
		{
			st.push(cur);
			cur = cur->left;
		}
		tree* cur1 = st.top();
		st.pop();
		s.push_back(cur1->data);
		cur = cur1->right;
	}


}
void seti(tree* root, vector<char>& s)
{
	stack<tree*>st;
	tree* cur = root;
	tree* prev = nullptr;
	while (cur != nullptr || !s.empty())
	{
		while (cur)
		{
			st.push(cur);
			cur = cur->left;
		}
		tree* cur1 = st.top();
		if (cur1->right == nullptr || cur1->right == prev)
		{
			st.pop();
			s.push_back(cur1->data);
			prev = cur1;
		}
		else
		{
			cur = cur1->right;
		}
	}


}
void chengxu(tree* root, vector<vector<char>>&s)
{
	if (root == nullptr)
	{
		return ;
	}
	queue<tree*>qu;
	qu.push(root);
	while (!qu.empty())
	{
		int n = qu.size();
		s.push_back(vector<char>());
		for (int i = 0; i < n; i++)
		{
			tree* point = qu.front();
			qu.pop();
			s.back().push_back(point->data);
			if (point->left)
			{
				qu.push(point->left);
			}
			if (point->right)
			{
				qu.push(point->right);
			}
		}
	}
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值