(Part 2) Infix, Prefix & Postfix Chapter 4: STACK (Part 2) Infix, Prefix & Postfix
Arithmetic Expressions Arithmetic expressions have: operands (variables or numeric constants). Operators Binary : +, -, *, / ,% Unary: - Priority convention: *, /, % have medium priority +, - have lowest priority
Infix, Prefix, Postfix Example: arithmetic expression a + b consists of operands a, b and operator +. Infix notation Is format where operator is specified in between the two operands. a+b Prefix notation Is format where operator is specified before the two operands. + a b Postfix notation Is format where operator is specified after the two operands. Postfix notation is also called RPN or Reverse Polish Notation. a b +
Why use Prefix and Postfix? To avoid ambiguous Infix notation requires precedence and associativity rules to disambiguate it, or addition of extra parentheses that are not usually considered part of the notation. Therefore, as long as the number of arguments to each operator are known in advance, both prefix and postfix notation are entirely unambiguous Example : "* + 5 6 3" is (5+6)*3, and cannot be interpreted as 5+(6*3), whereas parenthesis is required to achieve with infix.
Converting arithmetic expressions Example: Conversion from infix arithmetic expression to prefix and postfix. Infix Notation Prefix Notation Postfix Notation A + B * C +A * B C A B C * + (A+B) * C * + A B C A B + C * A – B + C + – A B C A B – C + A – (B+C) – A + B C A B C + –
Application of Stack 1:Infix to Postfix This application converts the infix notation of a given arithmetic expression into postfix notation. In an infix notation the operator is placed in between the operands : a+b In a postfix notation the operator is placed immediately after the operand: ab+
Infix to Postfix Conversion (Method 1) There are 3 rules or algorithm in infix to postfix conversion Rules (R) for using Stack to change infix to postfix notation: R1 R2 R3
Infix to Postfix Conversion (Method 1) R1: Initially, the operatorStack object is empty. R2: For each operator in the infix string, loop until the operator has been pushed onto the operatorStack object: if the operatorStack object is empty OR the operator has higher precedence than the operator on the top of the operatorStack object then Push the operator onto the operatorStack object. else Pop the operatorStack object and append that popped operator to the postfix string ** NOTE: Operatorstack -> * + -
Infix to Postfix Conversion (Method 1) R3: Once the end of the input string is encountered, Loop until the operatorStack object is empty Pop the operatorStack object and append that popped operator to the postfix string
Example: Infix to Postfix Example 1: Infix notation A - B STEPS INFIX OPERATOR STACK POSTFIX 1 A empty 2 - 3 B AB 4 AB-
Example: Infix to Postfix Example 2: Infix notation A + B - C STEPS INFIX OPERATOR STACK POSTFIX 1 A empty 2 + 3 B AB 4 - AB+ 5 C AB+C 6 AB+ C -
Example of Infix to Postfix Example 3: Infix notation A + B * C STEPS INFIX OPERATOR STACK POSTFIX 1 A empty 2 + 3 B AB 4 * + * 5 C ABC 6 ABC * 7 ABC *+
Example Infix to Postfix Convert this infix notation to postfix notation a + c – r / b * r
Solution STEPS INFIX OPERATOR STACK POSTFIX a empty + c ac - ac + r 1 a empty 2 + 3 c ac 4 - ac + 5 r ac + r 6 / - / 7 b ac + rb 8 * - * ac + rb / 9 ac + rb / r 10 ac + rb / r * 11 ac + rb / r * -
Parentheses in Infix notation How are parentheses handled when converting an infix notation to postfix notation? When a left parentheses “(“ is found in the infix string, it is immediately pushed onto the operatorStack object But its precedence is defined to be lower than any other binary operator
Parentheses in Infix notation (cont) When a right parentheses “)” is found in the infix string, the operator object is repeatedly popped, and the popped element appended to the postfix string, until the operator on the top of the operatorStack object is the left paretheses “(“ Then the left parentheses “(“ is popped but not appended to the postfix string and the scan of the infix string is continued.
Parentheses in Infix notation (cont) Example 5 Convert this infix notation to postfix notation x – (y * a / b – (z + d * e) + c) / f
Solution x – (y * a / b – (z + d * e) + c) / f Infix operatorStack Postfix x empty - ( -( y xy * -( * a xya / -( / xya * b xya * b -( - xya * b/ -( -( z -( -( xya * b/z
Solution (cont..) x – (y * a / b – (z + d * e) + c) / f INFIX OPERATORSTACK POSTFIX + -( -( + xya * b/z d -( - ( + xya * b/zd * -( - ( + * e xya * b/zde ) -( - xya * b/zde *+ -( + xya * b/zde *+- c xya * b/zde *+ -c - xya * b/zde *+ -c + / - / f xya * b/zde *+ -c + f xya * b/zde *+- c + f /
Solution (cont..) INFIX OPERATORSTACK POSTFIX - xya * b/zde *+- c + f / empty xya * b/zde *+-c+ f /-
Infix to Postfix Conversion (Method 2) Example: Infix notation A + B – C (A B + ) - C A B + C -
Infix to Postfix Conversion (Method 2) Example: Infix notation A + B * C A + (B C *) A B C * +
Infix to Postfix Conversion (Method 2) Example: Infix notation a + c – r / b * r a + c – (r b /) * r a + c – (r b / r *) (a c +) – (r b / r * ) a c + r b / r * -
Exercise Translate the following expressions into postfix notation a – b + c * d a + c – h / b * r (x + y) * z a + (c – h) / (b * r) x – (y * ( z + d * e) + c) / f
Application of Stack 2: Infix to Prefix Conversion It is the reverse process of converting an infix to postfix notation The saving of operand and operators is easily done with the help of 2 Stacks: operandStack operatorStack The precedence rules for the operatorStack object are exactly the same as converting infix to postfix
Infix to Prefix Conversion (Method 1) Algorithm (infix to prefix) When an operand is found in the infix string, the operand is pushed onto the operandStack object When an operator found, it is pushed onto the operatorStack object, if the stack is empty. Otherwise one of the following case applies: If the operator is left parentheses, push it onto the operatorStack object (but the left parentheses has the lowest precedence)
Infix to Prefix Conversion (Method 1) If the operator has a higher precedence than the top operator on the operatorStack object, push the operator onto the operatorStack object If the operator’s precedence is equal to, or lower than the precedence of the top operator on the operatorStack object, pop the operator precedence, opt1, from the operatorStack object and pop two operands, opnd1 and opnd2, from the operandStack object. Join together opt1, opnd2 and opnd1 and push the result onto the operandStack object
Infix to Prefix Conversion (Method 1) If the operator is a right parentheses treat is as having lower priority than +, -, *, /. Case 3 will applied until left parentheses is the top operator on the operatorStack object. Then pop that left parentheses. This process continues until we reach the end of the infix expression.
Infix to Prefix Conversion (Method 1) Repeat the following actions from case 3 until the operatorStack object is empty: Pop opt1 from the operatorStack object Pop opnd1 and opnd2 from the operandStack object Join together opt1, opnd2 and opnd1 and push the result onto the operandStack object When operatorStack is finally empty, the top (and only) operand on operandStack will be the prefix string corresponding to the original infix expression
Infix to Prefix Conversion (Method 1) Example 1: Convert infix notation to prefix notation a + b * c
Solution Infix operandStack operatorStack a empty + b * c * bc + a * bc
Infix to Prefix Conversion (Method 1) Example 2: Convert infix notation to prefix notation (a – b) * c
Solution Infix operandStack operatorStack ( a - b ) - ab * c ab
Infix to Prefix Conversion (Method 1) Example 3: Convert infix notation to prefix notation a + (c – h) / (b * d)
Solution Infix operandStack operatorStack a + ( c - h
Solution Infix operandStack operatorStack ) - ch a ( + / b ch
Solution Infix operandStack operatorStack * b - ch a ( / + d ) *bd ch
Solution Infix operandStack operatorStack /-ch*bd a + + a / - ch *bd
Infix to Prefix Conversion (Method 2) Convert infix notation to prefix notation (a – b) * c (- a b) * c * - a b c
Infix to Prefix Conversion (Method 2) Convert infix notation to prefix notation a + (c – h) / (b * d) a + (- c h) / ( b * d) a + (- c h) / ( * b d) a + (/ - c h * b d) + a / - c h * b d
Exercise Translate the following expressions into prefix notation a – b + c * d (k + l)/(m-n) a – (b + c * d) / e a + (c – h) / (b * d)
Evaluation of an expression An expression in postfix notation can be evaluated at run-time by means of a stack For example, we might have the following postfix expression that consists of integer values and binary operators only: 8 5 4 + * 7 – The final result, 65, lies on the top of the stack at the end of the calculation
Solution Postfix Stack Expression 4 5 8 5+4 9 8*9 72 7 72-7 65 8 5 4 + * 7 – 4 5 8 5+4 9 8*9 72 7 72-7 65
Cont… The evaluation proceeds as follows: When a value is encountered, it is pushed onto the stack When the operator is encountered, first and second elements on the stack are retrieved and popped, the operator is applied Note that the second element is the left operand, the first element is the right operand. The result is pushed onto the stack When the postfix expression has been processed, the value of that expression is the top (and only) element on the stack
EVALUATING EXPRESSION USING STACK(method 2)
EVALUATING EXPRESSION USING STACK (method 2)
Method 2 Evaluate the following expression using stack 8 5 4 + * 7 – 8 5 4 + * 7 – STEP 1 STEP 2 STEP 3 STEP 4 STEP 5 STEP 6 RESULT At Top stack 4 Second lowest stack 5 9 7 At the Lowest stack 8 72 65 + * -
EVALUATING EXPRESSION USING STACK (Method 3) POSTFIX: 8 6 * 8 4 + / 5 + POSTFIX: 8 6 * 8 4 + / 5 + 3) Push 12 Pop 12 Pop 48 evaluate 48 / 12 = 4 4) Push 4 Push 5 Pop 5 Pop 4 evaluate 4+5 = 9 5) Push 9 Pop 9 Null The result is 9 1) Push 8 Push 6 Pop 6 Pop 8 evaluate 8 * 6 = 48 2) Push 48 Push 8 Push 4 Pop 4 Pop 8 evaluate 8 + 4 = 12
Exercise 1: Show the sequence of stack configuration in the evaluation of the postfix expression below: 12 6 * 8 4 / / 7 5 - *
ANSWER Push 12 Push 6 Pop 6 Pop 12 Evaluate 12 * 6 = 72 Push 72 Push 8 Push 4 Pop 4 Pop 8 Evaluate 8 / 4 = 2 Push 2 Pop 2 Pop 72 Evaluate 72 / 2 = 36 Push 36 Push 7 Push 5 Pop 5 Pop 7 Evaluate 7 – 5 = 2 Push 2 Pop 2 Pop 36 Evaluate 36 * 2 = 72 Push 72 Pop 72 The answer is 72
EXERCISE
Exercise Use stack to evaluate the expression: 35 27 + 3 * 23 30 + 15 * / Convert the following expression into postfix notation and then use stack to evaluate the expression: 5 + 2 * (30 – 10 / 5)