输入前序遍历和中序遍历结果,重建二叉树
BinTree.h
//#include<stdio.h>
//#include<assert.h>
//#include<iostream>
//#include <stdlib.h>
//using namespace std;
//struct BinTreeNode{
// int _value;
// BinTreeNode* _left;
// BinTreeNode* _right;
//};
//BinTreeNode* ConstructCore(int * startPreorder,int* endperorder,int * startInorder,int *endInorder)
//{
// int rootvalue=startPreorder[0];
// BinTreeNode* root=new BinTreeNode();
// root->_value=rootvalue;
// root->_left=root->_right=NULL;
// if(startPreorder==endperorder)
// {
// if(startInorder==endInorder&&*startPreorder==*startInorder)
// {
// return root;
// }
// else
// return NULL;
// }
// int * rootInorder=startInorder;
// while (rootInorder<=endInorder&&*rootInorder!=rootvalue)
// {
// ++rootInorder;
// }
// int leftlength=rootInorder-startInorder;
// int * leftPreOrderEnd=startPreorder+leftlength;
// if(leftlength>0)
// {
// root->_left=ConstructCore(startPreorder+1,leftPreOrderEnd,startInorder,rootInorder-1);
// }
// if(leftlength<endperorder-startPreorder)
// {
// root->_right=ConstructCore(leftPreOrderEnd+1,endperorder,rootInorder+1,endInorder);
// }
// return root;
//}
//BinTreeNode* Construct(int * preorder,int* inorder,int length)
// {
// if (preorder==NULL||inorder==NULL||length<0)
// return NULL;
// return ConstructCore(preorder,preorder+length-1,inorder,inorder+length-1);
// }
//
//void _InorderPrint(BinTreeNode* root)
//{
// if (root==NULL)
// {
// return;
// }
// _InorderPrint(root->_left);
// cout<<root->_value<<" ";
// _InorderPrint(root->_right);
//
//}
//void InorderPrint(BinTreeNode* root)
//{
// _InorderPrint(root);
// cout<<endl;
//}
//void test()
//{
// int preorder[]={1,2,4,7,3,5,6,8};
// int inorder[]={4,7,2,1,5,3,8,6};
// BinTreeNode* root=Construct(preorder,inorder,8);
// InorderPrint(root);
//}
#include <iostream>
using namespace std;
struct BinTreeNode
{
int _value;
BinTreeNode* _left;
BinTreeNode* _right;
};
BinTreeNode* ConstructCore(int* startPreOrder,int* endPreOrder,int * startInOrder,int* endInOrder)
{
int rootValue=startPreOrder[0];
BinTreeNode* root=new BinTreeNode();
root->_value=rootValue;
root->_left=root->_right=NULL;
if(startPreOrder==endPreOrder)
{
if (startInOrder==endInOrder&&*startPreOrder==*startInOrder)
return root;
else
cout<<"测试用例错误"<<endl;
}
int* rootInorder=startInOrder;
while (rootInorder<=endInOrder&&*rootInorder!=rootValue)
++rootInorder;
if(rootInorder==endInOrder&&*rootInorder!=rootValue)
cout<<"测试用例错误"<<endl;
int leftlength=rootInorder-startInOrder;
int * leftEndPreorder=startPreOrder+leftlength;
if(leftlength>0)
{
root->_left=ConstructCore(startPreOrder+1,leftEndPreorder,startInOrder,rootInorder-1);
}
if(leftlength<endPreOrder-startPreOrder)
{
root->_right=ConstructCore(leftEndPreorder+1,endPreOrder,rootInorder+1,endInOrder);
}
return root;
}
BinTreeNode* ConstuctBinTree(int* Preorder,int* Inorder,int length)
{
if(Preorder==NULL||Inorder==NULL||length<0)
return NULL;
return ConstructCore(Preorder,Preorder+length-1,Inorder,Inorder+length-1);
}
void _inOrderBinTree(BinTreeNode* root)
{
if(root==NULL)
return ;
_inOrderBinTree(root->_left);
cout<<root->_value<<" ";
_inOrderBinTree(root->_right);
}
void InorderBinTree(BinTreeNode* root)
{
if(root==NULL)
{
cout<<"二叉树为空"<<endl;
return;
}
_inOrderBinTree(root);
cout<<endl;
}
void test()
{
int Preorder[]={1,2,4,7,3,5,6,8};
int Inorder[]={4,7,2,1,5,3,8,6};
BinTreeNode* root=ConstuctBinTree(Preorder,Inorder,8);
InorderBinTree(root);
}
void test2()
{
int *Preorder=NULL;
int *Inorder=NULL;
BinTreeNode* root=ConstuctBinTree(Preorder,Inorder,8);
InorderBinTree(root);
}
void test3()
{
int Preorder[]={8,7,6,5,4,3,2,1};
int Inorder[]={1,2,3,4,5,6,7,8};
BinTreeNode* root=ConstuctBinTree(Preorder,Inorder,8);
InorderBinTree(root);
}
test.c
#include "BinTree.h"
int main()
{
test();
test2();
test3();
system("pause");
return 0;
}