diff --git "a/\346\240\221\345\222\214\344\272\214\345\217\211\346\240\221/BinaryTree.h" "b/\346\240\221\345\222\214\344\272\214\345\217\211\346\240\221/BinaryTree.h" index 27104bbb4d51e7574a2ba3b608cd5fffdaf4b85e..3bd494e1807ad44e296824ded6c8133dd380a8f6 100644 --- "a/\346\240\221\345\222\214\344\272\214\345\217\211\346\240\221/BinaryTree.h" +++ "b/\346\240\221\345\222\214\344\272\214\345\217\211\346\240\221/BinaryTree.h" @@ -1,3 +1,5 @@ +#pragma once + //Include Liberary. #include #include @@ -42,11 +44,58 @@ void CreatBiTree_Rec(BiTree &root) //Loop to creat binary tree. void CreatBiTree_Loop(BiTree &root) { + char c; stack BiStack; - while (true) + root = new BiNode; + BiNode *p = root; + bool IsRight = false; + + //Define a pointer to do many thing. + p->data = getchar(); + //Push in. + BiStack.push(p); + + while (c = getchar()) { + switch (c) + { + case '#': + //If null stack then end. + if (BiStack.empty()) + { + return; + } + //pB revoke. + p = BiStack.top(); + BiStack.pop(); + IsRight = true; + break; + default: + if (IsRight) + { + p->RightChild = new BiNode; + p = p->RightChild; + } + else + { + p->LeftChild = new BiNode; + p = p->LeftChild; + } + + //Tag the pB. + p->data = c; + p->LeftChild = p->RightChild = NULL; + + //Push in. + BiStack.push(p); + + IsRight = false; + break; + } } + + return; } enum Type diff --git "a/\346\240\221\345\222\214\344\272\214\345\217\211\346\240\221/Extention.h" "b/\346\240\221\345\222\214\344\272\214\345\217\211\346\240\221/Extention.h" new file mode 100644 index 0000000000000000000000000000000000000000..a8d19c94afcaff5c217af758cbb2591f4d30da29 --- /dev/null +++ "b/\346\240\221\345\222\214\344\272\214\345\217\211\346\240\221/Extention.h" @@ -0,0 +1,44 @@ +#pragma once + +#include "BinaryTree.h" +#include + +void UnionPreIn(string Pre, string In, BiTree &root) +{ + //Pre order:ABDECFGH + //In order:DEBAGFCH + root = new BiNode; + root->data = Pre[0]; + string left, right; + + int rootPos = In.find(Pre[0]); + left = In.substr(0, rootPos); + right = In.substr(rootPos + 1, In.length()); + + int lLength = left.length(); + int rLength = right.length(); + + root->LeftChild = root->RightChild = NULL; + + if (lLength != 0) + { + cout << "LEFT" << endl; + cout << '\t' << Pre.substr(1, lLength) << " " << left << endl; + + root->LeftChild = new BiNode; + cout << root->data << "l" << Pre.substr(1, lLength)[0] << endl; + root->LeftChild->data = Pre.substr(1, lLength)[0]; + UnionPreIn(Pre.substr(1, lLength), left, root->LeftChild); + } + + if (rLength != 0) + { + cout << "RIGHT" << endl; + cout << '\t' << Pre.substr(Pre.length() - rLength, Pre.length()) << " " << right << endl; + + root->RightChild = new BiNode; + cout << root->data << "r" << Pre.substr(Pre.length() - rLength, Pre.length())[0] << endl; + root->RightChild->data = Pre.substr(Pre.length() - rLength, Pre.length())[0]; + UnionPreIn(Pre.substr(Pre.length() - rLength, Pre.length()), right, root->RightChild); + } +} \ No newline at end of file diff --git "a/\346\240\221\345\222\214\344\272\214\345\217\211\346\240\221/main.cpp" "b/\346\240\221\345\222\214\344\272\214\345\217\211\346\240\221/main.cpp" index a241eb7e0b0a1b99d664587d5b98624b2ccea68d..b0dc5155de01c63fbc175fbb0a44f5fb55130450 100644 --- "a/\346\240\221\345\222\214\344\272\214\345\217\211\346\240\221/main.cpp" +++ "b/\346\240\221\345\222\214\344\272\214\345\217\211\346\240\221/main.cpp" @@ -1,4 +1,5 @@ #include "BinaryTree.h" +#include "Extention.h" int main() { @@ -8,8 +9,11 @@ int main() //Pre order:ABDECFGH //In order:DEBAGFCH //Post order:EDBGFHCA + + /* cout << "Input ebt:" << endl; - CreatBiTree_Rec(root); + //CreatBiTree_Rec(root); + CreatBiTree_Loop(root); cout << "Pre order:" << endl; DisplayBiTree_Stack(root, Pre); @@ -28,6 +32,15 @@ int main() cout << endl; DisplayBiTree(root, Post); cout << endl; +*/ + UnionPreIn("ABDECFGH", "DEBAGFCH", root); + cout << endl; + DisplayBiTree(root, Pre); + cout << endl; + DisplayBiTree(root, In); + cout << endl; + DisplayBiTree(root, Post); + cout << endl; return 0; } \ No newline at end of file