標籤:int pac destory error tree ror stack 時間 tor
寫得時候思路還是很清晰的,所以沒花費多久便基本實現了,不過過程還是遇到一些bug。
版本1:
#include <iostream>#include <algorithm>#include <stack>#include <string>using namespace std;class tree {public:char val;tree * leftchild;tree * rightchild;};class ExpressionTree {public:void build_expression(tree * & root, string str);void proorder_print(tree * root);void inorder_print(tree * root);void postorder_print(tree * root);void show(tree * root);void destory(tree * & root);};void ExpressionTree::build_expression(tree * & root, string str){stack<tree *> treestack;for (auto c : str){tree*node = new tree;node->val = c;node->leftchild = nullptr;node->rightchild = nullptr;if (c >= ‘0‘ && c <= ‘9‘ || c >= ‘a‘ && c <= ‘z‘)treestack.push(node);else if (c == ‘+‘ || c == ‘-‘ || c == ‘*‘ || c == ‘/‘){if (!treestack.empty()){node->rightchild = treestack.top();treestack.pop();}if (!treestack.empty()){node->leftchild = treestack.top();treestack.pop();}treestack.push(node);}else{cout << "運算式有誤!" << endl;exit(1);}}root = treestack.top();while (!treestack.empty())treestack.pop();}void ExpressionTree::proorder_print(tree * root){if (root != nullptr){cout << root->val;proorder_print(root->leftchild);proorder_print(root->rightchild);}}void ExpressionTree::inorder_print(tree * root){if (root != nullptr){cout << ‘(‘;inorder_print(root->leftchild);cout << root->val;inorder_print(root->rightchild);cout << ‘)‘;}}void ExpressionTree::postorder_print(tree * root){if (root != nullptr){postorder_print(root->leftchild);postorder_print(root->rightchild);cout << root->val;}}void ExpressionTree::show(tree * root){cout << "首碼運算式:" << endl;proorder_print(root);cout << endl;cout << "中綴運算式:" << endl;inorder_print(root);cout << endl;cout << "尾碼運算式:" << endl;postorder_print(root);cout << endl;}void ExpressionTree::destory(tree * & root){if (root != nullptr){destory(root->leftchild);destory(root->rightchild);delete root;}}int main(){string str = "23+456+**";ExpressionTree res;tree * root;res.build_expression(root, str);res.show(root);res.destory(root);getchar(); return 0;}
版本2:
#include <iostream>#include <algorithm>#include <stack>#include <string>using namespace std;class tree {public:char val;tree * leftchild;tree * rightchild;};class ExpressionTree {public:tree * root;ExpressionTree(){this->root = new tree;}~ExpressionTree(){destory(this->root);}void insert(char c, stack<tree *> & treestack);void build_expression(string str);void proorder_print(tree * root);void inorder_print(tree * root);void postorder_print(tree * root);void show();void destory(tree * root);};void ExpressionTree::insert(char c, stack<tree *> & treestack){tree*node = new tree;node->val = c;node->leftchild = nullptr;node->rightchild = nullptr;if (c >= ‘0‘ && c <= ‘9‘)treestack.push(node);else if (c == ‘+‘ || c == ‘-‘ || c == ‘*‘ || c == ‘/‘){try {node->rightchild = treestack.top();treestack.pop();node->leftchild = treestack.top();treestack.pop();}catch (overflow_error) {cout << "運算式有誤!";exit(1);}treestack.push(node);}else{cout << "運算式有誤!" << endl;exit(1);}}void ExpressionTree::build_expression(string str){stack<tree *> treestack;for (auto s : str)insert(s, treestack);this->root = treestack.top();while (!treestack.empty())treestack.pop();}void ExpressionTree::proorder_print(tree * root){if (root != nullptr){cout << root->val;proorder_print(root->leftchild);proorder_print(root->rightchild);}}void ExpressionTree::inorder_print(tree * root){if (root != nullptr){ cout << ‘(‘;inorder_print(root->leftchild);cout << root->val;inorder_print(root->rightchild);cout << ‘)‘;}}void ExpressionTree::postorder_print(tree * root){if (root != nullptr){postorder_print(root->leftchild);postorder_print(root->rightchild);cout << root->val;}}void ExpressionTree::show(){cout << "首碼運算式:" << endl;proorder_print(this->root);cout << endl;cout << "中綴運算式:" << endl;inorder_print(this->root);cout << endl;cout << "尾碼運算式:" << endl;postorder_print(this->root);cout << endl;}void ExpressionTree::destory(tree * root){if (root != nullptr){destory(root->leftchild);destory(root->rightchild);delete root;}}int main(){string str = "23+456+**";ExpressionTree res;res.build_expression(str);res.show();getchar(); return 0;}
基本思路:建立樹的方式類似由下而上建立堆的辦法,所以時間複雜度為O(n),這樣演算法就會變得很簡單,只用考慮處理棧中的節點即可。好像也沒什麼好說的了。
C++ —— 尾碼運算式轉運算式樹狀架構