Download presentation

Presentation is loading. Please wait.

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
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). 2 4 6793 86 3

5
Max Heap With 9 Nodes Example of a Max-heap 9 8 6726 51 7

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

7
987672651 123456789100 A Heap Is Efficiently Represented As An Array 9 8 6726 51 7

8
Moving Up And Down A Heap 9 8 6726 51 7 1 23 4 56 7 89 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
Putting An Element Into A Max Heap Place to add the new key 9 8 6726 51 7 7

10
Putting An Element Into A Max Heap Example: New element is 5. 9 8 6726 51 7 75

11
Putting An Element Into A Max Heap New element is 20. 9 8 6 7 26 51 7 7 7

12
Putting An Element Into A Max Heap New element is 20. 9 8 6 7 26 51 7 7 7

13
Putting An Element Into A Max Heap New element is 20. 9 86 7 26 51 7 7 7

14
Putting An Element Into A Max Heap New element is 20. 9 86 7 26 51 7 7 7 20

15
Putting An Element Into A Max Heap Complete binary tree with 11 nodes. 9 86 7 26 51 7 7 7 20

16
Putting An Element Into A Max Heap New element is 15. 9 86 7 26 51 7 7 7 20

17
Putting An Element Into A Max Heap New element is 15. 9 8 6 7 26 51 7 7 7 20 8

18
Putting An Element Into A Max Heap New element is 15. 8 6 7 26 51 7 7 7 20 8 9 15

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

20
DeleteMax operation Max element is in the root. 8 6 7 26 51 7 7 7 20 8 9 15

21
DeleteMax After max element is removed. 8 6 7 26 51 7 7 7 8 9 15

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

23
DeleteMax 6 7 26 51 7 7 7 9 15 Reinsert 8 into the heap.

24
DeleteMax Reinsert 8 into the heap. 6 7 26 51 7 7 7 9 15

25
DeleteMax Reinsert 8 into the heap. 6 7 26 51 7 7 7 9 15 8

26
DeleteMax – Another example Max element is 15. 6 7 26 51 7 7 7 9 15 8

27
DeleteMax – Ex 2 After max element is removed. 6 7 26 51 7 7 7 9 8

28
DeleteMax – Ex 2 Heap with 9 nodes. 6 7 26 51 7 7 7 9 8

29
DeleteMax – Ex 2 Reinsert 7. 626 51 79 8

30
DeleteMax – Ex 2 Reinsert 7. 626 51 7 9 8

31
DeleteMax – Ex 2 Reinsert 7. 626 51 7 9 8 7

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

33
Delete a key at a given index Want an algorithm of complexity O(log n). 626 51 7 9 8 7 Delete (2)

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

35
Delete a key at a given index : Ex – 2 1626 15 7 19 18 17 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?

Similar presentations

© 2021 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google