[LeetCode] Design an Expression Tree With Evaluate Function

1628. Design an Expression Tree With Evaluate Function

Given the postfix tokens of an arithmetic expression, build and return the binary expression tree that represents this expression.

Postfix notation is a notation for writing arithmetic expressions in which the operands (numbers) appear before their operators. For example, the postfix tokens of the expression 4(5-(7+2)) are represented in the array postfix = [“4”,”5”,”7”,”2”,”+”,”-“,”“].

The class Node is an interface you should use to implement the binary expression tree. The returned tree will be tested using the evaluate function, which is supposed to evaluate the tree’s value. You should not remove the Node class; however, you can modify it as you wish, and you can define other classes to implement it if needed.

A binary expression tree is a kind of binary tree used to represent arithmetic expressions. Each node of a binary expression tree has either zero or two children. Leaf nodes (nodes with 0 children) correspond to operands (numbers), and internal nodes (nodes with two children) correspond to the operators ‘+’ (addition), ‘-‘ (subtraction), ‘*’ (multiplication), and ‘/‘ (division).

It’s guaranteed that no subtree will yield a value that exceeds 109 in absolute value, and all the operations are valid (i.e., no division by zero).

Follow up: Could you design the expression tree such that it is more modular? For example, is your design able to support additional operators without making changes to your existing evaluate implementation?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* This is the interface for the expression tree Node.
* You should not remove it, and you can define some classes to implement it.
*/

class Node {
public:
virtual ~Node () {};
virtual int evaluate() {
if(isdigit(exp[0])) {
return stoi(exp);
} else {
int l = left->evaluate();
int r = right->evaluate();
if(exp == "+") return l + r;
else if(exp == "-") return l - r;
else if(exp == "*") return l * r;
else return l / r;
}
}
Node(string exp) : exp(exp), left(nullptr), right(nullptr) {};
void setChild(Node* l, Node* r) {
left = l;
right = r;
}
protected:
// define your fields here
string exp;
Node *left, *right;
};


/**
* This is the TreeBuilder class.
* You can treat it as the driver code that takes the postinfix input
* and returns the expression tree represnting it as a Node.
*/

class TreeBuilder {
public:
Node* buildTree(vector<string>& postfix) {
vector<Node*> st;
for(auto& p : postfix) {
st.push_back(new Node(p));
if(!isdigit(p[0])) {
auto root = st.back(); st.pop_back();
auto r = st.back(); st.pop_back();
auto l = st.back(); st.pop_back();
root->setChild(l,r);
st.push_back(root);
}
}
return st[0];
}
};


/**
* Your TreeBuilder object will be instantiated and called as such:
* TreeBuilder* obj = new TreeBuilder();
* Node* expTree = obj->buildTree(postfix);
* int ans = expTree->evaluate();
*/
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/04/27/PS/LeetCode/design-an-expression-tree-with-evaluate-function/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.