Conversion of Postfix to Prefix expression

What is Postfix expression?

A postfix expression is said to be an expression in which the operator appears after the operands. It can be written as:

(operand) (operand) (operator)

For example:

If the expression is:

(A+B) * (C+D)

Firstly, operator precedence rules will be applied to the above expression. Since the parenthesis has higher precedence than the multiplication operator; therefore '+' will be resolved first, and the + operator will come after AB and CD shown as below:

(AB+) * (CD+)

Now, the multiplication operator will move after CD+ shown as below:

AB+ CD+*

What is Prefix Expression?

A prefix expression is said to be an expression in which the operator appears before the operands.

For example:

If the expression is given as:

(A+B) * (C+D)

Firstly, operator precedence rules will be applied to the above expression. Since the parenthesis has higher precedence than the multiplication operator; therefore, the '+' operator will be resolved first, and the '+' operator will move before the operands AB and CD shown as below:

(+AB) * (+CD)

Now, the multiplication operator will move before the +AB shown as below:

*+AB+CD

Conversion of Postfix to Prefix expression

There are two ways of converting a postfix into a prefix expression:

  1. Conversion of Postfix to Prefix expression manually.
  2. Conversion of Postfix to Prefix expression using stack.

Conversion of Postfix to Prefix expression manually

The following are the steps required to convert postfix into prefix expression:

  • Scan the postfix expression from left to right.
  • Select the first two operands from the expression followed by one operator.
  • Convert it into the prefix format.
  • Substitute the prefix sub expression by one temporary variable
  • Repeat this process until the entire postfix expression is converted into prefix expression.

Let's understand through an example.

a b - c +

First, we scan the expression from left to right. We will move '-' operator before the operand ab.

-abc+

The next operator '+' is moved before the operand -abc is shown as below:

+-abc

Conversion of Postfix to Prefix expression using Stack

The following are the steps used to convert postfix to prefix expression using stack:

  • Scan the postfix expression from left to right.
  • 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.

Create an expression by concatenating two operands and adding operator before the operands.

Push the result back to the stack.

  • Repeat the above steps until we reach the end of the postfix expression.

Pseudocode for the conversion of Postfix to Prefix

Let's understand the conversion of postfix to prefix expression using stack.

If the Postfix expression is given as:

AB + CD - *

Symbol ScannedActionStackDescription
APush A into the stackA
BPush B into the stackAB
+Pop B from the stack
Pop A from the stack
Push +AB into the stack.
+ABPop two operands from the stack, i.e., A and B. Add '+' operator before the operands AB, i.e., +AB.
CPush C into the stack+ABC
DPush D into the stack+ABCD
-Pop D from the stack.
Pop C from the stack.
Push -CD into the stack
+AB -CDPop two operands from the stack, i.e., D and C. Add '-' operator before the operands CD, i.e., -CD.
*Pop -CD from the stack.
Pop +AB from the stack.
Push *+AB -CD into the stack.
*+AB - CDPop two operands from the stack, i.e., -CD and +AB. Add '*' operator before +AB then the expression would become *+AB-CD.

The prefix expression of the above postfix expression is *+AB-CD.

Implementation of Postfix to Prefix conversion in C++

Output

Conversion of Postfix to Prefix expression




Latest Courses