Stacks: (Infix, Postfix and Prefix Expressions)
Stacks: (Infix, Postfix and Prefix Expressions)
A+B*C
A+(B*C) Parentheses for emphasis
A(D)+
ABC*+ Postfix Form
Postfix Examples
2-3*4+5 234*-5+ -5
(2 - 3) * (4 + 5) 23-45+* -9
postfixVect
Infix to postfix conversion
stackVect
infixVect
a+b-c)*d–(e+f)
postfixVect
(
Infix to postfix conversion
stackVect
infixVect
+b-c)*d–(e+f)
postfixVect
a
(
Infix to postfix conversion
stackVect
infixVect
b-c)*d–(e+f)
postfixVect
a
+
(
Infix to postfix conversion
stackVect
infixVect
-c)*d–(e+f)
postfixVect
ab
+
(
Infix to postfix conversion
stackVect
infixVect
c)*d–(e+f)
postfixVect
ab+
-
(
Infix to postfix conversion
stackVect
infixVect
)*d–(e+f)
postfixVect
ab+c
-
(
Infix to postfix conversion
stackVect
infixVect
*d–(e+f)
postfixVect
ab+c-
Infix to postfix conversion
stackVect
infixVect
d–(e+f)
postfixVect
ab+c-
*
Infix to postfix conversion
stackVect
infixVect
–(e+f)
postfixVect
ab+c-d
*
Infix to postfix conversion
stackVect
infixVect
(e+f)
postfixVect
ab+c–d*
-
Infix to postfix conversion
stackVect
infixVect
e+f)
postfixVect
ab+c–d*
(
-
Infix to postfix conversion
stackVect
infixVect
+f)
postfixVect
ab+c–d*e
(
-
Infix to postfix conversion
stackVect
infixVect
f)
postfixVect
+ ab+c–d*e
(
-
Infix to postfix conversion
stackVect
infixVect
)
postfixVect
+ ab+c–d*ef
(
-
Infix to postfix conversion
stackVect
infixVect
postfixVect
ab+c–d*ef+
-
Infix to postfix conversion
stackVect
infixVect
postfixVect
ab+c–d*ef+-
Infix to postfix conversion
stackVect
infixVect
(a+b-c)*d–(e+f)
postfixVect
ab+c–d*ef+-
Suppose we want to convert 2*3/(2-1)+5*3 into Postfix form,
Suppose we want to convert 2*3/(2-1)+5*3 into Postfix form,
Expression Stack Output
2 Empty 2
* * 2
3 * 23
/ / 23*
( /( 23*
2 /( 23*2
- /(- 23*2
1 /(- 23*21
) / 23*21-
+ + 23*21-/
5 + 23*21-/5
* +* 23*21-/53
3 +* 23*21-/53
Empty 23*21-/53*+
So, the Postfix Expression is 23*21-/53*+
Example
• ( 5 + 6) * 9 +10
will be
• 5 6 + 9 * 10 +
Evaluation a postfix expression
• Each operator in a postfix string refers to the previous
two operands in the string.
• Suppose that each time we read an operand we push
it into a stack. When we reach an operator, its
operands will then be top two elements on the stack
• We can then pop these two elements, perform the
indicated operation on them, and push the result on
the stack.
• So that it will be available for use as an operand of the
next operator.
Evaluating Postfix Notation
• Use a stack to evaluate an expression in
postfix notation.
• The postfix expression to be evaluated is
scanned from left to right.
• Variables or constants are pushed onto the
stack.
• When an operator is encountered, the
indicated action is performed using the top
elements of the stack, and the result
replaces the operands on the stack.
Evaluating a postfix expression
• Initialise an empty stack
• While token remain in the input stream
– Read next token
– If token is a number/variable (operand), push it
into the stack
– Else, if token is an operator, pop top two tokens
off the stack, apply the operator, and push the
answer back into the stack
• Pop the answer off the stack.
Example: postfix expressions
(cont.)
Postfix expressions:
Algorithm using stacks (cont.)
Algorithm for evaluating a postfix
expression
WHILE more input items exist
{
If symb is an operand
then push (opndstk,symb)
else //symbol is an operator
{
Opnd1=pop(opndstk);
Opnd2=pop(opndnstk);
Value = result of applying symb to opnd1 & opnd2 //opnd2 op opnd1
Push(opndstk,value);
} //End of else
} // end while
Result = pop (opndstk);
Question : Evaluate the following
expression in postfix :
623+-382/+*2^3+
Final answer is
• 49
• 51
• 52
• 7
• None of these
Evaluate 623+-382/+*2^3+
reverse
(C – B) * (B + A)
Infix to postfix
algorithm
C B - B A + *
reverse
Evaluation of Polish Expressions
47
– 3 – 8 3 2 – ~ 4 – 6 2
6–2 -4 -4 – 4 32
3
6 4 -4 2
2 4 4 -8
8 3
6 2 6
-8 -8 -8 Result = 14
48