CIIT_DS_lecture 06.ppt

  • Upload
    rana856

  • View
    220

  • Download
    0

Embed Size (px)

Citation preview

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    1/50

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    2/50

    Use of Stack

    Example of use: prefix, infix, postfixexpressions.

    Consider the expression A+B: we think of

    applying the operator+ to the operandsA and B.

    + is termed a binary operator: it takestwo operands.

    Writing the sum as A+B is called the infixform of the expression.

    11/10/2014 2CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    3/50

    Prefix, Infix, Postfix

    Two other ways of writing the expressionare

    + A B prefixA B + postfix

    The prefixes pre and post refer to theposition of the operator with respect to thetwo operands.

    11/10/2014 3CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    4/50

    Prefix, Infix, Postfix

    Consider the infix expression

    A + B * C

    We know that multiplication is done

    before addition.

    The expression is interpreted asA + ( B * C )

    Multiplication hasprecedenceoveraddition.

    11/10/2014 4CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    5/50

    Prefix, Infix, Postfix

    Conversion to postfix

    A + ( B * C ) infix form

    11/10/2014 5CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    6/50

    Prefix, Infix, Postfix

    Conversion to postfix

    A + ( B * C ) infix form

    A + ( B C * ) convert multiplication

    11/10/2014 6CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    7/50

    Prefix, Infix, Postfix

    Conversion to postfix

    A + ( B * C ) infix form

    A + ( B C * ) convert multiplication

    A ( B C * ) + convert addition

    11/10/2014 7CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    8/50

    Prefix, Infix, Postfix

    Conversion to postfix

    A + ( B * C ) infix form

    A + ( B C * ) convert multiplication

    A ( B C * ) + convert addition

    A B C * + postfix form

    11/10/2014 8CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    9/50

    Prefix, Infix, Postfix

    Conversion to postfix

    (A + B ) * C infix form

    11/10/2014 9CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    10/50

    Prefix, Infix, Postfix

    Conversion to postfix

    (A + B ) * C infix form

    ( A B + ) * C convert addition

    11/10/2014 10CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    11/50

    Prefix, Infix, Postfix

    Conversion to postfix

    (A + B ) * C infix form

    ( A B + ) * C convert addition

    ( A B + ) C * convert multiplication

    11/10/2014 11CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    12/50

    Prefix, Infix, Postfix

    Conversion to postfix

    (A + B ) * C infix form

    ( A B + ) * C convert addition

    ( A B + ) C * convert multiplication

    A B + C * postfix form

    11/10/2014 12CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    13/50

    Precedence of Operators

    The five binary operators are: addition,

    subtraction, multiplication, division andexponentiation.

    The order of precedence is (highest tolowest)

    Exponentiation

    Multiplication/division *, /

    Addition/subtraction +, -

    11/10/2014 13CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    14/50

    Precedence of Operators

    For operators of same precedence, theleft-to-right rule applies:

    A+B+C means (A+B)+C.

    For exponentiation, the right-to-left ruleapplies

    A B C means A ( B C )

    11/10/2014 14CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    15/50

    Infix to Postfix

    Infix Postfix

    A + B A B +

    12 + 6023 12 60 + 23(A + B)*(CD ) A B + C D*

    A B * CD + E/F A B C*DE F/+

    11/10/2014 15CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    16/50

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    17/50

    Infix to Postfix

    Note that the postfix form an expression

    does not require parenthesis.

    Consider 4+3*5 and (4+3)*5. The

    parenthesis are not needed in the first butthey are necessary in the second.

    The postfix forms are:

    4+3*5 435*+(4+3)*5 43+5*

    11/10/2014 17CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    18/50

    Evaluating Postfix

    Each operator in a postfix expression

    refers to the previous two operands.

    Each time we read an operand, we push iton a stack.

    When we reach an operator, we pop thetwo operands from the top of the stack,

    apply the operator and push the resultback on the stack.

    11/10/2014 18CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    19/50

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    20/50

    Evaluating Postfix

    Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +Input op1 op2 value stack

    6 6

    11/10/2014 20CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    21/50

    Evaluating Postfix

    Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +Input op1 op2 value stack

    6 6

    2 6,2

    11/10/2014 21CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    22/50

    Evaluating Postfix

    Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +Input op1 op2 value stack

    6 6

    2 6,2

    3 6,2,3

    11/10/2014 22CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    23/50

    Evaluating Postfix

    Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +Input op1 op2 value stack

    6 6

    2 6,2

    3 6,2,3

    + 2 3 5 6,5

    11/10/2014 23CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    24/50

    Evaluating Postfix

    Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +Input op1 op2 value stack

    6 6

    2 6,2

    3 6,2,3

    + 2 3 5 6,5

    - 6 5 1 1

    11/10/2014 24CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    25/50

    Evaluating Postfix

    Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +Input op1 op2 value stack

    6 6

    2 6,2

    3 6,2,3

    + 2 3 5 6,5

    - 6 5 1 1

    3 6 5 1 1,3

    11/10/2014 25CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    26/50

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    27/50

    Evaluating Postfix

    Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +Input op1 op2 value stack

    6 6

    2 6,2

    3 6,2,3

    + 2 3 5 6,5

    - 6 5 1 1

    3 6 5 1 1,3

    8 6 5 1 1,3,8

    2 6 5 1 1,3,8,2

    11/10/2014 27CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    28/50

    Evaluating Postfix

    Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +Input op1 op2 value stack

    6 6

    2 6,2

    3 6,2,3

    + 2 3 5 6,5

    - 6 5 1 1

    3 6 5 1 1,3

    8 6 5 1 1,3,8

    2 6 5 1 1,3,8,2

    / 8 2 4 1,3,4

    11/10/2014 28CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    29/50

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    30/50

    Evaluating Postfix

    Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +Input op1 op2 value stack

    6 6

    2 6,2

    3 6,2,3

    + 2 3 5 6,5- 6 5 1 1

    3 6 5 1 1,3

    8 6 5 1 1,3,8

    2 6 5 1 1,3,8,2

    / 8 2 4 1,3,4+ 3 4 7 1,7

    * 1 7 7 7

    11/10/2014 30CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    31/50

    Evaluating Postfix

    Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +Input op1 op2 value stack

    6 6

    2 6,2

    3 6,2,3

    + 2 3 5 6,5- 6 5 1 1

    3 6 5 1 1,3

    8 6 5 1 1,3,8

    2 6 5 1 1,3,8,2

    / 8 2 4 1,3,4+ 3 4 7 1,7

    * 1 7 7 7

    2 1 7 7 7,2

    11/10/2014 31CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    32/50

    Evaluating Postfix

    Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +Input op1 op2 value stack

    6 6

    2 6,2

    3 6,2,3

    + 2 3 5 6,5

    - 6 5 1 1

    3 6 5 1 1,3

    8 6 5 1 1,3,8

    2 6 5 1 1,3,8,2

    / 8 2 4 1,3,4

    + 3 4 7 1,7

    * 1 7 7 7

    2 1 7 7 7,2

    7 2 49 49

    11/10/2014 32CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    33/50

    Evaluating Postfix

    Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +Input op1 op2 value stack

    6 6

    2 6,2

    3 6,2,3

    + 2 3 5 6,5

    - 6 5 1 1

    3 6 5 1 1,3

    8 6 5 1 1,3,8

    2 6 5 1 1,3,8,2

    / 8 2 4 1,3,4

    + 3 4 7 1,7

    * 1 7 7 7

    2 1 7 7 7,2

    7 2 49 49

    3 7 2 49 49,3

    11/10/2014 33CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    34/50

    Evaluating Postfix

    Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +Input op1 op2 value stack

    6 6

    2 6,2

    3 6,2,3

    + 2 3 5 6,5

    - 6 5 1 1

    3 6 5 1 1,3

    8 6 5 1 1,3,8

    2 6 5 1 1,3,8,2

    / 8 2 4 1,3,4

    + 3 4 7 1,7

    * 1 7 7 7

    2 1 7 7 7,2

    7 2 49 49

    3 7 2 49 49,3

    + 49 3 52 52

    11/10/2014 34CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    35/50

    Evaluating Postfix

    Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +Input op1 op2 value stack

    6 6

    2 6,2

    3 6,2,3

    + 2 3 5 6,5

    - 6 5 1 1

    3 6 5 1 1,3

    8 6 5 1 1,3,8

    2 6 5 1 1,3,8,2

    / 8 2 4 1,3,4

    + 3 4 7 1,7

    * 1 7 7 7

    2 1 7 7 7,2

    7 2 49 49

    3 7 2 49 49,3

    + 49 3 52 52

    11/10/2014 35CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    36/50

    Converting Infix to Postfix

    Consider the infix expressions A+B*C

    and (A+B)*C.

    The postfix versions are ABC*+ and

    AB+C*.

    The order of operands in postfix is thesame as the infix.

    In scanning from left to right, the operandA can be inserted into postfix expression.

    11/10/2014 36CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    37/50

    Converting Infix to Postfix

    The + cannot be inserted until its secondoperand has been scanned and inserted.

    The + has to be stored away until its

    proper position is found. When B is seen, it is immediately inserted

    into the postfix expression.

    Can the + be inserted now? In the caseof A+B*C cannot because * hasprecedence.

    11/10/2014 37CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    38/50

    Converting Infix to Postfix

    In case of (A+B)*C, the closing

    parenthesis indicates that + must be

    performed first.

    Assume the existence of a function

    prcd(op1,op2) where op1 and op2 are

    two operators.

    Prcd(op1,op2) returns TRUE if op1 hasprecedence over op2, FASLE otherwise.

    11/10/2014 38CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    39/50

    Converting Infix to Postfix

    prcd(*,+) is TRUE

    prcd(+,+) is TRUE

    prcd(+,*) is FALSE Here is the algorithm that converts infix

    expression to its postfix form.

    The infix expression is withoutparenthesis.

    11/10/2014 39CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    40/50

    Converting Infix to Postfix1. Stack s;

    2. While( not end of input ) {

    3. c = next input character;

    4. if( c is an operand )

    5. add c to postfix string;

    6. else {

    7. while( !s.empty() && prcd(s.top(),c) ){8. op = s.pop();

    9. add op to the postfix string;

    10. }

    11. s.push( c );

    12. }13. while( !s.empty() ) {

    14. op = s.pop();

    15. add op to postfix string;

    16. }

    11/10/2014 40CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    41/50

    Converting Infix to Postfix

    Example: A + B * Csymb postfix stack

    A A

    11/10/2014 41CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    42/50

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    43/50

    Converting Infix to Postfix

    Example: A + B * Csymb postfix stack

    A A

    + A +B AB +

    11/10/2014 43CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    44/50

    Converting Infix to Postfix

    Example: A + B * Csymb postfix stack

    A A

    + A +B AB +

    * AB + *

    11/10/2014 44CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    45/50

    Converting Infix to Postfix

    Example: A + B * Csymb postfix stack

    A A

    + A +B AB +

    * AB + *

    C ABC + *

    11/10/2014 45CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    46/50

    Converting Infix to Postfix

    Example: A + B * Csymb postfix stack

    A A

    + A +

    B AB +

    * AB + *

    C ABC + *

    ABC * +

    11/10/2014 46CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    47/50

    Converting Infix to Postfix

    Example: A + B * Csymb postfix stack

    A A

    + A +

    B AB +

    * AB + *

    C ABC + *

    ABC * +ABC * +

    11/10/2014 47CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    48/50

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    49/50

    Converting Infix to Postfix

    When a ) is read, all operators up to the

    first ( must be popped and placed in the

    postfix string.

    To do this, prcd( op,) ) == TRUE.

    Both the ( and the ) must be discarded:

    prcd( (,) ) == FALSE.

    Need to change line 11 of the algorithm.

    11/10/2014 49CIIT Sahiwal Campus

  • 8/10/2019 CIIT_DS_lecture 06.ppt

    50/50

    Converting Infix to Postfix

    if( s.empty() || symb != ) )s.push( c );else

    s.pop(); // discard the (

    prcd( (, op ) = FALSE for any operatorprcd( op, ( ) = FALSE for any operator

    other than (

    prcd( op, ) ) = TRUE for any operatorother than (prcd( ), op ) = error for any operator.

    11/10/2014 50CIIT Sahiwal Campus