Infix notation is a type of notation in which arithmetic expressions are written in a manner such that the operators appear in between the operands.
Prefix notation is a type of notation in which arithmetic expressions are written in a manner such that the operands appear after operators. Let's see how to convert Prefix to Infix.
Flowchart for Prefix to Infix Conversion:
Algorithm for Prefix to Infix Conversion:
Step 1: Start
Step 2: Read the Prefix expression from right to left.
Step 3: If the scanned character is an operand, then push it onto the Stack.
Step 4: If the scanned character is an operator, pop two operands from the
stack and concatenate them in order of 'operand1,operator, operand2'.
Push the result into the stack.
Step 5: Repeat steps 2-4 until the prefix expression ends.
Step 6: Pop all the rest elements of the stack if any.
Step 7: Stop
Let's take an example to understand *a+bc,
- Reading from right to left, we scan the operands 'c' 'b' respectively and push it into the the stack.
- '+' is scanned and operands 'c', 'b' are popped and concatenated in form of b+c
- 'a' is then pushed into the stack.
- '*', is encountered and 'a' and string 'b+c' is popped and concatenated to give a string a*(b+c)
- Postfix form is obtained: a*(b+c)
You might be interested in this too.:
Our Quiz prepared by Experts Helps you identify your knowledge in Algorithms. Everyone should atleast attempt this Quiz Once.