Infix to Postfix Java

JavaTpoint

The infix and postfix expressions can have the following operators: '+', '-', '%','*', '/' and alphabets from a to z. The precedence of the operators (+, -) is lesser than the precedence of operators (*, /, %). Parenthesis has the highest precedence and the expression inside it must be converted first. In this section, we will learn how to convert infix expression to postfix expression and postfix to infix expression through a Java program.

For performing the conversion, we use Stack data structure. The stack is used to store the operators and parenthesis to enforce the precedence Start parsing the expression from left to right. Before moving ahead in this section, ensure that you are friendly with the stack and its operations. Let's have a look at infix and postfix expressions.

Infix Expression

Infix expressions are those expressions in which the operator is written in-between the two or more operands. Usually, we use infix expression. For example, consider the following expression.

Postfix Expression

Postfix expressions are those expressions in which the operator is written after their operands. For example, consider the following expression.

Infix vs Postfix Expression

Infix Expression Postfix Expression
A*B/C AB*C/
(A/(B-C+D))*(E-A)*C ABC-D+/EA-*C*
A/B-C+D*E-A*C AB/C-DE*AC*-

Infix to Postfix Conversion Example

Convert the (X - Y / (Z + U) * V) infix expression into postfix expression.

S.N. Input Operand Stack Postfix Expression
1 ( ( -
2 X ( X
3 - ( - X
4 Y ( - XY
5 / ( - / XY
6 ( ( - / ( XY
7 Z ( - / ( XYZ
8 + ( - / ( + XYZ
9 U ( - / ( + XYZU
10 ) ( - / XYZU+
11 * ( - * XYZU+/
12 V ( - * XYZU+/V
13 ) - XYZU+/V*-

Algorithm

  1. Scan the infix notation from left to right one character at a time.
  2. If the next symbol scanned as an operand, append it to the postfix string.
  3. If the next symbol scanned as an operator, the:
    1. Pop and append to the postfix string every operator on the stack that:
      1. Is above the most recently scanned left parenthesis, and
      2. Has precedence higher than or is a right-associative operator of equal precedence to that of the new operator symbol.

      Let's implement the above algorithm in a Java program.

      Java Program to Convert Infix Expression into Postfix Expression

      InfixToPostfixConversion.java

      Output:

      Infix to Postfix Java

      Postfix to Infix Conversion Example

      Convert the AB + CD - / postfix expression into infix expression.

      S.N. Input Operand Stack Infix Expression
      1 A A -
      2 B A, B -
      3 + A + B A+ B
      4 C A + B, C -
      5 D A + B, C, D -
      6 - A + B, C - D C - D
      7 / A + B / C - D A + B / C - D

      Algorithm

      1. Read the postfix expression from left to right one character at a time.
      2. If it is operand push into operand stack.
      3. If it is an operator, then:
        1. Pop two operand from the stack.
        2. From infix expression and push into the operand stack

        Let's implement the above algorithm in a Java program.

        Java Program to Convert Postfix Expression into Infix Expression

        PostfixToInfixConversion.java

        Output:

        Infix to Postfix Java

        Next Topic Memory Leak in Java

        Youtube

        For Videos Join Our Youtube Channel: Join Now

        Feedback

        • Send your Feedback to [email protected]