-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.py
41 lines (32 loc) · 870 Bytes
/
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
from constant import FONCTION, OPERATION
import parser
class Node:
def __init__(self, value, leftChild=None, rightChild=None):
self.value = value
self.leftChild = leftChild
self.rightChild = rightChild
def makeTree(postfixExpression):
stack = []
for x in postfixExpression:
if x in FONCTION:
stack.append(Node(x, stack.pop()))
elif x in OPERATION:
left = stack.pop()
right = stack.pop()
stack.append(Node(x, left, right))
else:
stack.append(Node(x))
return stack.pop()
def inOrder(tree):
if tree.leftChild:
inOrder(tree.leftChild)
if tree.rightChild:
inOrder(tree.rightChild)
print(tree.value)
'''
exp = "("
exp += input() + ")"
exp = parser.toPostfix(parser.parse(exp))
tree = makeTree(exp)
inOrder(tree)
'''