Share
Embed
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
60 slides
-
Terminology • A priority value is a value assigned to
an item which indicates its priority Usually used in a priority queue -
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 -
Terminology • For example, patients at a hospital are put
into a priority queue with the most urgent patient at the beginning of the queue -
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 -
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 -
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 -
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]; -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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; } -
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 -
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 -
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 -
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 -
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 -
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 -
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; } -
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 -
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 -
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 -
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 -
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 -
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(); -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
Example Code • QueueInterface interface See QueueInterface.java An
interface is a list of method prototypes Queue methods: empty(), offer(), poll(), peek() -
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) -
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>(); -
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 -
PriorityQueue & Heap Methods • Each PriorityQueue method calls the
corresponding Heap method -
PriorityQueue & Heap Methods • PriorityQueue empty() offer(T
element) poll() peek() • Heap methods heap.empty() heap.add(element) heap.remove() heap.get() -
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 -
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!