EVALUATE PREORDER TO POSTORDER EXPRESSION
EVALUATE PREORDER TO POSTORDER EXPRESSION
class preTOpost:
def __init__(self):
self.items=[]
self.size=-1
def push(self,value):
self.items.append(value)
self.size+=1
def popp(self):
# print("pop")
if self.isempty():
return 0
else:
self.size-=1
return self.items.pop()
def isempty(self):
if self.size==-1:
return True
else:
return False
def reverse(self,expr):
rev=""
for i in expr:
if i is '(':
i=')'
elif i is ')':
i='('
rev=i+rev
return rev
def pretoPOST(self,rev):
for i in rev:
if i in '-/*+^':
a=str(self.popp())
b=str(self.popp())
#print(type(a))
temp=a+b+i
self.push(temp)
else:
self.push(i)
return self.popp()
s=preTOpost()
expr=input("Enter the Expr: ")
rev=""
rev=s.reverse(expr)
res=s.pretoPOST(rev)
if res!=False:
print("POSTFIX: ",res)
Comments
Post a Comment