forked from Ishaan28malik/Hacktoberfest-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exp.py
46 lines (35 loc) · 835 Bytes
/
exp.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
class Et:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def isOperator(c):
if (c == '+' or c == '-' or c == '*' or c == '/' or c == '^'):
return True
else:
return False
def inorder(t):
ar = []
if t is not None:
inorder(t.left)
print(t.value)
inorder(t.right)
def constructTree(postfix):
stack = []
for char in postfix:
if not isOperator(char):
t = Et(char)
stack.append(t)
else:
t = Et(char)
t1 = stack.pop()
t2 = stack.pop()
t.right = t1
t.left = t2
stack.append(t)
t = stack.pop()
return t
postfix = "ab+c-"
r = constructTree(postfix)
print("Infix expression is")
inorder(r)