Infix to Postfix Conversion in Java

Infix to Postfix:

Postfix and prefix expressions are used by compilers to do faster calculations as they support operator precedence in order. An Infix expression is what we humans write mathematical logics as.

Understand what Postfix & Infix is

  • Infix Expression: When an operator is in between the two operands
    • Example: A * B is known as infix expression.
  • Postfix Expression: When operator is after the two operands
    • Example: BD * is known as postfix expression.

Algorithm for Infix to Postfix 

  1. Scan infix expression from left to right.
  2.  If there is a character as operand, output it.
  3.  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.)
  4. If the scanned character is an ‘(‘, push it to the stack.
  5.  If the character is an ‘)’, pop the stack and and output it until a ‘(‘ is encountered, and discard both the parenthesis.
  6. Repeat steps 2-6 until infix expression is scanned.
  7.  display the output
  8. Pop and output from the stack until it is not empty.
Infix to Postfixin in java using Stacks

All Different variation

For the above implementation we had assumed that expression will only have alphabets as operands and ‘(‘ or ‘)’ as braces.

We will also need to take care of the following

  • Operands can be anything alphabets or digits
    • Example – a-z or A-Z or 0 – 9
  • Brackets can be of different variations { } or [ ] or ( )

Output

The Infix Expression is: ((a+(b*c))-d)
The Postfix of the given Infix Expression is: abc*+d-

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.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Stacks

  • Introduction to Stack in Data Structure
    Click Here
  • Operations on a Stack
    Click Here
  • Stack: Infix, Prefix and Postfix conversions
    Click Here
  • Stack Representation in –
    C | C++ | Java
  • Representation of a Stack as an Array. –
    C | C++ | Java
  • Representation of a Stack as a Linked List. –
    C | C++ | Java
  • Infix to Postfix Conversion –
    C | C++ | Java
  • Infix to prefix conversion in –
    C | C++ | Java
  • Postfix to Prefix Conversion in –
    C | C++ | Java

Queues

  • Queues in Data Structures (Introduction)
    Click Here
  • Queues Program in C and implementation
    Click Here
  • Implementation of Queues using Arrays | C Program
    Click Here
  • Types of Queues in Data Structure
    Click Here
  • Application of Queue Data Structure
    Click Here
  • Insertion in Queues Program (Enqueuing) –
    C | C++ | Java
  • Deletion (Removal) in Queues Program(Dequeuing) –
    C | C++ | Java
  • Reverse a Queue –
    C | C++ | Java
  • Queues using Linked Lists –
    C | C++ | Java
  • Implement Queue using Stack –
    C | C++ | Java
  • Implement Queue using two Stacks –
    C | C++ | Java

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction –
    C | C++ | Java
  • Priority Queue Implementation using Array –
    C | C++ | Java
  • Priority Queue using Linked List –
    C | C++ | Java
  • Priority Queue Insertion and Deletion-
    C | C++ | Java

Stacks

Queues

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction – C | C++ | Java
  • Priority Queue Implementation using Array – C | C++ | Java
  • Priority Queue using Linked List – C | C++ | Java
  • Priority Queue Insertion and Deletion- C | C++ | Java