일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 컴퓨터의 구조
- 109. Convert Sorted List to Binary Search Tree
- iterator
- 315. Count of Smaller Numbers After Self
- Generator
- t1
- 밴픽
- Python
- attribute
- 43. Multiply Strings
- Convert Sorted List to Binary Search Tree
- Substring with Concatenation of All Words
- kaggle
- 715. Range Module
- Python Implementation
- DWG
- Protocol
- 프로그래머스
- 운영체제
- data science
- LeetCode
- Class
- concurrency
- shiba
- Regular Expression
- Python Code
- 30. Substring with Concatenation of All Words
- 파이썬
- Decorator
- 시바견
Archives
- Today
- Total
Scribbling
LeetCode: 1628. Design an Expression Tree With Evaluate Function 본문
Computer Science/Algorithms & Data Structures
LeetCode: 1628. Design an Expression Tree With Evaluate Function
focalpoint 2023. 1. 26. 02:19
First thing first:
To implement an expression tree with "Postfix", we can easily do so with a stack.
For the evaluation part:
"ABC" might be a bit unfamiliar, but all you need to know about it here is that you cannot initiate an instance with the ABC class (in this case Node class).
In addition, an abstract method must be implemented in the child class.
import abc
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 TreeNode(Node):
def __init__(self, val=None, opt=None, left=None, right=None):
self.val = val
self.opt = opt
self.left = left
self.right = right
def evaluate(self):
if self.val is not None:
return self.val
elif self.opt is not None:
if self.opt == '+':
return self.left.evaluate() + self.right.evaluate()
elif self.opt == '-':
return self.left.evaluate() - self.right.evaluate()
elif self.opt == '*':
return self.left.evaluate() * self.right.evaluate()
elif self.opt == '/':
return int(self.left.evaluate() / self.right.evaluate())
else:
raise(ValueError)
else:
raise(ValueError)
"""
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':
s = []
for pf in postfix:
if pf.isnumeric():
s.append(TreeNode(int(pf)))
else:
node2 = s.pop()
node1 = s.pop()
s.append(TreeNode(None, pf, node1, node2))
return s[0]
"""
Your TreeBuilder object will be instantiated and called as such:
obj = TreeBuilder();
expTree = obj.buildTree(postfix);
ans = expTree.evaluate();
"""
'Computer Science > Algorithms & Data Structures' 카테고리의 다른 글
LeetCode: 358. Rearrange String k Distance Apart (0) | 2023.03.10 |
---|---|
Fenwick Tree (or Binary Indexed Tree) Python Implementation, 308. Range Sum Query 2D - Mutable (0) | 2023.03.02 |
Recursive Descent Parsing - Python Implementation (0) | 2022.11.12 |
Rabin-Karp Algorithm (2) | 2022.11.09 |
Python Sorted Containers (0) | 2022.10.26 |