//
// main.cpp
// ConstructBinaryTree
//
// Created by Bazinga on 2017/3/12.
// Copyright © 2017年 Bazinga. All rights reserved.
//
// 根据前序遍历和中序遍历求二叉树的结构
// 已知前序遍历的第一个值为根节点 而根节点在中序遍历中的位置就能确定他的左右子树。然后对区分开的左右子树进行递归。
#include <iostream>
#include <exception>
#include <stdio.h>
using namespace std;
struct BinaryTreeNode{
int m_value;
BinaryTreeNode * m_pLeft;
BinaryTreeNode * m_pRight;
};
BinaryTreeNode * ContructTree(int * preorder , int * inorder , int length);
BinaryTreeNode * ContructTreeCore(int * startPreorder , int * endPreorder,int * startInorder ,int *endInorder);
BinaryTreeNode * ContructTree(int * preorder , int * inorder , int length){
if (preorder == NULL || inorder == NULL || length<0) {
return NULL;
}
return ContructTreeCore(preorder, preorder + length - 1, inorder,inorder + length -1);
}
BinaryTreeNode* ContructTreeCore(int * startPreorder , int * endPreorder,int * startInorder ,int *endInorder){
int rootVaule = startPreorder[0];
BinaryTreeNode * root = new BinaryTreeNode(); //初始化根节点
root->m_value = rootVaule;
root->m_pLeft = root->m_pRight = NULL;
if (startPreorder == endPreorder) { // 异常判断
if(startInorder == endInorder && *startPreorder == * startInorder)
return root;
else
throw string("输入有误");
}
//在中序遍历中找到根节点的值
int * rootInorder = startInorder;
while (rootInorder <= endInorder && *rootInorder != rootVaule ) {
++ rootInorder;
}
if(rootInorder > endInorder)
throw string("输入有误");
//
int leftLength = rootInorder - startInorder;
// cout<<"左子树的长度"<<leftLength<<endl;
int * leftPreorderEnd = startPreorder + leftLength;
if (leftLength > 0) {
root->m_pLeft = ContructTreeCore(startPreorder + 1, leftPreorderEnd, startInorder, rootInorder-1);
}
if (leftLength < endPreorder - startPreorder) {
root->m_pRight = ContructTreeCore(leftPreorderEnd + 1 , endPreorder, rootInorder + 1, endInorder);
}
return root;
}
void PrintTreeNode(BinaryTreeNode* pNode)
{
if(pNode != NULL)
{
printf("value of this node is: %d\n", pNode->m_value);
if(pNode->m_pLeft != NULL)
printf("value of its left child is: %d.\n", pNode->m_pLeft->m_value);
else
printf("left child is null.\n");
if(pNode->m_pRight != NULL)
printf("value of its right child is: %d.\n", pNode->m_pRight->m_value);
else
printf("right child is null.\n");
}
else
{
printf("this node is null.\n");
}
printf("\n");
}
void PrintTree(BinaryTreeNode* pRoot)
{
PrintTreeNode(pRoot);
if(pRoot != NULL)
{
if(pRoot->m_pLeft != NULL)
PrintTree(pRoot->m_pLeft);
if(pRoot->m_pRight != NULL)
PrintTree(pRoot->m_pRight);
}
}
void DestroyTree(BinaryTreeNode* pRoot)
{
if(pRoot != NULL)
{
BinaryTreeNode* pLeft = pRoot->m_pLeft;
BinaryTreeNode* pRight = pRoot->m_pRight;
delete pRoot;
pRoot = NULL;
DestroyTree(pLeft);
DestroyTree(pRight);
}
}
int main(int argc, const char * argv[]) {
const int length = 8;
int preorder[length] = {1, 2, 4, 7, 3, 5, 6, 8};
int inorder[length] = {4, 7, 2, 1, 5, 3, 8, 6};
const int length2 = 5;
int preorder2[length2] = {1, 2, 3, 4, 5};
int inorder2[length2] = {5, 4, 3, 2, 1};
const int length3 = 5;
int preorder3[length3] = {1, 2, 3, 4, 5};
int inorder3[length3] = {1, 2, 3, 4, 5};
const int length4 = 1;
int preorder4[length4] = {1};
int inorder4[length4] = {1};
const int length5 = 7;
int preorder5[length5] = {1, 2, 4, 5, 3, 6, 7};
int inorder5[length5] = {4, 2, 5, 1, 6, 3, 7};
const int length7 = 7;
int preorder7[length7] = {1, 2, 4, 5, 3, 6, 7};
int inorder7[length7] = {4, 2, 8, 1, 6, 3, 7};
const int length8 = 2;
int preorder8[length8] = {1,2};
int inorder8[length8] = {2,4};
BinaryTreeNode * root = ContructTree(preorder4, inorder4, length4);
PrintTree(root);
DestroyTree(root);
return 0;
}
已知中序遍历和先序遍历重建二叉树
最新推荐文章于 2022-11-29 16:15:31 发布