/
Data Structures and Algorithms Data Structures and Algorithms

Data Structures and Algorithms - PowerPoint Presentation

liane-varnes
liane-varnes . @liane-varnes
Follow
418 views
Uploaded On 2017-03-18

Data Structures and Algorithms - PPT Presentation

Prof Ajit A Diwan Prof Ganesh Ramakrishnan Prof Deepak B Phatak Department of Computer Science and Engineering IIT Bombay Session Infix to Postfix 1 Ajit A Diwan Ganesh Ramakrishnan and Deepak B Phatak IIT Bombay ID: 526055

append infix stack operator infix append operator stack ganesh deepak ajit bombay iit phatak ramakrishnan diwan push string postfix expression cde top

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Data Structures and Algorithms" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Data Structures and Algorithms

Prof. Ajit A. DiwanProf. Ganesh RamakrishnanProf. Deepak B. PhatakDepartment of Computer Science and EngineeringIIT BombaySession: Infix to Postfix

1

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT BombaySlide2

Infix to Postfix Expressions

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay2Operator is placed after all its operandsNo need of parentheses ExamplesInfix ExpressionsPostfix Expressions

a + b

a b +

(a + b) * (a - b)

a b + a b -

*

(a ^ b * (c + (d * e) - f ) ) / g

a

b ^ c d e * + f - * g /Slide3

Infix to Postfix using Stack

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay3Consider infix expressiona+b (Input string)Postfix expression will beab+ (Output string)We have 2 operands (a, b) and 1 operator (+)Use stack to facilitate the processPush operators on the stack based on some conditionsOutput String: Assemble operands and operators Slide4

Example 1: Implementation

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay4Infix elementOperator Stack

Postfix String

Comments

a

a

Append

‘a’

+

+

a

Push ‘+’

b

+

ab

Append ‘

b’ab+Append ‘+’

Infix Expression: a+bPush operator on stackAppend operands and operator to the string

Note: Operator in red color denotes the element at the top of the stackSlide5

Example 2: Implementation

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay5Infix elementOperator Stack

Postfix String

Comments

a

a

Append

‘a’

+

+

a

Push ‘+’

b

+

ab

Append

‘b’*+*abPush ‘*’

c

+

*

abc

Append

‘c’+abc*Append ‘*’abc*+Append ‘+’

Infix Expression: a+b*c

Note: Operator in red color denotes the element at the top of the stack

‘*’ has higher precedence than ‘+’Slide6

Example 3: Implementation

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay6Infix elementOperator Stack

Postfix String

Comments

a

a

Append ‘a’

*

*

a

Push ‘*’

b

*

ab

Append ‘b’

+

ab*Append ‘*’

+

ab*

Push ‘+’

c

+

ab*cAppend ‘c’ab*c+Append ‘+’Note: Operator in red color denotes the element at the top of the stackInfix Expression: a*b+c

‘+’ has lower precedence than ‘*’Slide7

Example 4: Implementation

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay7Note: Operator in red color denotes the element at the top of the stackInfix Expression: (a^b*(c+(d*e-f))) Infix String

Operator Stack

Postfix String

Comments

(

(

Push ‘(‘

a

(

a

Append ‘a’

^

(

^

a

Push ‘^’b(^

ab

Append ‘b’

*

(

ab^

Append ‘^’(*Push ‘*’(

(*(

ab^

Push ‘(‘

c

(*

(

ab^c

Append ‘c’Slide8

Example 4: Implementation

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay8Note: Operator in red color denotes the element at the top of the stackInfix Expression: (a^b*(c+(d*e-f))) Infix String

Operator Stack

Postfix String

Comments

+

(*(

+

ab^c

Push ‘+’

(

(*(+

(

ab^c

Push ‘(‘

d

(*(+(ab^cdAppend ‘d’*(*(+(*

ab^cd

Push ‘*‘

e

(*(+(

*

ab^cdeAppend ‘e’-(*(+(ab^cde*Append ‘*’

(*(+(

-

ab^cde

*

Push ‘-’Slide9

Example 4: Implementation

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay9Note: Operator in red color denotes the element at the top of the stackInfix Expression: (a^b*(c+(d*e-f))) Infix String

Operator Stack

Postfix String

Comments

f

(*(+(

-

ab^cde

*f

Append ‘f’

)

(*(+(

ab^cde

*f-

Append ‘-’

(*(+Pop ‘(‘)

(*(

ab^cde

*f-+

Append ‘+’

(

*Pop ‘(‘)(ab^cde*f-+*Append ‘*’

Pop ‘(‘Slide10

References

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay10https://en.wikipedia.org/wiki/Stack_(abstract_data_type)http://csis.pace.edu/~wolf/CS122/infix-postfix.htmSlide11

Thank you

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay11