{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

rec09 - Heap Algorithms Parent(A i Input A an array...

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Heap Algorithms Parent ( A, i ) // Input : A : an array representing a heap, i : an array index // Output : The index in A of the parent of i // Running Time : O (1) 1 if i == 1 return NULL 2 return i/ 2 Left ( A, i ) // Input : A : an array representing a heap, i : an array index // Output : The index in A of the left child of i // Running Time : O (1) 1 if 2 i heap-size [ A ] 2 return 2 i 3 else return NULL Right ( A, i ) // Input : A : an array representing a heap, i : an array index // Output : The index in A of the right child of i // Running Time : O (1) 1 if 2 i + 1 heap-size [ A ] 2 return 2 i + 1 3 else return NULL Max-Heapify ( A, i ) // Input : A : an array where the left and right children of i root heaps (but i may not), i : an array index // Output : A modified so that i roots a heap // Running Time : O (log n ) where n = heap-size [ A ] i 1 l Left ( i ) 2 r Right ( i ) 3 if l heap-size [ A ] and A [ l ] > A [ i ] 4 largest l 5 else largest i 6 if r heap-size [ A ] and A [ r ] < A [ largest ] 7 largest r 8 if largest = i 9 exchange A [ i ] and A [ largest ] 10
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}