Postfix to Prefix vs Prefix to Postfix

We have two converters. The first converter converts postfix to prefix expression. And the second one converts prefix to postfix expression.

You will get step by step conversion for your postfix to prefix or prefix to postfix.

1. Postfix to Prefix step by step

The following converter converts an postfix expression to a prefix expression.

Change the expression and converter will convert postfix to prefix step by step.

Postfix:

Prefix:

How to convert Postfix to Prefix?

  • Scan the given postfix expression from left to right character by character.

  • If the character is an operand, push it into the stack.

  • But if the character is an operator, pop the top two values from stack.

    Concatenate this operator with these two values (operator+2nd top value+1st top value) to get a new string.

  • Now push this resulting string back into the stack.

  • Repeat this process untill the end of postfix expression. Now the value in the stack is the desired prefix expression.

Input StringPostfix ExpressionStack (Prefix)

2. Prefix to Postfix step by step

The following converter converts an prefix expression to postfix expression.

Change the expression and converter will convert prefix to postfix step by step.

Prefix:

Postfix:

How to convert Prefix to Postfix?

  • Scan the given prefix expression from right to left character by character.

  • If the character is an operand, push it into the stack.

  • But if the character is an operator, pop the top two values from stack.

    Concatenate this operator with these two values (operator+1st top value+2nd top value) to get a new string.

  • Now push this resulting string back into the stack.

  • Repeat this process untill the end of prefix expression. Now the value in the stack is the desired postfix expression.

Input StringPrefix ExpressionStack (Postfix)

Was this article helpful?