0% found this document useful (0 votes)
13 views56 pages

Data Structure

Chapter 4 discusses the concepts of infix, prefix, and postfix notations in arithmetic expressions, detailing their structures and the importance of using prefix and postfix to avoid ambiguity. It explains the conversion methods from infix to postfix and prefix notations using stacks, including rules for handling operators and parentheses. The chapter also covers the evaluation of postfix expressions using stacks, demonstrating the process with examples.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views56 pages

Data Structure

Chapter 4 discusses the concepts of infix, prefix, and postfix notations in arithmetic expressions, detailing their structures and the importance of using prefix and postfix to avoid ambiguity. It explains the conversion methods from infix to postfix and prefix notations using stacks, including rules for handling operators and parentheses. The chapter also covers the evaluation of postfix expressions using stacks, demonstrating the process with examples.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 56

CHAPTER 4:

STACK
(Part 2)
Infix, Prefix & Postfix
Arithmetic Expressions
 Arithmetic expressions have:

 operands (variables or numeric


constants).

 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.

Infix Notation Prefix Notation Postfix Notation


A+B*C +A * B C A BC*+

(A+B) * C *+ABC AB+C*

A–B+C +–ABC AB–C+

A – (B+C) –A+BC A BC+–


Application of Stack 1:Infix to Postfix

• This application converts the infix notation of a given


arithmetic expression into postfix notation.

• In an infix notation the operator is placed in between the


operands : a+b

• In a postfix notation the operator is placed immediately


after the operand: ab+
Infix to Postfix Conversion (Method 1)
• There are 3 rules or algorithm in infix to postfix conversion

• Rules (R) for using Stack to change infix to postfix


notation:
• R1
• R2
• R3
Infix to Postfix Conversion (Method 1)
• R1: Initially, the operatorStack object is empty.

• R2: For each operator in the infix string, loop until the operator has
been pushed onto the operatorStack object:

 if the operatorStack object is empty OR the operator has


higher precedence than the operator on the top of the
operatorStack object then
 Push the operator onto the operatorStack object.
 else
 Pop the operatorStack object and append that popped operator to the
postfix string

 ** NOTE: Operatorstack -> * + -


Infix to Postfix Conversion (Method 1)
• R3: Once the end of the input string is encountered,
• Loop until the operatorStack object is empty
• Pop the operatorStack object and append that popped operator to the
postfix string
Example: Infix to Postfix

• Example 1: Infix notation A - B

STEPS INFIX OPERATOR POSTFIX


STACK
1 A empty A
2 - - A
3 B - AB
4 empty AB-
Example: Infix to Postfix
• Example 2: Infix notation A + B - C

STEPS INFIX OPERATOR POSTFIX


STACK
1 A empty A
2 + + A
3 B + AB
4 - - AB+
5 C - AB+C
6 empty AB+ C -
Example of Infix to Postfix
• Example 3: Infix notation A + B * C

STEPS INFIX OPERATOR POSTFIX


STACK
1 A empty A
2 + + A
3 B + AB
4 * +* AB
5 C +* ABC
6 + ABC *
7 empty ABC *+
Example Infix to Postfix
• Example 4
• Convert this infix notation to postfix notation
a+c–r/b*r
Solution
STEPS INFIX OPERATOR POSTFIX
STACK
1 a empty a
2 + + a
3 c c ac
4 - - ac +
5 r - ac + r
6 / -/ ac + r
7 b -/ ac + rb
8 * -* ac + rb /
9 r -* ac + rb / r
10 - ac + rb / r *
11 empty ac + rb / r * -
Parentheses in Infix notation
• How are parentheses handled when converting an infix
notation to postfix notation?

• When a left parentheses “(“ is found in the infix string, it is


immediately pushed onto the operatorStack object
• But its precedence is defined to be lower than any other binary operator
Parentheses in Infix notation (cont)
• When a right parentheses “)” is found in the infix string, the
operator object is repeatedly popped, and the popped
element appended to the postfix string, until the operator
on the top of the operatorStack object is the left paretheses
“(“

• Then the left parentheses “(“ is popped but not appended


to the postfix string and the scan of the infix string is
continued.
Parentheses in Infix notation (cont)
• Example 5
• Convert this infix notation to postfix notation
x – (y * a / b – (z + d * e) + c) / f
Solution
x – (y * a / b – (z + d * e) + c) / f
Infix operatorStack Postfix
x empty x
- - x
( -( x
y -( xy
* -( * xy
a -( * xya
/ -( / xya *
b -( / xya * b
- -( - xya * b/
( -( -( xya * b/
z -( -( xya * b/z
Solution (cont..)
x – (y * a / b – (z + d * e) + c) / f
INFIX OPERATORSTACK POSTFIX
+ -( -( + xya * b/z
d -( - ( + xya * b/zd
* -( - ( + * xya * b/zd
e -( - ( + * xya * b/zde
) -( - xya * b/zde *+
+ -( + xya * b/zde *+-
c -( + xya * b/zde *+ -c
) - xya * b/zde *+ -c +
/ -/ xya * b/zde *+ -c +
f -/ xya * b/zde *+ -c + f
- xya * b/zde *+- c + f /
Solution (cont..)
INFIX OPERATORSTACK POSTFIX
- xya * b/zde *+- c + f /
empty xya * b/zde *+-c+ f /-
Infix to Postfix Conversion (Method 2)
• Example: Infix notation A + B – C

Þ (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

• It is the reverse process of converting an infix to postfix


notation

• The saving of operand and operators is easily done with


the help of 2 Stacks:
• operandStack
• operatorStack

• The precedence rules for the operatorStack object are


exactly the same as converting infix to postfix
Infix to Prefix Conversion (Method 1)
• Algorithm (infix to prefix)

• When an operand is found in the infix string, the operand is


pushed onto the operandStack object

• When an operator found, it is pushed onto the


operatorStack object, if the stack is empty.
Otherwise one of the following case applies:
1. If the operator is left parentheses, push it onto the
operatorStack object (but the left parentheses has the lowest
precedence)
Infix to Prefix Conversion (Method 1)
2. If the operator has a higher precedence than the top operator on
the operatorStack object,
• push the operator onto the operatorStack object
3. If the operator’s precedence is equal to, or lower than the
precedence of the top operator on the operatorStack object,
• pop the operator precedence, opt1, from the operatorStack
object and
• pop two operands, opnd1 and opnd2, from the operandStack
object.
• Join together opt1, opnd2 and opnd1 and push the result onto
the operandStack object
Infix to Prefix Conversion (Method 1)
4. If the operator is a right parentheses treat is as
having lower priority than +, -, *, /.
• Case 3 will applied until left parentheses is the top operator on the
operatorStack object.
• Then pop that left parentheses.
• This process continues until we reach the end of the
infix expression.
Infix to Prefix Conversion (Method 1)
• Repeat the following actions from case 3 until the
operatorStack object is empty:
a)Pop opt1 from the operatorStack object
b)Pop opnd1 and opnd2 from the operandStack object
c) Join together opt1, opnd2 and opnd1 and push the result onto the
operandStack object
 When operatorStack is finally empty, the top (and only)
operand on operandStack will be the prefix string
corresponding to the original infix expression
Infix to Prefix Conversion (Method 1)
• Example 1:

Convert infix notation to prefix notation


a+b*c
Solution
Infix operandStack operatorStack
a a empty
+ a +
b b
a +
* b *
a +
c c *
b +
a
* bc +
a +
+ a * bc
Infix to Prefix Conversion (Method 1)
• Example 2:
Convert infix notation to prefix notation
(a – b) * c
Solution
Infix operandStack operatorStack
( (
a a (
- a -
(
b b -
a (
) - ab (
* - ab *

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–

STEP 1 STEP 2 STEP 3 STEP 4 STEP 5 STEP 6 RESULT

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 6 Pop 48 evaluate 48 / 12 = 4

Pop 8 evaluate 8 * 6 = 48
4) Push 4

2) Push 48 Push 5

Push 8 Pop 5

Push 4 Pop 4 evaluate 4+5 = 9

Pop 4
Pop 8 evaluate 8 + 4 = 12 5) Push 9
Pop 9
Null
The result is 9
Exercise 1:

Show the sequence of stack configuration in the evaluation of the


postfix expression below:

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)

You might also like