Download
problem solving with data structures using java a multimedia approach n.
Skip this Video
Loading SlideShow in 5 Seconds..
Problem Solving with Data Structures using Java: A Multimedia Approach PowerPoint Presentation
Download Presentation
Problem Solving with Data Structures using Java: A Multimedia Approach

Problem Solving with Data Structures using Java: A Multimedia Approach

88 Vues Download Presentation
Télécharger la présentation

Problem Solving with Data Structures using Java: A Multimedia Approach

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Problem Solving with Data Structures using Java: A Multimedia Approach Chapter 11: Abstract Data Types: Separating the Meaning from the Implementation

  2. Chapter Objectives

  3. Separating Meaning from Implementation • Powerful engineering idea in computer science. • Meaning • Identify the behavior of an object or data structure. • Build other program components to interact with that behavior. • Implementation • Hide how that behavior is implemented, so that no program components can be dependent on the implementation.

  4. Abstract Data Type: Stacks • An example of an Abstract Data Type (ADT): • We can define the methods and the behavior apart from any implementation. • There are multiple implementations, some better than others. • Can use a stack to reverse a list in less time, but more space. • A classic tradeoff! Space for time. Only put on top,Never pull from bottom.

  5. A Stack is a LIFO List • Last-In-First-Out (LIFO) List • First item in the list is the last one out. • Last one in is first one out. I got here third! I got here second! I got here first! This is the top of the stack

  6. New items go at the top I got here fourth! I got here third! I got here second! I got here first! This is the newtop of the stack

  7. Items only get removed from the top I got here fourth! And now I’m outta here! I got here third! I got here second! I got here first! This is the new (er, old)top of the stack

  8. What can we do with stacks? • push(anObject): Tack a new object onto the top of the stack • pop(): Pull the top (head) object off the stack. • peek(): Get the top of the stack, but don’t remove it from the stack. • size(): Return the size of the stack

  9. Separation of Concerns • Separation definition of behavior of data structure from implementation. • Lets one change without changing the other. • Java allows us to use an interfaceas a way specify that behavior definition.

  10. Stack Interface /** * An abstract definition of a stack * @author Barb Ericson */ public interface Stack<E> { /** * Method to add the element to the top of the stack * @param element the element to add */ public void push(E element); /** * Method to return the top element on the * stack, but not remove it from the stack * @return the top object from the stack */ public E peek(); Just defining the operations here. E is the type of the things in the Stack. Stack<E> represents the generic stack for any time element

  11. /** * Method to remove the top element from the * stack and return it * @return the top element from the stack */ public E pop(); /** * Method to return the number of elements in * the stack * @return the number of elements in the stack */ public int size(); /** * Method to check if the stack is empty * @return true if empty, else false */ public boolean isEmpty(); }

  12. Creating an implementation • We declare a class as implementing that interface. • We can continue to use the <E> generic, so that we can later create a stack for any type object.

  13. Stack as a LinkedList import java.util.LinkedList; // Need for LinkedList /** * Class that represents a stack using a linked list * of objects * @author Mark Guzdial * @author Barb Ericson */ public class LinkedListStack<E> implements Stack<E> { /** Where we store the elements */ private LinkedList<E> elements; /** * Constructor that takes no arguments */ public LinkedListStack() { elements = new LinkedList<E>(); }

  14. //// Methods /// /** * Method to add an element to the stack * @param element the element to add */ public void push(E element) { // New elements go at the front elements.addFirst(element); } /** * Method to return the top element on the stack * but leave the element on the stack * @return the top element on the stack */ public E peek() { return elements.getFirst(); }

  15. /** * Method to remove the top element from a stack * and return it * @return the top element from the stack and remove it */ public E pop() { E toReturn = this.peek(); elements.removeFirst(); return toReturn; } /** * Method to get the number of elements in the stack * @return the number of elements in the stack */ public int size(){return elements.size();} /** * Method to test if the stack is empty * @return true if the stack is empty, else false */ public boolean isEmpty() { return (size() == 0); } }

  16. Using the Stack with Strings > Stack<String> stack = new LinkedListStack<String>(); > stack.push("This") > stack.push("is") > stack.push("a") > stack.push("test") > stack.size() 4 > stack.peek() "test" > stack.pop() "test" > stack.pop() "a" > stack.pop() "is" > stack.pop() "This" Notice the use of <String>

  17. A Stack ofPictures > Stack<Picture> stack = new LinkedListStack<Picture>(); > stack.push(new Picture(FileChooser.getMediaPath("beach.jpg"))); > stack.push(new Picture(FileChooser.getMediaPath("arch.jpg"))); > stack.push(new Picture(FileChooser.getMediaPath("bridge.jpg"))); > stack.size() 3 > stack.peek() Picture, filename C:\dsBook\media-source/bridge.jpg height 640 width 480 > stack.pop() Picture, filename C:\dsBook\media-source/bridge.jpg height 640 width 480 > stack.pop() Picture, filename C:\dsBook\media-source/arch.jpg height 480 width 360 > stack.pop() Picture, filename C:\dsBook\media-source/beach.jpg height 480 width 640 Note: You can’t create an object using an interface name, e.g., new Stack<String>()

  18. A stack is a stack, no matter what lies beneath. • Our description of the stack minus the implementation is an example of an abstract data type (ADT). • An abstract type is a description of the methods that a data structure knows and what the methods do. • We can actually write programs that use the abstract data type without specifying the implementation. • There are actually many implementations that will work for the given ADT. • Some are better than others.

  19. New implementation: Stack as Array /** * Implementation of a stack as an array * @author Mark Guzdial * @author Barb Ericson */ public class ArrayStack<E> implements Stack<E> { /** default size of the array */ private static final int ARRAY_SIZE = 20; /** Where we'll store our elements */ private Object[] elements;

  20. Constructor /** Index where the top of the stack is */ private int top; /** * No argument constructor */ public ArrayStack() { elements = new Object[ARRAY_SIZE]; top = 0; }

  21. Methods for Stack as Array /** * Method to add an element to the top of the stack * @param element the element to add */ public void push(E element) { // New elements go at the top elements[top]=element; // then add to the top top++; if (top==ARRAY_SIZE) { System.out.println("Stack overflow!"); } }

  22. /** * Method to return the top element on the stack * but not remove it. * @return the object at the top of the stack */ public E peek() { if (top==0) { System.out.println("Stack empty!"); return null; } else { // this will give a warning but it is unavoidable return (E) elements[top-1]; } }

  23. /** * Method to remove and return the top element on the stack * @return the element on the top of the stack */ public E pop() { E toReturn = this.peek(); top--; return toReturn; } /** * Method to return the number of elements in the stack * @return the number of elements in the stack */ public int size(){return top;} /** * Method to check if the stack is empty * @return true if the stack is empty else false */ public boolean isEmpty() {return this.size() == 0;} }

  24. Trying out Stack as Array > Stack<String> stack = new ArrayStack<String>();

  25. Pushing one element > stack.push("Matt");

  26. Push two more, pop one > stack.push("Katie"); > stack.push("Jenny"); > stack.size() // without the ending ';' it prints out the result 3 > stack.peek() // without the ending ';' it prints out the result "Jenny" > stack.pop() "Jenny"

  27. Critique the array-based implementation • What happens if the number of elements in the stack is more than 20?

  28. What are stacks good for? • The algorithm for converting an equation into a tree uses a stack. • Often use stacks when describing card games, like solitaire. • The list of Web pages you have visited in a browser is stored in a stack, so that you can go “back.” • The list of operations you have executed is stored in a stack, so that you can “undo.”

  29. Stacks describe function calls • As new functions get called, position in old functions get pushed on a stack. • So you always return to the last function you were in. • If an error occurs, you get a stack trace. • If your recursion goes into an infinite loop, what error do you get? Stack overflow!

  30. A Stack Example: New Reverse • Recall our original implementation of reverse(). • We go to the end of the original list to find the last(). • We then remove() it (which involves walking the list until we find the one before last()) • We then insert it at the end of the new list (via add(), which does last().insertAfter()). • All told: For each node, we walk the whole list three times. • O(n*n2)=O(n3)

  31. Original Reverse /** * Reverse the list starting at this, * and return the last element of the list. * The last element becomes the FIRST element * of the list, and THIS points to null. */ public LayeredSceneElement reverse() { LayeredSceneElement reversed, temp; // Handle the first node outside the loop reversed = this.last(); this.remove(reversed); while (this.getNext() != null) { temp = this.last(); this.remove(temp); reversed.add(temp); } // Now put the head of the old list on the end of // the reversed list. reversed.add(this); // At this point, reversed // is the head of the list return reversed; } Highly inefficient.Touching each node requires touching every other node: O(n2)

  32. New Reverse: Push all, pull off reversed /** * Reverse2: Push all the elements on * the stack, then pop all the elements * off the stack. */ public LayeredSceneElement reverse2() { LayeredSceneElement reversed, current, popped; Stack<LayeredSceneElement> stack = new LinkedListStack<LayeredSceneElement>();

  33. // Push all the elements on the list current=this; while (current != null) { stack.push(current); current = current.getNext(); } // Make the last element (current top of stack) into new first reversed = stack.pop(); // Now, pop them all onto the list current = reversed; while (stack.size()>0) { popped = stack.pop(); current.insertAfter(popped); current = popped; } return reversed; }

  34. What’s the diff? Time • How often is each node touched in reverse2()? • Twice: Once going onto the stack, once coming off. • O(2*n) => O(n) • The stack-based reverse is faster than the original reverse.

  35. What’s the diff? Space • How much space does reverse2() take? • Whatever space the stack takes. • How much additional space does reverse() take? None • Very common tradeoff: Space for time. • You can make an algorithm go faster, by using more space. • If you need to fit into less memory, you have to do more processing, which takes more time.

  36. Testing Reverse in SoundListTest() public void reverseTest(){ Sound s = null; // For copying in sounds s = new Sound(FileChooser.getMediaPath("guzdial.wav")); SoundNode root = new SoundNode(s); s = new Sound(FileChooser.getMediaPath("is.wav")); SoundNode one = new SoundNode(s); root.last().insertAfter(one); s = new Sound(FileChooser.getMediaPath("scritch-q.wav")); SoundNode two = new SoundNode(s); root.last().insertAfter(two); s = new Sound(FileChooser.getMediaPath("clap-q.wav")); SoundNode three = new SoundNode(s); two.insertAfter(three); //root.playFromMeOn(); SoundNode reversed = (SoundNode) root.reverse2(); reversed.playFromMeOn(); }

  37. Second ADT: Introducing a Queue • First-In-First-Out List • First person in line is first person served I got here third! I got here second! I got here first! This is the tail of the queue This is the front or head of the queue

  38. First-in-First-out • New items only get added to the tail. • Never in the middle • Items only get removed from the head. I got here third! I got here second! I got here first! This is the tail of the queue This is the front or head of the queue

  39. As items leave, the head shifts I got here third! I got here second! I got here first! AND NOW I’M UP! Now, this is the front or head of the queue This is the tail of the queue Served!

  40. As new items come in, the tail shifts I got here fourth! I got here third! I got here second! Now, this is the tail of the queue Now, this is the front or head of the queue

  41. Queue Operations • push(element): Tack a new element onto the tail (end) of the queue. • pop(): Pull the top (head) element off the queue. • peek(): Get the head of the queue, but don’t remove it from the queue. • size(): Return the size of the queue. • isEmpty(): Return true or false, if the size of the queue is zero.

  42. /** * Interface to define an abstract queue * @author Barb Ericson */ public interface Queue<E> { /** * Push an element onto the tail of the Queue * @param element the element to add to the queue */ public void push(E element); /** * Peek at, but don't remove, the head of the queue * @return the head of the queue (top) */ public E peek(); Queue Interface

  43. /** * Pop an object from the Queue * @return the head (top) of the queue and * remove it from the queue */ public E pop(); /** * Return the size of a queue * @return the number of elements in the queue */ public int size(); /** * Method to see if the queue is empty * @return true if the queue is empty, else false */ public boolean isEmpty(); }

  44. Implementing a Queue as a Linked List import java.util.*; // LinkedList representation /** * Implements a simple queue using a linked list * @author Mark Guzdial * @author Barb Ericson */ public class LinkedListQueue<E> extends AbstractQueue<E> { /** Where we'll store our elements */ private LinkedList<E> elements;

  45. /** * No argument constructor */ public LinkedListQueue() { elements = new LinkedList<E>(); } /// Methods /** * Push an element onto the tail of the Queue * @param element the element to add to the queue */ public void push(E element) { elements.addFirst(element); } /** * Peek at, but don't remove, top (first) of queue * @return the first object in the queue */ public E peek() { return elements.getLast(); }

  46. //** * Pop an object from the Queue * @return the top object from the queue (and remove it) */ public E pop() { E toReturn = this.peek(); elements.removeLast(); return toReturn; } /** * Return the size of a queue * @return the number of elements in the queue */ public int size() { return elements.size(); } /** * Method to see if the queue is empty * @return true if the queue is empty, else false */ public boolean isEmpty() { return size() == 0; } }

  47. Testing our implementation:Behaving as expected? > Queue<String> line = new LinkedListQueue<String>(); > line.push("Fred"); > line.push("Mary"); > line.push("Jose"); > line.size() 3 > line.peek() // without ending ';' prints the result "Fred" > line.pop() "Fred" > line.peek() "Mary" > line.pop() "Mary" > line.peek() "Jose" > line.pop() "Jose"

  48. Queue as Array /** * Implements a simple queue using an array * @author Mark Guzdial * @author Barb Ericson */ public class ArrayQueue<E> extends AbstractQueue<E> { /** constant for the size of the queue */ private static final int ARRAY_SIZE = 20; /** Where we'll store our elements */ private Object[] elements; /** The index of the head */ private int head; /** The index of the tail */ private int tail;

  49. /** No argument constructor */ public ArrayQueue() { elements = new Object[ARRAY_SIZE]; head = 0; tail = 0; } /// Methods /** * Push an element onto the tail of the Queue * @param element the element to add to the queue */ public void push(E element) { if ((tail + 1) >= ARRAY_SIZE) { System.out.println("Queue underlying implementation failed");} else { // Store at the tail, // then increment to a new open position elements[tail] = element; tail++;} }

  50. /** * Peek at, but don't remove, the head of the queue * @return the head of the queue (top) */ public E peek() { // this will give a warning but there is no way around it return (E) elements[head]; }