#include #include #include using namespace std;struct BTreeNode{ int m_nValue; BTreeNode* m_pLeft; < span class="hljs-comment">//left child BTreeNode* m_pRight; };BTreeNode* ConstructionCore(int *startPre, int *endPre, < span class="hljs-keyword">int *startIn, int *endIn){ BTreeNode* root = new BTreeNode(); root->m_nValue = startPre[0]; root->m_pLeft = root->m_pRight = NULL; if(startPre == endPre) {if(startIn == endIn && *startPre == *startIn) {return root;} else {cout<<"Input Error!"<exit(-1<);}} int *rootIn = startIn; while(rootIn <= endIn && *rootIn! = root->m_nValue) ++rootIn; if(rootIn == endIn && *rootIn != root->m_nValue) {cout<<"Inout Error!"<exit(-1);} int leftlen = rootIn- startIn; int *leftPreEnd = startPre + leftlen; if(leftlen> 0) {root->m_pLeft = ConstructionCore(startPre+1, leftPreEnd, startIn, rootIn-1 );} if(leftlen m_pRight = ConstructionCore(leftPreEnd+1< /span>, endPre, rootIn+1, endIn);} return root;}BTreeNode* Construct(< span class="hljs-keyword">int *preorder, int *inorder, int len){ if(preorder == NULL || inorder == NULL || len <= < span class="hljs-number">0) return NULL; return ConstructionCore(preorder , preorder+len-1, inorder, inorder+len-1);}void< /span> PreBTree(BTreeNode *pHead){ if(pHead) {cout<< pHead->m_nValue << " "; PreBTree(pHead->m_pLeft); PreBTree(pHead->m_pRight); }void InBTree(BTreeNode *pHead){ < span class="hljs-keyword">if(pHead != NULL) {InBTree(pHead->m_pLeft); cout << pHead->m_nValue << " "; InBTree(pHead->m_pRight); }}void NRInBTree(BTreeNode *pHead){ stack sk; while(pHead! = NULL || !sk.empty()) {if(pHead != NULL) { sk.push(pHead); pHead = pHead->m_pLeft;} else {pHead = sk.top() ; sk.pop(); cout << pHead->m_nValue << < span class="hljs-string">" "; pHead = pHead->m_pRight;} }}void PostBTree(BTreeNode *pHead){ if(pHead != NU LL) {PostBTree(pHead->m_pLeft); PostBTree(pHead->m_pRight); cout << pHead->m_nValue << " "; }}void NRPostBTree (BTreeNode *pHead){ stack sk; BTreeNode* q; int flag = 0; do {while(pHead) {sk.push(pHead); pHead = pHead->m_pLeft;} q = NULL; flag = 1; while(!sk.empty() && flag) {pHead = sk.top(); if( pHead->m_pRight == q) {sk.pop(); cout<< pHead->m_nValue <<" "; q = pHead;} else< /span> {pHead = pHead->m_pRight; flag = 0;}}} while(!sk .empty());}int main(){ int pre[8] = {1,2,4,7,3,5,6,8}; int in[8] = {4,7,2,1,< span class="hljs-number">5,3,8,6}; BTreeNode *tr = Construct(pre, in, 8); cout<<"Preorder traversal recursion"<cout span><"Preorder traversal non-recursive"<cout span><<"In-order traversal recursion"<cout<< endl<<"In-order traversal non-recursive"<cout<< endl<<"Post-order traversal recursion"<cout<"Post-order traversal non-recursive"<cout<return 0;}
#include #include #include using namespace std;struct BTreeNode{ int m_nValue; BTreeNode* m_pLeft; BTreeNode* m_pRight; };BTreeNode* ConstructionCore(int *startPre, int *endPre, int *startI n, int *endIn){ BTreeNode* root = < span class="hljs-keyword">new BTreeNode(); root->m_nValue = startPre[0]; root->m_pLeft = root-> m_pRight = NULL; if(startPre == endPre) {if(startIn == endIn && *startPre = = *startIn) {return root;} else {cout <<"Input Error!"<exit(-1);}} int *rootIn = startIn; while (rootIn <= endIn && *rootIn != root->m_nValue) ++rootIn; if(roo tIn == endIn && *rootIn != root->m_nValue) {cout<<"Inout Error!"<exit(-1);} int leftlen = rootIn-startIn; int *leftPreEnd = startPre + leftlen; if(leftlen > 0) {root->m_pLeft = ConstructionCore(startPre+1, leftPreEnd, startIn, rootIn-1);} if(leftlen m_pRight = ConstructionCore(leftPreEnd+1, endPre, rootIn+1, endIn);} return root;}BTreeNode* Construct(int *preorder, int *inorder, int len){ if(preorder == NULL | | inorder == NULL || len <= 0) return NULL; return ConstructionCore(preorder, preorder+len-1, inorder, inorder+len-1< /span>);}< span class="hljs-keyword">void PreBTree(BTreeNode *pHead){ if(pHead) { cout<< pHead->m_nValue << " "; PreBTree(pHead->m_pLeft); PreBTree(pHead->m_pRight); }void InBTree(BTreeNode *pHead){ if(pHead != NULL) {InBTree(pHead->m_pLeft); cout span> << pHead->m_nValue << " "; I nBTree(pHead->m_pRight); }}void NRInBTree( BTreeNode *pHead){ stack sk; while(pHead != NULL || !sk.empty()) {if(pHead != NULL) { sk.push(pHead); pHead = pHead->m_pLeft;} else {pHead = sk.top(); sk.pop(); cout < m_nValue << " "; pHead = pHead->m_pRight;} }}void PostBTree(BTreeNode *pHead){ if(pHead != NULL) {PostBTree(pHead->m_pLeft); PostBTree(pHead->m_pRight); cout << pHead- >m_nValue << " "; }}void NRPostBTree(BTreeNode *pHead){ stack sk; BTreeNode* q; int flag = 0; do {while(pHead) {sk.push(pHead); pHead = pHead->m_pLeft;} q = NULL; flag = 1; while(!sk.empty() && flag) {pHead = sk.top(); if(pHead->m_pRight == q) {sk.pop(); cout<< pHead->m_nValue <<" "; q = pHead;} else {pHead = pHead->m_pRight; flag = 0;}}} while (!sk.empty());}int main(){ int pre[ 8] = {1,2, 4,7,3,5,6,8}; int in[8] = {4,7,2,1,5,3,8,6}; BTreeNode *tr = Construct(pre, in, 8 ); cout<<"Preorder traversal recursion"<cout<"Preorder traversal non-recursive"<cout<<"In-order traversal recursion"<cout<"In-order traversal non-recursive"<cout<"Post-order traversal recursion"<cout<"Post-order traversal non-recursive"<cout<return 0;}