Infix to Postfix Conversion in Java

infix to postfix

Infix to Postfix:

First we will understand what is infix and what is prefix conversion.

Infix expression:    Expression of the form a operator  b. When an operator is in between the operands.
Postfix expression: Expression of the form a b operator. When an operator is followed for every pair of operands.

ALGORITHM:

  •  Scan infix expression from left to right.
  •  If there is a character as operand, output it.
  •  if not
  •       1 If the precedence of the scanned operator is greater than the precedence of the operator in the stack(or the stack is empty           or the stack contains a ‘(‘ ), push it. 
          2 Else, Pop all the operators from the stack which are greater than or equal to in precedence than that of the scanned operator. After doing that Push the scanned operator to the stack. (If you encounter parenthesis while popping then stop there and push the scanned operator in the stack.)
  • If the scanned character is an ‘(‘, push it to the stack.
  •  If the character character is an ‘)’, pop the stack and and output it until a ‘(‘ is encountered, and discard both the parenthesis.
  • Repeat steps 2-6 until infix expression is scanned.
  •  display the output
  • Pop and output from the stack until it is not empty.

Java code :


 
import java.util.Stack;
 
public class InfixToPostfix {
    
    public static boolean isOperator(char op){
        if(op == ‘+’ || op == ‘-‘ || op == ‘*’ || op == ‘/’ || op ==‘%’)
            return true;
        return false;
    }
 
    /*  precedence to an operator */
    public static int precedence(char op){
        switch (op){
            case ‘+’ :
            case ‘-‘ :
                return 1;
            case ‘/’ :
            case ‘*’ :
                return 2;
            case ‘%’ :
                return 3;
            default :
                return 4;
        }
    }
 
    /* This function tell if the op1 has lower precedence than op2 */
    public static boolean isLowerPrecedence(char op1char op2){
        if(precedence (op1) < precedence(op2))
            return true;
        return false;
    }
    public static String convertInixToPostfix(String infix){
        Stack<Characterstack = new Stack();
        StringBuilder sb = new StringBuilder();
 
        for(int i=0; i<infix.length(); i++){
            char currentCharacter = infix.charAt(i);
            //If it’s an operand, put it on output string
            if(!isOperator(currentCharacter)){
                sb.append(currentCharacter);
            }
            else{
                if(stack.empty()) stack.push(currentCharacter);
                else{
                    while(!stack.empty()
                    && isLowerPrecedence(currentCharacter, stack.peek())){
                        sb.append(stack.pop());
                    }
                    stack.push(currentCharacter);
                }
            }
        }
        while(!stack.empty()) sb.append(stack.pop());
 
        return sb.toString();
    }
 
    public static void main(String[] args){
        System.out.println(convertInixToPostfix(“4*5+6/7”));
    }
}
OUTPUT:

input:

3+6*4/8

output:

36*48/+

 

ADVANTAGE OF POSTFIX:

  1. Any formula can be expressed without parenthesis.
  2. It is very convenient for evaluating formulas on computer with stacks.
  3. Postfix expression doesn’t has the operator precedence.
  4. Postfix is slightly easier to evaluate.
  5. It reflects the order in which operations are performed.
  6. You need to worry about the left and right associativity.