Infix : An expression is called the Infix expression if the operator appears in between the operands in the expression. Simply of the form (operand1 operator operand2).
Example : (A+B) * (C-D)
Prefix : An expression is called the prefix expression if the operator appears in the expression before the operands. Simply of the form (operator operand1 operand2).
Example : *+AB-CD (Infix : (A+B) * (C-D) )
Given an Infix expression, convert it into a Prefix expression using two stacks.
Input : A * B + C / D Output : + * A B/ C D Input : (A - B/C) * (A/K-L) Output : *-A/BC-/AKL
The idea is to use one stack for storing operators and other to store operands. The stepwise algo is:
- Traverse the infix expression and check if given character is an operator or an operand.
- If it is an operand, then push it into operand stack.
- If it is an operator, then check if priority of current operator is greater than or less than or equal to the operator at top of the stack. If priority is greater, then push operator into operator stack. Otherwise pop two operands from operand stack, pop operator from operator stack and push string operator + operand1 + operand 2 into operand stack. Keep popping from both stacks and pushing result into operand stack until priority of current operator is less than or equal to operator at top of the operator stack.
- If current character is ‘(‘, then push it into operator stack.
- If current character is ‘)’, then check if top of operator stack is opening bracket or not. If not pop two operands from operand stack, pop operator from operator stack and push string operator + operand1 + operand 2 into operand stack. Keep popping from both stacks and pushing result into operand stack until top of operator stack is an opening bracket.
- The final prefix expression is present at top of operand stack.
Below is the implementation of above algorithm:
Time Complexity: O(n)
Auxiliary Space: O(n)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Prefix to Infix Conversion
- Convert Infix To Prefix Notation
- Postfix to Prefix Conversion
- Prefix to Postfix Conversion
- Infix to Postfix using different Precedence Values for In-Stack and Out-Stack
- Iterative Postorder Traversal | Set 1 (Using Two Stacks)
- Bubble sort using two Stacks
- Postfix to Infix
- Program to convert Infix notation to Expression Tree
- Stack | Set 2 (Infix to Postfix)
- Implement two stacks in an array
- Merging and Sorting Two Unsorted Stacks
- Add two numbers represented by Stacks
- Check if the two given stacks are same
- Sorting array using Stacks
- Queue using Stacks
- Sudo Placement[1.3] | Playing with Stacks
- Maximize sum of topmost elements of S stacks by popping at most N elements
- List of Stacks in C++ STL
- Find maximum sum possible equal sum of three stacks
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.