FAST- National University of Computer and Emerging Sciences CFD Campus Data Structures and Algorithm Application of Stack Asma Sattar Asma.sattar@nu.edu.pk Lecture No. 7 Today’s Lecture • • • • • Algebraic Expression Infix to Prefix Prefix Evaluation Infix to Postfix Postfix Evaluation Algebraic Expression • An algebraic expression is a legal combination of operands and the operators. – Operand is the quantity on which a mathematical operation is performed. – Operator is a symbol which signifies a mathematical or logical operation. Infix, Postfix and Prefix Expressions • INFIX: expressions in which operands surround the operator. • POSTFIX: operator comes after the operands, also Known as Reverse Polish Notation (RPN). • PREFIX: operator comes before the operands, also Known as Polish notation. • Example – Infix: A+B-C Postfix: AB+C- Prefix: +A-BC Why do we need PREFIX/POSTFIX? • Appearance may be misleading, INFIX notations are not as simple as they seem • To evaluate an infix expression we need to consider – Operators’ Priority – Associative property – Delimiters Why do we need PREFIX/POSTFIX? • Infix Expression Is Hard To Parse and difficult to evaluate. • Postfix and prefix do not rely on operator priority and are easier to parse. Why do we need PREFIX/POSTFIX? • An expression in infix form is thus converted into prefix or postfix form and then evaluated without considering the operators priority and delimiters. Examples of infix to prefix and post fix Infix PostFix Prefix A+B AB+ +AB (A+B) * (C + D) AB+CD+* *+AB+CD Prefix example: 2+3*4-5/6^(2-1*3) Prefix= -+2*34/5^6-2*13 (solve on board) Prefix Task: 2*5^6/3 Prefix=/*2^563 Postfix example: 2+3*4-5/6^(2-1*3) Postfix= 234*+56231*-^/- Postfix Task: f^(g-h)+(j-k) Postfix= F g h - ^ j k -+ Algorithm for Infix to postfix conversion Algorithm: Q is the given infix expression & we want P. 1. Push “(“ on STACK and add “)” at the end of Q. 2. Scan Q from left to right and repeat steps 3 & 6 for each element of Q until the STACK is empty. 3. If an operand is encountered, add it to P 4. If a left parenthesis is encountered, push it onto STACK. 5. If an operator X is encountered, then: a. b. 6. If a right parenthesis is encountered, then: a. b. 7. Repeatedly pop from STACK and add to P each operator which has same or higher precedence than X Push X on STACK Repeatedly pop from STACK and add to P each operator until a left parenthesis is encountered Remove the left parenthesis. [Do not add it to P] Exit Infix to Postfix ((A+B)*(C-E))/(F+G) stack: <empty> output: [] Infix to Postfix ((A+B)*(C-E))/(F+G)) stack: ( output: [] Infix to Postfix (A+B)*(C-E))/(F+G)) stack: ( ( output: [] Infix to Postfix A+B)*(C-E))/(F+G)) stack: ( ( ( output: [] Infix to Postfix +B)*(C-E))/(F+G)) stack: ( ( ( output: [A] Infix to Postfix B)*(C-E))/(F+G)) stack: ( ( ( + output: [A] Infix to Postfix )*(C-E))/(F+G)) stack: ( ( ( + output: [A B] Infix to Postfix *(C-E))/(F+G)) stack: ( ( output: [A B + ] Infix to Postfix (C-E))/(F+G)) stack: ( ( * output: [A B + ] Infix to Postfix C-E))/(F+G)) stack: ( ( * ( output: [A B + ] Infix to Postfix -E))/(F+G)) stack: ( ( * ( output: [A B + C ] Infix to Postfix E))/(F+G)) stack: ( ( * ( output: [A B + C ] Infix to Postfix ))/(F+G)) stack: ( ( * ( output: [A B + C E ] Infix to Postfix )/(F+G)) stack: ( ( * output: [A B + C E - ] Infix to Postfix /(F+G)) stack: ( output: [A B + C E - * ] Infix to Postfix (F+G)) stack: ( / output: [A B + C E - * ] Infix to Postfix F+G)) stack: ( / ( output: [A B + C E - * ] Infix to Postfix +G)) stack: ( / ( output: [A B + C E - * F ] Infix to Postfix G)) stack: ( / ( + output: [A B + C E - * F ] Infix to Postfix )) stack: ( / ( + output: [A B + C E - * F G ] Infix to Postfix ) stack: ( / output: [A B + C E - * F G + ] Infix to Postfix stack: <empty> output: [A B + C E - * F G + / ] Task infix (a+b-c)*d–(e+f) postfixt Evaluating a postfix expression Algorithm: P is the given postfix expression. 1. Add a right parenthesis “)” at the end of P. [acts as a sentinel] 2. Scan P from left to right and repeat steps 3 & 4 for each element of P until “)” is encountered. 3. If an operand is encountered, push it on STACK 4. If an operator is encountered, then: a. b. c. 5. 6. Pop two operands from STACK: A & B Evaluate: B operator A Push result on STACK Set value equal to the top element on STACK Exit From Postfix to Answer • The reason to convert infix to postfix expression is that we can compute the answer of postfix expression easier by using a stack. From Postfix to Answer Ex: 10 2 8+* 3 • First, push(10) into the stack 10 From Postfix to Answer Ex: 10 + 3 28* • Then, push(2) into the stack 2 10 From Postfix to Answer Ex: 10 2 8 * + 3 • Push(8) into the stack 8 2 10 From Postfix to Answer Ex: 10 + 3 28* • Now we see an operator *, that means we can get an new number by calculation 8 2 10 From Postfix to Answer Ex: 10 2 8 * + 3 • Now we see an operator *, that means we can get an new number by calculation 8 • Pop the first two numbers 2 10 2 * 8 = 16 From Postfix to Answer Ex: 10 2 8 * + 3 • Now we see an operator *, that means we can get an new number by calculation • Push the new number back 16 10 2 * 8 = 16 From Postfix to Answer Ex: 10 2 8 * + 3 • Then we see the next operator + and perform the calculation 16 10 10 ++ 16 = 26 From Postfix to Answer Ex: 10 2 8 * + 3 • Then we see the next operator + and perform the calculation • Push the new number back 26 10 + 16 = 26 From Postfix to Answer Ex: 10 + 3 28* • We see the next number 3 • Push (3) into the stack 3 26 Compute the Answer Ex: 10 2 8 * + 3 • The last operation 26 - 3 = 23 From Postfix to Answer Ex: 10 2 8 * + 3 • The last operation 23 26 - 3 = 23 answe r! Infix to Prefix Conversion Move each operator to the left of its operands & remove the parentheses: ( ( A + B) * ( C + D ) ) Infix to Prefix Conversion Move each operator to the left of its operands & remove the parentheses: (+A B *(C+D)) Infix to Prefix Conversion Move each operator to the left of its operands & remove the parentheses: *+A B (C+D) Infix to Prefix Conversion Move each operator to the left of its operands & remove the parentheses: *+A B +C D Order of operands does not change! Algorithm for Infix to prefix conversion Algorithm: Q is the given infix expression & we want P. 1. Push “)“ on STACK and add “(” at the start of Q. 2. Scan Q from right to left and repeat steps 3 & 6 for each element of Q until the STACK is empty. 3. If an operand is encountered, add it to P 4. If a right parenthesis is encountered, push it onto STACK. 5. If an operator X is encountered, then: a. Repeatedly pop from STACK and add to P each operator which has same or higher precedence than X b. Push X on STACK 6. If a left parenthesis is encountered, then: a. Repeatedly pop from STACK and add to P each operator until a left parenthesis is encountered b. Remove the left parenthesis. [Do not add it to P] 7. Exit Example f^(g-h)+(j-k) Q = (f^ (g-h) +(j-k) Stack: ) P: Scan right to left Q = (f^ (g-h) +(j-k Stack: ) ) P: Scan right to left Q = (f^ (g-h) +(jStack: )) P:k Scan right to left Q = (f^ (g-h) +(j Stack: ))P:k Scan right to left Q = (f^ (g-h) +( Stack: ))P:jk Scan right to left Q = (f^ (g-h) +( Stack: ))P:-jk Scan right to left Q = (f^ (g-h) Stack: )+ P:-jk Scan right to left Q = (f^ (g-h Stack: )+) P:-jk Scan right to left Q = (f^ (gStack: )+) P:h-jk Scan right to left Q = (f^ (g Stack: )+)P:h-jk Scan right to left Q = (f^ ( Stack: )+)P:gh-jk Scan right to left Q = (f^ ( Stack: )+)P:-gh-jk Scan right to left Q = (f Stack: )+^ P:-gh-jk Scan right to left Q=( Stack: )+^ P:f-gh-jk Scan right to left Q=( Stack: )+^ P:+^f-gh-jk Evaluating a pretfix expression Algorithm: P is the given prefix expression. 1. Add parenthesis “(” at the start of P. [acts as a sentinel] 2. Scan P from right to left and repeat steps 3 & 4 for each element of P until “)” is encountered. 3. If an operand is encountered, push it on STACK 4. If an operator is encountered, then: 5. 6. a. b. c. Pop two operands from STACK: A & B Evaluate: A operator B Push result on STACK Set value equal to the top element on STACK Exit Reading Material • Nell Dale – Chapter#4 • Schaum’s Outlines – Chapter#6 • D. S. Malik – Chapter#7 • http://www.cs.man.ac.uk/~pjj/cs2121/fix.html • Examples – – – animations\in2post\infixtopostfix.exe animations\evaluation\evaluation.exe animations\tower\tower.exe CS 201 - Fall 2013 Algorithm Infix To Postfix • Assumptions: 1. Space delimited list of tokens represents a infix expression 2. Operands are single characters. 3. Operators +,-,*,/