Complete the code to insert a new element at the end of the heap array.
heap.append([1])The new element to insert is added at the end of the heap array using append with the value to insert.
Complete the code to find the parent index of the newly inserted element at index i.
parent = ([1] - 1) // 2
In a binary heap stored as an array, the parent index of a node at index i is calculated as (i - 1) // 2.
Fix the error in the condition to continue bubbling up while the current index is greater than 0.
while [1] > 0:
The bubbling up process continues while the current index i is greater than 0, meaning the node is not the root.
Fill both blanks to swap the current element with its parent if the heap property is violated.
if heap[[1]] > heap[[2]]: heap[[1]], heap[[2]] = heap[[2]], heap[[1]]
To maintain the max-heap property, if the current element at index i is greater than its parent at index parent, they are swapped.
Fill all three blanks to update the current index to the parent's index after swapping during bubble up.
heap[[1]], heap[[2]] = heap[[2]], heap[[1]] i = [3]
After swapping the current element at index i with its parent at index parent, update i to parent to continue bubbling up.