Data Structure
Data Structure
STACK
(Part 2)
Infix, Prefix & Postfix
Arithmetic Expressions
Arithmetic expressions have:
Operators
Binary : +, -, *, / ,%
Unary: -
Priority convention:
*, /, % have medium priority
+, - have lowest priority
Infix, Prefix, Postfix
• Example: arithmetic expression a + b consists of operands
a, b and operator +.
• Infix notation
• Is format where operator is specified in between the two
operands. a+b
• Prefix notation
• Is format where operator is specified before the two
operands. +ab
• Postfix notation
• Is format where operator is specified after the two operands.
Postfix notation is also called RPN or Reverse Polish
Notation. ab+
Why use Prefix and Postfix?
• To avoid ambiguous
• Infix notation requires precedence and associativity rules to
disambiguate it, or addition of extra parentheses that are
not usually considered part of the notation.
• Therefore, as long as the number of arguments to each
operator are known in advance, both prefix and postfix
notation are entirely unambiguous
• Example :
"* + 5 6 3" is (5+6)*3, and cannot be interpreted as 5+(6*3),
whereas parenthesis is required to achieve with infix.
Converting arithmetic expressions
Example: Conversion from infix arithmetic expression to prefix and postfix.
• R2: For each operator in the infix string, loop until the operator has
been pushed onto the operatorStack object:
Þ (A B + ) - C
ÞAB+C-
Infix to Postfix Conversion (Method 2)
• Example: Infix notation A + B * C
Þ A + (B C *)
ÞABC*+
Infix to Postfix Conversion (Method 2)
• Example: Infix notation a + c – r / b * r
Þ a + c – (r b /) * r
Þ a + c – (r b / r *)
Þ (a c +) – (r b / r * )
Þac+rb/r*-
Exercise
• Translate the following expressions into postfix notation
• a–b+c*d
• a+c–h/b*r
• (x + y) * z
• a + (c – h) / (b * r)
• x – (y * ( z + d * e) + c) / f
Application of Stack 2: Infix to Prefix Conversion
c c *
- ab
* - abc
Infix to Prefix Conversion (Method 1)
• Example 3:
Convert infix notation to prefix notation
a + (c – h) / (b * d)
Solution
Infix operandStack operatorStack
a a
+ a +
( a (
+
c c (
a +
- c -
a (
+
h h -
c (
a +
Solution
Infix operandStack operatorStack
) - ch (
a +
/ - ch /
a +
( - ch (
a /
+
b b (
-ch /
a +
Solution
Infix operandStack operatorStack
* b *
- ch (
a /
+
d d *
b (
- ch /
a +
) *bd (
-ch /
a +
Solution
Infix operandStack operatorStack
/-ch*bd +
a
+ a / - ch *bd
Infix to Prefix Conversion (Method 2)
Convert infix notation to prefix notation
(a – b) * c
Þ(- a b) * c
Þ* - a b c
Infix to Prefix Conversion (Method 2)
Convert infix notation to prefix notation
a + (c – h) / (b * d)
Þ a + (- c h) / ( b * d)
Þ a + (- c h) / ( * b d)
Þ a + (/ - c h * b d)
Þ+a/-ch*bd
Exercise
• Translate the following expressions into prefix notation
• a–b+c*d
• (k + l)/(m-n)
• a – (b + c * d) / e
• a + (c – h) / (b * d)
Evaluation of an expression
• An expression in postfix notation can be evaluated at run-
time by means of a stack
• For example, we might have the following postfix
expression that consists of integer values and binary
operators only:
• 854+*7–
• The final result, 65, lies on the top of the stack at the end of the
calculation
Solution
Postfix Stack Expression
854+*7– 4
5
8
854+*7– 8 5+4
9
8
854+*7– 8*9
72
854+*7– 7
72
854+*7– 72-7
65
Cont…
• The evaluation proceeds as follows:
1. When a value is encountered, it is pushed onto the stack
2. When the operator is encountered, first and second elements
on the stack are retrieved and popped, the operator is applied
• Note that the second element is the left operand, the first element
is the right operand.
3. The result is pushed onto the stack
4. When the postfix expression has been processed, the value of
that expression is the top (and only) element on the stack
EVALUATING EXPRESSION USING STACK(method 2)
EVALUATING EXPRESSION USING STACK (method 2)
Method 2
• Evaluate the following expression using stack
854+*7–
8 54+ * 7–
At Top
stack
4
Second
lowest
5 5 9 7
stack
At the
Lowest
8 8 8 8 72 72 65
stack
8 5 4 + * 7 -
EVALUATING EXPRESSION USING STACK
(Method 3)
POSTFIX: 8 6 * 8 4 + / 5 +
POSTFIX: 8 6 * 8 4 + / 5 +
1) Push 8 3) Push 12
Push 6 Pop 12
Pop 8 evaluate 8 * 6 = 48
4) Push 4
2) Push 48 Push 5
Push 8 Pop 5
Pop 4
Pop 8 evaluate 8 + 4 = 12 5) Push 9
Pop 9
Null
The result is 9
Exercise 1:
12 6 * 8 4 / / 7 5 - *
ANSWER
Push 12 Push 36
Push 6 Push 7
Pop 6 Push 5
Pop 12 Pop 5
Evaluate 12 * 6 = 72 Pop 7
Evaluate 7 – 5 = 2
Push 72
Push 8 Push 2
Push 4 Pop 2
Pop 4 Pop 36
Pop 8 Evaluate 36 * 2 = 72
Evaluate 8 / 4 = 2
Push 72
Push 2 Pop 72
Pop 2
Pop 72 The answer is 72
Evaluate 72 / 2 = 36
EXERCISE
Exercise
• Use stack to evaluate the expression:
• 35 27 + 3 *
• 23 30 + 15 * /
• Convert the following expression into postfix notation and
then use stack to evaluate the expression:
• 5 + 2 * (30 – 10 / 5)