題目:輸入某二叉樹的前序遍歷和中序遍歷的結果,請重建出該二叉樹。假設前序遍歷與中序遍歷的結果都不含重複的數字。
{
int _value;
BinaryTreeNode* _left;
BinaryTreeNode* _right;
}
#include<iostream>
using namespace std;
struct BinaryTreeNode
{
int _value;
BinaryTreeNode* _left;
BinaryTreeNode* _right;
BinaryTreeNode(int value = 0)
:_value(value),_left(NULL), _right(NULL)
{}
};
class BinaryTree
{
public:
BinaryTree()
:root(NULL)
{}
BinaryTree(int* pre, int* inOrder, int len)
{
_CreateBinaryTree(root, pre, inOrder, len);
}
~BinaryTree()
{
_DestoryBinaryTree(root);
}
private:
void _DestoryBinaryTree(BinaryTreeNode*& root)
{
if (root)
{
BinaryTreeNode* tmp = root;
if (tmp->_left)
{
_DestoryBinaryTree(tmp->_left);
}
if (tmp->_right)
{
_DestoryBinaryTree(tmp->_right);
}
delete[] tmp;
tmp = NULL;
}
}
BinaryTreeNode* _CreateBinaryTree(BinaryTreeNode*& root,int*& pre, int*& inOrder, int len)
{
if (pre == NULL || inOrder == NULL || len <= 0)
{
return NULL;
}
return _RealCreateTree(root, pre, pre + len - 1, inOrder, inOrder + len - 1);
}
BinaryTreeNode* _RealCreateTree(BinaryTreeNode*& root,int* startPre, int* endPre, int* startIn, int* endIn)
{
int rootValue = startPre[0];
root = new BinaryTreeNode(rootValue);
//1.有一個結點
if (startPre == endPre)
{
if (startIn == endIn && *startPre == *startIn)
{
return root;
}
else
{
cout << "preOrder and InOder is not matching" << endl;
return NULL;
}
}
//2.有多個結點
int* rootIn = startIn;
while (rootIn < endIn && *rootIn != root->_value)
{
++rootIn;
}
if (rootIn == endIn && *rootIn != root->_value)
{
cout << "preOrder and InOder is not matching" << endl;
return NULL;
}
//1.通過中序遍歷計算左子樹的節點個數leftLen
//2.通過計算出的左子樹個數,再計算先序遍歷中的左子樹中的最後一個節點位置preLeftOfend
int leftLen = rootIn - startIn;
int* preLeftOfend = startPre + leftLen;
//構建左子樹
if (leftLen > 0)
{
_RealCreateTree(root->_left, startPre + 1, preLeftOfend, startIn, rootIn - 1);
}
//構建右子樹
if (leftLen < endPre - startPre)
{
_RealCreateTree(root->_right, preLeftOfend + 1, endPre, rootIn + 1, endIn);
}
return root;
}
private:
BinaryTreeNode* root;
};
void Test()
{
int pre[] = { 1, 2, 4, 7, 3, 5, 6, 8 };
int in[] = { 4, 7, 2, 1, 5, 3, 8, 6 };
int len = sizeof(pre) / sizeof(pre[0]);
BinaryTree bt(pre, in, len);
}
void Test2()
{
int pre[] = { 1, 2, 4, 8, 9, 5, 3, 6, 7 };
int in[] = { 8, 4, 9, 2, 5, 1, 6, 3, 7 };
int len = sizeof(pre) / sizeof(pre[0]);
BinaryTree bt(pre, in, len);
}
void Test3()
{
int pre[] = { 1, 2, 3, 4, 5, 6 };
int in[] = { 6, 5, 4, 3, 2, 1 };
int len = sizeof(pre) / sizeof(pre[0]);
BinaryTree bt(pre, in, len);
}
void Test4()
{
int pre[] = { 1, 2, 3, 4, 5, 6 };
int in[] = { 1, 2, 3, 4, 5, 6 };
int len = sizeof(pre) / sizeof(pre[0]);
BinaryTree bt(pre, in, len);
}
void Test5()
{
int pre[] = { 10 };
int in[] = { 10 };
int len = sizeof(pre) / sizeof(pre[0]);
BinaryTree bt(pre, in, len);
}
void Test6()
{
int pre[] = { 1,2,3,4,5};
int in[] = {4,5,7,3,1};
int len = sizeof(pre) / sizeof(pre[0]);
BinaryTree bt(pre, in, len);
}
int main()
{
//1.普通二叉樹:Test():不完全二叉樹;
// Test2():完全二叉樹;
//2.特殊二叉樹:Test3():所有結點沒有右結點;
// Test4():所有結點沒有左節點;
// Test5():只有一個結點;
//3.特殊測試: Test6():輸入的前序與中序不匹配;
Test();
Test2();
Test3();
Test4();
Test5();
Test6();
return 0;
}