-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path07_stack_infix_to_postfix_and_prefix.py
More file actions
75 lines (58 loc) · 2.01 KB
/
07_stack_infix_to_postfix_and_prefix.py
File metadata and controls
75 lines (58 loc) · 2.01 KB
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
66
67
68
69
70
71
72
73
74
75
class Stack():
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
def pop(self):
if not self.is_empty():
return self.items.pop()
def is_empty(self):
return self.items == []
def peek(self):
if not self.is_empty():
return self.items[-1]
def get_Stack(self):
return self.items
class Solution:
def convertInfixToPostfix(self, expr: str) -> str:
precedence = {'+': 1, '-': 1, '*': 2, '/': 2}
stack = Stack()
postfix = ""
for ch in expr:
if ch.isalnum():
postfix += ch
elif ch == '(':
stack.push(ch)
elif ch == ')':
while not stack.is_empty() and stack.peek() != '(':
postfix += stack.pop()
stack.pop()
elif ch in precedence:
while (not stack.is_empty() and stack.peek() != '(' and
precedence[ch] <= precedence.get(stack.peek(), 0)):
postfix += stack.pop()
stack.push(ch)
while not stack.is_empty():
postfix += stack.pop()
return postfix
def convertInfixToPrefix(self, expr: str) -> str:
# Step 1: Reverse the expression and swap parentheses
expr = expr[::-1]
expr = list(expr)
for i in range(len(expr)):
if expr[i] == '(':
expr[i] = ')'
elif expr[i] == ')':
expr[i] = '('
expr = ''.join(expr)
# Step 2: Get postfix of the modified expression
postfix = self.convertInfixToPostfix(expr)
# Step 3: Reverse postfix to get prefix
prefix = postfix[::-1]
return prefix
if __name__ == "__main__":
obj = Solution()
expr = "(a+b)*(c+e)"
print("Infix Expression:", expr)
print("Postfix Expression:", obj.convertInfixToPostfix(expr))
print("Prefix Expression:", obj.convertInfixToPrefix(expr))