C++ —— 尾碼運算式轉運算式樹狀架構

來源:互聯網
上載者:User

標籤: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++ —— 尾碼運算式轉運算式樹狀架構

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.