0% found this document useful (0 votes)
230 views

Algorithm of Infix To Prefix

The document describes the algorithm for converting an infix notation mathematical expression to prefix notation. It involves the following steps: 1) Reverse the infix expression and change parentheses. 2) Convert to postfix notation using a stack. 3) Reverse the postfix notation to get the prefix form. An example is provided that takes the infix expression (A+B^C)*D+E^5 through each step to produce the prefix expression +*+A^BCD^E5.

Uploaded by

madhuri nimse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
230 views

Algorithm of Infix To Prefix

The document describes the algorithm for converting an infix notation mathematical expression to prefix notation. It involves the following steps: 1) Reverse the infix expression and change parentheses. 2) Convert to postfix notation using a stack. 3) Reverse the postfix notation to get the prefix form. An example is provided that takes the infix expression (A+B^C)*D+E^5 through each step to produce the prefix expression +*+A^BCD^E5.

Uploaded by

madhuri nimse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Infix to Prefix Conversion

Algorithm of Infix to Prefix

1. Step 1. Push “)” onto STACK, and add “(“ to end of the A
2. Step 2. Scan A from right to left and repeat step 3 to 6 for each
element of A until the STACK is empty
3. Step 3. If an operand is encountered add it to B
4. Step 4. If a right parenthesis is encountered push it onto STACK
5. Step 5. If an operator is encountered then:
6. a. Repeatedly pop from STACK and add to B each operator (on the top
of STACK) which has same
7. or higher precedence than the operator.
8. b. Add operator to STACK
9. Step 6. If left parenthesis is encontered then
10. a. Repeatedly pop from the STACK and add to B (each operator
on top of stack until a left parenthesis is encounterd)
11. b. Remove the left parenthesis
12. Step 7. Exit

 Prefix Infix Postfix converter Tool Online


Infix to prefix conversion

Expression = (A+B^C)*D+E^5
Step 1. Reverse the infix expression.
               5^E+D*)C^B+A(
Step 2. Make Every '(' as ')' and every ')' as '('
                5^E+D*(C^B+A)
Step 3. Convert expression to postfix form.

A+(B*C-(D/E-F)*G)*H
Expression Stack Output Comment
5^E+D*(C^B+A) Empty - Initial
^E+D*(C^B+A) Empty 5 Print
E+D*(C^B+A) ^ 5 Push
+D*(C^B+A) ^ 5E Push
D*(C^B+A) + 5E^ Pop And Push
*(C^B+A) + 5E^D Print
(C^B+A) +* 5E^D Push
C^B+A) +*( 5E^D Push
^B+A) +*( 5E^DC Print
B+A) +*(^ 5E^DC Push
+A) +*(^ 5E^DCB Print
A) +*(+ 5E^DCB^ Pop And Push
) +*(+ 5E^DCB^A Print
End +* 5E^DCB^A+ Pop Until '('
End Empty 5E^DCB^A+*+ Pop Every element

Step 4. Reverse the expression.


                +*+A^BCD^E5

Result

+*+A^BCD^E5

You might also like