Conversion of Infix To Postfix Expressions


Published on

Infix To Postfix conversion and evaluation of postfix expression

Published in: Education, Technology
  • it is very helpful for me
    Are you sure you want to  Yes  No
    Your message goes here
  • A great article on 'Infix to Postfix Conversion' with Visuals and Examples at the below link:
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Conversion of Infix To Postfix Expressions

  1. 1. Stacks Application Areas of Stacks
  2. 2. <ul><li>When functions are called. </li></ul><ul><li>To convert a infix expression to postfix. </li></ul><ul><li>To evaluate a postfix expression. </li></ul>Stacks are used : Application Areas of Stacks
  3. 3. Algorithm to convert INFIX to POSTFIX expression
  4. 4. Postfix Expression involving LOGICAL OPERATORS/CONSTANTS <ul><li>LOGICAL OPERATORS </li></ul><ul><li>NOT : Complementation (Unary Operator) (HIGHEST PRIORITY) </li></ul><ul><ul><li>Result is TRUE if operand is FALSE and vice versa. </li></ul></ul><ul><li>AND : Logical multiplication (PRIORITY LESS THAN NOT </li></ul><ul><ul><li>Result is TRUE if both operands are TRUE otherwise FALSE hi </li></ul></ul><ul><li>OR : Logical Addition (LOWEST PRIORITY) </li></ul><ul><ul><li>Result is FALSE if both operands are FALSE otherwise TRUE </li></ul></ul><ul><li>LOGICAL CONSTANTS </li></ul><ul><li>YES / TRUE </li></ul><ul><li>NO / FALSE </li></ul>Just For Reference
  5. 5. Q= A*(B+(C+D)*(E+F)/G)*H Convert INFIX Expression into POSTFIX Expression Following the algorithm showing stack status at each step ) Extra Bracket added 22 H 21 * 20 ) 19 G 18 / 17 ) 16 F 15 + 14 E 13 ( 12 * 11 ) 10 D 9 + 8 C 7 ( 6 + 5 B 4 ( 3 * 2 A 1 ( P (Postfix Expression) Description Stack Status Input Element STEP
  6. 6. Evaluation of POSTFIX Expression <ul><li>Postfix Expression is without parenthesis. </li></ul><ul><li>Postfix Expression has only Operands and Operators </li></ul><ul><li>Stack is used to hold operand/intermediate results (STACK APPLICATION) </li></ul><ul><li>Expression Evaluated from left to right </li></ul>Some points to remember regarding POSTFIX expressions:
  7. 7. Evaluation of POSTFIX Expression (Algorithm) <ul><li>Let P be the expression in POSTFIX notation </li></ul><ul><li>Expression is Evaluated from left to right </li></ul><ul><li>Add ; at the end to mark end of expression P </li></ul><ul><li>Scan the postfix expression from left to right taking one element at a time </li></ul><ul><li>While (element !=‘;’) repeat the steps 2.1 and 2.2 </li></ul><ul><ul><li>2.1 if (element = Operand ) it is PUSHed in STACK </li></ul></ul><ul><ul><li>2.2 If (element == Operator ) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><ul><li>POP first element from TOP call it A </li></ul></ul></ul><ul><ul><ul><li>POP second element from TOP call it B </li></ul></ul></ul><ul><ul><ul><li>Perform OPERATION B operator A </li></ul></ul></ul><ul><ul><ul><li>PUSH Result in STACK </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>Pop the value from stack TOP which is result of the expression </li></ul>{ {
  8. 8. Assignment Stack Applications
  9. 9. post fix , infix notation (2 marks) <ul><li>Give postfix form of the following expression: </li></ul><ul><li>(i) A*(B+(C+D)*(E+F)/G)*H </li></ul><ul><li>(ii) A+[(B+C)*(D+E)*F]/G </li></ul><ul><li>(iii) A*(B+D)/E-F-(G+H/K) </li></ul><ul><li>(iv) ((A-B)*(D/E))/(F*G*H) </li></ul><ul><li>(v) (True && false) || !(false||true) </li></ul>
  10. 10. post fix , infix notation (2 marks) <ul><li>Evaluate the following postfix expression using a stack and show the Contents of stack after each step: </li></ul><ul><li>(i) 50,40,+, 18,14,-,4,*,+ </li></ul><ul><li>(ii) 100,40,8,+,20,10,-,+,* </li></ul><ul><li>(iii) 5,6,9,+,80,5,*,-,/ </li></ul><ul><li>(iv) 120,45,20,+,25,15,-,+,* </li></ul><ul><li>(v) 20,45,+,20,10,-,15,+,* </li></ul><ul><li>(vi) TRUE,FALSE, TRUE FALSE, NOT, OR, TRUE , OR,OR,AND </li></ul>
  11. 11. post fix , infix notation (2 marks) <ul><li>3. Write the equivalent infix expression for : </li></ul><ul><ul><ul><li>i. 10,3,*,7,1,-,*,23,+ </li></ul></ul></ul><ul><ul><ul><li>ii. /+a*bc-c*db </li></ul></ul></ul><ul><ul><ul><li>iii. abc*+cdb*-/ </li></ul></ul></ul>
  12. 12. The End