Question: send me the original code which I can copy and paste, please. Thank you. Extract Max from Max Heap In this assignment, you will implement
send me the original code which I can copy and paste, please. Thank you.
Extract Max from Max Heap
In this assignment, you will implement the code to extract the max element from a heap, return it, and then rebalance the tree. One way to do this is to swap the head of the heap (i.e. the max element) with the last element of the heap, remove it from the end, and then rebalance the tree by "bubbling down" (sifting down) the new top element until the max heap is valid again.
Expected behavior:
heap = [13, 9, 12, 7, 1, 10, 5, 3]
len(heap) == 8
extract_max(heap) == 13
is_max_heap(heap) == True
len(heap) == 7
extract_max(heap) == 12
is_max_heap(heap) == True
len(heap) == 6
#complete the following code please
def get_parent(node_idx): """Return the index of the parent of a node.""" return (node_idx-1) // 2
def get_left_child(node_idx): """Return the index of the left child of a node.""" return node_idx * 2 + 1
def get_right_child(node_idx): """Return the index of the right child of a node.""" return node_idx * 2 + 2
def is_max_heap(heap): """Return true if the array is a max heap.""" size = len(heap) left = all(heap[i] > heap[get_left_child(i)] for i in range(size) if get_left_child(i) < size) right = all(heap[i] > heap[get_right_child(i)] for i in range(size) if get_right_child(i) < size) return left and right
def extract_max(heap):
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
