IIT Bombay Data Structures and Algorithms Prof Ajit

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

Evaluating Postfix Expressions IIT Bombay Example 1 (a=5, b=3) a b + a b - * 5 3 + 5 3 - * 8 2 * 16 (Answer) Example 2 (a=4, b=2, c=5, d=6, e=7, f=8, g=3) a b ^ c d e * + f - * g / 4 2 ^ 5 6 7 * + 8 - * 3 / 16 5 42 + 8 - * 3 / 16 47 8 - * 3 / 16 39 * 3 / 624 3 / 208 (Answer) Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 2

Postfix expression evaluation using Stack IIT Bombay • Consider postfix expression • abc*+ (Input string) • Values are • a=5, b=6, c=7 • We have 3 operands (a, b, c) and 2 operators (*, +) • Examine each character in the postfix expression • If it is an operand, push its value on the stack • If it is an operator, perform the operation on the top 2 elements of the stack • Push the result on the stack Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 3

Example 3: abc*+ IIT Bombay Infix: a+b*c Postfix: abc*+ Operand 2 1 (a = 5, b=6, c=7) Postfix element Stack a 5 Push value of ‘a’ on stack b 5, 6 Push value of ‘b’ on stack c 5, 6, 7 Push value of ‘c’ on stack Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay Result Comments 4

Example 3: abc*+ IIT Bombay Infix: a+b*c Postfix element * Postfix: abc*+ (a = 5, b=6, c=7) Stack Operand 2 Operand 1 5, 6 7 5 7 6 6*7=42 5, 42 7 6 42 Result Comments (It is an operator) operand 2 is assigned the value from top of stack Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay operand 1 is assigned the value from top of stack Multiply operand 1 and operand 2 Push the result on the stack 5

Example 3: abc*+ IIT Bombay Infix: a+b*c Postfix element Postfix: abc*+ Stack 5 + 47 (a = 5, b=6, c=7) Operand 2 1 Result Comments (It is an operator) 42 6 42 operand 2 is assigned the value from top of stack 42 5 42 operand 1 is assigned the value from top of stack 42 5 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 5+42=47 Add operand 1 and operand 2 47 Push the result on the stack 6

Example 4: ab*c+ IIT Bombay Infix: a*b+c Postfix: ab*c+ (a = 5, b=6, c=7) Postfix element Stack a 5 Push value of ‘a’ on stack b 5, 6 Push value of ‘b’ on stack 5 * 30 Operand 2 1 Result Comments 6 operand 2 is assigned the value from top of stack 6 5 operand 1 is assigned the value from top of stack 6 5 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 5*6=30 Multiply operand 1 and operand 2 30 Push the result on the stack 7

Example 4: ab*c+ IIT Bombay Infix: a*b+c Postfix: ab*c+ (a = 5, b=6, c=7) Postfix element Stack c 30, 7 6 5 30 Push value of ‘c’ on stack 30 7 5 30 operand 2 is assigned the value from top of stack 7 30 30 operand 1 is assigned the value from top of stack 7 30 + 37 Operand 2 1 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay Result Comments 30+7=37 Add operand 1 and operand 2 37 Push the result on the stack 8

Example 5: ab^cde*f-+* IIT Bombay Infix: (a^b*(c+(d*e-f))) Postfix: ab^cde*f-+* (a=4, b=2, c=6, d=7, e=8, f=9) Postfix element Stack a 4 b 4, 2 ^ Operand 2 Operand 1 Result 2 4 4^2=16 16 c 16, 6 d 16, 6, 7 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 9

Example 5: ab^cde*f-+* IIT Bombay Infix: (a^b*(c+(d*e-f))) Postfix: ab^cde*f-+* (a=4, b=2, c=6, d=7, e=8, f=9) Postfix element Stack Operand 2 Operand 1 Result e 16, 6, 7, 8 * 16, 6, 56 8 7 7*8=56 f 16, 6, 56, 9 - 16, 6, 47 9 56 56 -9=47 + 16, 53 47 6 6+47=53 * 848 53 16 16*53=848 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 10

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