1 / 36

Lec 6 Feb 17, 2011

Lec 6 Feb 17, 2011 Section 2.5 of text (review of heap) Chapter 3. 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.

zinna
Download Presentation

Lec 6 Feb 17, 2011

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lec 6 Feb 17, 2011 • Section 2.5 of text (review of heap) • Chapter 3

  2. 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, decrease-key, delete(j) – delete the key stored in index j of heap etc. • Operations that take O(n) time: search(x), delete(x) etc.

  3. Min Heap with 9 Nodes Complete binary tree with 9 nodes.

  4. 2 4 3 6 7 9 3 8 6 Min Heap With 9 Nodes Min-heap property: A[k] <= A[2*k] (if 2*k <= n) and A[k] <= A[2*k+1] (if 2*k+1 <= n).

  5. 9 8 7 6 7 2 6 5 1 Max Heap With 9 Nodes Example of a Max-heap

  6. Heap Height Since a heap is a complete binary tree, the height of an n node heap is log2 (n+1).

  7. 9 8 7 6 7 2 6 5 1 A Heap Is Efficiently Represented As An Array 9 8 7 6 7 2 6 5 1 0 1 2 3 4 5 6 7 8 9 10

  8. 1 9 2 3 8 7 4 7 5 6 6 7 2 6 5 1 8 9 Moving Up And Down A Heap 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

  9. 9 8 7 6 7 2 6 5 1 7 Putting An Element Into A Max Heap Place to add the new key

  10. Putting An Element Into A Max Heap 9 8 7 6 7 2 6 5 1 7 5 Example: New element is 5.

  11. Putting An Element Into A Max Heap 9 8 7 6 2 6 7 5 1 7 7 New element is 20.

  12. Putting An Element Into A Max Heap 9 8 7 6 2 6 5 1 7 7 7 New element is 20.

  13. Putting An Element Into A Max Heap 9 7 6 8 2 6 5 1 7 7 7 New element is 20.

  14. Putting An Element Into A Max Heap 20 9 7 6 8 2 6 5 1 7 7 7 New element is 20.

  15. Putting An Element Into A Max Heap 20 9 7 6 8 2 6 5 1 7 7 7 Complete binary tree with 11 nodes.

  16. Putting An Element Into A Max Heap 20 9 7 6 8 2 6 5 1 7 7 7 New element is 15.

  17. Putting An Element Into A Max Heap 20 9 7 6 2 6 8 5 1 7 7 8 7 New element is 15.

  18. Putting An Element Into A Max Heap 20 7 15 6 9 2 6 8 5 1 7 7 8 7 New element is 15.

  19. 20 7 15 6 9 2 6 8 5 1 7 7 8 7 Complexity of insert Complexity is O(log n), where n is heap size.

  20. 20 7 15 6 9 2 6 8 5 1 7 7 8 7 DeleteMax operation Max element is in the root.

  21. DeleteMax 7 15 6 9 2 6 8 5 1 7 7 8 7 After max element is removed.

  22. DeleteMax 7 15 6 9 2 6 8 5 1 7 7 8 7 Heap with 10 nodes. Location needs to be vacated. Find the right place to reinsert 8.

  23. DeleteMax 7 15 6 9 2 6 5 1 7 7 7 Reinsert 8 into the heap.

  24. DeleteMax 15 7 6 9 2 6 5 1 7 7 7 Reinsert 8 into the heap.

  25. DeleteMax 15 9 7 6 2 6 8 5 1 7 7 7 Reinsert 8 into the heap.

  26. DeleteMax – Another example 15 9 7 6 2 6 8 5 1 7 7 7 Max element is 15.

  27. DeleteMax – Ex 2 9 7 6 2 6 8 5 1 7 7 7 After max element is removed.

  28. DeleteMax – Ex 2 9 7 6 2 6 8 5 1 7 7 7 Heap with 9 nodes.

  29. DeleteMax – Ex 2 9 7 6 2 6 8 5 1 Reinsert 7.

  30. DeleteMax – Ex 2 9 7 6 2 6 8 5 1 Reinsert 7.

  31. DeleteMax – Ex 2 9 8 7 6 7 2 6 5 1 Reinsert 7.

  32. 9 8 7 6 7 2 6 5 1 Complexity of DeleteMax • Complexity is O(log n). • Involves working down the heap, two comparisons and 1 assignment per level. There are at most log2 (n+1) levels. • Total complexity <= 3 log2 (n+1) = O(log n).

  33. 9 8 7 6 7 2 6 5 1 Delete a key at a given index Delete (2) • Want an algorithm of complexity O(log n).

  34. 9 8 7 6 7 2 6 5 1 Delete a key at a given index Delete (2) similar to DeleteMax To perform Delete(j): A[j] = A[size]; size--; adjust the heap at position j; How to adjust?

  35. 19 18 7 17 16 2 6 15 Delete a key at a given index : Ex – 2 Delete (6) Adjustment may require percolate_up or percolate_down

  36. 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 • DeleteMin • DeleteMax • Any suggestion?

More Related