Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Commit 941f380

Browse files
authored
Merge pull request #235 from nutanaarohi123/Infix2Postfix
Added Infix to Postfix Conversion in Python
2 parents 3e1c57c + 30903b2 commit 941f380

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# function to convert infix to postfix using array as stack
2+
def infix_to_postfix(infix_expression):
3+
"""
4+
Function to change infix expression to postfix one
5+
Params:
6+
infix_expression: Infix Expression provided by user
7+
Returns:
8+
postfix_expression: Postfix Expression convertef from Infix one
9+
"""
10+
11+
# initially empty stack
12+
stack = []
13+
# initially empty postfix_expression
14+
postfix_expression = ""
15+
for char in infix_expression:
16+
# if an operand then put it directly in postfix expression
17+
if char not in operators:
18+
postfix_expression += char
19+
# else if operators should be put in stack
20+
elif char == "(":
21+
"""append function to push
22+
elements in the stack"""
23+
stack.append("(")
24+
elif char == ")":
25+
while stack and stack[-1] != "(":
26+
postfix_expression += stack.pop()
27+
""" pop function to pop
28+
elements from stack in LIFO order """
29+
stack.pop()
30+
else:
31+
"""if priority of char in infix_expression is less than or equal to
32+
char at stack[-1] pop out and put in postfix_expression"""
33+
while (
34+
stack and stack[-1] != "(" and priorities[char] <= priorities[stack[-1]]
35+
):
36+
postfix_expression += stack.pop()
37+
stack.append(char)
38+
while stack:
39+
postfix_expression += stack.pop()
40+
return postfix_expression
41+
42+
43+
# Set of operators
44+
operators = set(["+", "-", "*", "/", "(", ")", "^"])
45+
46+
# dictionary having priorities
47+
priorities = {"+": 1, "-": 1, "*": 2, "/": 2, "^": 3}
48+
49+
print("Input")
50+
infix_expression = input("Enter infix expression\n")
51+
print("Output")
52+
53+
# Displaying the Output
54+
print("Postfix expression: ", infix_to_postfix(infix_expression))
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
### Infix to Postfix
2+
3+
> • In Infix expression operator is in between every pair of operands.<br>
4+
> • In Postfix expression, the operator is followed for every pair of operands.
5+
6+
> Infix expression is converted to postfix conversion using Stack.
7+
> Postfix expression is evaluated using Stack in Left to Right order.
8+
9+
##### If the scanned character is operand, show it as output. Else, If precedence of scanned operator is greater than the precedence of the operator in the stack,push it.
10+
11+
> Else, Pop all the operators from the stack which are greater than or equal to in precedence than that of the scanned operator.
12+
13+
• If the scanned input is '(', push it into stack<br>
14+
• If the scanned input is ')' ,pop the stack and the output it until '(' comes.<br>
15+
• Repeat above steps. Continue Pop and output from stack until it becomes empty.
16+
17+
##### It makes the code more efficient and even reduces the time complexity.
18+
### Constraints
19+
"""
20+
Input:
21+
Enter infix expression: A+C*(B^D-E)^(G+H*K)-K
22+
Output:
23+
Postfix expression: ACBD^E-GHK*+^*+K-
24+
25+
Time Complexity : O(n)
26+
Space Complexity: Θ(n)
27+
"""

0 commit comments

Comments
 (0)