-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuildParseTree.py
53 lines (40 loc) · 1.45 KB
/
buildParseTree.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
import operator
from Stack import Stack
from BinaryTree import BinaryTree
def build_parse_tree(fully_parenthesized_expressions):
fplist = fully_parenthesized_expressions.split()
p_stack = Stack()
tree = BinaryTree('')
p_stack.push(tree)
current_tree = tree
for i in fplist:
if i == '(':
current_tree.insert_left('')
p_stack.push(current_tree)
current_tree = current_tree.get_left_child()
elif i not in ['+', '-', '/', '*', ')']:
current_tree.set_root_value(int(i))
current_tree = p_stack.pop()
elif i in ['+', '-', '/', '*']:
current_tree.set_root_value(i)
current_tree.insert_right('')
p_stack.push(current_tree)
current_tree = current_tree.get_right_child()
elif i == ')':
current_tree = p_stack.pop()
else:
raise ValueError
return tree
def evaluate(parse_tree):
operators = {'+': operator.add, '-': operator.sub, '*': operator.mul, '/': operator.truediv}
left_child = parse_tree.get_left_child()
right_child = parse_tree.get_right_child()
if left_child and right_child:
return operators[parse_tree.get_root_value()](evaluate(left_child), evaluate(right_child))
else:
return parse_tree.get_root_value()
def main():
tree = build_parse_tree('( ( 11 + 6 ) * 2 )')
print(evaluate(tree))
if __name__ == '__main__':
main()