-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathparse-tree.py
76 lines (55 loc) · 1.89 KB
/
parse-tree.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
from stack import Stack
import operator
import re
class TreeNode:
def __init__(self, key=None, left=None, right=None):
self.key = key
self.left = left
self.right = right
def insert_root_value(self, key=None):
self.key = key
def insert_left(self, key=None):
self.left = TreeNode(key, self.left)
def insert_right(self, key=None):
self.right = TreeNode(key, None, self.right)
def get_root_value(self):
return self.key
def get_left_child(self):
return self.left
def get_right_child(self):
return self.right
opers = {'+':operator.add, '-':operator.sub, '*':operator.mul, '/':operator.truediv}
pattern = re.compile("[0-9]")
string = "( ( 10 + 5 ) * 3 )"
def tree_parser(s: str) -> TreeNode:
arr: list = s.split()
stack: Stack = Stack()
node: TreeNode = TreeNode()
current_node: TreeNode = node
stack.push(node)
for e in arr:
if e == "(":
current_node.insert_left()
stack.push(current_node)
current_node = current_node.get_left_child()
elif e in "+-*/":
current_node.insert_root_value(e)
current_node.insert_right()
stack.push(current_node)
current_node = current_node.get_right_child()
elif pattern.match(e):
current_node.insert_root_value(int(e))
current_node = stack.pop()
elif e == ")":
current_node = stack.pop()
else:
raise Exception()
return node
tree_node: TreeNode = tree_parser(string)
def evaluate(node: TreeNode) -> int:
if node.get_left_child() is not None and node.get_right_child() is not None:
f = opers[node.get_root_value()]
return f(evaluate(node.get_left_child()), evaluate(node.get_right_child()))
else:
return node.get_root_value()
print(evaluate(tree_node)) # 45