Javatpoint Logo
Javatpoint Logo

Conversion of Prefix to Postfix expression

Before understanding the conversion of prefix to postfix conversion, we should know about the prefix and postfix expressions separately.

What is Prefix conversion?

An infix expression is an expression in which the operators are written between the two operands. If we move the operator before the operands then it is known as a prefix expression. In other words, prefix expression can be defined as an expression in which all the operators precede the two operands.

For example:

If the infix expression is given as: A + B * C

As we know that the multiplication operator * has a higher precedence than the addition operator. First, multiplication operator will move before operand B shown as below:

A + * B C

Once the multiplication operator is moved before 'B' operand, addition operator will move before the operand 'A' shown as below:

+ A * B C

Evaluation of Prefix Expression using Stack

Step 1: Initialize a pointer 'S' pointing to the end of the expression.

Step 2: If the symbol pointed by 'S' is an operand then push it into the stack.

Step 3: If the symbol pointed by 'S' is an operator then pop two operands from the stack. Perform the operation on these two operands and stores the result into the stack.

Step 4: Decrement the pointer 'S' by 1 and move to step 2 as long as the symbols left in the expression.

Step 5: The final result is stored at the top of the stack and return it.

Step 6: End

Let's understand the evaluation of prefix expression through an example.

Expression: +, -, *, 2, 2, /, 16, 8, 5

First, we will reverse the expression given above.

Expression: 5, 8, 16, /, 2, 2, *, -, +

We will use the stack data structure to evaluate the prefix expression.

Symbol Scanned Stack
5 5
8 5, 8
16 5, 8, 16
/ 5, 2
2 5, 2, 2
2 5, 2, 2, 2
* 5, 2, 4
- 5, 2
+ 7

The final result of the above expression is 7.

What is Postfix expression?

If we move the operators after the operands then it is known as a postfix expression. In other words, postfix expression can be defined as an expression in which all the operators are present after the operands.

For example:

If the infix expression is A + B * C

As we know that the multiplication operator has a higher precedence than the addition operator, so multiplication operator will move after the operands B and C shown as below:

A + B C *

Once the multiplication operator is moved after the operand C, then the addition operator will come after the multiplication operator shown as below:

A B C * +

Evaluation of Postfix expression using Stack

Algorithm for the evaluation of postfix expression using stack:

Step 1: Create an empty stack used for storing the operands.

Step 2: Scan each element of an expression one be one and do the following:

  • If the element is an operand then push it into the stack.
  • If the element is an operator then pop two operands from the stack. Perform operation on these operands. Push the final result into the stack.

Step 3: When the expression is scanned completely, the value available in the stack would be the final output of the given expression.

Let's understand the evaluation of postfix expression using stack through an example.

If the expression is: 5, 6, 2, +, *, 12, 4, /, -

Symbol Scanned Stack
5 5
6 5, 6
2 5, 6, 2
+ 5, 8
* 40
12 40, 12
4 40, 12, 4
/ 40, 3
- 37

The result of the above expression is 37.

Conversion of Prefix to Postfix Expression

Here, we will see the conversion of prefix to postfix expression using a stack data structure.

Rules for prefix to postfix expression using stack data structure:

  • Scan the prefix expression from right to left, i.e., reverse.
  • If the incoming symbol is an operand then push it into the stack.
  • If the incoming symbol is an operator then pop two operands from the stack. Once the operands are popped out from the stack, we add the incoming symbol after the operands. When the operator is added after the operands, then the expression is pushed back into the stack.
  • Once the whole expression is scanned, pop and print the postfix expression from the stack.

Pseudocode for prefix to postfix conversion

Let's understand the conversion of Prefix to Postfix expression using Stack through an example.

If the expression is: * - A / B C - / A K L

Symbols to be scanned Action Stack Description
L Push L into the stack L
K Push K into the stack L, K
A Push A into the stack L, K, A
/ Pop A from the stack
Pop K from the stack
Push A K / into the stack
L, A K / Pop two operands from the stack, i.e., A and K. Add '/' operator after K operand, i.e., AK/. Push AK/ into the stack.
- Pop A K / and L from the stack.
Push (A K / L -) into the stack
A K / L - Pop two operands from the stack, i.e., AK/ and L. Add '-' operator after 'L' operand.
C Push C into the stack AK/L-, C
B Push B into the stack AK/L-, C, B
/ Pop B and C from the stack.
Push BC/ into the stack.
AK/L-, BC/ Pop two operands from the stack, i.e., B and C. Add '/' operator after C operator, i.e., BC/. Push BC/ into the stack.
A Push A into the stack AK/L-, BC/, A
- Pop BC/ and A from the stack. Push ABC/- into the stack. AK/L-, ABC/- Pop two operands from the stack, i.e., A and BC/. Add '-' operator after '/'.
* Pop ABC/- and AK/L- from the stack. Push ABC/AK/L-* into the stack. ABC/-AK/L-* Pop two operands from the stack, i.e., ABC/-, and AK/L- . Add '*' operator after L and '-' operator, i.e., ABC/-AK/L-*.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA