Infixprefixpostfix
Infixprefixpostfix
1) Infix notation
2) Prefix notation
3) Postfix notation
Infix notation: It is most common notation in which, the operator is written or
placed in-between the two operands. For eg. The expression to add two numbers A
and B is written in infix notation as,
A+ B Operands
Operator
In this example, the operator is placed in-between the operands A and B. The
reason why this notation is called infix.
Prefix Notation: It is also called Polish notation, named after in the honor of the
mathematician Jan Lukasiewicz, refers to the notation in which the operator is
placed before the operand as,
+AB
As the operator ‘+’ is placed before the operands A and B, this notation is called
prefix (pre means before).
Postfix Notation: In the postfix notation the operators are written after the
operands, so it is called the postfix notation (post means after), it is also known as
suffix notation or reverse polish notation. The above postfix if written in postfix
notation looks like follows;
AB+
Page 1
Notation Conversions: Let us take an expression A+B*C which is given in
infix notation. To evaluate this expression for values 2, 3, 5 for A, B, C
respectively we must follow certain rule in order to have right result. For eg.
A+B*C = 2+3*5 = 5*5 = 25!!!!!!!!
Is this the right result? No, this is because the multiplication is to be done before
addition, because it has higher precedence over addition. This means that for an
expression to be calculated we must have the knowledge of precedence of
operators.
Operator Precedence:
Operator Symbol Precedence
Exponential $ Highest
Multiplication/Division *,/ Next highest
Addition/Substraction +,- Lowest
POSTFIX (Q, P)
Suppose Q is an arithmetic expression written in infix notation. This
algorithm finds the equivalent postfix expression P.
Step1: Push “(” onto STACK and add ’’)’’ to the end of Q.
Step2: Scan Q from left to right and repeat step 3 to 6 for each element of Q, until
the STACK is empty.
Step3: If an operand is encountered, push it to STACK.
Step4: If a left parenthesis is encountered, push it to STACK.
Step5: If an operator X is encountered, then
a) Repeatedly pop from STACK and add to P each operator (Top of Stack)
which has same precedence as or higher precedence than X.
b) Add X to STACK.
Example:
A-B /(C*D$E).
S.N. Symbol Scan STACK P (Postfix Expression)
(
1. A ( A
2. - (- A
3. B (- AB
4. / (-/ AB
5. ( (-/( AB
6. C (-/( ABC
7. * (-/(* ABC
8. D (-/(* ABCD
9. $ (-/(*$ ABCD
10. E (-/(*$ ABCDE
11. ) (-/ ABCDE$*
12. ) ABCDE$*/-
Page 3
d) Push value on STACK.
5) Return the value at top of the STACK.
6) Exit.
Tracing of algorithm
P= 623+-382/+*2$3+
S.N. Symbol Scan Operand 1 Operand 2 Value STACK
1. 6 6
2. 2 6,2
3. 3 6,2,3
4. + 2 3 5 6,5
5. - 6 5 1 1
6. 3 1,3
7. 8 1,3,8
8. 2 1,3,8,2
9. / 8 2 4 1,3,4
10. + 3 4 7 1,7
11. * 1 7 7 7
12. 2 7,2
13. $ 7 2 49 49
14. 3 49,3
15. + 49 3 52 52
Homework
Q. Convert the following infix expression into postfix expression.
1) (A+B)*C
2) (A+B)*C$E
3) ((A-(B+C))*D)$(E+F)
Q. Evaluate the following postfix expressions
1) AB+C-BA+C$-
2) ABC+*CBA-+*
Where, A=1, B=2, C=3.
Page 4
Q. Evaluate the following prefix expression
a) -/A$BCD
b) *-A/BC-*DEF
where, A=2, B=1, C=4, D=3, E=5, F=1
Page 5