题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。

来源:互联网 发布:德州扑克入门知乎 编辑:程序博客网 时间:2024/06/12 01:24
1》 stdafx.h
<span style="font-family: Arial, Helvetica, sans-serif;">#pragma once</span>
#include "targetver.h"#include <stdio.h>#include <tchar.h>

2>targetver.h


#pragma once// The following macros define the minimum required platform.  The minimum required platform// is the earliest version of Windows, Internet Explorer etc. that has the necessary features to run // your application.  The macros work by enabling all features available on platform versions up to and // including the version specified.// Modify the following defines if you have to target a platform prior to the ones specified below.// Refer to MSDN for the latest info on corresponding values for different platforms.#ifndef _WIN32_WINNT            // Specifies that the minimum required platform is Windows Vista.#define _WIN32_WINNT 0x0600     // Change this to the appropriate value to target other versions of Windows.#endif

3>BinaryTree.h

#pragma oncestruct BinaryTreeNode{int                    m_nValue;     BinaryTreeNode*        m_pLeft;      BinaryTreeNode*        m_pRight; };_declspec(dllexport)void PrintTree(BinaryTreeNode *pRoot);_declspec(dllexport)void DestroyTree(BinaryTreeNode *pRoot);

4>BinaryTree.cpp

#include "stdafx.h"#include "BinaryTree.h"void PrintTreeNode(BinaryTreeNode *pNode);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 PrintTreeNode(BinaryTreeNode *pNode){if(pNode != NULL){printf("value of this node is : %d\n", pNode->m_nValue);if(pNode->m_pLeft != NULL)printf("value of its left child is: %d.\n", pNode->m_pLeft->m_nValue);elseprintf("left child is null.\n");if(pNode->m_pRight != NULL)printf("value of its right childe is : %d.\n", pNode->m_pRight->m_nValue);elseprintf("right child is null.\n");}else{printf("this node is null.\n");}printf("\n");}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);}}

5>ConstructBinaryTree.cpp

/*题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。*/#include "stdafx.h"#include "BinaryTree.h"//#include "iostream"// 不能是 iostream.husing namespace std;/*preorder 前序遍历inorder中序遍历*/BinaryTreeNode* ConstructCore(int* startPreorder, int* endPreorder, int* startInorder, int* endInorder);BinaryTreeNode *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);}// startPreorder 前序遍历的第一个节点// endPreorder   前序遍历的左后一个节点// startInorder  中序遍历的第一个节点// startInorder  中序遍历的最后一个节点BinaryTreeNode* ConstructCore(int* startPreorder, int* endPreorder, int* startInorder, int* endInorder){ // 前序遍历序列的第一个数字是根结点的值int rootValue = startPreorder[0];BinaryTreeNode *root = new BinaryTreeNode();root->m_nValue = rootValue;root->m_pLeft = root->m_pRight = NULL;if(startPreorder == endPreorder){if(startInorder == endInorder && *startPreorder == *startInorder)return root;elsethrow std::exception("Invalid input.");}//// 在中序遍历中找到根结点的值int *rootInorder = startInorder;while(rootInorder <= endInorder && *rootInorder != rootValue)++rootInorder;if(rootInorder == endInorder && *rootInorder != rootValue)throw std::exception("Invalid input");//int leftLength = rootInorder - startInorder;//int rightLength = endInorder - rootInorder;int *leftPreorderEnd = startPreorder + leftLength;if(leftLength > 0){// 构建左子树root->m_pLeft = ConstructCore(startPreorder +1, leftPreorderEnd, startInorder, rootInorder -1);}if(leftLength < endPreorder -startPreorder){// 构建右子树root->m_pRight = ConstructCore(leftPreorderEnd + 1, endPreorder, rootInorder + 1, endInorder);}//return root;}// 测试代码void Test(char *testName, int *preorder, int *inorder, int length){if(testName != NULL)printf("%s Begins:\n", testName);printf("The preorder sequence is: ");for(int i = 0; i < length; ++i)printf("%d ", preorder[i]);printf("\n");printf("The inorder sequence is:");for(int i = 0; i < length; ++i)printf("%d ", inorder[i]);printf("\n");try{BinaryTreeNode *root = Construct(preorder, inorder,length);PrintTree(root);DestroyTree(root);}catch(std::exception &expection){printf("Invalid Input.\n");}}// 普通二叉树//              1//           /     \//          2       3  //         /       / \//        4       5   6//         \         ///          7       8void Test1(){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};Test("Test1",preorder, inorder, length);}// 所有结点都没有右子结点//            1//           / //          2   //         / //        3 //       ///      4//     ///    5void Test2(){const int length = 5;int preorder[length] = {1, 2, 3, 4, 5};int inorder[length] = {5, 4, 3, 2, 1};Test("Test2", preorder, inorder, length);}// 所有结点都没有左子结点//            1//             \ //              2   //               \ //                3 //                 \//                  4//                   \//                    5void Test3(){const int length = 5;int preorder[length] = {1, 2, 3, 4, 5,};int inorder[length] = {1, 2, 3, 4, 5};Test("Test3", preorder ,inorder, length);}// 树中只有一个结点void Test4(){const int length = 1;int preorder[length] = {1};int inorder[length] = {1};Test("Test4", preorder, inorder, length);}// 完全二叉树//              1//           /     \//          2       3  //         / \     / \//        4   5   6   7/*判断完全二叉树:完全二叉树:除最后一层外,每一层上的节点数均达到最大值;在最后一层上只缺少右边的若干结点。完全二叉树(Complete Binary Tree)定义:    若设二叉树的深度为h,除第 h 层外,其它各层 (1~h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树。*/void Test5(){const int length = 7;int preorder[length] = {1, 2, 4, 5, 3, 6, 7};int inorder[length] = {4, 2, 5, 1, 6, 3, 7};Test("Test5", preorder, inorder, length);}// 输入空指针void Test6(){Test("Test6", NULL, NULL, 0);}// 输入的两个序列不匹配void Test7(){const int length = 7;    int preorder[length] = {1, 2, 4, 5, 3, 6, 7};    int inorder[length] = {4, 2, 8, 1, 6, 3, 7};Test("Test7: for unmatched input", preorder, inorder, length);}int _tmain(int argc, _TCHAR *argv[]){//Test1();// Test2();//Test3();//Test4();//Test6();Test7();//Test5();system("pause");return 0;}







0 0