Python Program to Convert Postfix to Infix

Python Program to Convert Postfix to Infix

In computer science, postfix (also known as Reverse Polish Notation, RPN) is a mathematical notation where every operator follows all of its operands. On the other hand, in infix notation, the operator is placed between the operands, which is the common mathematical notation we use.
Converting postfix to infix is a common task when dealing with expression evaluations, compilers, or calculators. In this blog post, we will walk through how to write a Python program that converts a postfix expression into an infix expression.

Understanding Postfix and Infix Notation
  • Postfix Expression (RPN): Operators appear after their operands.
  • Example: AB+ → (A + B)
  • Infix Expression: Operators appear between their operands.
  • Example: (A + B)
Steps for Conversion from Postfix to Infix
To convert a postfix expression to infix:
1. Use a stack to store the operands.
2. Traverse the postfix expression from left to right:
  • If the current character is an operand, push it onto the stack.
  • If the current character is an operator, pop two operands from the stack. Place the operator between the two operands and form a string representing the infix expression. Push the resulting string back onto the stack.
3. After traversing the entire postfix expression, the stack should contain one element, which is the desired infix expression.

Python Code
Here’s a Python program to convert a postfix expression into an infix expression:
# Python program to convert postfix to infix
# Function to check if the character is an operand
def is_operand(char):
    return char.isalpha() or char.isdigit()
# Function to convert postfix expression to infix
def postfix_to_infix(postfix_expr):
    # Stack to store operands
    stack = []
     # Traverse the postfix expression
    for char in postfix_expr:
        # If the character is an operand, push it to the stack
        if is_operand(char):
            stack.append(char)
        else:
            # Pop two operands from the stack for the operator
            operand2 = stack.pop()
            operand1 = stack.pop()
            # Create a new infix expression and push it back to the stack
            new_expr = (' + operand1 + char + operand2 + ')
            stack.append(new_expr)
        # The last element in the stack is the final infix expression
    return stack.pop()
# Input: Postfix expression
postfix_expr = "AB+C*DE/-"
# Convert postfix to infix
infix_expr = postfix_to_infix(postfix_expr)
# Output the resulting infix expression
print("Postfix Expression:", postfix_expr)
print("Infix Expression:", infix_expr)

Explanation of the Code
1. is_operand() Function:
  • This function checks whether a character is an operand (either a letter or a digit). Operands are pushed directly onto the stack.
2. postfix_to_infix() Function:
Stack: A stack is initialized to hold operands.
Traversing the Postfix Expression: The program iterates through each character of the postfix expression:
  • If the character is an operand, it is pushed onto the stack.
  • If the character is an operator, two operands are popped from the stack. A new string is formed by placing the operator between the operands (in parentheses), and the result is pushed back onto the stack.
  • After processing all characters, the final infix expression is the last element in the stack.
3. Postfix Input and Conversion:
  • The postfix expression "AB+C*DE/-" is used as input.
  • The program converts the postfix expression to an infix expression by applying the above logic.
Output
Postfix Expression: AB+C*DE/-
Infix Expression: ((A+B)*C-(D/E))

Step-by-Step Breakdown of Conversion
1. For the postfix expression "AB+C*DE/-":
  • Read A → Push to stack.
  • Read B → Push to stack.
  • Read + → Pop B and A, form (A + B), push to stack.
  • Read C → Push to stack.
  • Read * → Pop C and (A + B), form ((A + B) * C), push to stack.
  • Read D → Push to stack.
  • Read E → Push to stack.
  • Read / → Pop E and D, form (D / E), and push to stack.
  • Read - → Pop (D / E) and ((A + B) * C), form (((A + B) * C) - (D / E)), push to stack.
2. The final expression is (((A + B) * C) - (D / E)), which is printed as ((A + B) * C - (D / E)) due to Python's formatting.

This is the screenshot of the Jupyter Notebook of the Above Program
Conclusion
Converting a postfix expression to an infix expression is a useful task in many areas such as expression evaluation, compiler design, and calculators. The key idea is to use a stack and systematically process operands and operators to form the correct infix expression.

Comments