diff --git a/.vscode/settings.json b/.vscode/settings.json index 5c4579f387387013cde82d8f0dd72135454ccb31..b93a2a3359e5635a95c81d1eef95ce8389406385 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -7,6 +7,7 @@ "ostream": "cpp", "vector": "cpp", "sstream": "cpp", - "cmath": "cpp" + "cmath": "cpp", + "stack": "cpp" } } \ No newline at end of file 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 d0fe12975bc466828beb5aaba01cc967372e9baa..27104bbb4d51e7574a2ba3b608cd5fffdaf4b85e 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,211 +1,122 @@ -/** - * Introduce: - * 1, We use char '#' to behalf the null node. -*/ - -//Include some library. +//Include Liberary. #include -#include #include -//Imprt the namespace to this file. +//Import the namespace. using namespace std; -//Marco define. -#define MAXSIZE 100 - -//Define a struct to behalf the BinaryTree. +//Define the binary tree struction. typedef struct BiNode { + char data; struct BiNode *LeftChild; - char Data; struct BiNode *RightChild; } BiNode, *BiTree; -//Expanding the first ordered binary tree - -//Creat BiNode. -//recieves: -// BiNode *&BN : The node u want to open. -// char c : The data u want to push in. -//returns: -// None -void CreatBiNode(BiNode *&BN, char c) +//Pre-order creat binary tree. +void CreatBiTree_Rec(BiTree &root) { - //Open a new node. - BN = new BiNode; - cout << "Now we new a new node." << c << endl; - - //Push the char into data. - BN->Data = c; - cout << "Push:\t" << BN->Data << endl; -} - -//Generating binary tree by recursion. -//recieves: -// BiTree &B : The node we should allocate memory space. -//returns: -// None -void CreatBiTree_Recursion(BiTree &B) -{ - //Get the new node tag. - char s; - cin >> s; + //Get the input. + char c; + cin >> c; - //Determine the char entered. - switch (s) + switch (c) { - //The node will be null. case '#': - B = NULL; + root = NULL; break; - //The node is not null. default: - //Creat a new node which data is s. - CreatBiNode(B, s); - - //Start recursion. - CreatBiTree_Recursion(B->LeftChild); - CreatBiTree_Recursion(B->RightChild); + //Creat a new root. + root = new BiNode; + //Set its left child and right child be null. + root->LeftChild = root->RightChild = NULL; + //Set the data value. + root->data = c; + //Recursively generated child tree. + CreatBiTree_Rec(root->LeftChild); + CreatBiTree_Rec(root->RightChild); break; } } -//Define a stack of BiTree. -typedef struct BiTreeStack -{ - BiNode *Stack[MAXSIZE]; - int Top; - int Size; -} BiTreeStack; - -//Init a BiTreeStack. -void InitBiTreeStack(BiTreeStack &BTS) +//Loop to creat binary tree. +void CreatBiTree_Loop(BiTree &root) { - BTS.Top = 0; - BTS.Size = MAXSIZE; -} + stack BiStack; -//Push to BiTreeStakc. -void Push(BiTreeStack &BTS, BiNode *c) -{ - //Safety inspection. - if (BTS.Top >= BTS.Size) + while (true) { - cout << "The stakc is full" << endl; - return; } - - //Push in. - //Creat a new node which data is s - BTS.Stack[BTS.Top++] = c; - return; } -//Pop from BiTreeStakc. -void Pop(BiTreeStack &BTS, BiNode *&c) +enum Type +{ + Pre, + In, + Post +}; + +void DisplayBiTree(BiTree root, enum Type t) { - /* - //Safety inspection. - if (BTS.Top <= 0) + if (!root) { - cout << "The stakc is full" << endl; return; } - */ - - //Pop it. - c = BTS.Stack[--BTS.Top]; - return; + if (t == Pre) + { + cout << root->data; + } + DisplayBiTree(root->LeftChild, t); + if (t == In) + { + cout << root->data; + } + DisplayBiTree(root->RightChild, t); + if (t == Post) + { + cout << root->data; + } } -void CreatBiTree_Ergodic(BiTree &B) +void DisplayBiTree_Stack(BiTree root, enum Type t) { - //Init a stack. - BiTreeStack BTS; - InitBiTreeStack(BTS); - - //Define a pointer to do many thing. - B = new BiNode; - BiNode *pB = B; - pB->Data = getchar(); - cout << "GET\t" << pB->Data << endl; - //Push in. - Push(BTS, pB); - cout << "PUSH\t" << pB->Data << endl; - - //This condition is not well-defined. - char c; - bool isRight = false; - while (c = getchar()) + stack BiStack; + BiNode *p = root, *temp = NULL; + do { - switch (c) + if (!p) { - case '#': - //If null stack then end. - if (BTS.Top == 0) + p = BiStack.top(); + if (t != Post) { - return; + BiStack.pop(); } - //pB revoke. - Pop(BTS, pB); - cout << "POP\t" << pB->Data << endl; - isRight = true; - break; - default: - if (!isRight) + if (t == In) { - pB->LeftChild = new BiNode; - pB = pB->LeftChild; + cout << p->data; } - else + if (t == Post) { - pB->RightChild = new BiNode; - pB = pB->RightChild; + if (!p->RightChild || p->RightChild == temp) + { + cout << p->data; + BiStack.pop(); + temp = p; + p = NULL; //This is important. + continue; + } } - - //Tag the pB. - pB->Data = c; - pB->LeftChild = pB->RightChild = NULL; - cout << "GET\t" << c << endl; - - //Push in. - Push(BTS, pB); - cout << "PUSH\t" << pB->Data << endl; - - cout << "DIR\t" << (isRight ? "Right" : "Left") << endl; - - isRight = false; - break; + p = p->RightChild; + continue; } - } - - return; -} -void ShowBiTree(BiTree &B) -{ - if (!B) - { - return; - } - cout << B->Data << " "; - ShowBiTree(B->LeftChild); - ShowBiTree(B->RightChild); - return; -} - -void ShowTree_Ergodic(BiTree B) -{ - //Init a stack. - BiTreeStack BTS; - InitBiTreeStack(BTS); - - //左可左入栈,tag初始0 - //无根出栈一,tag++ - //tag==1,转向看入栈 - //tag==2,出栈继续走 + //cout << p->data; + BiStack.push(p); + if (t == Pre) + { + cout << p->data; + } + p = p->LeftChild; - //因为要维护一个Stack,所以我不写了 - + } while (!BiStack.empty() || p); } \ No newline at end of file diff --git "a/\346\240\221\345\222\214\344\272\214\345\217\211\346\240\221/MotherFucker.cpp" "b/\346\240\221\345\222\214\344\272\214\345\217\211\346\240\221/MotherFucker.cpp" deleted file mode 100644 index 3b1c78da2ba1ebaf4e2332640b48fda2c9614265..0000000000000000000000000000000000000000 --- "a/\346\240\221\345\222\214\344\272\214\345\217\211\346\240\221/MotherFucker.cpp" +++ /dev/null @@ -1,79 +0,0 @@ -#include -#include -#include - -using namespace std; - -class BiNode -{ -public: - BiNode(){}; - BiNode(char c) : val(c){}; - char val; - BiNode *left = NULL; - BiNode *right = NULL; -}; - -BiNode *addTree(string str) -{ - BiNode *root = new BiNode(str[0]); - stack stk; - stk.push(root); - int id = 1; - bool A = false; - while (!stk.empty()) - { - if (stk.top()->left == NULL && str[id - 1] != '#') - { - if (str[id] != '#') - { - BiNode *left = new BiNode(str[id]); - stk.top()->left = left; - stk.push(left); - } - id++; - } - else - { - if (stk.top()->right == NULL) - { - if (str[id] != '#') - { - BiNode *right = new BiNode(str[id]); - stk.top()->right = right; - stk.push(right); - } - else - { - stk.pop(); - } - id++; - } - else - { - stk.pop(); - } - } - } - return root; -} - -void printTree(BiNode *root) -{ - if (root != NULL) - { - cout << root->val; - if (root->left != NULL) - { - printTree(root->left); - } - if (root->right != NULL) - { - printTree(root->right); - } - } -} - -int main() -{ -} \ 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 e3b95bb573094105d417a6d2e09eb81d8261ec0d..a241eb7e0b0a1b99d664587d5b98624b2ccea68d 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,25 +1,33 @@ -//Include some library. #include "BinaryTree.h" -//Function entrance. int main() { - /** - * Introduce: - * 1, Test input: AB#C##DE#F##G## - */ + BiTree root; - //State a BiTree. - BiTree b; + //ABD#E###CFG###H## + //Pre order:ABDECFGH + //In order:DEBAGFCH + //Post order:EDBGFHCA + cout << "Input ebt:" << endl; + CreatBiTree_Rec(root); - //Recursion type. - //CreatBiTree_Recursion(b); - //ShowBiTree(b); - //cout << endl; + cout << "Pre order:" << endl; + DisplayBiTree_Stack(root, Pre); + cout << endl; + DisplayBiTree(root, Pre); + cout << endl; - //Ergodic type. - CreatBiTree_Ergodic(b); - ShowBiTree(b); + cout << "In order:" << endl; + DisplayBiTree_Stack(root, In); + cout << endl; + DisplayBiTree(root, In); + cout << endl; + + cout << "Post order:" << endl; + DisplayBiTree_Stack(root, Post); + cout << endl; + DisplayBiTree(root, Post); + cout << endl; return 0; } \ 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.exe" "b/\346\240\221\345\222\214\344\272\214\345\217\211\346\240\221/main.exe" index 6b22373439ab65d7de5b98cd9f675ecc2fd555bc..cd4ecd16d5984752458f877d4ed21d6ed47641ad 100644 Binary files "a/\346\240\221\345\222\214\344\272\214\345\217\211\346\240\221/main.exe" and "b/\346\240\221\345\222\214\344\272\214\345\217\211\346\240\221/main.exe" differ diff --git "a/\346\240\221\345\222\214\344\272\214\345\217\211\346\240\221/tempCodeRunnerFile.cpp" "b/\346\240\221\345\222\214\344\272\214\345\217\211\346\240\221/tempCodeRunnerFile.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..25f0176d5a3af1d59c7a0a248b52c87554ae9adf --- /dev/null +++ "b/\346\240\221\345\222\214\344\272\214\345\217\211\346\240\221/tempCodeRunnerFile.cpp" @@ -0,0 +1 @@ +ABD#E###CFG###H## \ No newline at end of file