Top Banner
Data Structures and Algorithms V22.0102 Otávio Br ag a
72

In Fix to Post Fix Examples

Apr 04, 2018

Download

Documents

arunabio
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 1/72

Data Structures and Algorithms

V22.0102

Otávio Braga

Page 2: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 2/72

Infix to Postfix Conversion

• We use a stack

• When an operand is read, output it

• When an operator is read – Pop until the top of the stack has an element of lower

precedence – Then push it

• When ) is found, pop until we find the matching (

• ( has the lowest precedence when in the stack

• but has the highest precedence when in the input• When we reach the end of input, pop until the stack is

empty

Page 3: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 3/72

Infix to Postfix Conversion

Example 1

• 3+4*5/6

Page 4: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 4/72

Infix to Postfix Conversion

Example 1

• 3+4*5/6

• Stack:

Output:

Page 5: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 5/72

Infix to Postfix Conversion

Example 1

• 3+4*5/6

• Stack:

Output: 3

Page 6: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 6/72

Infix to Postfix Conversion

Example 1

• 3+4*5/6

• Stack: +

Output: 3

Page 7: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 7/72

Infix to Postfix Conversion

Example 1

• 3+4*5/6

• Stack: +

Output: 3 4

Page 8: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 8/72

Infix to Postfix Conversion

Example 1

• 3+4*5/6

• Stack: + *

Output: 3 4

Page 9: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 9/72

Infix to Postfix Conversion

Example 1

• 3+4*5/6

• Stack: + *

Output: 3 4 5

Page 10: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 10/72

Infix to Postfix Conversion

Example 1

• 3+4*5/6

• Stack: +

Output: 3 4 5 *

Page 11: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 11/72

Infix to Postfix Conversion

Example 1

• 3+4*5/6

• Stack: + /

Output: 3 4 5 *

Page 12: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 12/72

Infix to Postfix Conversion

Example 1

• 3+4*5/6

• Stack: + /

Output: 3 4 5 * 6

Page 13: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 13/72

Infix to Postfix Conversion

Example 1

• 3+4*5/6

• Stack: +

Output: 3 4 5 * 6 /

Page 14: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 14/72

Infix to Postfix Conversion

Example 1

• 3+4*5/6

• Stack:

Output: 3 4 5 * 6 / +

• Done!

Page 15: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 15/72

Infix to Postfix Conversion

Example 2

• (300+23)*(43-21)/(84+7)

Page 16: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 16/72

Infix to Postfix Conversion

Example 2

• (300+23)*(43-21)/(84+7)

• Stack:

Output:

Page 17: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 17/72

Infix to Postfix Conversion

Example 2

• (300+23)*(43-21)/(84+7)

• Stack: (

Output:

Page 18: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 18/72

Infix to Postfix Conversion

Example 2

• (300+23)*(43-21)/(84+7)

• Stack: (

Output: 300

Page 19: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 19/72

Infix to Postfix Conversion

Example 2

• (300+23)*(43-21)/(84+7)

• Stack: ( +

Output: 300

Page 20: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 20/72

Infix to Postfix Conversion

Example 2

• (300+23)*(43-21)/(84+7)

• Stack: ( +

Output: 300 23

Page 21: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 21/72

Infix to Postfix Conversion

Example 2

• (300+23)*(43-21)/(84+7)

• Stack: (

Output: 300 23 +

Page 22: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 22/72

Infix to Postfix Conversion

Example 2

• (300+23)*(43-21)/(84+7)

• Stack:

Output: 300 23 +

Page 23: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 23/72

Infix to Postfix Conversion

Example 2

• (300+23)*(43-21)/(84+7)

• Stack: *

Output: 300 23 +

Page 24: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 24/72

Infix to Postfix Conversion

Example 2

• (300+23)*(43-21)/(84+7)

• Stack: * (

Output: 300 23 +

Page 25: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 25/72

Infix to Postfix Conversion

Example 2

• (300+23)*(43-21)/(84+7)

• Stack: * (

Output: 300 23 + 43

Page 26: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 26/72

Infix to Postfix Conversion

Example 2

• (300+23)*(43-21)/(84+7)

• Stack: * ( -

Output: 300 23 + 43

Page 27: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 27/72

Infix to Postfix Conversion

Example 2

• (300+23)*(43-21)/(84+7)

• Stack: * ( -

Output: 300 23 + 43 21

Page 28: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 28/72

Infix to Postfix Conversion

Example 2

• (300+23)*(43-21)/(84+7)

• Stack: * (

Output: 300 23 + 43 21 -

Page 29: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 29/72

Infix to Postfix Conversion

Example 2

• (300+23)*(43-21)/(84+7)

• Stack: *

Output: 300 23 + 43 21 -

Page 30: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 30/72

Infix to Postfix Conversion

Example 2

• (300+23)*(43-21)/(84+7)

• Stack:

Output: 300 23 + 43 21 - *

Page 31: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 31/72

Infix to Postfix Conversion

Example 2

• (300+23)*(43-21)/(84+7)

• Stack: /

Output: 300 23 + 43 21 - *

Page 32: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 32/72

Infix to Postfix Conversion

Example 2

• (300+23)*(43-21)/(84+7)

• Stack: / (

• Output: 300 23 + 43 21 - *

Page 33: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 33/72

Infix to Postfix Conversion

Example 2

• (300+23)*(43-21)/(84+7)

• Stack: / (

• Output: 300 23 + 43 21 - * 84

Page 34: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 34/72

Infix to Postfix Conversion

Example 2

• (300+23)*(43-21)/(84+7)

• Stack: / ( +

• Output: 300 23 + 43 21 - * 84

Page 35: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 35/72

Infix to Postfix Conversion

Example 2

• (300+23)*(43-21)/(84+7)

• Stack: / ( +

• Output: 300 23 + 43 21 - * 84 7

Page 36: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 36/72

Infix to Postfix Conversion

Example 2

• (300+23)*(43-21)/(84+7)

• Stack: / (

• Output: 300 23 + 43 21 - * 84 7 +

Page 37: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 37/72

Infix to Postfix Conversion

Example 2

• (300+23)*(43-21)/(84+7)

• Stack: /

• Output: 300 23 + 43 21 - * 84 7 +

Page 38: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 38/72

Infix to Postfix Conversion

Example 2

• (300+23)*(43-21)/(84+7)

• Stack:

• Output: 300 23 + 43 21 - * 84 7 + /

• Done!

Page 39: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 39/72

Infix to Postfix Conversion

Example 3

• (4+8)*(6-5)/((3-2)*(2+2))

Page 40: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 40/72

Infix to Postfix Conversion

Example 3

• (4+8)*(6-5)/((3-2)*(2+2))

• Stack:

• Output:

Page 41: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 41/72

Page 42: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 42/72

Infix to Postfix Conversion

Example 3

• (4+8)*(6-5)/((3-2)*(2+2))

• Stack: (

• Output: 4

Page 43: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 43/72

Infix to Postfix Conversion

Example 3

• (4+8)*(6-5)/((3-2)*(2+2))

• Stack: ( +

• Output: 4

Page 44: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 44/72

Infix to Postfix Conversion

Example 3

• (4+8)*(6-5)/((3-2)*(2+2))

• Stack: ( +

• Output: 4 8

f f

Page 45: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 45/72

Infix to Postfix Conversion

Example 3

• (4+8)*(6-5)/((3-2)*(2+2))

• Stack: (

• Output: 4 8 +

f f

Page 46: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 46/72

Infix to Postfix Conversion

Example 3

• (4+8)*(6-5)/((3-2)*(2+2))

• Stack:

• Output: 4 8 +

fi fi i

Page 47: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 47/72

Infix to Postfix Conversion

Example 3

• (4+8)*(6-5)/((3-2)*(2+2))

• Stack: *

• Output: 4 8 +

I fi P fi C i

Page 48: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 48/72

Infix to Postfix Conversion

Example 3

• (4+8)*(6-5)/((3-2)*(2+2))

• Stack: * (

• Output: 4 8 +

I fi P fi C i

Page 49: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 49/72

Infix to Postfix Conversion

Example 3

• (4+8)*(6-5)/((3-2)*(2+2))

• Stack: * (

• Output: 4 8 + 6

I fi P fi C i

Page 50: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 50/72

Infix to Postfix Conversion

Example 3

• (4+8)*(6-5)/((3-2)*(2+2))

• Stack: * ( -

• Output: 4 8 + 6

I fi t P tfi C i

Page 51: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 51/72

Infix to Postfix Conversion

Example 3

• (4+8)*(6-5)/((3-2)*(2+2))

• Stack: * ( -

• Output: 4 8 + 6 5

I fi t P tfi C i

Page 52: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 52/72

Infix to Postfix Conversion

Example 3

• (4+8)*(6-5)/((3-2)*(2+2))

• Stack: * (

• Output: 4 8 + 6 5 -

I fi t P tfi C i

Page 53: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 53/72

Infix to Postfix Conversion

Example 3

• (4+8)*(6-5)/((3-2)*(2+2))

• Stack: *

• Output: 4 8 + 6 5 -

I fi t P tfi C i

Page 54: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 54/72

Infix to Postfix Conversion

Example 3

• (4+8)*(6-5)/((3-2)*(2+2))

• Stack:

• Output: 4 8 + 6 5 - *

I fi t P tfi C i

Page 55: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 55/72

Infix to Postfix Conversion

Example 3

• (4+8)*(6-5)/((3-2)*(2+2))

• Stack: /

• Output: 4 8 + 6 5 - *

I fi t P tfi C i

Page 56: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 56/72

Infix to Postfix Conversion

Example 3

• (4+8)*(6-5)/((3-2)*(2+2))

• Stack: / (

• Output: 4 8 + 6 5 - *

I fi t P tfi C i

Page 57: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 57/72

Infix to Postfix Conversion

Example 3

• (4+8)*(6-5)/((3-2)*(2+2))

• Stack: / ( (

• Output: 4 8 + 6 5 - *

Infix to Postfix Conversion

Page 58: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 58/72

Infix to Postfix Conversion

Example 3

• (4+8)*(6-5)/((3-2)*(2+2))

• Stack: / ( (

• Output: 4 8 + 6 5 - * 3

Infix to Postfix Conversion

Page 59: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 59/72

Infix to Postfix Conversion

Example 3

• (4+8)*(6-5)/((3-2)*(2+2))

• Stack: / ( ( -

• Output: 4 8 + 6 5 - * 3

Infix to Postfix Conversion

Page 60: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 60/72

Infix to Postfix Conversion

Example 3

• (4+8)*(6-5)/((3-2)*(2+2))

• Stack: / ( ( -

• Output: 4 8 + 6 5 - * 3 2

Infix to Postfix Conversion

Page 61: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 61/72

Infix to Postfix Conversion

Example 3

• (4+8)*(6-5)/((3-2)*(2+2))

• Stack: / ( (

• Output: 4 8 + 6 5 - * 3 2 -

Infix to Postfix Conversion

Page 62: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 62/72

Infix to Postfix Conversion

Example 3

• (4+8)*(6-5)/((3-2)*(2+2))

• Stack: / (

• Output: 4 8 + 6 5 - * 3 2 -

Infix to Postfix Conversion

Page 63: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 63/72

Infix to Postfix Conversion

Example 3

• (4+8)*(6-5)/((3-2)*(2+2))

• Stack: / ( *

• Output: 4 8 + 6 5 - * 3 2 -

Infix to Postfix Conversion

Page 64: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 64/72

Infix to Postfix Conversion

Example 3

• (4+8)*(6-5)/((3-2)*(2+2))

• Stack: / ( * (

• Output: 4 8 + 6 5 - * 3 2 -

Infix to Postfix Conversion

Page 65: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 65/72

Infix to Postfix Conversion

Example 3

• (4+8)*(6-5)/((3-2)*(2+2))

• Stack: / ( * (

• Output: 4 8 + 6 5 - * 3 2 - 2

Infix to Postfix Conversion

Page 66: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 66/72

Infix to Postfix Conversion

Example 3

• (4+8)*(6-5)/((3-2)*(2+2))

• Stack: / ( * ( +

• Output: 4 8 + 6 5 - * 3 2 - 2

Infix to Postfix Conversion

Page 67: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 67/72

Infix to Postfix Conversion

Example 3

• (4+8)*(6-5)/((3-2)*(2+2))

• Stack: / ( * ( +

• Output: 4 8 + 6 5 - * 3 2 – 2 2

Infix to Postfix Conversion

Page 68: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 68/72

Infix to Postfix Conversion

Example 3

• (4+8)*(6-5)/((3-2)*(2+2))

• Stack: / ( * (

• Output: 4 8 + 6 5 - * 3 2 – 2 2 +

Page 69: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 69/72

Infix to Postfix Conversion

Page 70: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 70/72

Infix to Postfix Conversion

Example 3

• (4+8)*(6-5)/((3-2)*(2+2))

• Stack: / (

• Output: 4 8 + 6 5 - * 3 2 – 2 2 + *

Infix to Postfix Conversion

Page 71: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 71/72

Infix to Postfix Conversion

Example 3

• (4+8)*(6-5)/((3-2)*(2+2))

• Stack: /

• Output: 4 8 + 6 5 - * 3 2 – 2 2 + *

Infix to Postfix Conversion

Page 72: In Fix to Post Fix Examples

7/31/2019 In Fix to Post Fix Examples

http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 72/72

Infix to Postfix Conversion

Example 3

• (4+8)*(6-5)/((3-2)*(2+2))

• Stack:

• Output: 4 8 + 6 5 - * 3 2 – 2 2 + * /

• Done!