Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures and Algorithms

Similar presentations


Presentation on theme: "Data Structures and Algorithms"— Presentation transcript:

1 Data Structures and Algorithms
Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering IIT Bombay Session: Infix to Postfix Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

2 Infix to Postfix Expressions
Operator is placed after all its operands No need of parentheses Examples Infix Expressions Postfix 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 / Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

3 Infix to Postfix using Stack
Consider infix expression a+b (Input string) Postfix expression will be ab+ (Output string) We have 2 operands (a, b) and 1 operator (+) Use stack to facilitate the process Push operators on the stack based on some conditions Output String: Assemble operands and operators Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

4 Example 1: Implementation
Infix Expression: a+b Push operator on stack Append operands and operator to the string Infix element Operator Stack Postfix String Comments a Append ‘a’ + Push ‘+’ b ab Append ‘b’ ab+ Append ‘+’ Note: Operator in red color denotes the element at the top of the stack Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

5 Example 2: Implementation
Infix Expression: a+b*c Infix element Operator Stack Postfix String Comments a Append ‘a’ + Push ‘+’ b ab Append ‘b’ * +* Push ‘*’ c abc Append ‘c’ abc* Append ‘*’ abc*+ Append ‘+’ ‘*’ has higher precedence than ‘+’ Note: Operator in red color denotes the element at the top of the stack Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

6 Example 3: Implementation
Infix Expression: a*b+c Infix element Operator Stack Postfix String Comments a Append ‘a’ * Push ‘*’ b ab Append ‘b’ + ab* Append ‘*’ Push ‘+’ c ab*c Append ‘c’ ab*c+ Append ‘+’ ‘+’ has lower precedence than ‘*’ Note: Operator in red color denotes the element at the top of the stack Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

7 Example 4: Implementation
Infix Expression: (a^b*(c+(d*e-f))) Infix String Operator Stack Postfix String Comments ( Push ‘(‘ a Append ‘a’ ^ (^ Push ‘^’ b ab Append ‘b’ * ab^ Append ‘^’ (* Push ‘*’ (*( c ab^c Append ‘c’ Note: Operator in red color denotes the element at the top of the stack Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

8 Example 4: Implementation
Infix Expression: (a^b*(c+(d*e-f))) Infix String Operator Stack Postfix String Comments + (*(+ ab^c Push ‘+’ ( (*(+( Push ‘(‘ d ab^cd Append ‘d’ * (*(+(* Push ‘*‘ e ab^cde Append ‘e’ - ab^cde* Append ‘*’ (*(+(- Push ‘-’ Note: Operator in red color denotes the element at the top of the stack Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

9 Example 4: Implementation
Infix 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 ‘+’ (* ( ab^cde*f-+* Append ‘*’ Note: Operator in red color denotes the element at the top of the stack Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

10 References https://en.wikipedia.org/wiki/Stack_(abstract_data_type)
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

11 Thank you Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay


Download ppt "Data Structures and Algorithms"

Similar presentations


Ads by Google