41
DATA STRUCTURES AND ALGORITHMS IMRAN IHSAN ASSISTANT PROFESSOR, AIR UNIVERSITY, ISLAMABAD WWW.IMRANIHSAN.COM LECTURES ADAPTED FROM: DANIEL KANE, NEIL RHODES DEPARTMENT OF CS & ENGINEERING UNIVERSITY OF CALIFORNIA, SAN DIEGO 09 STACK APPLICATION REVERSE POLISH NOTATION

STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

Embed Size (px)

Citation preview

Page 1: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

DATA STRUCTURES AND ALGORITHMS

IMRAN IHSANASSISTANT PROFESSOR, AIR UNIVERSITY, ISLAMABADWWW.IMRANIHSAN.COM

LECTURES ADAPTED FROM:DANIEL KANE, NEIL RHODESDEPARTMENT OF CS & ENGINEERINGUNIVERSITY OF CALIFORNIA, SAN DIEGO

09STACK APPLICATIONREVERSE POLISH NOTATION

Page 2: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

ALGEBRAIC EXPRESSION

2

X + Y * Z

• An algebraic expression is a legal combination of operands and the operators.

• Operand is the quantity (unit of data) on which a mathematical operation is performed.

• Operand may be a variable like x, y, z or a constant like 5, 4,0,9,1 etc.

• Operator is a symbol which signifies a mathematical or logical operation between the operands. Example of familiar operators include +,-,*, /, ^

• Considering these definitions of operands and operators now we can write an example of expression as x+y*z.

• Infix, Postfix and Prefix notations are three different but equivalent ways of writing expressions. It is easiest to demonstrate the differences by looking at examples of operators that take two operands.

Page 3: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

INFIX NOTATION

3

X + Y

• Operators are written in-between their operands. This is the usual way we write expressions.

• An expression such as A * ( B + C ) / D is usually taken to mean something like: "First add B and C together, then multiply the result by A, then divide by D to give the final answer.“

• Infix notation needs extra information to make the order of evaluation of the operators clear: rules built into the language about operator precedence and associativity, and brackets ( ) to allow users to override these rules.

• For example, the usual rules for associativity say that we perform operations from left to right, so the multiplication by A is assumed to come before the division by D. Similarly, the usual rules for precedence say that we perform multiplication and division before we perform addition and subtraction.

Page 4: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

POSTFIX NOTATION

4

X Y +

• Also known as "Reverse Polish notation“

• Operators are written after their operands. The infix expression given above is equivalent to A B C + * D /

• The order of evaluation of operators is always left-to-right, and brackets cannot be used to change this order. Because the "+" is to the left of the "*" in the example above, the addition must be performed before the multiplication.

• Operators act on values immediately to the left of them. For example, the "+" above uses the "B" and "C". We can add (totally unnecessary) brackets to make this explicit:

( (A (B C +) *) D /)

• Thus, the "*" uses the two values immediately preceding: "A", and the result of the addition. Similarly, the "/" uses the result of the multiplication and the "D".

Page 5: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

PREFIX NOTATION

5

+ X Y

• Also known as "Polish notation“

• Operators are written before their operands. The expressions given above are equivalent to / * A + B C D

• to make this clear:

(/ (* A (+ B C) ) D)

Page 6: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

OPERATOR PRIORITIES

6

TIE BREAKER, DELIMITERS

• How do you figure out the operands of an operator?• a + b * c• a * b + c / d

• This is done by assigning operator priorities.• priority(*) = priority(/) > priority(+) = priority(-)

• When an operand lies between two operators, the operand associates with the operator that has higher priority.

• Tie Breaker• When an operand lies between two operators that have the same priority, the

operand associates with the operator on the left.• a + b - c• a * b / c / d

• Delimiters• Sub-expression within delimiters is treated as a single operand, independent

from the remainder of the expression.• (a + b) * (c – d) / (e – f)

Page 7: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

WHY POSTFIX?

7

INFIX EXPRESSION IS HARD TO PARSE

• Why to use these weird looking PREFIX and POSTFIX notations when we have simple INFIX notation?

• To our surprise INFIX notations are not as simple as they seem specially while evaluating them. To evaluate an infix expression we need to consider Operators’ Priority and Associative property• For example expression 3+5*4 evaluate to 32 i.e. (3+5)*4 • or to 23 i.e. 3+(5*4).

• To solve this problem Precedence or Priority of the operators were defined. Operator precedence governs evaluation order. An operator with higher precedence is applied before an operator with lower precedence.

• Need operator priorities, tie breaker, and delimiters.

• This makes computer evaluation more difficult than is necessary.

• Postfix and prefix expression forms do not rely on operator priorities, a tie breaker, or delimiters.

• So it is easier to evaluate expressions that are in these forms.

Page 8: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

INFIX TO POSTFIX (RPN)

8

ALGORITHM

1. Examine the next element in the input.

2. If it is operand, output it.

3. If it is opening parenthesis, push it on stack.

4. If it is an operator, then1. If stack is empty, push operator on stack.2. If the top of stack is opening parenthesis, push operator on stack3. If it has higher priority than the top of stack, push operator on

stack.4. Else pop the operator from the stack and output it, repeat step 4

5. If it is a closing parenthesis, pop operators from stack and output them until an opening parenthesis is encountered. pop and discard the opening parenthesis.

6. If there is more input go to step 1

7. If there is no more input, pop the remaining operators to output.

Page 9: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

EXAMPLE

9

INFIX TO POSTFIX

• Let the incoming the Infix expression be:

A * (B + C) – D / E

• Stage 1: • Stack is empty and we only have the Infix Expression.

• Infix Notation

A * (B + C) – D / E

• Postfix Notation

STACK

NULL

Page 10: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

EXAMPLE

10

INFIX TO POSTFIX

• Let the incoming the Infix expression be:

A * (B + C) – D / E

• Stage 2: • First token is Operand A and is Appended to Output as it is.

• Infix Notation

* (B + C) – D / E

• Postfix Notation

A

STACK

NULL

Page 11: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

EXAMPLE

11

INFIX TO POSTFIX

• Let the incoming the Infix expression be:

A * (B + C) – D / E

• Stage 3: • Next token is * and it is pushed into the Stack

• Infix Notation

(B + C) – D / E

• Postfix Notation

A

STACK

NULL

*

Page 12: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

EXAMPLE

12

INFIX TO POSTFIX

• Let the incoming the Infix expression be:

A * (B + C) – D / E

• Stage 4: • Next token is ( and it is pushed into the Stack as well

• Infix Notation

B + C) – D / E

• Postfix Notation

A

STACK

NULL

(

*

Page 13: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

EXAMPLE

13

INFIX TO POSTFIX

• Let the incoming the Infix expression be:

A * (B + C) – D / E

• Stage 5: • Next token is B and will go to output expression as it is

• Infix Notation

+ C) – D / E

• Postfix Notation

A B

STACK

NULL

(

*

Page 14: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

EXAMPLE

14

INFIX TO POSTFIX

• Let the incoming the Infix expression be:

A * (B + C) – D / E

• Stage 6: • Next token is + and is pushed in stack.

• Infix Notation

C ) – D / E

• Postfix Notation

A B

STACK

NULL

+

(

*

Page 15: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

EXAMPLE

15

INFIX TO POSTFIX

• Let the incoming the Infix expression be:

A * (B + C) – D / E

• Stage 7: • Next token is C and is appended to output

• Infix Notation

) – D / E

• Postfix Notation

A B C

STACK

NULL

+

(

*

Page 16: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

EXAMPLE

16

INFIX TO POSTFIX

• Let the incoming the Infix expression be:

A * (B + C) – D / E

• Stage 8: • Next token is ) pop all elements and append to output

• Infix Notation

– D / E

• Postfix Notation

A B C +

STACK

NULL

*

Page 17: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

EXAMPLE

17

INFIX TO POSTFIX

• Let the incoming the Infix expression be:

A * (B + C) – D / E

• Stage 9: • Next token is -. Precedence of * is high. So Pop * and Push -

• Infix Notation

D / E

• Postfix Notation

A B C + *

STACK

NULL

-

Page 18: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

EXAMPLE

18

INFIX TO POSTFIX

• Let the incoming the Infix expression be:

A * (B + C) – D / E

• Stage 10: • Next token is D. Append to Output

• Infix Notation

/ E

• Postfix Notation

A B C+ * D

STACK

NULL

-

Page 19: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

EXAMPLE

19

INFIX TO POSTFIX

• Let the incoming the Infix expression be:

A * (B + C) – D / E

• Stage 11: • Next token is /. Precedence of / is high, so Push / in stack.

• Infix Notation

E

• Postfix Notation

A B C + * D

STACK

NULL

/

-

Page 20: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

EXAMPLE

20

INFIX TO POSTFIX

• Let the incoming the Infix expression be:

A * (B + C) – D / E

• Stage 12: • Last token is E. Append to Output

• Infix Notation

• Postfix Notation

A B C + * D E

STACK

NULL

/

-

Page 21: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

EXAMPLE

21

INFIX TO POSTFIX

• Let the incoming the Infix expression be:

A * (B + C) – D / E

• Stage 13: • Input Expression Complete. Pop all to complete Postfix

• Infix Notation

• Postfix Notation

A B C + * D E / -

STACK

NULL

Page 22: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

RPN USING STACK

22

REVERSE-POLISH NOTATION

• The easiest way to parse reverse-Polish notation is to use an operand stack:

• operands are processed by pushing them onto the stack

• when processing an operator:• pop the last two items off the operand stack,

• perform the operation, and

• push the result back onto the stack

Page 23: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

RPN EXAMPLEREVERSE-POLISH NOTATION

• Evaluate the following reverse-Polish expression using a stack:

1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

23

Page 24: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

RPN EXAMPLEREVERSE-POLISH NOTATION

• Push 1 onto the stack

1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

1

24

Page 25: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

RPN EXAMPLEREVERSE-POLISH NOTATION

• Push 2 onto the stack

1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

2

1

25

Page 26: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

RPN EXAMPLEREVERSE-POLISH NOTATION

• Push 3 onto the stack

1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

3

2

1

26

Page 27: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

RPN EXAMPLEREVERSE-POLISH NOTATION

• Pop 3 and 2 and push 2 + 3 onto the stack

1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

5

1

27

Page 28: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

RPN EXAMPLEREVERSE-POLISH NOTATION

• Push 4 onto the stack

1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

4

5

1

28

Page 29: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

RPN EXAMPLEREVERSE-POLISH NOTATION

• Push 5 onto the stack

1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

5

4

5

1

29

Page 30: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

RPN EXAMPLEREVERSE-POLISH NOTATION

• Push 6 onto the stack

1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

6

5

4

5

1

30

Page 31: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

RPN EXAMPLEREVERSE-POLISH NOTATION

• Pop 6 and 5 and Push 5 x 6 onto the stack

1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

30

4

5

1

31

Page 32: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

RPN EXAMPLEREVERSE-POLISH NOTATION

• Pop 30 and 4 and Push 4 - 30 onto the stack

1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

–26

5

1

32

Page 33: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

RPN EXAMPLEREVERSE-POLISH NOTATION

• Push 7 onto the stack

1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

7

–26

5

1

33

Page 34: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

RPN EXAMPLEREVERSE-POLISH NOTATION

• Pop 7 and -26 and Push -26 x 7 onto the stack

1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

–182

5

1

34

Page 35: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

RPN EXAMPLEREVERSE-POLISH NOTATION

• Pop -182 and 5 and Push -185 + 5 onto the stack

1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

–177

1

35

Page 36: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

RPN EXAMPLEREVERSE-POLISH NOTATION

• Pop -117 and 1 and Push 1 – (– 177 ) onto the stack

1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

178

36

Page 37: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

RPN EXAMPLEREVERSE-POLISH NOTATION

• Push 8 onto the stack

1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

8

178

37

Page 38: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

RPN EXAMPLEREVERSE-POLISH NOTATION

• Push 9 onto the stack

1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

9

8

178

38

Page 39: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

RPN EXAMPLEREVERSE-POLISH NOTATION

• Pop 9 and 8 and Push 8 x 9 onto the stack

1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

72

178

39

Page 40: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

RPN EXAMPLEREVERSE-POLISH NOTATION

• Pop 72 and 178 and Push 178 + 72 onto the stack

1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

250

40

Page 41: STACK APPLICATION - Imran Ihsanimranihsan.com/upload/lecture/DSAF1709.pdf · STACK APPLICATION REVERSE POLISH ... Postfix and Prefix notations are three different but ... evaluation

RPN

41

REVERSE-POLISH NOTATION

• Thus• 1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

• evaluates to the value on the top: 250

• The equivalent in-fix notation is• ((1 – ((2 + 3) + ((4 – (5 × 6)) × 7))) + (8 × 9))

• We reduce the parentheses using order-of-operations:• 1 – (2 + 3 + (4 – 5 × 6) × 7) + 8 × 9

• Incidentally,• 1 – 2 + 3 + 4 – 5 × 6 × 7 + 8 × 9 = – 132

• which has the reverse-Polish notation of• 1 2 – 3 + 4 + 5 6 7 × × – 8 9 × +