|
4 | 4 | */
|
5 | 5 |
|
6 | 6 | abstract class Node {
|
7 |
| - public abstract int evaluate(); |
8 |
| - // define your fields here |
| 7 | + public abstract int evaluate(); |
| 8 | + // define your fields here |
9 | 9 | };
|
10 | 10 |
|
| 11 | + |
11 | 12 | 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 | + } |
22 | 23 | }
|
23 | 24 |
|
| 25 | + |
24 | 26 | 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; |
46 | 54 | }
|
47 |
| - } |
48 | 55 | }
|
49 | 56 |
|
50 |
| - |
51 | 57 | /**
|
52 | 58 | * This is the TreeBuilder class.
|
53 | 59 | * You can treat it as the driver code that takes the postinfix input
|
54 | 60 | * and returns the expression tree represnting it as a Node.
|
55 | 61 | */
|
56 | 62 |
|
57 | 63 | 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; |
68 | 84 | }
|
69 |
| - return stack.pop(); |
70 |
| - } |
71 | 85 | };
|
72 | 86 |
|
73 | 87 |
|
|
0 commit comments