Department of Technical Education Andhra Pradesh Name Designation Branch Institute Year/Semester Subject Sub.Code Topic Duration Subtopic Teaching aids Revised by : B.Vijaya Kumari : Lecturer : Computer Engineering : Smt.B.Seetha Polytechinic, Bhimavaram : III SEM : Data Structures THROUGTH C : 9CM305 : Stacks & Queues : 50 Mins : Evaluation of Postfix Expression. : ppt, Animations, Photographs : Bapuji naik 9CM305.26 1 Recap • Last class we have discussed about and Infix, Prefix, Postfix expressions and conversion of Infix to Postfix notation 9CM305.26 2 Objective On completion of this class, you would be able to learn • Evaluation of Postfix expression 9CM305.26 3 Recap • Infix Expression: The Operators are placed between the Operands • Example: A + B • A,B are Operands and + is Operator • + is placed between A&B 9CM305.26 4 Contd.. Recap Postfix Expression: The Operator s are placed after the Operands. also known as reverse Polish notation • Example: AB+ • Here A&B are Operands and + is an Operator • + is placed after the Operands 9CM305.26 5 Contd.. Recap • Prefix Expression: The Operators are placed before the Operands is known as Prefix Expression. And also known as Polish notation • Example: + AB Here A&B are Operands and + is an Operator + is placed before the Operand 9CM305.26 6 Contd.. Recap Evaluation of postfix expression • Postfix form can be evaluated by reading the expression from left to right. • The operands are stored in a Stack. • The arithmetic expressions to evaluate the Postfix expression using Stack. 9CM305.26 7 Algorithm to Evaluate Postfix Expression • Step 1: Start • Step 2: Scanning the Postfix expression from left to right when an operand is encountered then push the value of operand into Stack. • Step 3: The Scanned character is an operator then pop the two operands from top of the Stack. 9CM305.26 8 Algorithm to Evaluate Postfix Expression • Step 4: Perform the operation on the operands • Step 5: The result is pushed into the Stack. • Step 6: This process is continued until the Postfix expression is completed. 9CM305.26 9 Example for Evaluation of Postfix Expression Example : AB+C*. where A=7,B=4,C=2. • Step 1: • Step 2: Start Scanning the Postfix expression AB+C* from left to right first an operand “A” is encountered then push the value of “A” i.e. 7 onto the Stack. Empty Push A=7 7 9CM305.26 10 Example for Evaluation of Postfix Expression Contd., • Step 3: The scanned character is an operand then push “B” value into the Stack. • Step 4: The scanned character is an arithmetic operator i.e “+”. pop A,B values into the Stack and perform an operation using that operator. i.e. 7+4=11. Push B=4 4 7 9CM305.26 11 Example for Evaluation of Postfix Expression Contd., • Step 5: The result will be pushed into the Stack. • Step 6: Next the scanned character is an operand i.e., C then push C value into the Stack. • Step7: Next the scanned character is an operator i.e “+” then pop the values into the stack and perform the operation. Push result=11 Push c=2 2 11 11 9CM305.26 12 Example for Evaluation of Postfix Expression Contd., • Step 8: The operation “*” is performed on the two operands. i.e. 11*2=22. • Step 9: The result will be pushed into the Stack. • Step 10: Pop the result from the stack The process is completed. • Step 11: Stop. Push c=22 Result :11*2=22 22 9CM305.26 13 Example for Evaluation of Postfix Expression Items Stack 7 7 4 7 4 + 11 2 11 2 * Contd., 22 9CM305.26 14 Contd., Examples on Evaluation of Postfix Expression. 1 The stack during the evaluation of the postfix expression a b / when a is 2 and b is 4 9CM305.26 15 2 The stack during the evaluation of the postfix expression a b + c / when a is 2, b is 4 and c is 3 9CM305.26 16 3 9CM305.26 17 Contd., Quiz 1.A+B+C is an a) Infix Expression b) Prefix Expression c) Postfix Expression d) None 9CM305.26 18 Contd., Quiz 2. When an operator enconntered while evaluating a postfix expression (a) Push the operands into the stack (b) Pop the operands from the stack (c) Both a&b (d) None 9CM305.26 19 Contd., Quiz 3. Postfix expression means a) The operators are placed between operands b) The operators are placed after the operands c) The operators are placed before the operands d) Both b&c 9CM305.26 20 Contd., Quiz 4.Prefix expression means a) The operators are placed between operands b) The operators are placed after the operands c) The operators are placed before the operands d) Both b&c 9CM305.26 21 Contd., Quiz 5.+AB is a a) Infix Expression b) Prefix Expression c) Postfix Expression d) None 9CM305.26 22 Contd., Quiz 6.AB+C* is a a) Infix Expression b) Prefix Expression c) Postfix Expression d) None 9CM305.26 23 Contd., Quiz 7.Infix expression means a) The operators are placed between operands b) The operators are placed after the operands c) The operators are placed before the operands d) Both b&c 9CM305.26 24 Frequently Asked Questions • Explain how to evaluate a postfix expression • For the values of a=5, b=3, c=4 &d=2; Evaluate the following postfix expression ab-cd*/ • Write an algorithm to evaluate a postfix expression 9CM305.26 25 9CM305.26 26