Skip to content

Commit 78dad25

Browse files
authored
Update Design an Expression Tree With Evaluate Function.java
1 parent 31e3692 commit 78dad25

File tree

1 file changed

+61
-47
lines changed

1 file changed

+61
-47
lines changed

Medium/Design an Expression Tree With Evaluate Function.java

Lines changed: 61 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,70 +4,84 @@
44
*/
55

66
abstract class Node {
7-
public abstract int evaluate();
8-
// define your fields here
7+
public abstract int evaluate();
8+
// define your fields here
99
};
1010

11+
1112
class NumericNode extends Node {
12-
13-
private int val;
14-
15-
public NumericNode(int val) {
16-
this.val = val;
17-
}
18-
19-
public int evaluate() {
20-
return this.val;
21-
}
13+
private int val;
14+
15+
public NumericNode(int val) {
16+
this.val = val;
17+
}
18+
19+
@Override
20+
public int evaluate() {
21+
return this.val;
22+
}
2223
}
2324

25+
2426
class OperatorNode extends Node {
25-
private char operator;
26-
private Node leftNode;
27-
private Node rightNode;
28-
29-
public OperatorNode(char operator, Node leftNode, Node rightNode) {
30-
this.operator = operator;
31-
this.leftNode = leftNode;
32-
this.rightNode = rightNode;
33-
}
34-
35-
public int evaluate() {
36-
int leftValue = this.leftNode == null ? 0 : this.leftNode.evaluate();
37-
int rightValue = this.rightNode == null ? 0 : this.rightNode.evaluate();
38-
if (this.operator == '+') {
39-
return leftValue + rightValue;
40-
} else if (this.operator == '-') {
41-
return leftValue - rightValue;
42-
} else if (this.operator == '*') {
43-
return leftValue * rightValue;
44-
} else {
45-
return leftValue / rightValue;
27+
private char operator;
28+
private Node left;
29+
private Node right;
30+
31+
public OperatorNode(char operator) {
32+
this.operator = operator;
33+
this.left = null;
34+
this.right = null;
35+
}
36+
37+
@Override
38+
public int evaluate() {
39+
return switch(operator) {
40+
case '+' -> left.evaluate() + right.evaluate();
41+
case '-' -> left.evaluate() - right.evaluate();
42+
case '*' -> left.evaluate() * right.evaluate();
43+
case '/' -> left.evaluate() / right.evaluate();
44+
default -> 0;
45+
};
46+
}
47+
48+
public void setRightNode(Node right) {
49+
this.right = right;
50+
}
51+
52+
public void setLeftNode(Node left) {
53+
this.left = left;
4654
}
47-
}
4855
}
4956

50-
5157
/**
5258
* This is the TreeBuilder class.
5359
* You can treat it as the driver code that takes the postinfix input
5460
* and returns the expression tree represnting it as a Node.
5561
*/
5662

5763
class TreeBuilder {
58-
Node buildTree(String[] postfix) {
59-
Stack<Node> stack = new Stack<>();
60-
for(String token: postfix){
61-
if (token.equals("*") || token.equals("+") || token.equals("-") || token.equals("/")) {
62-
Node o2 = stack.pop();
63-
Node o1 = stack.pop();
64-
stack.push(new OperatorNode(token.charAt(0), o1, o2));
65-
} else{
66-
stack.push(new NumericNode(Integer.parseInt(token)));
67-
}
64+
private static final Set<String> OPERATORS = Set.of("+", "-", "/", "*");
65+
66+
Node buildTree(String[] postfix) {
67+
int[] idx = {postfix.length - 1};
68+
return buildTree(postfix, idx);
69+
}
70+
71+
private Node buildTree(String[] postfix, int[] idx) {
72+
if (idx[0] < 0) {
73+
return null;
74+
}
75+
String val = postfix[idx[0]--];
76+
if (OPERATORS.contains(val)) {
77+
OperatorNode node = new OperatorNode(val.charAt(0));
78+
node.setRightNode(buildTree(postfix, idx));
79+
node.setLeftNode(buildTree(postfix, idx));
80+
return node;
81+
}
82+
NumericNode node = new NumericNode(Integer.parseInt(val));
83+
return node;
6884
}
69-
return stack.pop();
70-
}
7185
};
7286

7387

0 commit comments

Comments
 (0)