Apply the heapify method to build a heap for the following list.
arr = [15, 5, 20, 1, 17, 10, 30]
[15, 5, 20, 1, 17, 10, 30]
.Initialization
The program starts by calling the build_heap
function with the array arr = [15, 5, 20, 1, 17, 10, 30]
.
arr = [15, 5, 20, 1, 17, 10, 30]
build_heap(arr)
build_heap
function
The length of the array n = 7
(since there are 7 elements in the array).
Now, the for-loop inside build_heap
starts from the last non-leaf node and calls heapify
on each node, moving upwards toward the root.
The formula to get the last non-leaf node is n // 2 - 1
. In this case, n // 2 - 1 = 7 // 2 - 1 = 2
. So, the loop starts from index 2
and decrements to 0
.
for i in range(n//2 - 1, -1, -1):
heapify(arr, n, i)
Heapify process starts:
heapify
call: (i = 2)
We start with i = 2
, the value at index 2
is 20
.
Left child: 2 * i + 1 = 2 * 2 + 1 = 5
→ arr[5] = 10
Right child: 2 * i + 2 = 2 * 2 + 2 = 6
→ arr[6] = 30
Now, we compare the value at index 2
(which is 20
) with its left and right children (10
and 30
).
The right child 30
is greater than 20
, so we swap 20
with 30
.
After swapping:
arr = [15, 5, 30, 1, 17, 10, 20]
Since 20
was swapped with 30
, we've to recursively call heapify
on index 6
(where 20
is now located). However, since index 6
is a leaf node (no children), the recursive call ends immediately.
heapify
call: (i = 1)
Next, the loop continues with i = 1
, the value at index 1
is 5
.
Left child: 2 * i + 1 = 2 * 1 + 1 = 3
→ arr[3] = 1
Right child: 2 * i + 2 = 2 * 1 + 2 = 4
→ arr[4] = 17
Now, we compare the value at index 1
(5
) with its left and right children (1
and 17
).
The right child 17
is greater than 5
, so we swap 5
with 17
.
After swapping:
arr = [15, 17, 30, 1, 5, 10, 20]
Since 5
was swapped with 17
, we've to recursively call heapify
on index 4
(where 5
is now located). However, index 4
is a leaf node, so the recursive call ends immediately.
heapify
call: (i = 0)
Finally, the loop continues with i = 0
, the value at index 0
is 15
.
Left child: 2 * i + 1 = 2 * 0 + 1 = 1
→ arr[1] = 17
Right child: 2 * i + 2 = 2 * 0 + 2 = 2
→ arr[2] = 30
Now, we compare the value at index 0
(15
) with its left and right children (17
and 30
).
The right child 30
is greater than 15
, so we swap 15
with 30
.
After swapping:
arr = [30, 17, 15, 1, 5, 10, 20]
Since 15
was swapped with 30
, we've to recursively call heapify
on index 2
(where 15
is now located).
heapify
call: (i = 2)
Now, we call heapify
on index 2
(where 15
is located).
Left child: 2 * i + 1 = 5
→ arr[5] = 10
Right child: 2 * i + 2 = 6
→ arr[6] = 20
We compare 15
with its children 10
and 20
.
The right child 20
is greater than 15
, so we've to swap 15
with 20
.
After swapping:
arr = [30, 17, 20, 1, 5, 10, 15]
Since 15
is now at index 6
, and index 6
is a leaf node, the recursive call ends.
After all the heapify calls, the array is now a valid Max-Heap:
arr = [30, 17, 20, 1, 5, 10, 15]
This is the final Max-Heap representation of the given list.
Python Code:
def heapify(arr, n, i):
largest = i
left = 2 * i + 1
right = 2 * i + 2
if left < n and arr[left] > arr[largest]:
largest = left
if right < n and arr[right] > arr[largest]:
largest = right
if largest != i:
arr[i], arr[largest] = arr[largest], arr[i]
# Recursively heapify the affected sub-tree
heapify(arr, n, largest)
def build_heap(arr):
n = len(arr)
for i in range(n//2 - 1, -1, -1):
heapify(arr, n, i)
if __name__ == "__main__":
arr = [15, 5, 20, 1, 17, 10, 30]
build_heap(arr)
print("Max-Heap array:", arr)