二叉树(建立,插入,遍历,删除,由前序和中序重建树)

#include<iostream>
#include<stdio.h>
#include <windows.h>
#include<string>
#include<vector>
using namespace std;
typedef struct tree {
	int data;
	struct tree *left_tree;
	struct tree *right_tree;
}tree;

tree *node;
void creat_tree()
{
	node = (tree*)malloc(sizeof(tree));
	node->left_tree = NULL;
	node->right_tree = NULL;
	node->data = 5;

	tree *p;
	p= (tree*)malloc(sizeof(tree));
	p->left_tree = p->right_tree = NULL;
	p->data = 4;
	node->left_tree = p;
}


tree* tree_insert(tree *head_node, int data)    //插入
{
	//tree *p;
	if (data < head_node->data)
	{
		if (head_node->left_tree)
			tree_insert(head_node->left_tree, data);
		else
		{
			tree *p;
			p = (tree*)malloc(sizeof(tree));
			p->data = data;
			p->left_tree = p->right_tree = NULL;
			head_node->left_tree = p;
		}
	}
	else
	{
		if (head_node->right_tree)
			tree_insert(head_node->right_tree, data);
		else
		{
			tree *p;
			p = (tree*)malloc(sizeof(tree));
			p->data = data;
			p->left_tree = p->right_tree = NULL;
			head_node->right_tree = p;
		}
	}
	return head_node;
}
void pro_order(tree *head_node)   //前序遍历 :中左右
{
	if (head_node)
	{
		cout << head_node->data;
		pro_order(head_node->left_tree);
		pro_order(head_node->right_tree);
	}
}

void in_order(tree *head_node)   //中序遍历:左中右
{
	if (head_node)
	{
		in_order(head_node->left_tree);
		cout << head_node->data;
		in_order(head_node->right_tree);
	}
}

void post_order(tree *head_node) //后序遍历:左右中
{
	if (head_node)
	{
		post_order(head_node->left_tree);
		post_order(head_node->right_tree);
		cout << head_node->data;
	}
}

void delete_tree(tree *head_node)   //删除要用后序遍历的方法,中要最后删除,否则找不到左节点与右节点
{
	if (head_node)
	{
		delete_tree(head_node->left_tree);
		delete_tree(head_node->right_tree);
		free(head_node);
		head_node = NULL;
	}
}
tree* reConstructBinaryTree(vector<int> pre, vector<int> in)   //由前序和中序得到树
{
	if (pre.size() == NULL)
		return NULL;
	int len = pre.size();
	tree *temp;
	temp = (tree*)malloc(sizeof(tree));
	temp->left_tree = temp->right_tree = NULL;
	temp->data = pre[0];
	int i = 0;
	while (in[i] != pre[0])
	{
		i++;
	}
	vector<int>pre_left, in_left, pre_right, in_right;
	int j;
	for (j = 0; j < i; j++)
	{
		pre_left.push_back(pre[j + 1]);
		in_left.push_back(in[j]);
	}
	for (j = i + 1; j < len; j++)
	{
		pre_right.push_back(pre[j]);
		in_right.push_back(in[j]);
	}
	temp->left_tree = reConstructBinaryTree(pre_left, in_left);
	temp->right_tree = reConstructBinaryTree(pre_right, in_right);
	return temp;
}

tree* reConstructBinaryTree_post(vector<int> in, vector<int> post)   //由中序以及后序重建树
{
	if (in.size() == NULL)
		return NULL;
	int len = in.size();
	tree *temp;
	temp = (tree*)malloc(sizeof(tree));
	temp->left_tree = temp->right_tree = NULL;
	temp->data = post[len-1];
	int i = 0;
	while (in[i] != post[len-1])
	{
		i++;
	}
	vector<int>post_left, in_left, post_right, in_right;  
	int j;
	for (j = 0; j < i; j++)
	{
		post_left.push_back(post[j]);
		in_left.push_back(in[j]);
	}
	for (j = i + 1; j < len; j++)
	{
		post_right.push_back(post[j-1]);
		in_right.push_back(in[j]);
	}
	temp->left_tree = reConstructBinaryTree_post(in_left,post_left);
	temp->right_tree = reConstructBinaryTree_post(in_right,post_right);
	return temp;
}
int main()
{
	creat_tree();
	tree_insert(node,3);
	tree_insert(node,6);   //插值
	cout << "前序:";
	pro_order(node);       //前序
	cout << endl;

	cout << "中序:";
	in_order(node);        //中序
	cout << endl;

	cout << "后序:";
	post_order(node);     //后序
	cout << endl;

	vector<int>a = {5,4,3,6};
	vector<int>b = {3,4,5,6 };
	vector<int>c = {3,4,6,5};
	tree *p = reConstructBinaryTree(a,b);
	post_order(p);
	cout << endl;

	tree *p1 = reConstructBinaryTree_post(b,c);
	pro_order(p1);

	//delete_tree(node);   //销毁树
	//post_order(node);

	system("pause");
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值