Top Banner
Infix to Postfix Conversion Using Stack
21

Infix to Postfix Conversion Using Stackwkhon/ds/ds11/codes/infix-to-postfix.pdf · 20 Bonus: Handling the Parenthesis Have to be done first! ... 21 Bonus: Handling the Parenthesis

Sep 12, 2018

Download

Documents

truongcong
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: Infix to Postfix Conversion Using Stackwkhon/ds/ds11/codes/infix-to-postfix.pdf · 20 Bonus: Handling the Parenthesis Have to be done first! ... 21 Bonus: Handling the Parenthesis

Infix to Postfix ConversionUsing Stack

Page 2: Infix to Postfix Conversion Using Stackwkhon/ds/ds11/codes/infix-to-postfix.pdf · 20 Bonus: Handling the Parenthesis Have to be done first! ... 21 Bonus: Handling the Parenthesis

2

Observation 1

o

A B

Infix: AoB

Postfix: ABo

Page 3: Infix to Postfix Conversion Using Stackwkhon/ds/ds11/codes/infix-to-postfix.pdf · 20 Bonus: Handling the Parenthesis Have to be done first! ... 21 Bonus: Handling the Parenthesis

3

Observation 1

o2

C D

Infix: Ao1Bo

3Co

2D

Postfix: ABo1CDo

2o

3

o1

A B

o3

Page 4: Infix to Postfix Conversion Using Stackwkhon/ds/ds11/codes/infix-to-postfix.pdf · 20 Bonus: Handling the Parenthesis Have to be done first! ... 21 Bonus: Handling the Parenthesis

4

Observation 2

1 + 2 - 3 + 4

= ((1 + 2) - 3) + 4

=> 1 2 + 3 - 4 +

Page 5: Infix to Postfix Conversion Using Stackwkhon/ds/ds11/codes/infix-to-postfix.pdf · 20 Bonus: Handling the Parenthesis Have to be done first! ... 21 Bonus: Handling the Parenthesis

5

Observation 3

1 + 2 * 3 * 2 + 4

= (1 + ((2 * 3) * 2)) + 4

=> 1 2 3 * 2 * + 4 +

Page 6: Infix to Postfix Conversion Using Stackwkhon/ds/ds11/codes/infix-to-postfix.pdf · 20 Bonus: Handling the Parenthesis Have to be done first! ... 21 Bonus: Handling the Parenthesis

6

Observation 3

1 + 2 * 3 * 2 + 4

=> 1

+

Page 7: Infix to Postfix Conversion Using Stackwkhon/ds/ds11/codes/infix-to-postfix.pdf · 20 Bonus: Handling the Parenthesis Have to be done first! ... 21 Bonus: Handling the Parenthesis

7

Observation 3

1 + 2 * 3 * 2 + 4

=> 1 2 *+

Page 8: Infix to Postfix Conversion Using Stackwkhon/ds/ds11/codes/infix-to-postfix.pdf · 20 Bonus: Handling the Parenthesis Have to be done first! ... 21 Bonus: Handling the Parenthesis

8

Observation 3

1 + 2 * 3 * 2 + 4

=> 1 2 3 *+

Page 9: Infix to Postfix Conversion Using Stackwkhon/ds/ds11/codes/infix-to-postfix.pdf · 20 Bonus: Handling the Parenthesis Have to be done first! ... 21 Bonus: Handling the Parenthesis

9

Observation 3

1 + 2 * 3 * 2 + 4

=> 1 2 3 *

+

Page 10: Infix to Postfix Conversion Using Stackwkhon/ds/ds11/codes/infix-to-postfix.pdf · 20 Bonus: Handling the Parenthesis Have to be done first! ... 21 Bonus: Handling the Parenthesis

10

Observation 3

1 + 2 * 3 * 2 + 4

=> 1 2 3 * *+

Page 11: Infix to Postfix Conversion Using Stackwkhon/ds/ds11/codes/infix-to-postfix.pdf · 20 Bonus: Handling the Parenthesis Have to be done first! ... 21 Bonus: Handling the Parenthesis

11

Observation 3

1 + 2 * 3 * 2 + 4

=> 1 2 3 * 2 *+

Page 12: Infix to Postfix Conversion Using Stackwkhon/ds/ds11/codes/infix-to-postfix.pdf · 20 Bonus: Handling the Parenthesis Have to be done first! ... 21 Bonus: Handling the Parenthesis

12

Observation 3

1 + 2 * 3 * 2 + 4

=> 1 2 3 * 2 *+

Page 13: Infix to Postfix Conversion Using Stackwkhon/ds/ds11/codes/infix-to-postfix.pdf · 20 Bonus: Handling the Parenthesis Have to be done first! ... 21 Bonus: Handling the Parenthesis

13

Observation 3

1 + 2 * 3 * 2 + 4

=> 1 2 3 * 2 * +

Page 14: Infix to Postfix Conversion Using Stackwkhon/ds/ds11/codes/infix-to-postfix.pdf · 20 Bonus: Handling the Parenthesis Have to be done first! ... 21 Bonus: Handling the Parenthesis

14

Observation 3

1 + 2 * 3 * 2 + 4

=> 1 2 3 * 2 * +

+

Page 15: Infix to Postfix Conversion Using Stackwkhon/ds/ds11/codes/infix-to-postfix.pdf · 20 Bonus: Handling the Parenthesis Have to be done first! ... 21 Bonus: Handling the Parenthesis

15

Observation 3

1 + 2 * 3 * 2 + 4

=> 1 2 3 * 2 * + 4

+

Page 16: Infix to Postfix Conversion Using Stackwkhon/ds/ds11/codes/infix-to-postfix.pdf · 20 Bonus: Handling the Parenthesis Have to be done first! ... 21 Bonus: Handling the Parenthesis

16

Observation 3

1 + 2 * 3 * 2 + 4

=> 1 2 3 * 2 * + 4 +

Page 17: Infix to Postfix Conversion Using Stackwkhon/ds/ds11/codes/infix-to-postfix.pdf · 20 Bonus: Handling the Parenthesis Have to be done first! ... 21 Bonus: Handling the Parenthesis

17

Observation 3

* , /+ , -

Page 18: Infix to Postfix Conversion Using Stackwkhon/ds/ds11/codes/infix-to-postfix.pdf · 20 Bonus: Handling the Parenthesis Have to be done first! ... 21 Bonus: Handling the Parenthesis

18

Observation 4

2 ^ 3 ^ 4

= 2 ^ (3 ^ 4)

=> 2 3 4 ^ ^.................................How?

Page 19: Infix to Postfix Conversion Using Stackwkhon/ds/ds11/codes/infix-to-postfix.pdf · 20 Bonus: Handling the Parenthesis Have to be done first! ... 21 Bonus: Handling the Parenthesis

19

Observation 4

^ ^ ^* , /+ , -

Page 20: Infix to Postfix Conversion Using Stackwkhon/ds/ds11/codes/infix-to-postfix.pdf · 20 Bonus: Handling the Parenthesis Have to be done first! ... 21 Bonus: Handling the Parenthesis

20

Bonus: Handling the Parenthesis

Have to be done first!

5 * (2 + 3) + 1

Page 21: Infix to Postfix Conversion Using Stackwkhon/ds/ds11/codes/infix-to-postfix.pdf · 20 Bonus: Handling the Parenthesis Have to be done first! ... 21 Bonus: Handling the Parenthesis

21

Bonus: Handling the Parenthesis

5 * ((2 + 3) * 2) + 11

2