Appendix A: Heap Bottom-up Construction Course: Algorithm Analysis and Design SinhVienZone.Com... Heap bottom up constructionThis method views every position in the array as the root of
Trang 1Appendix A: Heap
Bottom-up Construction
Course: Algorithm Analysis and Design
SinhVienZone.Com
Trang 2Heap bottom up construction
This method views every position in the array as the root of a
small heap and uses downheap procedure for such small
heaps.
SinhVienZone.Com
Trang 3Heap bottom-up construction procedure
procedure build_heap;
begin
for k:= M div 2 downto 1 do
downheap(k);
end
M: the number of elements in the heap.
The keys in a[ (M div 2)+1 M] each form heaps of one
element, so they satisfy the heap condition and don’t need to be checked.SinhVienZone.Com
Trang 4 Property: Bottom-up heap construction is
linear-time.
For example: To build a heap of 127 elements, the
method calls downheap on
- 64 heaps of size 1
- 32 heaps of size 3
- 16 heaps of size 7
- 8 heaps of size 15
- 4 heaps of size 31
- 2 heaps of size 63
- 1 heaps of size 127
So the method needs 64.0 + 32.1 + 16.2 + 8.3 + 4.4 + 2.5 + 1.6 = 120 “promotions”.
0.26 + 1.25 + 2.2SinhVienZone.Com4 + 3.23 + 4.22 + 5.21 + 6.20
Trang 50.26 + 1.25 + 2.24 + 3.23 + 4.22 + 5.21 + 6.20