#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;
}