chapter - 6.ppt
TRANSCRIPT
![Page 1: chapter - 6.ppt](https://reader036.vdocuments.us/reader036/viewer/2022062307/554f5ebdb4c905bb178b4593/html5/thumbnails/1.jpg)
Heap Data Structure
The heap data structure is an array object
that can be viewed as a nearly complete
binary tree.
![Page 2: chapter - 6.ppt](https://reader036.vdocuments.us/reader036/viewer/2022062307/554f5ebdb4c905bb178b4593/html5/thumbnails/2.jpg)
Heap
16
2
98
3
10
7
14
4 1
1
2 3
45 6 7
8 9 10
(a)
![Page 3: chapter - 6.ppt](https://reader036.vdocuments.us/reader036/viewer/2022062307/554f5ebdb4c905bb178b4593/html5/thumbnails/3.jpg)
Heap
1 2 3 4 5 6 7 8 9 10
16 14 10 8 7 9 3 2 4 1
(b)
![Page 4: chapter - 6.ppt](https://reader036.vdocuments.us/reader036/viewer/2022062307/554f5ebdb4c905bb178b4593/html5/thumbnails/4.jpg)
Heap
There are two kinds of binary heaps
• max-heaps
• min heaps
![Page 5: chapter - 6.ppt](https://reader036.vdocuments.us/reader036/viewer/2022062307/554f5ebdb4c905bb178b4593/html5/thumbnails/5.jpg)
Maintaining the Heap Property
The function of MAX-HEAPIFY is to let the
value at A[i] “float down” in the max-heap
so that the sub-tree rooted at index i
becomes a max-heap
![Page 6: chapter - 6.ppt](https://reader036.vdocuments.us/reader036/viewer/2022062307/554f5ebdb4c905bb178b4593/html5/thumbnails/6.jpg)
Maintaining the Heap Property
MAX-HEAPIFY(A, i)
l LEFT(i)
r RIGHT(i)
if l <= heap-size[A] and A[l] >A [i]
then largest l
else largest i
![Page 7: chapter - 6.ppt](https://reader036.vdocuments.us/reader036/viewer/2022062307/554f5ebdb4c905bb178b4593/html5/thumbnails/7.jpg)
Maintaining the Heap Property
If r<= heap-size[A] and A[r] > A[largest]
then largest r
If largest ≠ i
then exchange A[i] A[largest]
MAX-HEAPIFY(A, largest)
![Page 8: chapter - 6.ppt](https://reader036.vdocuments.us/reader036/viewer/2022062307/554f5ebdb4c905bb178b4593/html5/thumbnails/8.jpg)
Maintaining the Heap Property
16
2
914
3
10
7
4
8 1
1
2 3
45 6 7
8 9 10
i
(a)
![Page 9: chapter - 6.ppt](https://reader036.vdocuments.us/reader036/viewer/2022062307/554f5ebdb4c905bb178b4593/html5/thumbnails/9.jpg)
Maintaining the Heap Property
16
2
94
3
10
7
14
8 1
1
2 3
45 6 7
8 9 10
i
(b)
![Page 10: chapter - 6.ppt](https://reader036.vdocuments.us/reader036/viewer/2022062307/554f5ebdb4c905bb178b4593/html5/thumbnails/10.jpg)
Maintaining the Heap Property
16
2
96
3
10
7
14
4 1
1
2 3
45 6 7
8 9 10i
(c)
![Page 11: chapter - 6.ppt](https://reader036.vdocuments.us/reader036/viewer/2022062307/554f5ebdb4c905bb178b4593/html5/thumbnails/11.jpg)
Building a Heap
BUILD-MAX-HEAP(A)
heap-size[A] length[A]
for i length[A]/2 downto 1
do MAX-HEAPIFY(A, i)
![Page 12: chapter - 6.ppt](https://reader036.vdocuments.us/reader036/viewer/2022062307/554f5ebdb4c905bb178b4593/html5/thumbnails/12.jpg)
Building a Heap
A 4 1 3 2 16 9 10 14 8 7
![Page 13: chapter - 6.ppt](https://reader036.vdocuments.us/reader036/viewer/2022062307/554f5ebdb4c905bb178b4593/html5/thumbnails/13.jpg)
Building a Heap
4
14
92
10
3
16
1
8 7
1
2 3
45 6 7
8 9 10
i
(a)
![Page 14: chapter - 6.ppt](https://reader036.vdocuments.us/reader036/viewer/2022062307/554f5ebdb4c905bb178b4593/html5/thumbnails/14.jpg)
Building a Heap
4
14
92
10
3
16
1
8 7
1
2 3
45 6 7
8 9 10
i
(b)
![Page 15: chapter - 6.ppt](https://reader036.vdocuments.us/reader036/viewer/2022062307/554f5ebdb4c905bb178b4593/html5/thumbnails/15.jpg)
Building a Heap
4
2
914
10
3
16
1
8 7
1
2 3
45 6 7
8 9 10
i
(c)
![Page 16: chapter - 6.ppt](https://reader036.vdocuments.us/reader036/viewer/2022062307/554f5ebdb4c905bb178b4593/html5/thumbnails/16.jpg)
Building a Heap
4
2
914
3
10
16
1
8 7
1
2 3
45 6 7
8 9 10
i
(d)
![Page 17: chapter - 6.ppt](https://reader036.vdocuments.us/reader036/viewer/2022062307/554f5ebdb4c905bb178b4593/html5/thumbnails/17.jpg)
Building a Heap
4
2
914
3
10
7
16
8 1
1
2 3
45 6 7
8 9 10
i
(e)
![Page 18: chapter - 6.ppt](https://reader036.vdocuments.us/reader036/viewer/2022062307/554f5ebdb4c905bb178b4593/html5/thumbnails/18.jpg)
Building a Heap
16
2
98
3
10
7
14
4 1
1
2 3
45 6 7
8 9 10
(f)
![Page 19: chapter - 6.ppt](https://reader036.vdocuments.us/reader036/viewer/2022062307/554f5ebdb4c905bb178b4593/html5/thumbnails/19.jpg)
Heapsort Algorithm
HEAPSORT(A)
BUILD-MAX-HEAP(A)
for I length[A] downto 2
do exchange A[1] A[i]
heap-size[A] heap-size[A] -1
MAX-HEAPIFY(A,1)
![Page 20: chapter - 6.ppt](https://reader036.vdocuments.us/reader036/viewer/2022062307/554f5ebdb4c905bb178b4593/html5/thumbnails/20.jpg)
Heapsort Algorithm
16
2
98
3
10
7
14
4 1
(a)
![Page 21: chapter - 6.ppt](https://reader036.vdocuments.us/reader036/viewer/2022062307/554f5ebdb4c905bb178b4593/html5/thumbnails/21.jpg)
Heapsort Algorithm
14
2
94
3
10
7
8
1
(b)
16i
![Page 22: chapter - 6.ppt](https://reader036.vdocuments.us/reader036/viewer/2022062307/554f5ebdb4c905bb178b4593/html5/thumbnails/22.jpg)
Heapsort Algorithm
10
2
14
3
9
7
8
(c)
16
i
14
![Page 23: chapter - 6.ppt](https://reader036.vdocuments.us/reader036/viewer/2022062307/554f5ebdb4c905bb178b4593/html5/thumbnails/23.jpg)
Heapsort Algorithm
9
14
2
3
7
8
(d)
16
i
1410
![Page 24: chapter - 6.ppt](https://reader036.vdocuments.us/reader036/viewer/2022062307/554f5ebdb4c905bb178b4593/html5/thumbnails/24.jpg)
Heapsort Algorithm
8
14
3
2
7
(e)
16
i
1410
19
![Page 25: chapter - 6.ppt](https://reader036.vdocuments.us/reader036/viewer/2022062307/554f5ebdb4c905bb178b4593/html5/thumbnails/25.jpg)
Heapsort Algorithm
7
1
3
2
4
(f)
16
i
1410
98
![Page 26: chapter - 6.ppt](https://reader036.vdocuments.us/reader036/viewer/2022062307/554f5ebdb4c905bb178b4593/html5/thumbnails/26.jpg)
Heapsort Algorithm
4
1
32
(g)
16
i
1410
987
![Page 27: chapter - 6.ppt](https://reader036.vdocuments.us/reader036/viewer/2022062307/554f5ebdb4c905bb178b4593/html5/thumbnails/27.jpg)
Heapsort Algorithm
3
12
(h)
16
i
1410
9874
![Page 28: chapter - 6.ppt](https://reader036.vdocuments.us/reader036/viewer/2022062307/554f5ebdb4c905bb178b4593/html5/thumbnails/28.jpg)
Heapsort Algorithm
2
1
(i)
16
i
1410
9874
3
![Page 29: chapter - 6.ppt](https://reader036.vdocuments.us/reader036/viewer/2022062307/554f5ebdb4c905bb178b4593/html5/thumbnails/29.jpg)
Heapsort Algorithm
1
2
(j)
16
i
1410
9874
3
![Page 30: chapter - 6.ppt](https://reader036.vdocuments.us/reader036/viewer/2022062307/554f5ebdb4c905bb178b4593/html5/thumbnails/30.jpg)
Heapsort Algorithm
A 1 2 3 4 7 8 9 10 14 16
k