You are on page 1of 24

Infix, Prefix and Postfix notations

Yawar Abbas Abid


yawar.abid@cuisahiwal.edu.pk
Operator and Operands
Consider the following expression

(A+B )^C-Z/G (A, B, C, G, Z are operands)

+,/,-,+,^ (operators)
Infix Notation
The operator symbol is placed between the two operands

Example:
A+B C-D (A+B)*C A+(B*C)

Disadvantage:
We must use
a. parenthesis
b. operator precedence rules
to evaluate the expression
Prefix Notation
In Prefix notation the operator is placed before the two
operands

Examples:

A+B +AB
C-D -CD
E*F *EF
G/H /GH
Prefix Example…
(a) (A+B)*C (b) A+(B*C)

A+
(+AB)*
(*BC)
C

*+ABC +A*BC
Prefix Notation Example…
(c) (A+B)/(C-D) Class Exercise
(d) 5 * ( 6 + 2 ) – 3 / 4

(+AB)/(-
CD) 5*(+62)-/34

/+AB-CD *5+62 - /34

-*5+62/34
Postfix Notation
In Postfix notation the operator is placed after the two
operands

Examples:

A+B AB+
C-D CD-
E*F EF*
G/H GH/
Postfix Example…
(a) (A+B)*C (b) A+(B*C)

A+
(AB+)*
(BC*)
C

AB+C* ABC*+
MoreExamples
(e) (A+B^D)/(E-F)+G

(A+BD^)/(EF-)
+G

ABD^+ / (EF-)+G

ABD^+ (EF-)/+G ABD^+EF-/G+


Algorithm
Q=infix expression 5. If an operator is encountered then
a) Repeatedly pop from stack and
P=postfix expression add to P each operator (on the
Polish(Q,P) stack) which has the same
precedence or higher than the
1. Push “(” on stack and “)” at operator encountered
the end of the expression Q. b) Add the encountered operator to
2. Scan Q from left to right and the stack
repeat steps 3 to 6 for each c) [end of if structure]
element of the Q until the
stack is empty 6. If a right parenthesis “)” is
encountered , then
3. If an operand is encountered a. Repeatedly pop from Stack and
add it to P add to each operator (on the top
of stack) until a left parenthesis
4. If a left parenthesis “(”
“(“ is enountered
encountered push it on the b. Remove the left parenthesis
stack [end of if structure]
[end of step 2 loop]
exit
Example Q=A+(B*C-(D/E^F)*G)*H
Symbol Stack Expression (P)
1. A 1. ( 1. A

2. + 2. (+ 2. A

3. ( 3. (+( 3. A

4. B 4. (+( 4. AB

5. * 5. (+(* 5. AB

6. C 6. (+(* 6. ABC

7. - 7. (+(- 7. ABC*

8. ( 8. (+(-( 8. ABC*

9. D 9. (+(-( 9. ABC*D

10. / 10. (+(-(/ 10. ABC*D

11. E 11. (+(-(/ 11. ABC*DE

12. ^ 12. (+(-(/^ 12. ABC*DE


Example (A+(B*C-(D/E^F)*G)*H
Symbo Stack Expression
l 13. (+(-(/^ 13. ABC*DEF
13. F 14. (+(- 14. ABC*DEF^/
14. ) 15. (+(-* 15. ABC*DEF^/
15. * 16. (+(-* 16. ABC*DEF^/G
16. G 17. (+ 17. ABC*DEF^/G*-
17. ) 18. (+* 18. ABC*DEF^/G*-
18. * 19. (+* 19. ABC*DEF^/G*-H
19. H 20. null 20. ABC*DEF^/G*-H*+
20. )
Infix to postfix conversion
Infix String: 1 + 2 * 3 - 4
Postfix String: 1 2 3 * + 4 -

S
T
A
C
K

Expression
Infix to postfix conversion
Postfix String: 1 2 3 * + 4 -
Infix to postfix conversion
Postfix String: 1 2 3 * + 4 -

1
Infix to postfix conversion
Postfix String: 1 2 3 * + 4 -

2
1
Infix to postfix conversion
Postfix String: 1 2 3 * + 4 -

3
2
1
Infix to postfix conversion
Postfix String: 1 2 3 * + 4 -

2*3=6
Infix to postfix conversion
Postfix String: 1 2 3 * + 4 -

6
1
Infix to postfix conversion
Postfix String: 1 2 3 * + 4 -

1+6=7
Infix to postfix conversion
Postfix String: 1 2 3 * + 4 -

7
Infix to postfix conversion
Postfix String: 1 2 3 * + 4 -

4
7
Infix to postfix conversion
Postfix String: 1 2 3 * + 4 -

7-4=3
Infix to postfix conversion
Postfix String: 1 2 3 * + 4 -

You might also like