The Infix, Prefix, Postfix Notation:: Data Structure and Algorithms
The Infix, Prefix, Postfix Notation:: Data Structure and Algorithms
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+
By: Surya Bam Page 1
Data Structure and Algorithms
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.
[End of if]
[End of step 2 loop].
Step7: Exit.
Note: In the Infix to postfix conversion expression algorithm x means any
mathematical operator such as +,-,*,/,$.
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$*/-
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
d) if ‘)’ found pop and push to prestack until the matching ‘)’ is found and
ignore (cancel) both.
Step 3: pop and push to prestack until stack is empty.
Step 4: pop and display from prestack until stack is empty.
Example;
(A-(B/C))*((D*E)-F)
Tracing
+-*+12/421$42
S.N. Scan Symbol Operand 1 Operand 2 Value Prestack
1. 2 2
2. 4 2,4
3. $ 4 2 16 16
4. 1 16,1
5. 2 16,1,2
6. 4 16,1,2,4
7. / 4 2 2 16,1,2
8. 2 16,1,2,2
9. 1 16,1,2,2,1
10. + 1 2 3 16,1,2,3
11. * 3 2 6 16,1,6
12. - 6 1 5 16,5
13. + 5 16 21 21
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.
Q. Convert the following infix expression into prefix expression.
1) A/B$C-D
2) ((A+B)*C/D-E)+F$G
3) (A*B+(C/D))-F