1 / 26

CSE 246 Data Structures and Algorithms

CSE 246 Data Structures and Algorithms. Spring2011 Lecture#11. Stack Application. Run time Stack procedures Parenthesis Validation Postfix Calculator Interpret infix with precedence. Stack Application.

elata
Download Presentation

CSE 246 Data Structures and Algorithms

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. CSE 246Data Structures and Algorithms Spring2011 Lecture#11

  2. Stack Application • Run time Stack procedures • Parenthesis Validation • Postfix Calculator • Interpret infix with precedence Quratulain

  3. Stack Application • Almost invariably, programs compiled from modern high level languages (even C!) make use of a stack frame for the working memory of each procedure or function invocation. • When any procedure or function is called, a number of words - the stack frame - is pushed onto a program stack. Quratulain

  4. Visualizing Recursion • To understand how recursion works, it helps to visualize what’s going on. • To help visualize, we will use a common concept called the Stack. • A stack basically operates like a container of trays in a cafeteria. It has only two operations: • Push: you can push something onto the stack. • Pop: you can pop something off the top of the stack.

  5. Stacks The diagram below shows a stack over time. We perform two pushes (of 2 and then of 8), and one pop. 8 2 2 2 Time 3: Pop: Gets 8 Time: 0 Empty Stack Time 1: Push “2” Time 2: Push “8” Time 4: Pop: Gets 2

  6. Stacks and Methods • When you run a program, the computer creates a stack for you. • Each time you invoke a method, the method is placed on top of the stack. • When the method returns or exits, the method is popped off the stack. • The diagram on the next page shows a sample stack for a simple Java program.

  7. Stacks and Methods square() main() main() main() Time 4: Pop: main() returns a value. method exits. Time 3: Pop: square() returns a value. method exits. Time: 0 Empty Stack Time 1: Push: main() Time 2: Push: square() This is called an activation record or stack frame. Usually, this actually grows downward.

  8. Stacks and Recursion • Each time a method is called, you push the method on the stack. • Each time the method returns or exits, you pop the method off the stack. • If a method calls itself recursively, you just push another copy of the method onto the stack. • We therefore have a simple way to visualize how recursion really works.

  9. Back to the Simple Recursion Program • Here’s the code again. Now, that we understand stacks, we can visualize the recursion. public class Recursion { public static void main (String args[]) { count(0); System.out.println(); } public static void count (int index) { System.out.print(index); if (index < 2) count(index+1); } }

  10. Stacks and Recursion in Action count(2) count(1) count(1) count(0) count(0) count(0) … main() main() main() main() Time 3: Push: count(1) Time 4: Push: count(2) Time: 0 Empty Stack Time 1: Push: main() Time 2: Push: count(0) Times 5-8: Pop everything one by one Inside count(0): print (index);  0 if (index < 2) count(index+1); Inside count(1): print (index);  1 if (index < 2) count(index+1); Inside count(2): print (index);  2 if (index < 2) count(index+1); This condition now fails! Hence, recursion stops, and we proceed to pop all methods off the stack,one by one.

  11. Arithmetic Expression • Infix, Postfix and Prefix notations are three different but equivalent ways of writing expressions. Quratulain

  12. Arithmetic Expressions Infix Expressions An expression in which every binary operation appears between its operands Example: (i) a+b “+” is a binary operation and a and b are its operands (ii) (a+b)*c Prefix Expressions An expression in which operator comes before its operands Example: (i) a+b = +ab (ii) (a+b)*c = *+abc (iii) a+(b*c) =+a*bc Postfix Expressions An expression in which operator comes after its operands Example: (i) a+b = ab+ (ii) (a+b)*c = ab+c* (iii) a+(b*c) = abc*+ Quratulain

  13. Arithmetic Expression validate • Pushing an item on to stack correspond to opening a scope, and popping an item from the stack corresponds to closing a scope. • When the stack is empty and scope ender encountered, so the parenthesis pattern is invalid. Quratulain

  14. Infix notation • Infix notation: A * ( B + C ) / D • Infix notation needs extra information to make the order of evaluation of the operators clear: rules built into the language about operator precedence and associativity, and brackets ( ) to allow users to override these rules. Quratulain

  15. Postfix notation • The order of evaluation of operators is always left-to-right, and brackets cannot be used to change this order. • Operators act on values immediately to the left of them. • RPN has the advantage of being extremely easy, and therefore fast, for a computer to analyze. Quratulain

  16. Postfix • In the 1920's, Jan Lukasiewicz developed a formal logic system which allowed mathematical expressions to be specified without parentheses by placing the operators before (prefix notation) or after (postfix notation) the operands. • postfix notation for a calculator keyboard. • computer scientists realized that RPN or postfix notation was very efficient for computer math. • As a postfix expression is scanned from left to right, operands are simply placed into a last-in, first-out (LIFO) stack and operators may be immediately applied to the operands at the bottom of the stack. • Another advantage is consistency between machines. Quratulain

  17. Practical implications • Calculations proceed from left to right • There are no brackets or parentheses, as they are unnecessary. • Operands precede operator. They are removed as the operation is evaluated. • When an operation is made, the result becomes an operand itself (for later operators) • There is no hidden state. No need to wonder if you hit an operator or not. Quratulain

  18. Precedence of Operators • The five binary operators are: addition, subtraction, multiplication, division and exponentiation. • The order of precedence is (highest to lowest) • Exponentiation  • Multiplication/division *, / • Addition/subtraction +, -

  19. Example • The calculation: ((1 + 2) * 4) + 3 can be written down like this in RPN: • The expression is evaluated in the following way (the Stack is displayed after Operation has taken place): • Input Stack Operation 1 1 Push operand 2 1, 2 Push operand + 3 Addition 4 3, 4 Push operand * 12 Multiplication 3 12,3 Push operand + 15 Addition 1 2 + 4 * 3 + Quratulain

  20. Infix to Postfix conversion Algorithm Opstk = the empty stack while(not end of input) { symb=next input char; if (symbol is operand) add symb in postfix string else { while(!empty(opstk)&& prcd(stacktop(opstk), symb)) { topsymb=pop(opstk); add topsymb to postfix string; } } while(!empty(opstk)) { topsymb=pop(opstk); add topsymb to postfix string; } Quratulain

  21. Algorithm for postfix evalution Opndstk = the emty stack; While (not end of input) { Symb = next input character; If (symb is an operand) Push(opndstk,symb); Else { Opnd2=pop(opndstk); Opnd1=pop(opndstk); Value = result of applying symb to opnd1 and opnd2; Push(opndstk, value); } } Return (pop(opndstk)); Quratulain

  22. A Stack Interface in Java • The stack data structure is included as a "built-in" class in the java.util package of Java. • it is instructive to learn how to design and implement a stack "from scratch.“ • Implementing an abstract data type in Java involves two steps • Define interface • Define exceptions for any error conditions that can arise. • Provide a concrete class that implements the methods of the interface associated with that ADT. Quratulain

  23. Stack interface public interface Stack <E> { public int size(); public booleanisEmpty(); public E top() throws EmptyStackException; public void push( E element); public E pop() throws EmptyStackException; } Quratulain

  24. Quratulain

  25. Quratulain

  26. Implementing a Stack with a Generic Linked List Left as homework Quratulain

More Related