Question: class MaxHeap: def __init__(self): self.H = [None] def size(self): return len(self.H)-1 def __repr__(self): return str(self.H[1:]) def satisfies_assertions(self): for i in range(2, len(self.H)): assert self.H[i] def
![class MaxHeap: def __init__(self): self.H = [None] def size(self): return len(self.H)-1](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f1143fd3fcf_39166f1143f5a2fa.jpg)
![def __repr__(self): return str(self.H[1:]) def satisfies_assertions(self): for i in range(2, len(self.H)): assert](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f1144073dd7_39266f11440101f0.jpg)
class MaxHeap: def __init__(self): self.H = [None] def size(self): return len(self.H)-1 def __repr__(self): return str(self.H[1:]) def satisfies_assertions(self): for i in range(2, len(self.H)): assert self.H[i]
def bubble_down(self, index): # your code here
# Function: insert # Insert elt into minheap # Use bubble_up/bubble_down function def insert(self, elt): # your code here
# Function: delete_max # delete the largest element in the heap. Use bubble_up/bubble_down def delete_max(self): # your code here
(C) Implement the algorithm Complete the implementation for maxheap data structure. First complete the implementation of MaxHeap. You can cut and paste relevant parts from previous problems although we do not really recommend doing that. A better solution would have been to write a single implementation that could have served as min/max heap based on a flag. class MaxHeap: def -init_(self): self.H=[None] def size(self): return len(self,H)-1 def _repr_(self): return str(self.H[1:]) def satisfies_assertions(self): for i in range(2, len(self.H)): assert self.H[i] = self.H[i//2], f'Maxheap property fails at position {i//2}, parent elt: \{self. H[i//2]\}, chi def max_element(self): return self.H[1] def bubble_up(self, index): \# your code here def bubble_down(self, index): \#your code here \# Function: insert \# Incert olt into winheap \# UEe bubble_up/bubble_down function def insert(self, elt): \#your code here \# Function: delete-max \# dolete the largest lement in the heap. Use bubble_up/bubble_down def delete_max(self): \# your code here
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
