Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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 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 hereStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started