Complete the code to add a new element at the end of the heap list.
heap = [10, 15, 20] new_value = 12 heap.[1](new_value)
We use append to add the new element at the end of the list representing the heap.
Complete the code to find the parent index of the newly added element at index i.
i = len(heap) - 1 parent = ([1] - 1) // 2
The parent index in a heap is calculated as (i - 1) // 2 where i is the current node index.
Fix the error in the bubble up loop condition to continue while the current node is greater than its parent.
while i > 0 and heap[i] [1] heap[parent]: heap[i], heap[parent] = heap[parent], heap[i] i = parent parent = (i - 1) // 2
In a max-heap, bubble up continues while the current node is greater than its parent, so the condition uses >.
Fill both blanks to correctly update the current index and parent index after swapping.
i = [1] parent = ([2] - 1) // 2
i instead of parent for updatingAfter swapping, the current index becomes the parent's index, so both i and parent update accordingly.
Fill all three blanks to complete the bubble up function for a max-heap insert.
def bubble_up(heap): i = len(heap) - 1 parent = ([1] - 1) // 2 while i > 0 and heap[i] [2] heap[parent]: heap[i], heap[parent] = heap[parent], heap[i] i = [3] parent = (i - 1) // 2
i and parent updatesThe bubble up function starts with the last index i, calculates the parent, and swaps while the child is greater than the parent. After swapping, i updates to parent.
