Access over 20 million homework & study documents

search

6 3 2 Why do we want the loop index i in line 2 of B UILD MAX HEA

Content type
User Generated
Rating
Showing Page:
1/2
6. 3-2
Why do we want the loop index i in line 2 of B UILD-MAX-
HEAP to decrease from
[A,length/2] to 1 rather than increase from 1 to [A.
length/2] ?
Solution
Otherwise we won\'t be allowed to call MAX-HEAPIFY,
since it will fail the condition of having the subtrees be
max-heaps. That is, if we start with 1, there is no
guarantee that A[2] and A[3] are roots of max-heaps.
When MAX-HEAPIFY is called in the loop, it is assumed
that the binary trees rooted at LEFT(i) and RIGHT(i) are
max-heaps. To make sure this assumption holds in each
iteration, we must handle the nodes from bottom to top, ie.
want the loop index i in line 2 of BUILD-MAX-HEAP to
decrease from ë n/2 û down to 1 rather than increase from
1 to L n/2 L.

Sign up to view the full document!

lock_open Sign Up
Showing Page:
2/2

Sign up to view the full document!

lock_open Sign Up
Unformatted Attachment Preview
6. 3-2 Why do we want the loop index i in line 2 of B UILD -MAXHEAP to decrease from [A,length/2] to 1 rather than increase from 1 to [A. length/2] ? Solution Otherwise we won\'t be allowed to call MAX-HEAPIFY, since it will fail the condition of having the subtrees be max-heaps. That is, if we start with 1, there is no guarantee that A[2] and A[3] are roots of max -heaps. When MAX-HEAPIFY is called in the loop, it is assumed that the binary trees rooted at LEFT(i) and RIGHT(i) are max-heaps. To make sure this assumption holds in each iteration, we must handle the nodes from bottom to top, i ...
Purchase document to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Anonymous
Great! 10/10 would recommend using Studypool to help you study.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4