Open In App

Online Postfix to Prefix Converter

Last Updated : 19 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Our Postfix to Prefix converter tool helps you convert an expression written in postfix notation (Reverse Polish Notation) to its equivalent prefix notation (Polish Notation). Convert your postfix expressions to prefix notation easily with this free online tool.

How to Use Postfix to Prefix Converter?

Step 1: Enter your postfix expression in the text box below.

Step 2: Click the “Convert” button.

Step 3: The equivalent prefix expression will be displayed below.

What are Postfix and Prefix Notation?

Infix notation is the standard way we write mathematical expressions, with operators placed between operands (e.g., 2 + 3). Postfix notation puts operators after operands (e.g., 2 3 +), while prefix notation places operators before operands (e.g., + 2 3).

Example:

Postfix: 2 3 +
Prefix: + 2 3

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 the 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 until the end of the postfix expression. Now the value in the stack is the desired prefix expression.

Illustration:

Input String(Postfix) Remaining Postfix Expression Stack (Prefix)
AB+c* B+c* A
AB+c* +c* AB
AB+c* c* +AB
AB+c* * +ABc
AB+c* *+ABc

Prefix Notation is in the stack that is *+ABc.

Important Notes:

  • This converter supports basic arithmetic operators like addition (+), subtraction (-), multiplication (*), and division (/).
  • The operands should be separated by spaces.
  • Make sure the expression is well-formed and follows postfix notation rules.

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads