Lec 6 Section 2 5 of text review




































- Slides: 36
Lec 6 Section 2. 5 of text (review of heap) Chapter 3 Feb 17, 2011
Review of heap (Sec 2. 5) Heap is a data structure that supports a priority queue. Two versions (Max-heap, min-heap) Max-heap operations (can do in O(log n) time. Insert(H, x) – add x to H. Delete-max(H) – remove the max elt. from H. Other operations: increase-key, delete(j) – delete the key stored in index j of heap etc. Operations that take O(n) time: search(x), delete(x) etc.
Min Heap with 9 Nodes Complete binary tree with 9 nodes.
Min Heap With 9 Nodes 2 4 6 8 3 7 9 6 Min-heap property: A[k] <= A[2*k] (if 2*k <= n) and A[k] <= A[2*k+1] (if 2*k+1 <= n). 3
Max Heap With 9 Nodes 9 8 6 5 7 7 2 1 Example of a Max-heap 6
Heap Height Since a heap is a complete binary tree, the height of an n node heap is log 2 (n+1).
A Heap Is Efficiently Represented As An Array 9 8 7 6 5 7 2 1 0 9 8 7 6 7 2 6 5 1 1 2 3 4 5 6 7 8 9 10 6
Moving Up And Down A Heap 1 9 2 3 8 7 4 5 6 7 5 1 8 9 7 6 2 Parent of node with index k is k/2 Left child of a node with index j is 2*j Right child of a node with index j is 2*j + 1 6
Putting An Element Into A Max Heap 9 8 7 6 5 7 1 2 7 Place to add the new key 6
Putting An Element Into A Max Heap 9 8 7 6 5 7 1 2 75 Example: New element is 5. 6
Putting An Element Into A Max Heap 9 8 7 6 5 7 1 7 New element is 20. 2 6
Putting An Element Into A Max Heap 9 8 7 6 5 2 1 7 New element is 20. 6
Putting An Element Into A Max Heap 9 7 6 5 8 1 7 New element is 20. 2 6
Putting An Element Into A Max Heap 20 9 7 6 5 8 1 7 New element is 20. 2 6
Putting An Element Into A Max Heap 20 9 7 6 5 8 1 2 7 Complete binary tree with 11 nodes. 6
Putting An Element Into A Max Heap 20 9 7 6 5 8 1 7 New element is 15. 2 6
Putting An Element Into A Max Heap 20 9 7 6 5 2 1 7 8 New element is 15. 6
Putting An Element Into A Max Heap 20 7 15 6 5 9 1 7 2 8 New element is 15. 6
Complexity of insert 20 7 15 6 5 9 1 7 2 6 8 Complexity is O(log n), where n is heap size.
Delete. Max operation 20 7 15 6 5 9 1 7 2 8 Max element is in the root. 6
Delete. Max 7 15 6 5 9 1 7 2 8 After max element is removed. 6
Delete. Max 7 15 6 5 9 1 7 2 8 Heap with 10 nodes. Location needs to be vacated. Find the right place to reinsert 8. 6
Delete. Max 7 15 6 5 9 1 7 Reinsert 8 into the heap. 2 6
Delete. Max 15 7 6 5 9 1 2 7 Reinsert 8 into the heap. 6
Delete. Max 15 9 7 6 5 8 1 2 7 Reinsert 8 into the heap. 6
Delete. Max – Another example 15 9 7 6 5 8 1 7 Max element is 15. 2 6
Delete. Max – Ex 2 9 7 6 5 8 1 2 7 After max element is removed. 6
Delete. Max – Ex 2 9 7 6 5 8 1 7 Heap with 9 nodes. 2 6
Delete. Max – Ex 2 9 6 5 7 8 1 Reinsert 7. 2 6
Delete. Max – Ex 2 9 7 6 5 8 1 Reinsert 7. 2 6
Delete. Max – Ex 2 9 8 6 5 7 7 1 Reinsert 7. 2 6
Complexity of Delete. Max 9 8 6 5 7 7 2 1 • Complexity is O(log n). • Involves working down the heap, two comparisons and 1 assignment per level. There at most log 2 (n+1) levels. • Total complexity <= 3 log 2 (n+1) = O(log n). 6
Delete a key at a given index 9 Delete (2) 8 6 5 7 7 2 1 Want an algorithm of complexity O(log n). 6
Delete a key at a given index 9 Delete (2) 8 6 5 7 7 1 To perform Delete(j): A[j] = A[size]; size--; adjust the heap at position j; How to adjust? 2 similar to Delete. Max 6
Delete a key at a given index : Ex – 2 19 Delete (6) 18 16 17 7 2 6 15 Adjustment may require percolate_up or percolate_down
Augmenting a heap Suggest a data structure that acts like both a min-heap and max-heap. i. e. , it should support all three operations in O(log n) time: • Insert • Delete. Min • Delete. Max Any suggestion?