Download presentation

Presentation is loading. Please wait.

Published byKayleigh Bendel Modified over 2 years ago

1
1 Appendix A: Heap Bottom- up Construction Course: Algorithm Analysis and Design

2
2 Heap 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. Figure 1: The heap created from the array of characters: A, S, O, R, T, I, N, G, E, X, A, M, P, L, E.

3
3 Heap 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.

4
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 heaps of size heaps of size heaps of size heaps of size heaps of size heaps of size 127 So the method needs = 120 “promotions”

5
5 (M= 127 = ) m = 7 For M = 2 m, an upper bound on the number of comparisions is (1-1)2 m-1 + (2-1)2 m-2 + (3 -1)2 m-3 +… + (k-1)2 m-k + … (m-1-1)2 m-(m-1) + (m -1)2 m-m. = 1.2 m m m m-5 +…+ (k-1)2 m-k + … (m-2)2 1 + (m -1)2 0 = (2 m m-3 + …+ 2 0 ) + (2 m-3 + …+ 2 0 ) + (2 m-4 + …+ 2 0 ) …+( ) + ( ) + 1 = (2 m-1 -1)+ (2 m-2 -1) + … (2 3 -1) + (2 2 -1) +(2 1 -1) = (2 m m-2 + … )– m +1 = (2 m m-2 + … ) – m = (2 m -1) – m < M So the complexity of heap bottom-up building is O(M).

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google