Example of Stacks
INFIX, POSTFIX and PREFIX
– Infix: A+B-C
– Postfix: AB+C-
– Prefix: -+ABC
Order of Precedence of Operators
– Exponentiation
– Multiplication/Division
– Addition/Subtraction
Infix: ( (A+B)*C-(D-E) ) $ (F+G)
Conversion to Postfix Expression
– ( (AB+)*C-(DE-) ) $ (FG+)
– ( (AB+C*)-(DE-) ) $ (FG+)
– (AB+C*DE--) $ (FG+)
– AB+C*DE- -FG+$
Exercise: Convert the following to Postfix
– ( A + B ) * ( C – D)
– A $ B * C – D + E / F / (G + H)
Conversion to Prefix Expression
The precedence rules for converting an expression from infix to
prefix are identical. The only change from postfix is that the
operator is placed before the operands rather than after them.
Evaluating a Postfix Expression
Each operator in a postfix string refers to the previous two
operands in the string.
Algorithm to Evaluate a Postfix
Expression
opndstk = the empty stack
/* scan the input string reading one Example:
element */ Postfix Expression: 6 2 3 + - 3 8 2 / + * 2 $ 3 +
/* at a time into symb */
while (not end of input) { sym opnd opnd valu opndstk
b 1 2 e
symb = next input character;
6 6
if (symb is an operand)
2 6,2
push(opndstk, symb)
3 6,2,3
else {
+ 2 3 5 6,5
/* symb is an operator */
- 6 5 1 1
op2 = pop(opndstk);
3 6 5 1 1,3
op1 = pop(opndstk);
8 6 5 1 1,3,8
value = result of applying
symb to op1 & op2 2 6 5 1 1,3,8,2
push(opndstk, value); / 8 2 4 1,3,4
} /* end else */ + 3 4 7 1,7
} /* end while */ * 1 7 7 7
return (pop(opndstk)); 2 1 7 7 7,2
$ 7 2 49 49
3 7 2 49 49,3
symb opnd1 opnd2 value opndstk
6 6
Example:
2 6,2
Postfix Expression: 6 2 3 + - 3 8 2 / + * 2 $ 3 +
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
Conversion of Infix Expression to
postfix
A+B*C = ABC*+
(A+B)*C = AB+C*
There must be a precedence function.
prcd(op1, op2), where op1 and op2 are characters representing operators.
This function returns TRUE if op1 has precedence over op2 when op1 appears to the
left of op2 in an infix expression without parenthesis. prcd(op1,op2) returns FALSE
otherwise.
For example prcd(‘*’,’+’) and prcd(‘+’,’+’) are TRUE whereas prcd(‘+’,’*’) is FALSE.
prcd(‘$’,’$’) = FALSE
prcd( ‘(‘ , op) = FALSE for any operator op
prcd( op, ‘(‘ ) = FALSE for any operator op other than ‘)’
prcd( op, ‘)‘ ) = TRUE for any operator op other than ‘(‘
prcd( ‘)‘ ,op ) = undefined for any operator op (an error)
Algorithm to Convert Infix to Postfix
opstk = the empty stack;
while (not end of input) {
symb = next input character; Example-1: A+B*C
if (symb is an operand)
add symb to the postfix string symb Postfix string opstk
else {
while (!empty(opstk) && A A
prcd(stacktop(opstk),symb) )
{ + A +
topsymb = pop(opstk); B AB +
add topsymb to the postfix string; * AB +*
} /* end while */
C ABC +*
push(opstk, symb); ABC* +
} /* end else */
} /* end while */ ABC*+
/* output any remaining operators */
while (!empty(opstk) ) {
topsymb = pop(opstk);
add topsymb to the postfix string;
} /* end while */
Algorithm to Convert Infix to Postfix
opstk = the empty stack;
while (not end of input) { Example-2: (A+B)*C
symb = next input character;
if (symb is an operand)
add symb to the postfix string symb Postfix opstk
else { string
while (!empty(opstk) &&
prcd(stacktop(opstk),symb) ) ( (
{
topsymb = pop(opstk); A A (
add topsymb to the postfix string;
} /* end while */ + A (+
B AB (+
push(opstk, symb);
} /* end else */ ) AB+
} /* end while */ * AB+ *
/* output any remaining operators */ C AB+C *
while (!empty(opstk) ) { AB+C*
topsymb = pop(opstk);
add topsymb to the postfix string;
} /* end while */
Algorithm to Convert Infix to Postfix
opstk = the empty stack;
while (not end of input) {
symb = next input character;
Example-2: (A+B)*C
if (symb is an operand)
add symb to the postfix string
else { sym Postfix opstk
while (!empty(opstk) && prcd(stacktop(opstk),symb) ) b string
{
topsymb = pop(opstk); ( (
add topsymb to the postfix string;
} /* end while */ A A (
If (empty(opstk) || symb != ‘)’ ) + A (+
push(opstk, symb);
else B AB (+
topsymb = pop(opstk);
) AB+
} /* end else */
* AB+ *
} /* end while */
/* output any remaining operators */ C AB+C *
while (!empty(opstk) ) {
topsymb = pop(opstk); AB+C*
add topsymb to the postfix string;
} /* end while */
Algorithm to Convert Infix to Postfix
opstk = the empty stack; Example-3: ( (A-(B+C) ) *D ) $ (E+F)
while (not end of input) {
symb = next input character;
if (symb is an operand)
add symb to the postfix string
prcd(‘$’,’$’) = FALSE
else {
prcd( ‘(‘ , op) = FALSE for any operator op
while (!empty(opstk) &&
prcd(stacktop(opstk),symb) )
{ prcd( op, ‘(‘ ) = FALSE for any op other than ‘)’
topsymb = pop(opstk);
prcd( op, ‘)‘ ) = TRUE for any op other than ‘(‘
add topsymb to the postfix string;
} /* end while */
prcd( ‘)‘ ,op ) = error
If (empty(opstk) || symb != ‘)’ )
push(opstk, symb);
else
topsymb = pop(opstk);
} /* end else */
} /* end while */
/* output any remaining operators */
while (!empty(opstk) ) {
topsymb = pop(opstk);
add topsymb to the postfix string;
} /* end while */
Algorithm to Convert Infix to Postfix
opstk = the empty stack; Example-3: ( (A-(B+C) ) *D ) $ (E+F)
while (not end of input) {
symb = next input character; symb Postfix string opstk
if (symb is an operand) ( (
add symb to the postfix string ( ((
else {
A A ((
while (!empty(opstk) && prcd(stacktop(opstk),symb) )
{ - A ((-
topsymb = pop(opstk); ( A ((-(
add topsymb to the postfix string;
B AB ((-(
} /* end while */
+ AB ((-(+
If (empty(opstk) || symb != ‘)’ ) C ABC ((-(+
push(opstk, symb);
) ABC+ ((-
else
topsymb = pop(opstk); ) ABC+- (
* ABC+- (*
} /* end else */
D ABC+-D (*
} /* end while */ ) ABC+-D*
/* output any remaining operators */ $ ABC+-D* $
while (!empty(opstk) ) { ( ABC+-D* $(
topsymb = pop(opstk);
add topsymb to the postfix string; E ABC+-D*E $(
} /* end while */ + ABC+-D*E $(+
F ABC+-D*EF $(+
) ABC+-D*EF+ $
Algorithm to Convert Infix to Postfix
opstk = the empty stack;
while (not end of input) { Example-4: ( A + B ) * ( C – D)
symb = next input character;
if (symb is an operand)
add symb to the postfix string
else { symb Postfix opstk
while (!empty(opstk) && prcd(stacktop(opstk),symb) ) string
{
topsymb = pop(opstk);
add topsymb to the postfix string;
} /* end while */
If (empty(opstk) || symb != ‘)’ )
push(opstk, symb);
else
topsymb = pop(opstk);
} /* end else */
} /* end while */
/* output any remaining operators */
while (!empty(opstk) ) {
topsymb = pop(opstk);
add topsymb to the postfix string;
} /* end while */
Factorial of a number
• Write function to calculate the factorial of a
number.
– Iterative definition
– Recursive