Complete the code to insert an element into a min-heap using heapq.
import heapq heap = [1, 3, 5] heapq.[1](heap, 2) print(heap)
The heappush function adds an element to the heap while maintaining the heap property.
Complete the code to remove the smallest element from the heap.
import heapq heap = [1, 3, 5] smallest = heapq.[1](heap) print(smallest)
The heappop function removes and returns the smallest element from the heap.
Fix the error in the code to maintain the heap property after changing an element.
import heapq heap = [1, 3, 5] heap[1] = 0 heapq.[1](heap) print(heap)
After changing an element directly, heapify rebuilds the heap to maintain order.
Fill both blanks to create a min-heap from a list and then pop the smallest element.
import heapq numbers = [5, 3, 8, 1] heapq.[1](numbers) smallest = heapq.[2](numbers) print(smallest)
heapify builds the heap from the list, then heappop removes the smallest element.
Fill all three blanks to create a max-heap using a min-heap by negating values.
import heapq nums = [2, 7, 4, 1] neg_nums = [-n for n in nums] heapq.[1](neg_nums) max_val = -heapq.[2](neg_nums) heapq.[3](neg_nums, -5) print(max_val, neg_nums)
Negate numbers to use min-heap as max-heap. heapify builds heap, heappop removes max (negated), heappush adds new negated value.
