lec 6 feb 17, 2011
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Lec 6 Feb 17, 2011](https://reader035.vdocuments.site/reader035/viewer/2022062422/56813ca6550346895da65538/html5/thumbnails/1.jpg)
Lec 6 Feb 17, 2011
Section 2.5 of text (review of heap)
Chapter 3
![Page 2: Lec 6 Feb 17, 2011](https://reader035.vdocuments.site/reader035/viewer/2022062422/56813ca6550346895da65538/html5/thumbnails/2.jpg)
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.
![Page 3: Lec 6 Feb 17, 2011](https://reader035.vdocuments.site/reader035/viewer/2022062422/56813ca6550346895da65538/html5/thumbnails/3.jpg)
Min Heap with 9 Nodes
Complete binary tree with 9 nodes.
![Page 4: Lec 6 Feb 17, 2011](https://reader035.vdocuments.site/reader035/viewer/2022062422/56813ca6550346895da65538/html5/thumbnails/4.jpg)
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
6 7 9 3
8 6
3
![Page 5: Lec 6 Feb 17, 2011](https://reader035.vdocuments.site/reader035/viewer/2022062422/56813ca6550346895da65538/html5/thumbnails/5.jpg)
Max Heap With 9 Nodes
Example of a Max-heap
9
8
6 7 2 6
5 1
7
![Page 6: Lec 6 Feb 17, 2011](https://reader035.vdocuments.site/reader035/viewer/2022062422/56813ca6550346895da65538/html5/thumbnails/6.jpg)
Heap Height
Since a heap is a complete binary tree, the height of an n node heap is log2 (n+1).
![Page 7: Lec 6 Feb 17, 2011](https://reader035.vdocuments.site/reader035/viewer/2022062422/56813ca6550346895da65538/html5/thumbnails/7.jpg)
9 8 7 6 7 2 6 5 1
1 2 3 4 5 6 7 8 9 100
A Heap Is Efficiently Represented As An Array
9
8
6 7 2 6
5 1
7
![Page 8: Lec 6 Feb 17, 2011](https://reader035.vdocuments.site/reader035/viewer/2022062422/56813ca6550346895da65538/html5/thumbnails/8.jpg)
Moving Up And Down A Heap
9
8
6 7 2 6
5 1
7
1
2 3
4 5 6 7
8 9Parent of node with index k is k/2
Left child of a node with index j is 2*jRight child of a node with index j is 2*j + 1
![Page 9: Lec 6 Feb 17, 2011](https://reader035.vdocuments.site/reader035/viewer/2022062422/56813ca6550346895da65538/html5/thumbnails/9.jpg)
Putting An Element Into A Max Heap
Place to add the new key
9
8
6 7 2 6
5 1
7
7
![Page 10: Lec 6 Feb 17, 2011](https://reader035.vdocuments.site/reader035/viewer/2022062422/56813ca6550346895da65538/html5/thumbnails/10.jpg)
Putting An Element Into A Max Heap
Example: New element is 5.
9
8
6 7 2 6
5 1
7
75
![Page 11: Lec 6 Feb 17, 2011](https://reader035.vdocuments.site/reader035/viewer/2022062422/56813ca6550346895da65538/html5/thumbnails/11.jpg)
Putting An Element Into A Max Heap
New element is 20.
9
8
6
7
2 6
5 1
7
7
7
![Page 12: Lec 6 Feb 17, 2011](https://reader035.vdocuments.site/reader035/viewer/2022062422/56813ca6550346895da65538/html5/thumbnails/12.jpg)
Putting An Element Into A Max Heap
New element is 20.
9
8
6
7
2 6
5 1
7
77
![Page 13: Lec 6 Feb 17, 2011](https://reader035.vdocuments.site/reader035/viewer/2022062422/56813ca6550346895da65538/html5/thumbnails/13.jpg)
Putting An Element Into A Max Heap
New element is 20.
9
86
7
2 6
5 1
7
77
![Page 14: Lec 6 Feb 17, 2011](https://reader035.vdocuments.site/reader035/viewer/2022062422/56813ca6550346895da65538/html5/thumbnails/14.jpg)
Putting An Element Into A Max Heap
New element is 20.
9
86
7
2 6
5 1
7
77
20
![Page 15: Lec 6 Feb 17, 2011](https://reader035.vdocuments.site/reader035/viewer/2022062422/56813ca6550346895da65538/html5/thumbnails/15.jpg)
Putting An Element Into A Max Heap
Complete binary tree with 11 nodes.
9
86
7
2 6
5 1
7
77
20
![Page 16: Lec 6 Feb 17, 2011](https://reader035.vdocuments.site/reader035/viewer/2022062422/56813ca6550346895da65538/html5/thumbnails/16.jpg)
Putting An Element Into A Max Heap
New element is 15.
9
86
7
2 6
5 1
7
77
20
![Page 17: Lec 6 Feb 17, 2011](https://reader035.vdocuments.site/reader035/viewer/2022062422/56813ca6550346895da65538/html5/thumbnails/17.jpg)
Putting An Element Into A Max Heap
New element is 15.
9
8
6
7
2 6
5 1
7
77
20
8
![Page 18: Lec 6 Feb 17, 2011](https://reader035.vdocuments.site/reader035/viewer/2022062422/56813ca6550346895da65538/html5/thumbnails/18.jpg)
Putting An Element Into A Max Heap
New element is 15.
8
6
7
2 6
5 1
7
77
20
8
9
15
![Page 19: Lec 6 Feb 17, 2011](https://reader035.vdocuments.site/reader035/viewer/2022062422/56813ca6550346895da65538/html5/thumbnails/19.jpg)
Complexity of insert
Complexity is O(log n), where n is heap size.
8
6
7
2 6
5 1
7
77
20
8
9
15
![Page 20: Lec 6 Feb 17, 2011](https://reader035.vdocuments.site/reader035/viewer/2022062422/56813ca6550346895da65538/html5/thumbnails/20.jpg)
DeleteMax operation
Max element is in the root.
8
6
7
2 6
5 1
7
77
20
8
9
15
![Page 21: Lec 6 Feb 17, 2011](https://reader035.vdocuments.site/reader035/viewer/2022062422/56813ca6550346895da65538/html5/thumbnails/21.jpg)
DeleteMax
After max element is removed.
8
6
7
2 6
5 1
7
77 8
9
15
![Page 22: Lec 6 Feb 17, 2011](https://reader035.vdocuments.site/reader035/viewer/2022062422/56813ca6550346895da65538/html5/thumbnails/22.jpg)
DeleteMax
Heap with 10 nodes. Location needs to be vacated. Find the right place to reinsert 8.
8
6
7
2 6
5 1
7
77 8
9
15
![Page 23: Lec 6 Feb 17, 2011](https://reader035.vdocuments.site/reader035/viewer/2022062422/56813ca6550346895da65538/html5/thumbnails/23.jpg)
DeleteMax
6
7
2 6
5 1
7
77
9
15
Reinsert 8 into the heap.
![Page 24: Lec 6 Feb 17, 2011](https://reader035.vdocuments.site/reader035/viewer/2022062422/56813ca6550346895da65538/html5/thumbnails/24.jpg)
DeleteMax
Reinsert 8 into the heap.
6
7
2 6
5 1
7
77
9
15
![Page 25: Lec 6 Feb 17, 2011](https://reader035.vdocuments.site/reader035/viewer/2022062422/56813ca6550346895da65538/html5/thumbnails/25.jpg)
DeleteMax
Reinsert 8 into the heap.
6
7
2 6
5 1
7
77
9
15
8
![Page 26: Lec 6 Feb 17, 2011](https://reader035.vdocuments.site/reader035/viewer/2022062422/56813ca6550346895da65538/html5/thumbnails/26.jpg)
DeleteMax – Another example
Max element is 15.
6
7
2 6
5 1
7
77
9
15
8
![Page 27: Lec 6 Feb 17, 2011](https://reader035.vdocuments.site/reader035/viewer/2022062422/56813ca6550346895da65538/html5/thumbnails/27.jpg)
DeleteMax – Ex 2
After max element is removed.
6
7
2 6
5 1
7
77
9
8
![Page 28: Lec 6 Feb 17, 2011](https://reader035.vdocuments.site/reader035/viewer/2022062422/56813ca6550346895da65538/html5/thumbnails/28.jpg)
DeleteMax – Ex 2
Heap with 9 nodes.
6
7
2 6
5 1
7
77
9
8
![Page 29: Lec 6 Feb 17, 2011](https://reader035.vdocuments.site/reader035/viewer/2022062422/56813ca6550346895da65538/html5/thumbnails/29.jpg)
DeleteMax – Ex 2
Reinsert 7.
6 2 6
5 1
79
8
![Page 30: Lec 6 Feb 17, 2011](https://reader035.vdocuments.site/reader035/viewer/2022062422/56813ca6550346895da65538/html5/thumbnails/30.jpg)
DeleteMax – Ex 2
Reinsert 7.
6 2 6
5 1
7
9
8
![Page 31: Lec 6 Feb 17, 2011](https://reader035.vdocuments.site/reader035/viewer/2022062422/56813ca6550346895da65538/html5/thumbnails/31.jpg)
DeleteMax – Ex 2
Reinsert 7.
6 2 6
5 1
7
9
8
7
![Page 32: Lec 6 Feb 17, 2011](https://reader035.vdocuments.site/reader035/viewer/2022062422/56813ca6550346895da65538/html5/thumbnails/32.jpg)
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).
6 2 6
5 1
7
9
8
7
![Page 33: Lec 6 Feb 17, 2011](https://reader035.vdocuments.site/reader035/viewer/2022062422/56813ca6550346895da65538/html5/thumbnails/33.jpg)
Delete a key at a given index
Want an algorithm of complexity O(log n).
6 2 6
5 1
7
9
8
7
Delete (2)
![Page 34: Lec 6 Feb 17, 2011](https://reader035.vdocuments.site/reader035/viewer/2022062422/56813ca6550346895da65538/html5/thumbnails/34.jpg)
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?
6 2 6
5 1
7
9
8
7
Delete (2)
similar to DeleteMax
![Page 35: Lec 6 Feb 17, 2011](https://reader035.vdocuments.site/reader035/viewer/2022062422/56813ca6550346895da65538/html5/thumbnails/35.jpg)
Delete a key at a given index : Ex – 2
16 2 6
15
7
19
18
17
Delete (6)
Adjustment may require percolate_up or percolate_down
![Page 36: Lec 6 Feb 17, 2011](https://reader035.vdocuments.site/reader035/viewer/2022062422/56813ca6550346895da65538/html5/thumbnails/36.jpg)
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?