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.
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.
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.
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:
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, /, -
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:
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