1 / 24

CS2006 - Data Structures I

CS2006 - Data Structures I. Chapter 7 Stacks III. Topics. Applications Infix to postfix expression Evaluate postfix expression. Infix Expressions. Binary operators appear between operands: W - X / Y – Z (4+3)*2 (2+3)/(9-4) Order of evaluation is determined by:

Download Presentation

CS2006 - Data Structures I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS2006 - Data Structures I Chapter 7StacksIII

  2. Topics • Applications • Infix to postfix expression • Evaluate postfix expression

  3. Infix Expressions • Binary operators appear between operands: W - X / Y – Z (4+3)*2 (2+3)/(9-4) • Order of evaluation is determined by: • precedence rules • parentheses • association (L to R or R to L)

  4. Application: Algebraic Expressions • Infix Expression Evaluation • To evaluate an infix expression • Convert the infix expression into postfix • Evaluate the postfix expression using stacks

  5. Postfix Expressions • Binary operators appear after both operands: • X + Y in postfix form is: X Y + • Order of operations is completely determined by the expression • no parentheses or precedence required • for example: • A * B - C becomes A B * C -

  6. Postfix Expression Infix Postfix 6-1 (4+3)*2 (2+3)/(9-4)

  7. Postfix Expression Evaluation • Assumptions: • The string is syntactically correct postfix expression • No unary operators are present • No exponentiation operators are present

  8. Postfix Expression Evaluation • Evaluation of postfix expressions makes use of an operand stack. • Algorithm: • Parse expression from left to right • When an operand is encountered, push it onto stack • when an operator is encountered, pop the last two operands off the stack and apply the operation, and push the result onto the stack • when the expression is completely scanned, the final result will be on the stack 2 3 + 9 4 -

  9. Postfix Expression Evaluation • Pseudocode: for ( each character ch in the string ) { if (ch is an operand ) Push value that operand ch represents onto stack else / / ch is an operator named op { / / evaluate and push the result operand2 = top of stack Pop the stack operand1 = top of stack Pop the stack result = operand1 op operand2 Push Result onto stack } / / end if } / / end for

  10. Example • Evaluate 2 3 4 5 + 2 * 1 + + 3 2 * + *

  11. Converting Infix into Postfix • Since postfix expressions are easily evaluated, the easiest way to evaluate infix is to convert from infix to postfix and then evaluate. • This conversion uses an operator stack since low precedence operators must be saved and applied after high precedence ones.

  12. Converting Infix into Postfix • The operands always stay in the same order with respect to one another • An operator will move only to “ the right” with respect to the operands • If in infix expression, the operand x precedes the operator op, in the postfix, the operand x also precedes the operator x • All parentheses are removed

  13. Converting Infix into Postfix • Converting Infix into Postfix • Scan infix string from left to right • Each time an operand is encountered, copy it to output • When a bracket is encountered, check its orientation. • Push left brackets onto stack • If it is a right bracket, pop all operators on the stack and copy them to output until a matching left bracket is encountered. Discard the left bracket

  14. Converting Infix into Postfix • Converting Infix into Postfix • When an operator is encountered, check the top item of the stack • If the priority is >= the current operator, pop the top operator and copy it to output • Continue until an operator of lower priority is encountered or until the stack is empty • Finally, push current operator onto the stack • When the end of the expression is reached, copy the remaining stack contents to output in the order popped

  15. Converting Infix into Postfix • Converting Infix into Postfix • Precedence Table

  16. Conversion Example • W - X /Y + Z • '-' is pushed onto operator stack • '/' has higher precedence than '-' on stack - it gets pushed • '+' has lower precedence than '/' on stack - pop '/' and output - then '-' is on top of stack - since subtraction associates left to right - pop '-' off stack and output - push '+' onto stack • when end of expression is reached pop remaining operator and output • resulting expression is: W X Y /- Z +

  17. Converting Infix into Postfix stack.push(EOL marker) for (each character ch in the infix expression) { switch (ch) { case operand: //append ch to output postfix expression postfixExpr = postfixExpr + ch; case '(': stack.push(ch); // push ch onto operator stack case ')': while (top of stack is not '(' ) { postfixExpr = postfixExpr + stack.pop(); // pop and append expr. } stack.pop(); case operator: while (!stack.isEmpty() && inputPrec(ch) <= stackPrec(stack.top())) { postfixExpr = postfixExpr + stack.pop(); } stack.push(ch); } // end for while (!stack.isEmpty() { // pop all remaining operators off stack and append postfixExpr = postfixExpr + stack.pop(); }

  18. Conversion Example • 2 + 3 * [(7-5)/2] 2 3 7 5 • 2 copy into output • ‘+' is pushed onto operator stack • 3 copy into output • ‘*' has higher precedence than ‘+' on stack - it gets pushed • ‘[‘ is left bracket, push it onto stack • ‘(‘is left bracket, push it onto stack • 7 is copied into output • ‘-' has higher precedence than ‘(‘, push onto stack

  19. Conversion Example • 2 + 3 * [(7-5)/2]

  20. Prefix Expressions Infix Prefix 6-1 - 6 1 (4+3)*2 * + 4 3 2 (2+3)/(9-4) / + 2 3 – 9 4

  21. Review • Which of the following is NOT true about converting infix expressions to postfix expressions? • the operands always stay in the same order with respect to one another • the operators always stay in the same order with respect to one another • an operator will move only “to the right” with respect to the operands • all parentheses are removed

  22. Review • Which of the following is the postfix form of the infix expression: (a + b) * c / d • a b + c * d / • a b * c / d + • a + b * c / d • a b + c d * /

  23. Review • StackInterface provides the specifications for ______. • only the array-based implementation of a stack • only the reference-based implementation of a stack • only an implementation of a stack that uses the ADT list • all the implementations of a stack

  24. Review • In the StackInterface class, the push method accepts as its parameter an item that is an instance of ______. • Integer • Double • String • Object

More Related