Infix to Postfix Java

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 ExpressionPostfix Expression
A*B/CAB*C/
(A/(B-C+D))*(E-A)*CABC-D+/EA-*C*
A/B-C+D*E-A*CAB/C-DE*AC*-

Infix to Postfix Conversion Example

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

S.N.InputOperand StackPostfix Expression
1((-
2X(X
3-( -X
4Y( -XY
5/( - /XY
6(( - / (XY
7Z( - / (XYZ
8+( - / ( +XYZ
9U( - / ( +XYZU
10)( - /XYZU+
11*( - *XYZU+/
12V( - *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.
    2. Push the new operator onto the stack
  4. If a left parenthesis is scanned, push it into the stack.
  5. If a right parenthesis is scanned, all operators down to the most recently scanned left parenthesis must be popped and appended to the postfix string. Furthermore, the pair of parentheses must be discarded.
  6. When the infix string is fully scanned, the stack may still contain some operators. All the remaining operators should be popped and appended to the postfix string.

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

Explanation

This Java code uses a stack data structure to convert infix expressions to postfix expressions. It uses a stack class to implement fundamental stack operations like push, pop, isEmpty(), and peek. To convert an infix expression to a postfix, the main class InfixToPostfix first asks the user to submit the expression and then calls the toPostfix() method.

The toPostfix() method handles every character in the infix expression. Opening brackets are placed onto the stack, whereas operands (letters) are inserted straight into the postfix string. Operators are removed from the stack and added to the postfix string until the corresponding opening parenthesis is reached when a closing parenthesis is found.

Postfix to Infix Conversion Example

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

S.N.InputOperand StackInfix Expression
1AA-
2BA, B-
3+A + BA + B
4CA + B, CA + B - C
5DA + B, C, DA + B - C
6-A + B, C - DA + B - C - D
7/A + B / C - DA + 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
  4. If the expression is not ended go to the first step.
  5. Pop operand stack and display.
  6. Exit

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

Explanation

This Java code converts a postfix expression to an infix expression using a stack data structure. The isOperator() method checks whether a character is an operator. In the convertPostfixToInfix() method, a stack is used to store operands and intermediate infix expressions.

It iterates through each character of the postfix expression. If an operator is encountered, it pops two operands from the stack, concatenates them with the operator enclosed in parentheses, and pushes the resulting string back onto the stack. If the character is an operand, it simply pushes it onto the stack. Finally, the top element from the stack that represents the infix expression, is returned.

The main() method creates an object of the PostfixToInfixConversion class, prompts the user to enter a postfix expression, calls the conversion method, and prints the resulting infix expression.

Evaluating Postfix Expressions

Postfix expressions are particularly convenient for evaluation because they eliminate the need for parentheses and explicit operator precedence rules. Here's an algorithm for evaluating postfix expressions:

  1. Scan the Postfix Expression: Start scanning the postfix expression from left to right.
  2. Operand Encountered: If an operand is encountered, push it onto the operand stack.
  3. Operator Encountered: If an operator is encountered, pop the top two elements from the operand stack (considered as operands for the operator), perform the operation, and push the result back onto the operand stack.
  4. Repeat Until End: Repeat steps 2 and 3 until the end of the postfix expression.
  5. Final Result: After processing the entire expression, the final result will be the only element remaining in the operand stack.

Let's implement the evaluation of postfix expressions in Java:

PostfixEvaluation.java

Output:

Result of evaluating postfix expression: 11

Explanation

In this program, the operator handling section is crucial for evaluating the postfix expression. When encountering an operator, the top two operands from the stack are popped. These operands represent the right and left operands in the expression. Then, based on the operator encountered, the program performs the corresponding arithmetic operation between the two operands. The result is then pushed back onto the stack.

For instance, when the program encounters the '*' operator, it pops the top two operands, multiplies them, and pushes the result back onto the stack. Similarly, for other operators like '+', '-', '/', and '%', the program performs the respective arithmetic operations.

After iterating through the entire postfix expression and evaluating all the operators and operands, the final result is the only element remaining in the stack, which is then popped and returned as the result of the postfix expression evaluation. This method provides an efficient and systematic approach to evaluating postfix expressions, leveraging the stack data structure and arithmetic operations.

Conclusion

To sum up, knowledge of infix and postfix expressions and how to convert them is essential to computer science and programming, especially for tasks like parsing and expression evaluation. The notions of infix and postfix expressions, their distinctions, and the techniques for transforming infix expressions into postfix and vice versa were all covered in this article. For both conversion procedures, we also included thorough descriptions and Java implementations.