case study: infix, postfix & prefix expressions

10
21-Oct-21 Data Structures and Algorithms - Fall 2021 Instructor: Saima Jawad 1 Case Study: Infix, Postfix & Prefix Expressions 34 W02-Stacks 35 Infix, Postfix and Prefix A + B Infix AB + Postfix + AB Prefix

Upload: others

Post on 19-Apr-2022

18 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Case Study: Infix, Postfix & Prefix Expressions

21-Oct-21

Data Structures and Algorithms - Fall 2021Instructor: Saima Jawad 1

Case Study: Infix, Postfix & Prefix Expressions

34

W02-Stacks 35

Infix, Postfix and Prefix

A + B Infix

AB + Postfix

+ AB Prefix

Page 2: Case Study: Infix, Postfix & Prefix Expressions

21-Oct-21

Data Structures and Algorithms - Fall 2021Instructor: Saima Jawad 2

W02-Stacks 36

Examples

A + (B * C) infix

A + B C *

A B C * + postfix

----------------------------------------------------------------

(A + B) * C infix

+ A B * C

* + A B C prefix

W02-Stacks 37

Convert the following into Postfix and Prefix:

1. (A+B)*(C-D)

2. A-B/(C*D^E)

Let’s Exercise

Page 3: Case Study: Infix, Postfix & Prefix Expressions

21-Oct-21

Data Structures and Algorithms - Fall 2021Instructor: Saima Jawad 3

W02-Stacks 38

Infix Postfix Prefix

(A+B)*(C-D) AB+CD-* *+AB-CD

A-B/(C*D^E) ABCDE^*/- -A/B*C^DE

Solution

W02-Stacks 39

Why “Postfix”?

It is the most efficient method for representing arithmetic expressions.

There is no need to use ()’s with postfix notation.

Page 4: Case Study: Infix, Postfix & Prefix Expressions

21-Oct-21

Data Structures and Algorithms - Fall 2021Instructor: Saima Jawad 4

W02-Stacks 40

Postfix to infix Conversion Example

Infix: a + b * c + (d * e + f) * g

Postfix Conversion:

a b c * + d e * f + g * +

b*c

a + ( b*c)d*e

d*e + f

(d*e + f) * g

a + (b*c) + (d*e + f)* g

W02-Stacks 41

Infix to Postfix Conversion

A stack is used to write an algorithm to convert an infix expression to a postfix expression.

◦ It can handle the presence of ()’s.

◦ It is efficient.

Page 5: Case Study: Infix, Postfix & Prefix Expressions

21-Oct-21

Data Structures and Algorithms - Fall 2021Instructor: Saima Jawad 5

W02-Stacks 42

Infix to Postfix Conversion

Infix:a + b * c + (d * e + f) * g

Postfix:a b c * + d e * f + g * +

W02-Stacks

Infix to Postfix Conversion using a Stack

43

+

*

+ +

(

+

*

(

+

+

(

+ +

*

+

Output:

Stack:

a b c * + d e * f + g * +

Arrows represent

pop and output

a + b * c + (d * e + f) * gInput:

Page 6: Case Study: Infix, Postfix & Prefix Expressions

21-Oct-21

Data Structures and Algorithms - Fall 2021Instructor: Saima Jawad 6

W02-Stacks

W02-Stacks 45

Infix to Postfix Conversion Algorithm

oRead infix expression character by character as input

oIf input is an operand, output the operand

oIf input is an operator then opop and output all operators of >= precedence oPush operator in the stack

oIf input is [{(< then push in the stack

oIf input is >)}] then opop and output all operators until see a matching parenthesis on the stack

oPop the parenthesis without output

oIf no more input then opop and output all operators in the stack

Page 7: Case Study: Infix, Postfix & Prefix Expressions

21-Oct-21

Data Structures and Algorithms - Fall 2021Instructor: Saima Jawad 7

W02-Stacks 46

Let’s Exercise

Convert the following infix expression into postfix:

((6-(2+3))*(3+8/2)) ^ 2 + 3

Postfix Expression:6 2 3 + - 3 8 2 / + * 2 ^ 3 +

W02-Stacks 47

stacktop()

returns the element at the top of the stack without

actually removing it.

prcd(op1, op2)

returns true if op1 has higher or equal precedence

over op2, false otherwise.

Conversion of an Infix Expression to Postfix

Page 8: Case Study: Infix, Postfix & Prefix Expressions

21-Oct-21

Data Structures and Algorithms - Fall 2021Instructor: Saima Jawad 8

W02-Stacks 48

opstk the empty stack

while (not end of input)

{ symb next input character

if (symb is an operand)

add symb to the postfix string

else {

while(!opstk.empty() && prcd(opstk.stacktop(), symb) )

{ topsymb opstk.pop();

add topsymb to the postfix string

} /* end while */

opstk.push(symb)

} /* end else */

} /* end while */

Conversion of an Infix Expression to Postfix

W02-Stacks 49

/* output any remaining operators */

while (! opstk.empty())

{

topsymb opstk.pop();

add topsymb to the postfix string;

} /* end while

Conversion of an Infix Expression to Postfix

Page 9: Case Study: Infix, Postfix & Prefix Expressions

21-Oct-21

Data Structures and Algorithms - Fall 2021Instructor: Saima Jawad 9

W02-Stacks 50

Postfix Evaluation Algorithm

The algorithm is simple

◦ Read in input character by character

◦ If input is an operand then◦ push on stack

◦ If input is an operator then ◦ pop top two operands off stack

◦ perform operation

◦ place result on stack

Example: Evaluate 3 2 5 * +

51

Postfix Expression: 6 2 3 + - 3 8 2 / + * 2 ^ 3 +

symb opnd1 opnd2 value opndstk

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

Page 10: Case Study: Infix, Postfix & Prefix Expressions

21-Oct-21

Data Structures and Algorithms - Fall 2021Instructor: Saima Jawad 10

W02-Stacks

Algorithm to Evaluate a Postfix Expressionopndstk the empty stack

// scan the input string reading one element at a time

while (not end of input string)

{

symb next input character

if (symb is operand)

opndstk.push (symb)

else

opnd2 opndstk.pop ()

opnd1 opndstk .pop ()

value result of applying symb to opnd1 and opnd2

opndstk.push (value)

}

return (opndstk.pop () )

52