Answered step by step
Verified Expert Solution
Link Copied!

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

image text in transcribed

image text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Analytics And Quality Management Fundamental Tools

Authors: Joseph Nguyen

1st Edition

B0CNGG3Y2W, 979-8862833232

More Books

Students also viewed these Databases questions

Question

Differentiate the function. r(z) = 2-8 - 21/2 r'(z) =

Answered: 1 week ago