![Page 1: 7/23/2009 Many thanks to David Sun for some of the included slides!](https://reader035.vdocuments.us/reader035/viewer/2022070502/56814ad0550346895db7eb41/html5/thumbnails/1.jpg)
7/23/2009Many thanks to David Sun for some of the included slides!
![Page 2: 7/23/2009 Many thanks to David Sun for some of the included slides!](https://reader035.vdocuments.us/reader035/viewer/2022070502/56814ad0550346895db7eb41/html5/thumbnails/2.jpg)
Impo
rtan
t Dat
es• Project 2 due– DESIGN, DESIGN, DESIGN!!! – You may have 0 or 1 partner.• NO EXCEPTIONS!
– Due Friday 7/24/2009 – 10pm• Midterm Review– Saturday 7/26/2009 – 1-4pm in 306 Soda
• Midterm 2– Tuesday 7/28/2009 – 5-6pm in 10 Evans
![Page 3: 7/23/2009 Many thanks to David Sun for some of the included slides!](https://reader035.vdocuments.us/reader035/viewer/2022070502/56814ad0550346895db7eb41/html5/thumbnails/3.jpg)
Queues (Review)
![Page 4: 7/23/2009 Many thanks to David Sun for some of the included slides!](https://reader035.vdocuments.us/reader035/viewer/2022070502/56814ad0550346895db7eb41/html5/thumbnails/4.jpg)
Queues (Review)
![Page 5: 7/23/2009 Many thanks to David Sun for some of the included slides!](https://reader035.vdocuments.us/reader035/viewer/2022070502/56814ad0550346895db7eb41/html5/thumbnails/5.jpg)
What data structure would you use to store a queue?
•Hash Table•Binary Search Tree• Linked List•Array
![Page 6: 7/23/2009 Many thanks to David Sun for some of the included slides!](https://reader035.vdocuments.us/reader035/viewer/2022070502/56814ad0550346895db7eb41/html5/thumbnails/6.jpg)
Using a Hash Table to implement a Queue (not a “good” idea)
•Key is the # in line•Value is the object
in the queue•Keep track of the
current number
![Page 7: 7/23/2009 Many thanks to David Sun for some of the included slides!](https://reader035.vdocuments.us/reader035/viewer/2022070502/56814ad0550346895db7eb41/html5/thumbnails/7.jpg)
Regular Queues with an Array
6 3 2 8 9 7
Start End
• Insert 5
6 3 2 8 9 7 5
Start End
![Page 8: 7/23/2009 Many thanks to David Sun for some of the included slides!](https://reader035.vdocuments.us/reader035/viewer/2022070502/56814ad0550346895db7eb41/html5/thumbnails/8.jpg)
Regular Queues with an Array
6 3 2 8 9 7 5
Start End
3 2 8 9 7 5
Start End
• Remove First
![Page 9: 7/23/2009 Many thanks to David Sun for some of the included slides!](https://reader035.vdocuments.us/reader035/viewer/2022070502/56814ad0550346895db7eb41/html5/thumbnails/9.jpg)
Start
End
An Empty Queue
![Page 10: 7/23/2009 Many thanks to David Sun for some of the included slides!](https://reader035.vdocuments.us/reader035/viewer/2022070502/56814ad0550346895db7eb41/html5/thumbnails/10.jpg)
![Page 11: 7/23/2009 Many thanks to David Sun for some of the included slides!](https://reader035.vdocuments.us/reader035/viewer/2022070502/56814ad0550346895db7eb41/html5/thumbnails/11.jpg)
A Queue Based upon PriorityPriorityQueue
![Page 12: 7/23/2009 Many thanks to David Sun for some of the included slides!](https://reader035.vdocuments.us/reader035/viewer/2022070502/56814ad0550346895db7eb41/html5/thumbnails/12.jpg)
PriorityQueues Implemented with Sorted Arrays
Operation Run Time Other Requirements
size() O(1) Keep size var.
isEmpty() O(1) Call size() == 0
insert(…) O(n)
max() O(1)
removeMax() O(1)
![Page 13: 7/23/2009 Many thanks to David Sun for some of the included slides!](https://reader035.vdocuments.us/reader035/viewer/2022070502/56814ad0550346895db7eb41/html5/thumbnails/13.jpg)
PriorityQueues Implemented with Non-Sorted Arrays
Operation Run Time Other Requirements
size() O(1) Keep size var.
isEmpty() O(1) Call size() == 0
insert(…) O(1)*
max() O(n)
removeMax() O(n)
![Page 14: 7/23/2009 Many thanks to David Sun for some of the included slides!](https://reader035.vdocuments.us/reader035/viewer/2022070502/56814ad0550346895db7eb41/html5/thumbnails/14.jpg)
This is a Max Heap (And Heaps are AWESOME!)
![Page 15: 7/23/2009 Many thanks to David Sun for some of the included slides!](https://reader035.vdocuments.us/reader035/viewer/2022070502/56814ad0550346895db7eb41/html5/thumbnails/15.jpg)
Binary Heaps• A Binary Heap is a binary tree, with two
additional properties– Shape Property: It is a complete binary tree – a
binary tree in which every row is full, except possibly the bottom row, which is filled from left to right.
– Heap Property (or Heap Order Property): No child has a key greater than its parent's key. This property is applied recursively: any subtree of a binary heap is also a binary heap.
• If we use the notion of smaller than in the Heap Property we get a min-heap. We’ll look at max-heap in this lecture.
![Page 16: 7/23/2009 Many thanks to David Sun for some of the included slides!](https://reader035.vdocuments.us/reader035/viewer/2022070502/56814ad0550346895db7eb41/html5/thumbnails/16.jpg)
Heaps Implement PriorityQueue
![Page 17: 7/23/2009 Many thanks to David Sun for some of the included slides!](https://reader035.vdocuments.us/reader035/viewer/2022070502/56814ad0550346895db7eb41/html5/thumbnails/17.jpg)
max()
• Trivial: The heap-order property ensures that the entry with the maximum key is always at the top of the heap. Hence, we simply return the entry at the root node. – If the heap is empty, return null or throw an
exception.• Runs in O(1) time.
![Page 18: 7/23/2009 Many thanks to David Sun for some of the included slides!](https://reader035.vdocuments.us/reader035/viewer/2022070502/56814ad0550346895db7eb41/html5/thumbnails/18.jpg)
insert (8)
![Page 19: 7/23/2009 Many thanks to David Sun for some of the included slides!](https://reader035.vdocuments.us/reader035/viewer/2022070502/56814ad0550346895db7eb41/html5/thumbnails/19.jpg)
insert(8)Let x be the new entry (k, v). 1. Place the new entry x in the
bottom level of the tree, at the first free spot from the left. If the bottom level is full, start a new level with x at the far left.
2. If the new entry's key violates the heap-order property then compare x's key with its parent's key; if x's key is larger, we exchange x with its paren. Repeat the procedure with x's new parent.
Original
Dashed boxes show where the heap property violated
Re-heapify up
Inserting <8,v>
![Page 20: 7/23/2009 Many thanks to David Sun for some of the included slides!](https://reader035.vdocuments.us/reader035/viewer/2022070502/56814ad0550346895db7eb41/html5/thumbnails/20.jpg)
We might have to bubble it all the way up!
insert(18)
![Page 21: 7/23/2009 Many thanks to David Sun for some of the included slides!](https://reader035.vdocuments.us/reader035/viewer/2022070502/56814ad0550346895db7eb41/html5/thumbnails/21.jpg)
insert(18)Inserting <18,v>
What’s the time complexity of insert?
![Page 22: 7/23/2009 Many thanks to David Sun for some of the included slides!](https://reader035.vdocuments.us/reader035/viewer/2022070502/56814ad0550346895db7eb41/html5/thumbnails/22.jpg)
Remove Max
![Page 23: 7/23/2009 Many thanks to David Sun for some of the included slides!](https://reader035.vdocuments.us/reader035/viewer/2022070502/56814ad0550346895db7eb41/html5/thumbnails/23.jpg)
removeMax() 1. If the heap is empty, return
null or throw an exception. 2. Otherwise, remove the entry
at the root node. Replace the root with the last entry in the tree x, so that the tree is still complete.
3. If the root violates the heap property then compare x with it’s children, swap x with the child with the larger key, repeat until x is greater than or equal to its children or reach a leaf.
Starting Replace root
Re-heapify downFinal
![Page 24: 7/23/2009 Many thanks to David Sun for some of the included slides!](https://reader035.vdocuments.us/reader035/viewer/2022070502/56814ad0550346895db7eb41/html5/thumbnails/24.jpg)
There are tons of Heap Animations
• Heapify– http://students.ceid.upatras.gr/~perisian/
data_structure/HeapSort/heap_applet.html
![Page 25: 7/23/2009 Many thanks to David Sun for some of the included slides!](https://reader035.vdocuments.us/reader035/viewer/2022070502/56814ad0550346895db7eb41/html5/thumbnails/25.jpg)
Representing Trees (Review) Array representations are common for Heaps
10 5 15 3 7 13 18 2
Left Child at 2n + 1Right Child at 2n + 2
0 1 2 3 4 5 6 7 8 9
This is not a heap!
![Page 26: 7/23/2009 Many thanks to David Sun for some of the included slides!](https://reader035.vdocuments.us/reader035/viewer/2022070502/56814ad0550346895db7eb41/html5/thumbnails/26.jpg)
A Different Tree Implementation(don’t use the 0 index)
10 5 15 3 7 180 1 2 3 4 5 6 7 8 9 10
Left Child at 2nRight Child at 2n + 1
This is not a heap!
![Page 27: 7/23/2009 Many thanks to David Sun for some of the included slides!](https://reader035.vdocuments.us/reader035/viewer/2022070502/56814ad0550346895db7eb41/html5/thumbnails/27.jpg)
Remove Max
![Page 28: 7/23/2009 Many thanks to David Sun for some of the included slides!](https://reader035.vdocuments.us/reader035/viewer/2022070502/56814ad0550346895db7eb41/html5/thumbnails/28.jpg)
removeMax()Starting Replace root
Re-heapify downFinal
![Page 29: 7/23/2009 Many thanks to David Sun for some of the included slides!](https://reader035.vdocuments.us/reader035/viewer/2022070502/56814ad0550346895db7eb41/html5/thumbnails/29.jpg)
Bottom-Up Heap Construction• Suppose we are given a bunch of randomly
ordered entries, and want to make a heap out of them.
• What’s the obvious way– Apply insert to each item in O(n log n) time.
• A better way: bottomUpHeap()1.Make a complete tree out of the entries, in any
random order.2.Start from the last internal node (non-leaf node), in
reverse order of the level order traversal, heapify down the heap as in removeMax().
![Page 30: 7/23/2009 Many thanks to David Sun for some of the included slides!](https://reader035.vdocuments.us/reader035/viewer/2022070502/56814ad0550346895db7eb41/html5/thumbnails/30.jpg)
Example7
4 9
5 3 5 1
7
4 9
5 3 5 1
7
4 9
5 3 5 1
7
5 9
4 3 5 1
9
5 7
4 3 5 1
Why this works? We can argue inductively:
1. The leaf nodes satisfy the heap order property vacuously (they have no children).
2. Before we bubble an entry down, we know that its two child subtrees must be heaps. Hence, by bubbling the entry down, we create a larger heap rooted at the node where that entry started.
![Page 31: 7/23/2009 Many thanks to David Sun for some of the included slides!](https://reader035.vdocuments.us/reader035/viewer/2022070502/56814ad0550346895db7eb41/html5/thumbnails/31.jpg)
Cost of Bottom Up Construction
• If each internal node bubbles all the way down, then the running time is proportional to the sum of the heights of all the nodes in the tree.
• Turns out this sum is less than n, where n is the number of entries being coalesced into a heap.
• Hence, the running time is in O(n), which is better than inserting n entries into a heap individually.
![Page 32: 7/23/2009 Many thanks to David Sun for some of the included slides!](https://reader035.vdocuments.us/reader035/viewer/2022070502/56814ad0550346895db7eb41/html5/thumbnails/32.jpg)
Heap Sort Demo…
![Page 33: 7/23/2009 Many thanks to David Sun for some of the included slides!](https://reader035.vdocuments.us/reader035/viewer/2022070502/56814ad0550346895db7eb41/html5/thumbnails/33.jpg)
Running Times (Appendix)• We could use a list or array, sorted or unsorted,
to implement a priority queue. The following table shows running times for different implementations, with n entries in the queue.
Binary Heap Sorted List/Array Unsorted List/Array
max Theta(1) Theta(1) Theta(n)
insert (worst-cast) Theta(log n)* Theta(n) Theta(1)*
insert(best-case) Theta(1)* it depends Theta(1)*
removeMax (worst) Theta(log n) Theta(1) Theta(n)
removeMax (best) Theta(1) Theta(1) Theta(n)
* If you are using an array-based data structure, these running times assume that you don’t run of room. If you do, it will take Omega(n) time to allocate a larger array and copy them into it.