Lecture 05
Lecture 05
1
Prefix, Infix, Postfix
Two other ways of writing the expression
are
+AB prefix
AB+ postfix
4
Prefix, Infix, Postfix
Conversion to postfix
5
Prefix, Infix, Postfix
Conversion to postfix
6
Prefix, Infix, Postfix
Conversion to postfix
7
Prefix, Infix, Postfix
Conversion to postfix
(A + B ) * C infix form
8
Prefix, Infix, Postfix
Conversion to postfix
(A + B ) * C infix form
(AB+)*C convert addition
9
Prefix, Infix, Postfix
Conversion to postfix
(A + B ) * C infix form
(AB+)*C convert addition
(AB+)C* convert multiplication
10
Prefix, Infix, Postfix
Conversion to postfix
(A + B ) * C infix form
(AB+)*C convert addition
(AB+)C* convert multiplication
AB+C* postfix form
11
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 +, -
12
Precedence of Operators
For operators of same precedence, the
left-to-right rule applies:
A B C means A ( B C )
13
Infix to Postfix
Infix Postfix
A+B AB+
12 + 60 – 23 12 60 + 23 –
(A + B)*(C – D ) AB+CD–*
14
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*
15
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.
16
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.
17
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.
18
Converting Infix to Postfix
prcd(‘*’,’+’) is TRUE
prcd(‘+’,’+’) is TRUE
prcd(‘+’,’*’) is FALSE
19
Converting Infix to Postfix
Example: A + B * C
symb postfix stack
A A
20
Converting Infix to Postfix
Example: A + B * C
symb postfix stack
A A
+ A +
21
Converting Infix to Postfix
Example: A + B * C
symb postfix stack
A A
+ A +
B AB +
22
Converting Infix to Postfix
Example: A + B * C
symb postfix stack
A A
+ A +
B AB +
* AB +*
23
Converting Infix to Postfix
Example: A + B * C
symb postfix stack
A A
+ A +
B AB +
* AB +*
C ABC +*
24
Converting Infix to Postfix
Example: A + B * C
symb postfix stack
A A
+ A +
B AB +
* AB +*
C ABC +*
ABC * +
25
Converting Infix to Postfix
Example: A + B * C
symb postfix stack
A A
+ A +
B AB +
* AB +*
C ABC +*
ABC * +
ABC * +
26
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.
27
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.
if( s.empty() || symb != ‘)’ )
s.push( c );
else
s.pop(); // discard the ‘(‘
28
Converting Infix to Postfix
32
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();
33
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input op1 op2 value stack
6 6
34
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input op1 op2 value stack
6 6
2 6,2
35
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
36
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
37
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
38
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
39
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
40
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
41
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
/ 8 2 4 1,3,4
42
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
/ 8 2 4 1,3,4
+ 3 4 7 1,7
43
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
/ 8 2 4 1,3,4
+ 3 4 7 1,7
* 1 7 7 7
44
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
/ 8 2 4 1,3,4
+ 3 4 7 1,7
* 1 7 7 7
2 1 7 7 7,2
45
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
/ 8 2 4 1,3,4
+ 3 4 7 1,7
* 1 7 7 7
2 1 7 7 7,2
7 2 49 49
46
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
/ 8 2 4 1,3,4
+ 3 4 7 1,7
* 1 7 7 7
2 1 7 7 7,2
7 2 49 49
3 7 2 49 49,3
47
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
/ 8 2 4 1,3,4
+ 3 4 7 1,7
* 1 7 7 7
2 1 7 7 7,2
7 2 49 49
3 7 2 49 49,3
+ 49 3 52 52
48
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
/ 8 2 4 1,3,4
+ 3 4 7 1,7
* 1 7 7 7
2 1 7 7 7,2
7 2 49 49
3 7 2 49 49,3
+ 49 3 52 52
49