Upgrade to Pro — share decks privately, control downloads, hide ads and more …

lecture25.pdf

 lecture25.pdf

Avatar for William Albritton

William Albritton

September 14, 2014
Tweet

More Decks by William Albritton

Other Decks in Programming

Transcript

  1. Terminology • A priority value is a value assigned to

    an item which indicates its priority  Usually used in a priority queue
  2. Terminology • A priority queue is a kind of queue,

    in which items are added in order of occurrence, but removed according to their priority value  The item with the largest value is removed first
  3. Terminology • For example, patients at a hospital are put

    into a priority queue with the most urgent patient at the beginning of the queue
  4. Priority Queue Implementation • A priority queue can be implemented

    in many ways  For example, a linked list could be used  The item would be inserted in descending order  The largest item would be at the beginning of the list
  5. Priority Queue Implementation • Or, a binary tree could be

    used  The item would be inserted into the binary tree  The largest item would be at the rightmost node • Typically, a heap is used to implement a priority queue
  6. Heap & HeapException Code • See Heap.java for code 

    This is an array-based heap • Data fields • MAX_ARRAY_SIZE, indexOfLastElement, array • Public methods • Heap, toString, empty, add, get, remove, and main
  7. Heap in Memory • Instantiating a Heap object will call

    the constructor, which initializes the data fields public Heap() { } • Data fields: private final int MAX_ARRAY_SIZE = 100; private int indexOfLastElement = -1; private T array[] = (T[]) new Object[MAX_ARRAY_SIZE];
  8. Heap in Memory • Instantiate a Heap object (of Integers)

    Heap<Integer> heap = new Heap<Integer>(); heap index: 0 1 2 3 4 5 6 7 8 9 element: | | | | | | | | | | | MAX_ARRAY_SIZE indexOfLastElement array 100 -1
  9. Method add() in Memory • Add 8 to heap heap.add(8);

    heap index: 0 1 2 3 4 5 6 7 8 9 element: | | | | | | | | | | | MAX_ARRAY_SIZE indexOfLastElement array 100 -1
  10. Method add() in Memory indexOfLastElement++; array[indexOfLastElement] = item; heap index:

    0 1 2 3 4 5 6 7 8 9 element: | 8| | | | | | | | | | MAX_ARRAY_SIZE indexOfLastElement array 100 0
  11. Method add() in Memory heap.add(6); heap.add(7); heap index: 0 1

    2 3 4 5 6 7 8 9 element: | 8| 6| 7| | | | | | | | MAX_ARRAY_SIZE indexOfLastElement array 100 2
  12. Method add() in Memory • Add 9 to heap and

    show “trickle up” heap.add(9); heap index: 0 1 2 3 4 5 6 7 8 9 element: | 8| 6| 7| | | | | | | | MAX_ARRAY_SIZE indexOfLastElement array 100 2
  13. Method add() in Memory indexOfLastElement++; array[indexOfLastElement] = item; heap index:

    0 1 2 3 4 5 6 7 8 9 element: | 8| 6| 7| 9| | | | | | | MAX_ARRAY_SIZE indexOfLastElement array 100 3
  14. Method add() in Memory int indexOfChild = indexOfLastElement; int indexOfParent

    = (indexOfChild - 1) / 2; heap indexOfChild indexOfParent index: 0 1 2 3 4 5 6 7 8 9 element: | 8| 6| 7| 9| | | | | | | MAX_ARRAY_SIZE indexOfLastElement array 100 3 3 1
  15. Method add() in Memory this.trickleUp(indexOfParent,indexOfChild); private void trickleUp(int indexOfParent, int

    indexOfChild) { heap indexOfChild indexOfParent index: 0 1 2 3 4 5 6 7 8 9 element: | 8| 6| 7| 9| | | | | | | MAX_ARRAY_SIZE indexOfLastElement array 100 3 3 1
  16. Method add() in Memory if ((indexOfParent >= 0) && (compare(array[indexOfParent],

    array[indexOfChild]) < 0)) { heap indexOfChild indexOfParent index: 0 1 2 3 4 5 6 7 8 9 element: | 8| 6| 7| 9| | | | | | | MAX_ARRAY_SIZE indexOfLastElement array 100 3 3 1
  17. Compare Method • Since arrays as a whole cannot be

    cast to java.lang.Comparable each element being compared is individually cast to java.lang.Comparable  The compare() method casts the parameter to java.lang.Comparable, and then calls the compareTo() method for that particular variable’s class
  18. Method compare() Code • Method that compares two items 

    item1 = 6, item2 = 9  result = 6 – 9 = -3 private int compare(T item1, T item2) { java.lang.Comparable itemOne = (java.lang.Comparable) item1; int result = itemOne.compareTo(item2); return result; }
  19. Method add() in Memory T temporary = array[indexOfParent]; temporary heap

    indexOfChild indexOfParent index: 0 1 2 3 4 5 6 7 8 9 element: | 8| 6| 7| 9| | | | | | | MAX_ARRAY_SIZE indexOfLastElement array 100 3 3 1 6
  20. Method add() in Memory array[indexOfParent] = array[indexOfChild]; temporary heap indexOfChild

    indexOfParent index: 0 1 2 3 4 5 6 7 8 9 element: | 8| 9| 7| 9| | | | | | | MAX_ARRAY_SIZE indexOfLastElement array 100 3 3 1 6
  21. Method add() in Memory array[indexOfChild] = temporary; temporary heap indexOfChild

    indexOfParent index: 0 1 2 3 4 5 6 7 8 9 element: | 8| 9| 7| 6| | | | | | | MAX_ARRAY_SIZE indexOfLastElement array 100 3 3 1 6
  22. Method add() in Memory indexOfChild = indexOfParent; temporary heap indexOfChild

    indexOfParent index: 0 1 2 3 4 5 6 7 8 9 element: | 8| 9| 7| 6| | | | | | | MAX_ARRAY_SIZE indexOfLastElement array 100 3 1 1 6
  23. Method add() in Memory indexOfParent = (indexOfChild – 1)/2 temporary

    this.trickleUp(indexOfParent, indexOfChild); heap indexOfChild indexOfParent index: 0 1 2 3 4 5 6 7 8 9 element: | 8| 9| 7| 6| | | | | | | MAX_ARRAY_SIZE indexOfLastElement array 100 3 1 0 6
  24. Method add() in Memory if ((indexOfParent >= 0) && (compare(array[indexOfParent],array[indexOfChild])

    < 0)) { heap indexOfChild indexOfParent index: 0 1 2 3 4 5 6 7 8 9 element: | 8| 9| 7| 6| | | | | | | MAX_ARRAY_SIZE indexOfLastElement array 100 3 1 0
  25. Method compare() Code • Method that compares two items 

    item1 = 8, item2 = 9  result = 8 – 9 = -1 private int compare(T item1, T item2) { java.lang.Comparable itemOne = (java.lang.Comparable) item1; int result = itemOne.compareTo(item2); return result; }
  26. Method add() in Memory T temporary = array[indexOfParent]; temporary heap

    indexOfChild indexOfParent index: 0 1 2 3 4 5 6 7 8 9 element: | 8| 9| 7| 6| | | | | | | MAX_ARRAY_SIZE indexOfLastElement array 100 3 1 0 8
  27. Method add() in Memory array[indexOfParent] = array[indexOfChild]; temporary heap indexOfChild

    indexOfParent index: 0 1 2 3 4 5 6 7 8 9 element: | 9| 9| 7| 9| | | | | | | MAX_ARRAY_SIZE indexOfLastElement array 100 3 1 0 8
  28. Method add() in Memory array[indexOfChild] = temporary; temporary heap indexOfChild

    indexOfParent index: 0 1 2 3 4 5 6 7 8 9 element: | 9| 8| 7| 6| | | | | | | MAX_ARRAY_SIZE indexOfLastElement array 100 3 1 0 8
  29. Method add() in Memory indexOfChild = indexOfParent; temporary heap indexOfChild

    indexOfParent index: 0 1 2 3 4 5 6 7 8 9 element: | 9| 8| 7| 6| | | | | | | MAX_ARRAY_SIZE indexOfLastElement array 100 3 0 0 8
  30. Method add() in Memory indexOfParent = (indexOfChild - 1) /

    2; temporary this.trickleUp(indexOfParent, indexOfChild); heap indexOfChild indexOfParent index: 0 1 2 3 4 5 6 7 8 9 element: | 9| 8| 7| 6| | | | | | | MAX_ARRAY_SIZE indexOfLastElement array 100 3 0 0 8
  31. Method get() • Gets an item from the top of

    the heap  Returns an address (pointer) to the element at array index zero • Example method call: • int top = heap.get();
  32. Method get() in Memory T root = array[0]; return root;

    heap root index: 0 1 2 3 4 5 6 7 8 9 element: | 9| 8| 7| 6| | | | | | | MAX_ARRAY_SIZE indexOfLastElement array 100 3
  33. Method get() in Memory int top = heap.get(); heap top

    index: 0 1 2 3 4 5 6 7 8 9 element: | 9| 8| 7| 6| | | | | | | MAX_ARRAY_SIZE indexOfLastElement array 100 3
  34. Method remove() in Memory int next = heap.remove(); heap index:

    0 1 2 3 4 5 6 7 8 9 element: | 9| 8| 7| 6| 5| 4| 3| | | | MAX_ARRAY_SIZE indexOfLastElement array 100 6
  35. Method remove() in Memory T root = array[0]; array[0] =

    array[indexOfLastElement]; heap root index: 0 1 2 3 4 5 6 7 8 9 element: | 3| 8| 7| 6| 5| 4| 3| | | | MAX_ARRAY_SIZE indexOfLastElement array 100 6 9
  36. Method remove() in Memory indexOfLastElement--; this.trickleDown(0); heap root index: 0

    1 2 3 4 5 6 7 8 9 element: | 3| 8| 7| 6| 5| 4| | | | | MAX_ARRAY_SIZE indexOfLastElement array 100 5 9
  37. Method remove() in Memory int indexOfLeftChild = 2 * indexOfParent

    + 1; int indexOfRightChild = 2 * indexOfParent + 2; heap indexOfParent indexOfLeftChild indexOfRightChild index: 0 1 2 3 4 5 6 7 8 9 element: | 3| 8| 7| 6| 5| 4| | | | | MAX_ARRAY_SIZE indexOfLastElement array 100 5 1 0 2
  38. Method remove() in Memory int indexOfLargestOfThreeNodes=indexOfParent; heap indexOfParent indexOfLeftChild indexOfRightChild

    indexOfLargestOfThreeNodes index: 0 1 2 3 4 5 6 7 8 9 element: | 3| 8| 7| 6| 5| 4| | | | | MAX_ARRAY_SIZE indexOfLastElement array 100 5 1 0 2 0
  39. Method remove() in Memory //calculate index of largest of 3

    nodes indexOfParent heap indexOfLeftChild indexOfRightChild indexOfLargestOfThreeNodes index: 0 1 2 3 4 5 6 7 8 9 element: | 3| 8| 7| 6| 5| 4| | | | | MAX_ARRAY_SIZE indexOfLastElement array 100 5 1 0 2 1
  40. Method remove() in Memory //swap parent with largest child indexOfParent

    heap indexOfLeftChild indexOfRightChild indexOfLargestOfThreeNodes index: 0 1 2 3 4 5 6 7 8 9 element: | 8| 3| 7| 6| 5| 4| | | | | MAX_ARRAY_SIZE indexOfLastElement array 100 5 1 0 2 1
  41. Method remove() in Memory this.trickleDown(indexOfLargestOfThreeNodes); indexOfParent heap indexOfLeftChild indexOfRightChild indexOfLargestOfThreeNodes

    index: 0 1 2 3 4 5 6 7 8 9 element: | 8| 3| 7| 6| 5| 4| | | | | MAX_ARRAY_SIZE indexOfLastElement array 100 5 1 0 2 1
  42. Method remove() in Memory int indexOfLeftChild = 2 * indexOfParent

    + 1; indexOfParent int indexOfRightChild = 2 * indexOfParent + 2; heap indexOfLeftChild indexOfRightChild index: 0 1 2 3 4 5 6 7 8 9 element: | 8| 3| 7| 6| 5| 4| | | | | MAX_ARRAY_SIZE indexOfLastElement array 100 5 3 1 4
  43. Method remove() in Memory indexOfParent int indexOfLargestOfThreeNodes = indexOfParent; heap

    indexOfLeftChild indexOfRightChild indexOfLargestOfThreeNodes index: 0 1 2 3 4 5 6 7 8 9 element: | 8| 3| 7| 6| 5| 4| | | | | MAX_ARRAY_SIZE indexOfLastElement array 100 5 3 1 4 1
  44. Method remove() in Memory //calculate index of largest of 3

    nodes indexOfParent heap indexOfLeftChild indexOfRightChild indexOfLargestOfThreeNodes index: 0 1 2 3 4 5 6 7 8 9 element: | 8| 3| 7| 6| 5| 4| | | | | MAX_ARRAY_SIZE indexOfLastElement array 100 5 3 1 4 3
  45. Method remove() in Memory //swap parent with largest child indexOfParent

    heap indexOfLeftChild indexOfRightChild indexOfLargestOfThreeNodes index: 0 1 2 3 4 5 6 7 8 9 element: | 8| 6| 7| 3| 5| 4| | | | | MAX_ARRAY_SIZE indexOfLastElement array 100 5 3 1 4 3
  46. Method remove() in Memory this.trickleDown(indexOfLargestOfThreeNodes); indexOfParent heap indexOfLeftChild indexOfRightChild indexOfLargestOfThreeNodes

    index: 0 1 2 3 4 5 6 7 8 9 element: | 8| 6| 7| 3| 5| 4| | | | | MAX_ARRAY_SIZE indexOfLastElement array 100 5 3 1 4 3
  47. Method remove() in Memory int indexOfLeftChild = 2 * indexOfParent

    + 1; indexOfParent int indexOfRightChild = 2 * indexOfParent + 2; heap indexOfLeftChild indexOfRightChild index: 0 1 2 3 4 5 6 7 8 9 element: | 8| 6| 7| 3| 5| 4| | | | | MAX_ARRAY_SIZE indexOfLastElement array 100 5 7 3 8
  48. Method remove() in Memory return root;} int next = heap.remove();

    heap next root index: 0 1 2 3 4 5 6 7 8 9 element: | 8| 6| 7| 3| 5| 4| | | | | MAX_ARRAY_SIZE indexOfLastElement array 100 5 9
  49. Heap main() Method • See the main() method for example

    code that instantiates a Heap object and calls the methods of the class  See the last method in Heap.java  For output, see the toString() method
  50. Example Code • QueueInterface interface  See QueueInterface.java  An

    interface is a list of method prototypes  Queue methods: empty(), offer(), poll(), peek()
  51. Example Code • PriorityQueue class  See PriorityQueue.java  This

    priority queue contains a heap & calls the heap’s methods  Same method names for Queue class, but a PriorityQueue (first-in highest-out) works slightly different than a Queue (first-in last-out)
  52. PriorityQueue in Memory • Instantiating a PriorityQueue object will call

    the constructor, which initializes the data fields public PriorityQueue() { } • Data fields: private Heap<T> heap = new Heap<T>();
  53. PriorityQueue in Memory • PriorityQueue object (of Integers) QueueInterface<Integer> queue

    = new PriorityQueue<Integer>(); queue index: 0 1 2 3 4 5 6 7 8 9 element: | | | | | | | | | | | MAX_ARRAY_SIZE indexOfLastElement array 100 -1 heap
  54. PriorityQueue & Heap Methods • PriorityQueue  empty()  offer(T

    element)  poll()  peek() • Heap methods  heap.empty()  heap.add(element)  heap.remove()  heap.get()
  55. PriorityQueue main() Method • See the main() method for example

    code that instantiates a PriorityQueue object and calls the methods of the class  See the last method in PriorityQueue.java
  56. Task Manager • Before the next class, you need to:

    1.Do the assignment corresponding to this lecture 2.Email me any questions you may have about the material 3.Turn in the assignment before the due date 4.Eat some grits!