Sword Point of Offer question sixth: reconstruction of Binary tree based on the given pre-order and middle sequence traversal sequence
1 //============================================================================2 //Name:jz-c-06.cpp3 //Author:laughing_lz4 //Version:5 //Copyright:all Right Reserved6 //Description:hello World in C + +, Ansi-style7 //============================================================================8 9#include <iostream>Ten#include <stdio.h> One#include <iomanip> A#include <exception> - using namespacestd; - thetypedefstructBinarytreenode {//two fork tree nodes - intData//data fields - structBinarytreenode *left;//left child - structBinarytreenode *right;//Right Child + } BTN; -btn* Constructbtcore (int*startpreorder,int*endpreorder,int*Startinorder, + int*endinorder);//function call before, defined in, need to first explain function prototype A atbtn* CONSTRUCTBT (int*preorder,int*inorder,intlength) { - if(preorder = = NULL | | inorder = NULL | | length <=0) { -cout <<"the sequence is empty? Please check two sequences"<<Endl; - returnNULL; - } -BTN *root = Constructbtcore (preorder, preorder + length-1, Inorder, inInorder + Length-1); -cout << root->data << Endl;//return root to returnRoot; + } - thebtn* Constructbtcore (int*startpreorder,int*endpreorder,int*Startinorder, * int*Endinorder) { $BTN *root =NewBTN ();//Generating NodesPanax NotoginsengRoot->data = startpreorder[0]; -Root->left = Root->right =NULL; the if(Endpreorder = =Startpreorder) { + if(Startinorder = = Endinorder && *startpreorder = = *Startinorder) { A returnRoot; the}Else { +cout <<"The sequence of pre-order and sequence does not match, error! "<<Endl; - Throwstd::exception (); $ } $ } - intInorderlength = Endinorder-startinorder;//Middle Sequence Traversal series length - inti =0; the for(; i < Inorderlength +1; i++) {//find the value of the head node in the middle sequence traversal sequence - //int Rootinorder =*startinorder;Wuyi //While (*startinorder+rootinorder) {} the if(*startpreorder = = * (Startinorder + i)) {//comparison of head nodes of sequence traversal sequences -Root->data = *startpreorder;//Head knot Point Wu if(I >0) {//If there is a left dial hand tree -Root->left = Constructbtcore (Startpreorder +1, AboutStartpreorder + I, Startinorder, Startinorder + i-1);//recursive traversal of the left subtree sequence $ } - if(I < Inorderlength) {//If there is a right subtree -Root->right = Constructbtcore (startpreorder + i +1, -Endpreorder, Startinorder + i +1, Endinorder);//recursive traversal of the right sub-tree sequence A } + Break;//Exit Loop the } - } $ if(i = = Inorderlength +1) {//Note that there are no nodes in the sequence traversal sequences that have the same value as the header nodes of the preamble traversal sequence, error thecout <<"There is no first-order sequence in the sequence of sequences of the head node, error! "<<Endl; the Throwstd::exception (); the } the returnRoot; - } in intMain () { the Const intLength =7;//Complete binary Tree the intPreorder[length] = {1,2,4,5,3,6,7 }; About intInorder[length] = {4,2,5,1,6,3,7 }; the //const int length = 8;//Ordinary binary Tree the //int Preorder[length] = {1, 2, 4, 7, 3, 5, 6, 8}; the //int Inorder[length] = {4, 7, 2, 1, 5, 3, 8, 6}; + //const int length = 7;//Mismatched sequences - //int Preorder[length] = {1, 2, 4, 5, 3, 6, 7}; the //int Inorder[length] = {4, 2, 8, 1, 6, 3, 7};Bayi //const int length = 5;//all nodes have no right child nodes. the //int Preorder[length] = {1, 2, 3, 4, 5}; the //int Inorder[length] = {5, 4, 3, 2, 1}; - //const int length = 5;//all nodes have no left dial hand nodes. - //int Preorder[length] = {1, 2, 3, 4, 5}; the //int Inorder[length] = {1, 2, 3, 4, 5}; the //const int length = 1;//There is only one node in the tree . the //int Preorder[length] = {1}; the //int Inorder[length] = {1}; - CONSTRUCTBT (preorder, inorder, length); the //CONSTRUCTBT (null, NULL, 0);//Enter a null pointer the the return 0;94}
jz-c-06