Download presentation
Presentation is loading. Please wait.
1
(Part 2) Infix, Prefix & Postfix
Chapter 4: STACK (Part 2) Infix, Prefix & Postfix
2
Arithmetic Expressions
Arithmetic expressions have: operands (variables or numeric constants). Operators Binary : +, -, *, / ,% Unary: - Priority convention: *, /, % have medium priority +, - have lowest priority
3
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 +
4
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 : "* " is (5+6)*3, and cannot be interpreted as 5+(6*3), whereas parenthesis is required to achieve with infix.
8
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 + –
9
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+
10
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
11
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 -> * + -
12
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
13
Example: Infix to Postfix
Example 1: Infix notation A - B STEPS INFIX OPERATOR STACK POSTFIX 1 A empty 2 - 3 B AB 4 AB-
14
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 -
15
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 *+
16
Example Infix to Postfix
Convert this infix notation to postfix notation a + c – r / b * r
17
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 * -
18
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
19
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.
20
Parentheses in Infix notation (cont)
Example 5 Convert this infix notation to postfix notation x – (y * a / b – (z + d * e) + c) / f
21
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
22
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 /
23
Solution (cont..) INFIX OPERATORSTACK POSTFIX -
xya * b/zde *+- c + f / empty xya * b/zde *+-c+ f /-
24
Infix to Postfix Conversion (Method 2)
Example: Infix notation A + B – C (A B + ) - C A B + C -
25
Infix to Postfix Conversion (Method 2)
Example: Infix notation A + B * C A + (B C *) A B C * +
26
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 * -
27
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
28
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
29
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)
30
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
31
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.
32
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
33
Infix to Prefix Conversion (Method 1)
Example 1: Convert infix notation to prefix notation a + b * c
34
Solution Infix operandStack operatorStack a empty + b * c * bc
+ a * bc
35
Infix to Prefix Conversion (Method 1)
Example 2: Convert infix notation to prefix notation (a – b) * c
36
Solution Infix operandStack operatorStack ( a - b ) - ab * c ab
37
Infix to Prefix Conversion (Method 1)
Example 3: Convert infix notation to prefix notation a + (c – h) / (b * d)
38
Solution Infix operandStack operatorStack a + ( c - h
39
Solution Infix operandStack operatorStack ) - ch a ( + / b ch
40
Solution Infix operandStack operatorStack * b - ch a ( / + d ) *bd ch
41
Solution Infix operandStack operatorStack /-ch*bd a + + a / - ch *bd
42
Infix to Prefix Conversion (Method 2)
Convert infix notation to prefix notation (a – b) * c (- a b) * c * - a b c
43
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
44
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)
45
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: * 7 – The final result, 65, lies on the top of the stack at the end of the calculation
46
Solution Postfix Stack Expression 4 5 8 5+4 9 8*9 72 7 72-7 65
* 7 – 4 5 8 5+4 9 8*9 72 7 72-7 65
47
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
48
EVALUATING EXPRESSION USING STACK(method 2)
49
EVALUATING EXPRESSION USING STACK (method 2)
50
Method 2 Evaluate the following expression using stack 8 5 4 + * 7 –
* 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 + * -
51
EVALUATING EXPRESSION USING STACK (Method 3)
POSTFIX: 8 6 * / 5 + POSTFIX: 8 6 * / 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 = 12
52
Exercise 1: Show the sequence of stack configuration in the evaluation of the postfix expression below: * / / *
53
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
55
EXERCISE
56
Exercise Use stack to evaluate the expression:
* * / Convert the following expression into postfix notation and then use stack to evaluate the expression: 5 + 2 * (30 – 10 / 5)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.