Lecture No.
Postfix/Infix/Prefix
lecture delivered
by
Zulfiqar Ali
Use of Stack
Example of use: prefix, infix, postfix
expressions.
Consider the expression A+B: we think of
applying the operator + to the operands
A and B.
+ is termed a binary operator: it takes
two operands.
Writing the sum as A+B is called the infix
form of the expression.
11/10/2014
CIIT Sahiwal Campus
Prefix, Infix, Postfix
Two other ways of writing the expression
are
+AB
AB+
prefix
postfix
The prefixes pre and post refer to the
position of the operator with respect to the
two operands.
11/10/2014
CIIT Sahiwal Campus
Prefix, Infix, Postfix
Consider the infix expression
A+B*C
We know that multiplication is done
before addition.
The expression is interpreted as
A+(B*C)
Multiplication has precedence over
addition.
11/10/2014
CIIT Sahiwal Campus
Prefix, Infix, Postfix
Conversion to postfix
A+(B*C)
11/10/2014
infix form
CIIT Sahiwal Campus
Prefix, Infix, Postfix
Conversion to postfix
A+(B*C)
A+(BC*)
11/10/2014
infix form
convert multiplication
CIIT Sahiwal Campus
Prefix, Infix, Postfix
Conversion to postfix
A+(B*C)
A+(BC*)
A(BC*)+
11/10/2014
infix form
convert multiplication
convert addition
CIIT Sahiwal Campus
Prefix, Infix, Postfix
Conversion to postfix
A+(B*C)
A+(BC*)
A(BC*)+
ABC*+
11/10/2014
infix form
convert multiplication
convert addition
postfix form
CIIT Sahiwal Campus
Prefix, Infix, Postfix
Conversion to postfix
(A + B ) * C
11/10/2014
infix form
CIIT Sahiwal Campus
Prefix, Infix, Postfix
Conversion to postfix
(A + B ) * C
(AB+)*C
11/10/2014
infix form
convert addition
CIIT Sahiwal Campus
10
Prefix, Infix, Postfix
Conversion to postfix
(A + B ) * C
(AB+)*C
(AB+)C*
11/10/2014
infix form
convert addition
convert multiplication
CIIT Sahiwal Campus
11
Prefix, Infix, Postfix
Conversion to postfix
(A + B ) * C
(AB+)*C
(AB+)C*
AB+C*
11/10/2014
infix form
convert addition
convert multiplication
postfix form
CIIT Sahiwal Campus
12
Precedence of Operators
The five binary operators are: addition,
subtraction, multiplication, division and
exponentiation.
The order of precedence is (highest to
lowest)
Exponentiation
Multiplication/division *, /
Addition/subtraction
+, 11/10/2014
CIIT Sahiwal Campus
13
Precedence of Operators
For operators of same precedence, the
left-to-right rule applies:
A+B+C means (A+B)+C.
For exponentiation, the right-to-left rule
applies
A B C means A ( B C )
11/10/2014
CIIT Sahiwal Campus
14
Infix to Postfix
Infix
A+B
12 + 60 23
(A + B)*(C D )
A B * C D + E/F
11/10/2014
Postfix
AB+
12 60 + 23
AB+CD*
A B C*D E F/+
CIIT Sahiwal Campus
15
Infix to Postfix
Infix
A+B
12 + 60 23
(A + B)*(C D )
A B * C D + E/F
11/10/2014
Postfix
AB+
12 60 + 23
AB+CD*
A B C*D E F/+
CIIT Sahiwal Campus
16
Infix to Postfix
Note that the postfix form an expression
does not require parenthesis.
Consider 4+3*5 and (4+3)*5. The
parenthesis are not needed in the first but
they are necessary in the second.
The postfix forms are:
4+3*5
435*+
(4+3)*5
43+5*
11/10/2014
CIIT Sahiwal Campus
17
Evaluating Postfix
Each operator in a postfix expression
refers to the previous two operands.
Each time we read an operand, we push it
on a stack.
When we reach an operator, we pop the
two operands from the top of the stack,
apply the operator and push the result
back on the stack.
11/10/2014
CIIT Sahiwal Campus
18
Evaluating Postfix
Stack s;
while( not end of input ) {
e = get next element of input
if( e is an operand )
s.push( e );
else {
op2 = s.pop();
op1 = s.pop();
value = result of applying operator e to op1 and op2;
s.push( value );
}
}
finalresult = s.pop();
11/10/2014
CIIT Sahiwal Campus
19
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input
6
11/10/2014
op1
op2
value
CIIT Sahiwal Campus
stack
6
20
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input
6
2
11/10/2014
op1
op2
value
CIIT Sahiwal Campus
stack
6
6,2
21
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input
6
2
3
11/10/2014
op1
op2
value
CIIT Sahiwal Campus
stack
6
6,2
6,2,3
22
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input
6
2
3
+
11/10/2014
op1
op2
value
CIIT Sahiwal Campus
stack
6
6,2
6,2,3
6,5
23
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input
6
2
3
+
-
11/10/2014
op1
op2
value
2
6
3
5
5
1
CIIT Sahiwal Campus
stack
6
6,2
6,2,3
6,5
1
24
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input
6
2
3
+
3
11/10/2014
op1
op2
value
2
6
6
3
5
5
5
1
1
CIIT Sahiwal Campus
stack
6
6,2
6,2,3
6,5
1
1,3
25
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input
6
2
3
+
3
8
11/10/2014
op1
op2
value
2
6
6
6
3
5
5
5
5
1
1
1
CIIT Sahiwal Campus
stack
6
6,2
6,2,3
6,5
1
1,3
1,3,8
26
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input
6
2
3
+
3
8
2
11/10/2014
op1
op2
value
2
6
6
6
6
3
5
5
5
5
5
1
1
1
1
CIIT Sahiwal Campus
stack
6
6,2
6,2,3
6,5
1
1,3
1,3,8
1,3,8,2
27
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input
6
2
3
+
3
8
2
/
11/10/2014
op1
op2
value
2
6
6
6
6
8
3
5
5
5
5
2
5
1
1
1
1
4
CIIT Sahiwal Campus
stack
6
6,2
6,2,3
6,5
1
1,3
1,3,8
1,3,8,2
1,3,4
28
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input
6
2
3
+
3
8
2
/
+
11/10/2014
op1
op2
value
2
6
6
6
6
8
3
3
5
5
5
5
2
4
5
1
1
1
1
4
7
CIIT Sahiwal Campus
stack
6
6,2
6,2,3
6,5
1
1,3
1,3,8
1,3,8,2
1,3,4
1,7
29
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input
6
2
3
+
3
8
2
/
+
*
11/10/2014
op1
op2
value
2
6
6
6
6
8
3
1
3
5
5
5
5
2
4
7
5
1
1
1
1
4
7
7
CIIT Sahiwal Campus
stack
6
6,2
6,2,3
6,5
1
1,3
1,3,8
1,3,8,2
1,3,4
1,7
7
30
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input
6
2
3
+
3
8
2
/
+
*
2
11/10/2014
op1
op2
value
2
6
6
6
6
8
3
1
1
3
5
5
5
5
2
4
7
7
5
1
1
1
1
4
7
7
7
CIIT Sahiwal Campus
stack
6
6,2
6,2,3
6,5
1
1,3
1,3,8
1,3,8,2
1,3,4
1,7
7
7,2
31
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input
6
2
3
+
3
8
2
/
+
*
2
11/10/2014
op1
op2
value
2
6
6
6
6
8
3
1
1
7
3
5
5
5
5
2
4
7
7
2
5
1
1
1
1
4
7
7
7
49
CIIT Sahiwal Campus
stack
6
6,2
6,2,3
6,5
1
1,3
1,3,8
1,3,8,2
1,3,4
1,7
7
7,2
49
32
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input
6
2
3
+
3
8
2
/
+
*
2
3
11/10/2014
op1
op2
value
2
6
6
6
6
8
3
1
1
7
7
3
5
5
5
5
2
4
7
7
2
2
5
1
1
1
1
4
7
7
7
49
49
CIIT Sahiwal Campus
stack
6
6,2
6,2,3
6,5
1
1,3
1,3,8
1,3,8,2
1,3,4
1,7
7
7,2
49
49,3
33
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input
6
2
3
+
3
8
2
/
+
*
2
3
+
11/10/2014
op1
op2
value
2
6
6
6
6
8
3
1
1
7
7
49
3
5
5
5
5
2
4
7
7
2
2
3
5
1
1
1
1
4
7
7
7
49
49
52
CIIT Sahiwal Campus
stack
6
6,2
6,2,3
6,5
1
1,3
1,3,8
1,3,8,2
1,3,4
1,7
7
7,2
49
49,3
52
34
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input
6
2
3
+
3
8
2
/
+
*
2
3
+
11/10/2014
op1
op2
value
2
6
6
6
6
8
3
1
1
7
7
49
3
5
5
5
5
2
4
7
7
2
2
3
5
1
1
1
1
4
7
7
7
49
49
52
CIIT Sahiwal Campus
stack
6
6,2
6,2,3
6,5
1
1,3
1,3,8
1,3,8,2
1,3,4
1,7
7
7,2
49
49,3
52
35
Converting Infix to Postfix
Consider the infix expressions A+B*C
and (A+B)*C.
The postfix versions are ABC*+ and
AB+C*.
The order of operands in postfix is the
same as the infix.
In scanning from left to right, the operand
A can be inserted into postfix expression.
11/10/2014
CIIT Sahiwal Campus
36
Converting Infix to Postfix
The + cannot be inserted until its second
operand has been scanned and inserted.
The + has to be stored away until its
proper position is found.
When B is seen, it is immediately inserted
into the postfix expression.
Can the + be inserted now? In the case
of A+B*C cannot because * has
precedence.
11/10/2014
CIIT Sahiwal Campus
37
Converting Infix to Postfix
In case of (A+B)*C, the closing
parenthesis indicates that + must be
performed first.
Assume the existence of a function
prcd(op1,op2) where op1 and op2 are
two operators.
Prcd(op1,op2) returns TRUE if op1 has
precedence over op2, FASLE otherwise.
11/10/2014
CIIT Sahiwal Campus
38
Converting Infix to Postfix
prcd(*,+) is TRUE
prcd(+,+) is TRUE
prcd(+,*) is FALSE
Here is the algorithm that converts infix
expression to its postfix form.
The infix expression is without
parenthesis.
11/10/2014
CIIT Sahiwal Campus
39
Converting Infix to Postfix
1.
Stack s;
2.
While( not end of input ) {
3.
c = next input character;
4.
if( c is an operand )
5.
add c to postfix string;
6.
else {
7.
while( !s.empty() && prcd(s.top(),c) ){
8.
op = s.pop();
9.
add op to the postfix string;
10.
}
11.
s.push( c );
12.
}
13.
while( !s.empty() ) {
14.
op = s.pop();
15.
add op to postfix string;
16.
}
11/10/2014
CIIT Sahiwal Campus
40
Converting Infix to Postfix
Example: A + B * C
symb
postfix
A
A
11/10/2014
stack
CIIT Sahiwal Campus
41
Converting Infix to Postfix
Example: A + B * C
symb
postfix
A
A
+
A
11/10/2014
stack
+
CIIT Sahiwal Campus
42
Converting Infix to Postfix
Example: A + B * C
symb
postfix
A
A
+
A
B
AB
11/10/2014
stack
+
+
CIIT Sahiwal Campus
43
Converting Infix to Postfix
Example: A + B * C
symb
postfix
A
A
+
A
B
AB
*
AB
11/10/2014
stack
+
+
+*
CIIT Sahiwal Campus
44
Converting Infix to Postfix
Example: A + B * C
symb
postfix
A
A
+
A
B
AB
*
AB
C
ABC
11/10/2014
stack
+
+
+*
+*
CIIT Sahiwal Campus
45
Converting Infix to Postfix
Example: A + B * C
symb
postfix
A
A
+
A
B
AB
*
AB
C
ABC
ABC *
11/10/2014
stack
+
+
+*
+*
+
CIIT Sahiwal Campus
46
Converting Infix to Postfix
Example: A + B * C
symb
postfix
A
A
+
A
B
AB
*
AB
C
ABC
ABC *
ABC * +
11/10/2014
stack
+
+
+*
+*
+
CIIT Sahiwal Campus
47
Converting Infix to Postfix
Handling parenthesis
When an open parenthesis ( is read, it
must be pushed on the stack.
This can be done by setting prcd(op,( ) to
be FALSE.
Also, prcd( (,op ) == FALSE which
ensures that an operator after ( is pushed
on the stack.
11/10/2014
CIIT Sahiwal Campus
48
Converting Infix to Postfix
When a ) is read, all operators up to the
first ( must be popped and placed in the
postfix string.
To do this, prcd( op,) ) == TRUE.
Both the ( and the ) must be discarded:
prcd( (,) ) == FALSE.
Need to change line 11 of the algorithm.
11/10/2014
CIIT Sahiwal Campus
49
Converting Infix to Postfix
if( s.empty() || symb != ) )
s.push( c );
else
s.pop(); // discard the (
prcd( (, op ) = FALSE for any operator
prcd( op, ( ) = FALSE for any operator
other than (
prcd( op, ) ) = TRUE for any operator
other than (
prcd( ), op ) = error
for any operator.
11/10/2014
CIIT Sahiwal Campus
50