Infix to Prefix Conversion Using Stack

Infix to Prefix conversion refers to the process of converting an infix expression (where operators are placed between operands) into a prefix expression (where operators are placed before operands). This conversion is useful in various applications, including compilers, and evaluating expressions in programming languages.

Here’s the algorithm for infix-to-prefix conversion using a stack:

Algorithm: Infix to Prefix Conversion Using Stack

Step 01: Initialize

  • Create an empty stack.
  • Reverse the infix expression.
    • Swap the left parenthesis ( with the right parenthesis ) and vice versa.
  • Start scanning the reversed expression from left to right.

Step 02: For each symbol in the expression

  • If the symbol is an operand:
    • Append that operand to the output.
  • If the symbol is a left parenthesis (:
    • Push it onto the stack.
  • If the symbol is a right parenthesis ):
    • Pop operators from the stack and append them to the output until a left parenthesis ( is encountered.
    • Discard both the left and right parentheses.
  • If the symbol is an operator:
    • If the stack is not empty and the precedence of the operator on top of the stack is greater than or equal to the current operator:
      • Pop the operators from the stack and append them to the output.
    • Otherwise (else part): Push the current operator onto the stack.

Step 03: After scanning all the symbols

  • Pop any remaining operators from the stack and and append it to output.

Step 04: Final Step

  • Reverse the output of step 03 to get the prefix expression.

The following diagram will explain it in detail
Infix to Prefix Conversion

Important:

  1. Reversed expression to postfix conversion and standard infix to postfix conversion use the same rules. The process of converting reversed infix to postfix is fundamentally identical to converting standard infix to postfix.
  2. The result of an infix expression will be the same when evaluated in both prefix and postfix notations.

Example: Infix expression 7 + 5 * 3 / 5 ^ 1 + (3 - 2)

Step 01: Reverse the Expression

Reversing the original expression and swapping parentheses:

  • Original expression: 7 + 5 * 3 / 5 ^ 1 + (3 - 2)
  • Reversed expression: ( 2 - 3 ) + 1 ^ 5 / 3 * 5 + 7

the following diagram shows that how to reverse the original infix expression

Reverse the infix in step 01

Step 02: Apply Infix-to-Postfix Conversion on the Reversed Expression

Now we will scan the reversed infix expression ( 2 - 3 ) + 1 ^ 5 / 3 * 5 + 7

Initialization

  • Stack = []
  • Output = []

Infix to Prefix Conversion - step 2

Step-by-Step Processing of the Reversed Expression

Symbol: (
Operator: Push onto the stack.
Stack: ['('], Output: []

Infix to Prefix Conversion - step 3

Symbol: 2
Operand: Append to the output.
Stack: ['('], Output: ['2']

Infix to Prefix Conversion - step 4

Symbol: -
Operator: Push onto the stack.
Stack: ['(', '-'], Output: ['2']

Infix to Prefix Conversion - step 5

Symbol: 3
Operand: Append to the output.
Stack: ['(', '-'], Output: ['2', '3']

Infix to Prefix Conversion - step 6

Symbol: )
Operator: Pop from the stack until ( is encountered.
Pop - and append it to the output, then discard the parentheses.
Stack: [], Output: ['2', '3', '-']

Infix to Prefix Conversion - step 7

Symbol: +
Operator: Push onto the stack.
Stack: ['+'], Output: ['2', '3', '-']

Infix to Prefix Conversion - step 8

Symbol: 1
Operand: Append to the output.
Stack: ['+'], Output: ['2', '3', '-', '1']

Infix to Prefix Conversion - step 9

Symbol: ^
Operator: Push onto the stack.
Stack: ['+', '^'], Output: ['2', '3', '-', '1']

Infix to Prefix Conversion - step 10

Symbol: 5
Operand: Append to the output.
Stack: ['+', '^'], Output: ['2', '3', '-', '1', '5']

Infix to Prefix Conversion - step 11

Symbol: /
Operator: Pop ^first (because it has higher precedence) from the stack and append it to the output.
Push / onto the stack.
Stack: ['+', '/'], Output: ['2', '3', '-', '1', '5', '^']

Infix to Prefix Conversion - step 12

 

Symbol: 3
Operand: Append to the output.
Stack: ['+', '/'], Output: ['2', '3', '-', '1', '5', '^', '3']

Infix to Prefix Conversion - step 13

Symbol: *
Operator: Push * onto the stack.
Stack: ['+', '/', '*'], Output: ['2', '3', '-', '1', '5', '^', '3']

Infix to Prefix Conversion - step 14

Symbol: 5
Operand: Append to the output.
Stack: ['+', '/', '*'], Output: ['2', '3', '-', '1', '5', '^', '3', '5']

Infix to Prefix Conversion - step 15

Symbol: +
Operator: Pop all operators of equal or higher precedence (*, /) from the stack and append them to the output.
Push + onto the stack. Note: if the same operator occurs as (+), push it into the stack over the previous +.
Stack: ['+', '+'], Output: ['2', '3', '-', '1', '5', '^', '3', '5', '*', '/']

Infix to Prefix Conversion - step 16

Symbol: 7
Operand: Append to the output.
Stack: ['+','+'], Output: ['2', '3', '-', '1', '5', '^', '3', '5', '*', '/', '7']

Infix to Prefix Conversion - step 17

Step 03: Pop Remaining Operators

Pop the remaining operators from the stack and append them to the output.
Stack: [], Output: ['2', '3', '-', '1', '5', '^', '3', '5', '*', '/', '7', '+', '+']

Infix to Prefix Conversion - step 18

Step 03 provides the postfix notation: 2 3 – 1 5 ^ 3 5 * / 7 + +

Step 04: Reverse the Output of Step 03

Reverse the output of step 03 to get the final prefix expression.

+ + 7 / * 5 3 ^ 5 1 - 3 2
Reverse the output of step 03, infix to prefix

This is the required prefix expression for the given infix expression 7 + 5 * 3 / 5 ^ 1 + (3 - 2).

Important: After evaluation, the output of any infix expression gives the same output for their prefix or postfix notation.

Given Infix Expression: 7 + 5 * 3 / 5 ^ 1 + (3 - 2).

  • Prefix Expression:+ + 7 / * 5 3 ^ 5 1 - 3 2
  • Postfix Expression7 5 3 * 5 1 ^ / 3 2 - ++