Question: ' ' ' Familiarize yourself with the minheap implementation ` ` MaxHeap ` ` found in the exercise package. The class can be used for
Familiarize yourself with the minheap implementation MaxHeap found in the exercise package. The class can be used for simple task prioritization using a queue. The task with the greatest integer has the highest priority. For example, in the task queue Do homework" "Eat "Sleep" the task "Eat" has the highest priority. Several of the methods needed by the max heap to work properly have not yet been implemented. Your task is to fix all the methods which contain a row with NotImplementedError class MaxHeap: A max heap implementation. Elements are stored internally in an array of tuples priority element where element can be any object and priority is a comparable object. For example for object object object the priority of the objects should be: object object object def initself initialdataNone: If initialdata is given, it should be an iterable sequence of tuples lists of tuples are fine if initialdata is None: self.clear else: self.array listinitialdata self.size lenselfarray for i in rangeselfsize : self.heapifydowni def clearself: Delete all elements from the heap and reset its size.""" self.array self.size def isemptyself: return self.size def buildheapself: Heapify the existing array.""" # Build the heap by heapifying all internal nodes, # starting from the last internal node up to the root for i in rangeselfsize : self.heapifydowni def swapself i j: Swaps inplace the priorityobject pairs at indexes i and j if i j: self.arrayj self.arrayi self.arrayi self.arrayj def heapifyupself i: Restore heap property from the element at i to the root.""" while i : # Select the parent of i parentindex i # If the parent has a lower priority than i swap parent with i if self.higherpriorityi parentindex: self.swapi parentindex # Heapify the parent i parentindex def leftchildself i: Return the index of the left child of i Return if i has no left child. Raise an IndexError if the index i is invalid.""" if not i self.size: raise IndexErrorA node which does not exist cannot have a left child" return i if i self.size else def rightchildself i: Return the index of the right child of i Return if i has no right child. Raise an IndexError if the index i is invalid.""" if not i self.size: raise IndexErrorA node which does not exist cannot have a right child" return i if i selfsize else def isleafself i: Return False if the node at i has one or more children, else True. Raise an IndexError if the index i is invalid.""" if not i self.size: raise IndexErrorA node which does not exist cannot be a leaf" return self.leftchildi self.rightchildi def maxchildself i: Return the index of the child with higher priority of the element at index i Raise an IndexError if the node at index i is a leaf.""" # Get indexes for the left and right child ileft self.leftchildi iright self.rightchildi if ileft iright: # Node i is a leaf raise IndexErrorThe node at index i has no children." if iright: # Node i has only a left child return ileft # Node i has two children # Compare the elements and return the index with the higher priority return ileft if self.higherpriorityileft, iright else iright Implement the methods below def higherpriorityself i j: Return True if element at index i has a higher priority than the element at index j else False.""" raise NotImplementedErrorMaxHeaphigherpriority not yet implemented." def insertself pair: Insert a new priority object pair into the heap and heapify the heap.""" raise NotImplementedErrorMaxHeapinsert not yet implemented." def topself: Return the object at the top of the heap or None if heap is empty. if self.isempty: return None raise NotImplementedErrorMaxHeaptop not yet implemented." def heapifydownself i: Restore heap property from the element at i to the last leaf.""" while i self.size and not self.isleafi: raise NotImplementedErrorMaxHeapheapifydown not yet implemented." def popself: Remove the pair at the top of the heap and return the removed object. Raises a RuntimeError if the heap is empty.""" # Raise error if the heap is empty if self.isempty: raise RuntimeErrorCannot pop from an empty heap." raise NotImplementedErrorMaxHeappop not yet implemented."
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
