Infix to Postfix using different Precedence Values for In-Stack and Out-Stack
Conversion of infix to postfix expression can be done elegantly using two precedence function. Each operator is assigned a value (larger value means higher precedence) which depends upon whether the operator is inside or outside the stack. Also the right and left associativity for different operators can be handled by varying it’s values in the two precedence functions.
Infix expression example: a+b*c
Its corresponding postfix expression: abc*+
To know more about infix and postfix expressions visit the links.

Precedence Values Table for Operators
Examples:
Input: str = “a+b*c-(d/e+f^g^h)”
Output: abc*+de/fgh^^+-Input: a+b*c
Output: abc*+
Approach:
- If the input character is an operand, print it.
- If the input character is an operator-
- If stack is empty push it to the stack.
- If it’s precedence value is greater than the precedence value of the character on top, push.
- If it’s precedence value is lower then pop from stack and print while precedence of top char is more than the precedence value of the input character.
- If the input character is ‘)’, then pop and print until top is ‘(‘. (Pop ‘(‘ but don’t print it.)
- If stack becomes empty before encountering ‘(‘, then it’s a invalid expression.
- Repeat steps 1-4 until input expression is completely read.
- Pop the remaining elements from stack and print them.
The above method handles right associativity of exponentiation operator (here, ^) by assigning it higher precedence value outside stack and lower precedence value inside stack whereas it’s opposite for left associative operators.
Below is the implementation of the above approach:
C++
// C++ program to convert an infix expression to a // postfix expression using two precedence function #include <bits/stdc++.h> using namespace std; // to check if the input character // is an operator or a '(' int isOperator(char input) { switch (input) { case '+': return 1; case '-': return 1; case '*': return 1; case '^': return 1; case '%': return 1; case '/': return 1; case '(': return 1; } return 0; } // to check if the input character is an operand int isOperand(char input) { if (input >= 65 && input <= 90 || input >= 97 && input <= 122) return 1; return 0; } // function to return precedence value // if operator is present in stack int inPrec(char input) { switch (input) { case '+': case '-': return 2; case '*': case '%': case '/': return 4; case '^': return 5; case '(': return 0; } } // function to return precedence value // if operator is present outside stack. int outPrec(char input) { switch (input) { case '+': case '-': return 1; case '*': case '%': case '/': return 3; case '^': return 6; case '(': return 100; } } // function to convert infix to postfix void inToPost(char* input) { stack<char> s; // while input is not NULL iterate int i = 0; while (input[i] != '\0') { // if character an operand if (isOperand(input[i])) { printf("%c", input[i]); } // if input is an operator, push else if (isOperator(input[i])) { if (s.empty() || outPrec(input[i]) > inPrec(s.top())) s.push(input[i]); else { while (!s.empty() && outPrec(input[i]) < inPrec(s.top())) { printf("%c", s.top()); s.pop(); } s.push(input[i]); } } // condition for opening bracket else if (input[i] == ')') { while (s.top() != '(') { printf("%c", s.top()); s.pop(); // if opening bracket not present if (s.empty()) { printf("Wrong input\n"); exit(1); } } // pop the opening bracket. s.pop(); } i++; } // pop the remaining operators while (!s.empty()) { if (s.top() == '(') { printf("\n Wrong input\n"); exit(1); } printf("%c", s.top()); s.pop(); } } // Driver code int main() { char input[] = "a+b*c-(d/e+f^g^h)"; inToPost(input); return 0; } |
Java
import static java.lang.System.exit; import java.util.Stack; // Java program to convert an infix expression to a // postfix expression using two precedence function class GFG { // to check if the input character // is an operator or a '(' static int isOperator(char input) { switch (input) { case '+': return 1; case '-': return 1; case '*': return 1; case '^': return 1; case '%': return 1; case '/': return 1; case '(': return 1; } return 0; } // to check if the input character is an operand static int isOperand(char input) { if (input >= 65 && input <= 90 || input >= 97 && input <= 122) { return 1; } return 0; } // function to return precedence value // if operator is present in stack static int inPrec(char input) { switch (input) { case '+': case '-': return 2; case '*': case '%': case '/': return 4; case '^': return 5; case '(': return 0; } return 0; } // function to return precedence value // if operator is present outside stack. static int outPrec(char input) { switch (input) { case '+': case '-': return 1; case '*': case '%': case '/': return 3; case '^': return 6; case '(': return 100; } return 0; } // function to convert infix to postfix static void inToPost(char[] input) { Stack<Character> s = new Stack<Character>(); // while input is not NULL iterate int i = 0; while (input.length!=i) { // if character an operand if (isOperand(input[i]) == 1) { System.out.printf("%c", input[i]); } // if input is an operator, push else if (isOperator(input[i]) == 1) { if (s.empty() || outPrec(input[i]) > inPrec(s.peek())) { s.push(input[i]); } else { while (!s.empty() && outPrec(input[i]) < inPrec(s.peek())) { System.out.printf("%c", s.peek()); s.pop(); } s.push(input[i]); } } // condition for opening bracket else if (input[i] == ')') { while (s.peek() != '(') { System.out.printf("%c", s.peek()); s.pop(); // if opening bracket not present if (s.empty()) { System.out.printf("Wrong input\n"); exit(1); } } // pop the opening bracket. s.pop(); } i++; } // pop the remaining operators while (!s.empty()) { if (s.peek() == '(') { System.out.printf("\n Wrong input\n"); exit(1); } System.out.printf("%c", s.peek()); s.pop(); } } // Driver code static public void main(String[] args) { char input[] = "a+b*c-(d/e+f^g^h)".toCharArray(); inToPost(input); } } // This code is contributed by Rajput-Ji |
C#
// C# program to convert an infix expression to a // postfix expression using two precedence function using System.Collections.Generic; using System; public class GFG { // to check if the input character // is an operator or a '(' static int isOperator(char input) { switch (input) { case '+': return 1; case '-': return 1; case '*': return 1; case '^': return 1; case '%': return 1; case '/': return 1; case '(': return 1; } return 0; } // to check if the input character is an operand static int isOperand(char input) { if (input >= 65 && input <= 90 || input >= 97 && input <= 122) { return 1; } return 0; } // function to return precedence value // if operator is present in stack static int inPrec(char input) { switch (input) { case '+': case '-': return 2; case '*': case '%': case '/': return 4; case '^': return 5; case '(': return 0; } return 0; } // function to return precedence value // if operator is present outside stack. static int outPrec(char input) { switch (input) { case '+': case '-': return 1; case '*': case '%': case '/': return 3; case '^': return 6; case '(': return 100; } return 0; } // function to convert infix to postfix static void inToPost(char[] input) { Stack<char> s = new Stack<char>(); // while input is not NULL iterate int i = 0; while (input.Length!=i) { // if character an operand if (isOperand(input[i]) == 1) { Console.Write(input[i]); } // if input is an operator, push else if (isOperator(input[i]) == 1) { if (s.Count==0 || outPrec(input[i]) > inPrec(s.Peek())) { s.Push(input[i]); } else { while (s.Count!=0 && outPrec(input[i]) < inPrec(s.Peek())) { Console.Write(s.Peek()); s.Pop(); } s.Push(input[i]); } } // condition for opening bracket else if (input[i] == ')') { while (s.Peek() != '(') { Console.Write(s.Peek()); s.Pop(); // if opening bracket not present if (s.Count==0) { Console.Write("Wrong input\n"); return; } } // pop the opening bracket. s.Pop(); } i++; } // pop the remaining operators while (s.Count!=0) { if (s.Peek() == '(') { Console.Write("\n Wrong input\n"); return; } Console.Write(s.Peek()); s.Pop(); } } // Driver code static public void Main() { char []input = "a+b*c-(d/e+f^g^h)".ToCharArray(); inToPost(input); } } // This code is contributed by Rajput-Ji |
abc*+de/fgh^^+-
Recommended Posts:
- Postfix to Infix
- Stack | Set 2 (Infix to Postfix)
- Prefix to Infix Conversion
- Convert Infix To Prefix Notation
- Infix to Prefix conversion using two stacks
- Postfix to Prefix Conversion
- Prefix to Postfix Conversion
- Stack | Set 4 (Evaluation of Postfix Expression)
- Mapping external values to dataframe values in Pandas
- Assign other value to a variable from two possible values
- How to get multiple selected values of select box in php?
- Find K items with the lowest values
- Maximum and Minimum Values of an Algebraic Expression
- Get unique values from a column in Pandas DataFrame
- Replace repeating elements with greater that greatest values
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.
Improved By : Rajput-Ji



