Generation

generate functionSat, 12 Nov 2022

build a parse tree from a mathematical expression

Node* build_parse_tree(const string& input) { stack<Node*> stack; for (int i = 0; i < input.size(); i++) { string token = input[i]; Node* n = new Node(token); if (token == "+" || token == "-" || token == "*" || token == "/") { Node* right = stack.top(); stack.pop(); Node* left = stack.top(); stack.pop(); n->left = left; n->right = right; stack.push(n); } else { stack.push(n); } } return stack.top(); }

Questions about programming?Chat with your personal AI assistant