Question: Why isn't my code working ? from dynamic _ array import * class MinHeapException ( Exception ) : Custom exception to be
Why isn't my code working
from dynamicarray import
class MinHeapExceptionException:
Custom exception to be used by MinHeap class
DO NOT CHANGE THIS CLASS IN ANY WAY
pass
class MinHeap:
def initself startheapNone:
Initialize a new MinHeap
DO NOT CHANGE THIS METHOD IN ANY WAY
self.heap DynamicArray
# populate MinHeap with initial values if provided
# before using this feature, implement add method
if startheap:
for node in startheap:
self.addnode
def strself str:
Return MinHeap content in humanreadable form
DO NOT CHANGE THIS METHOD IN ANY WAY
heapdata selfheapi for i in rangeselfheap.length
return "HEAP strheapdata
def addself node: object None:
This method adds a new object to the MinHeap while maintaining heap property
self.heap.appendnode
self.bubbleupselfheap.length
def bubbleupself index: int None:
Helper function that restores the heap property by moving the element at index upward.
while index :
parent index
if self.heapindex self.heapparent:
break
self.heapindex self.heapparent self.heapparent self.heapindex
index parent
def isemptyself bool:
This method returns True if the heap is empty; otherwise, it returns False.
if self.heap.length:
return True
return False
def getminself object:
TODO: Write this implementation
if self.isempty:
raise MinHeapExceptionHeap is empty"
return self.heap
def removeminself object:
Removes and returns the smallest element root of the heap.
if self.isempty:
raise MinHeapExceptionHeap is empty"
minvalue self.heap.getatindex
# Step : Replace the root with the last element and shrink the heap
lastindex self.heap.length
self.heap.setatindex self.heap.getatindexlastindex
self.heap.pop # Remove the last element
# Step : Restore the heap property by percolating down from the root
if not self.isempty:
self.percolatedown
return minvalue
def buildheapself da: DynamicArray None:
TODO: Write this implementation
self.heap DynamicArray
for i in rangedalength:
self.heap.appenddagetatindexi
# Step : Perform heapifydown from the last nonleaf node to the root
startindex selfheap.length # Index of the last nonleaf node
for index in rangestartindex, :
self.percolatedownselfheap, index
def sizeself int:
TODO: Write this implementation
return self.heap.length
def clearself None:
TODO: Write this implementation
self.heap DynamicArray # Reinitialize the heap as an empty DynamicArray
self.size
def heapsortda: DynamicArray None:
TODO: Write this implementation
n dalength
# Build the heap from the last nonleaf node
for i in rangen :
percolatedownda i
# Step : Extract elements one by one and place them at the end
for i in rangen :
# Swap the root maximum with the last element
dasetatindexi dagetatindex # Swap root and last element
dasetatindex dagetatindexi # Move maximum to end
# Percolate down the root to restore the heap property
percolatedownda
# It's highly recommended that you implement the following optional #
# helper function for percolating elements down the MinHeap. You can call #
# this from inside the MinHeap class. You may edit the function definition. #
def percolatedownda: DynamicArray, parent: int None:
This is a helper method that moves the element at the given parent index
down the heap to restore the MinHeap property.
while True:
leftchild parent
rightchild parent
smallest parent
# Compare parent with left child
if leftchild dalength and dagetatindexleftchild dagetatindexsmallest:
smallest leftchild
# Compare smallest so far with right child
if rightchild dalength and dagetatindexrightchild dagetatindexsmallest:
smallest rightchild
# If the parent is already smaller than both children, we're done
if smallest parent:
break
# Swap parent with the smallest child and continue
dasetatindexparent dagetatindexsmallest
dasetatindexsmallest dagetatindexparent
parent smallest
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
