72
Data Structures and Algorithms V22.0102 Otávio Braga

Infix to Postfix conversion examples

Embed Size (px)

Citation preview

Page 1: Infix to Postfix conversion examples

Data Structures and AlgorithmsV22.0102

Otávio Braga

Page 2: Infix to Postfix conversion examples

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: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 1

• 3+4*5/6

Page 4: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 1

• 3+4*5/6

• Stack:

• Output:

Page 5: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 1

• 3+4*5/6

• Stack:

• Output: 3

Page 6: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 1

• 3+4*5/6

• Stack: +

• Output: 3

Page 7: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 1

• 3+4*5/6

• Stack: +

• Output: 3 4

Page 8: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 1

• 3+4*5/6

• Stack: + *

• Output: 3 4

Page 9: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 1

• 3+4*5/6

• Stack: + *

• Output: 3 4 5

Page 10: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 1

• 3+4*5/6

• Stack: +

• Output: 3 4 5 *

Page 11: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 1

• 3+4*5/6

• Stack: + /

• Output: 3 4 5 *

Page 12: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 1

• 3+4*5/6

• Stack: + /

• Output: 3 4 5 * 6

Page 13: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 1

• 3+4*5/6

• Stack: +

• Output: 3 4 5 * 6 /

Page 14: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 1

• 3+4*5/6

• Stack:

• Output: 3 4 5 * 6 / +

• Done!

Page 15: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 2

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

Page 16: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 2

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

• Stack:

• Output:

Page 17: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 2

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

• Stack: (

• Output:

Page 18: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 2

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

• Stack: (

• Output: 300

Page 19: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 2

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

• Stack: ( +

• Output: 300

Page 20: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 2

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

• Stack: ( +

• Output: 300 23

Page 21: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 2

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

• Stack: (

• Output: 300 23 +

Page 22: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 2

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

• Stack:

• Output: 300 23 +

Page 23: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 2

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

• Stack: *

• Output: 300 23 +

Page 24: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 2

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

• Stack: * (

• Output: 300 23 +

Page 25: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 2

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

• Stack: * (

• Output: 300 23 + 43

Page 26: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 2

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

• Stack: * ( -

• Output: 300 23 + 43

Page 27: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 2

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

• Stack: * ( -

• Output: 300 23 + 43 21

Page 28: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 2

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

• Stack: * (

• Output: 300 23 + 43 21 -

Page 29: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 2

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

• Stack: *

• Output: 300 23 + 43 21 -

Page 30: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 2

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

• Stack:

• Output: 300 23 + 43 21 - *

Page 31: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 2

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

• Stack: /

• Output: 300 23 + 43 21 - *

Page 32: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 2

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

• Stack: / (

• Output: 300 23 + 43 21 - *

Page 33: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 2

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

• Stack: / (

• Output: 300 23 + 43 21 - * 84

Page 34: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 2

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

• Stack: / ( +

• Output: 300 23 + 43 21 - * 84

Page 35: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 2

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

• Stack: / ( +

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

Page 36: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 2

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

• Stack: / (

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

Page 37: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 2

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

• Stack: /

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

Page 38: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 2

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

• Stack:

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

• Done!

Page 39: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 3

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

Page 40: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 3

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

• Stack:

• Output:

Page 41: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 3

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

• Stack: (

• Output:

Page 42: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 3

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

• Stack: (

• Output: 4

Page 43: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 3

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

• Stack: ( +

• Output: 4

Page 44: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 3

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

• Stack: ( +

• Output: 4 8

Page 45: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 3

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

• Stack: (

• Output: 4 8 +

Page 46: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 3

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

• Stack:

• Output: 4 8 +

Page 47: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 3

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

• Stack: *

• Output: 4 8 +

Page 48: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 3

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

• Stack: * (

• Output: 4 8 +

Page 49: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 3

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

• Stack: * (

• Output: 4 8 + 6

Page 50: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 3

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

• Stack: * ( -

• Output: 4 8 + 6

Page 51: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 3

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

• Stack: * ( -

• Output: 4 8 + 6 5

Page 52: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 3

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

• Stack: * (

• Output: 4 8 + 6 5 -

Page 53: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 3

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

• Stack: *

• Output: 4 8 + 6 5 -

Page 54: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 3

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

• Stack:

• Output: 4 8 + 6 5 - *

Page 55: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 3

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

• Stack: /

• Output: 4 8 + 6 5 - *

Page 56: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 3

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

• Stack: / (

• Output: 4 8 + 6 5 - *

Page 57: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 3

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

• Stack: / ( (

• Output: 4 8 + 6 5 - *

Page 58: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 3

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

• Stack: / ( (

• Output: 4 8 + 6 5 - * 3

Page 59: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 3

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

• Stack: / ( ( -

• Output: 4 8 + 6 5 - * 3

Page 60: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 3

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

• Stack: / ( ( -

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

Page 61: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 3

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

• Stack: / ( (

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

Page 62: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 3

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

• Stack: / (

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

Page 63: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 3

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

• Stack: / ( *

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

Page 64: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 3

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

• Stack: / ( * (

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

Page 65: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 3

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

• Stack: / ( * (

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

Page 66: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 3

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

• Stack: / ( * ( +

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

Page 67: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 3

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

• Stack: / ( * ( +

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

Page 68: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 3

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

• Stack: / ( * (

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

Page 69: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 3

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

• Stack: / ( *

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

Page 70: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 3

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

• Stack: / (

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

Page 71: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 3

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

• Stack: /

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

Page 72: Infix to Postfix conversion examples

Infix to Postfix ConversionExample 3

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

• Stack:

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

• Done!