VOOZH about

URL: https://www.geeksforgeeks.org/dsa/infix-to-prefix-conversion-using-two-stacks/

⇱ Infix to Prefix conversion using two stacks - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Infix to Prefix conversion using two stacks

Last Updated : 11 Jul, 2025

Given an infix expression, the task is to convert it into a prefix expression using two stacks.

An infix expression is one in which the operator appears between operands. The general structure is: (operand1 operator operand2).
Example: (A + B) * (C - D)

A prefix expression (also called Polish Notation) places the operator before the operands. The structure becomes: (operator operand1 operand2). Example: * + A B - C D (equivalent to infix: (A + B) * (C - D))

Examples:

Input: (A+B)*(C-D)
Output: *+AB-CD
Explanation: (A + B) becomes +AB, (C - D) becomes -CD, then combined with * -> *+AB-CD

Input: A+(B*C)
Output: +A*BC
Explanation: B*C becomes *BC, then added to A -> +A*BC

Input: (A+B)/(C+D)
Output: /+AB+CD

We have already discussed an infix to prefix approach that first reverses the given infix, then converts to postfix and then reverses the postfix to get the final result. Here we are going to discuss a two stack approach.

Approach:

The idea is to use one stack for operators and one for operands. The thought process is to simulate how expressions are evaluated, giving precedence to higher priority operators and handling parentheses carefully. When encountering an operator, we apply the top ones with greater or equal precedence before pushing the new one. Finally, we pop remaining operators and build the result by combining them with top operands in prefix order.

Steps to implement the above idea:

  • Start by initializing two stacks: one stack (operands) to hold operand strings, and another (operators) to hold operator characters.
  • Loop through each character of the given infix expression.
  • If the character is an opening parenthesis '(', simply push it onto the operators stack to mark the beginning of a sub-expression.
  • If it's a closing parenthesis ')', repeatedly pop from the operators stack, and for each operator, pop two operands, combine them into a prefix expression, and push it back into the operands stack until we encounter the matching '('.
  • If the character is an operand (alphanumeric), convert it into a string and push it into the operands stack as it will be part of the final expression.
  • If it's an operator, then check precedence: while the operator at the top of the stack has greater or equal precedence, pop that operator and build prefix using the top two operands.
  • After the loop ends, apply all the remaining operators in the stack the same way by combining them with top two operands and pushing back the result until only one operand (the prefix expression) remains.

Output
*+AB-CD

Time Complexity: O(n), each character is processed at most once using stack operations.
Space Complexity: O(n), stacks for operators and operands store up to n characters total.

Comment
Article Tags: