Skip to content

1628. Design an Expression Tree With Evaluate Function 👍

  • Time: $O(n)$
  • Space: $O(n)$
 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
/**
 * 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() const = 0;

 protected:
  // define your fields here
};

class ExpNode : public Node {
 public:
  ExpNode(const string& val, ExpNode* left, ExpNode* right)
      : val(val), left(left), right(right) {}

  int evaluate() const override {
    return left == nullptr && right == nullptr
               ? stoi(val)
               : op.at(val)(left->evaluate(), right->evaluate());
  }

 private:
  static const inline unordered_map<string, function<long(long, long)>> op{
      {"+", std::plus<long>()},
      {"-", std::minus<long>()},
      {"*", std::multiplies<long>()},
      {"/", std::divides<long>()}};
  const string val;
  const ExpNode* const left;
  const ExpNode* const 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) {
    stack<ExpNode*> stack;

    for (const string& val : postfix)
      if (val == "+" || val == "-" || val == "*" || val == "/") {
        ExpNode* right = stack.top();
        stack.pop();
        ExpNode* left = stack.top();
        stack.pop();
        stack.push(new ExpNode(val, left, right));
      } else {
        stack.push(new ExpNode(val, nullptr, nullptr));
      }

    return stack.top();
  }
};
 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
/**
 * This is the interface for the expression tree Node.
 * You should not remove it, and you can define some classes to implement it.
 */

abstract class Node {
  public abstract int evaluate();
  // define your fields here
};

class ExpNode extends Node {
  public ExpNode(String val, ExpNode left, ExpNode right) {
    this.val = val;
    this.left = left;
    this.right = right;
  }

  public int evaluate() {
    if (left == null && right == null)
      return Integer.parseInt(val);
    return op.get(val).apply(left.evaluate(), right.evaluate());
  }

  private static final Map<String, BinaryOperator<Integer>> op =
      Map.of("+", (a, b) -> a + b, "-", (a, b) -> a - b, "*", (a, b) -> a *b, "/", (a, b) -> a / b);
  private final String val;
  private final ExpNode left;
  private final ExpNode 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 {
  Node buildTree(String[] postfix) {
    Deque<ExpNode> stack = new ArrayDeque<>();

    for (final String val : postfix)
      if (val.equals("+") || val.equals("-") || val.equals("*") || val.equals("/")) {
        ExpNode right = stack.pop();
        ExpNode left = stack.pop();
        stack.push(new ExpNode(val, left, right));
      } else {
        stack.push(new ExpNode(val, null, null));
      }

    return stack.pop();
  }
}
 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
from abc import ABC, abstractmethod

"""
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(ABC):
  @abstractmethod
  # define your fields here
  def evaluate(self) -> int:
    pass


class ExpNode(Node):
  op = {
      '+': lambda a, b: a + b,
      '-': lambda a, b: a - b,
      '*': lambda a, b: a * b,
      '/': lambda a, b: int(a / b),
  }

  def __init__(self, val: str, left: Optional['ExpNode'], right: Optional['ExpNode']):
    self.val = val
    self.left = left
    self.right = right

  def evaluate(self) -> int:
    if not self.left and not self.right:
      return int(self.val)
    return ExpNode.op[self.val](self.left.evaluate(), self.right.evaluate())


"""
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(object):
  def buildTree(self, postfix: List[str]) -> 'Node':
    stack: List[Optional[ExpNode]] = []

    for val in postfix:
      if val in '+-*/':
        right = stack.pop()
        left = stack.pop()
        stack.append(ExpNode(val, left, right))
      else:
        stack.append(ExpNode(val, None, None))

    return stack.pop()