-
Notifications
You must be signed in to change notification settings - Fork 129
/
Expression_Tree.py
65 lines (51 loc) · 1.19 KB
/
Expression_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
# Python program for expression tree
# An expression tree node
class Et:
# Constructor to create a node
def __init__(self , value):
self.value = value
self.left = None
self.right = None
# A utility function to check if 'c'
# is an operator
def isOperator(c):
if (c == '+' or c == '-' or c == '*'
or c == '/' or c == '^'):
return True
else:
return False
# A utility function to do inorder traversal
def inorder(t):
if t is not None:
inorder(t.left)
print t.value,
inorder(t.right)
# Returns root of constructed tree for
# given postfix expression
def constructTree(postfix):
stack = []
# Traverse through every character of input expression
for char in postfix :
# if operand, simply push into stack
if not isOperator(char):
t = Et(char)
stack.append(t)
# Operator
else:
# Pop two top nodes
t = Et(char)
t1 = stack.pop()
t2 = stack.pop()
# make them children
t.right = t1
t.left = t2
# Add this subexpression to stack
stack.append(t)
# Only element will be the root of expression tree
t = stack.pop()
return t
# Driver program to test above
postfix = "ab+ef*g*-"
r = constructTree(postfix)
print ("Infix expression is")
inorder(r)