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 dynamic_array import *
class MinHeapException(Exception):
"""
Custom exception to be used by MinHeap class
DO NOT CHANGE THIS CLASS IN ANY WAY
"""
pass
class MinHeap:
def __init__(self, start_heap=None):
"""
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 start_heap:
for node in start_heap:
self.add(node)
def __str__(self)-> str:
"""
Return MinHeap content in human-readable form
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
heap_data =[self._heap[i] for i in range(self._heap.length())]
return "HEAP "+ str(heap_data)
def add(self, node: object)-> None:
"""
This method adds a new object to the MinHeap while maintaining heap property
"""
self._heap.append(node)
self._bubble_up(self._heap.length()-1)
def _bubble_up(self, index: int)-> None:
"""
Helper function that restores the heap property by moving the element at index upward.
"""
while index >0:
parent =(index -1)//2
if self._heap[index]>= self._heap[parent]:
break
self._heap[index], self._heap[parent]= self._heap[parent], self._heap[index]
index = parent
def is_empty(self)-> bool:
"""
This method returns True if the heap is empty; otherwise, it returns False.
"""
if self._heap.length()==0:
return True
return False
def get_min(self)-> object:
"""
TODO: Write this implementation
"""
if self.is_empty():
raise MinHeapException("Heap is empty")
return self._heap[0]
def remove_min(self)-> object:
"""
Removes and returns the smallest element (root) of the heap.
"""
if self.is_empty():
raise MinHeapException("Heap is empty")
min_value = self._heap.get_at_index(0)
# Step 2: Replace the root with the last element and shrink the heap
last_index = self._heap.length()-1
self._heap.set_at_index(0, self._heap.get_at_index(last_index))
self._heap.pop() # Remove the last element
# Step 3: Restore the heap property by percolating down from the root
if not self.is_empty():
self._percolate_down(0)
return min_value
def build_heap(self, da: DynamicArray)-> None:
"""
TODO: Write this implementation
"""
self._heap = DynamicArray()
for i in range(da.length()):
self._heap.append(da.get_at_index(i))
# Step 2: Perform heapify-down from the last non-leaf node to the root
start_index =(self._heap.length()-2)//2 # Index of the last non-leaf node
for index in range(start_index, -1,-1):
self._percolate_down(self._heap, index)
def size(self)-> int:
"""
TODO: Write this implementation
"""
return self._heap.length()
def clear(self)-> None:
"""
TODO: Write this implementation
"""
self._heap = DynamicArray() # Reinitialize the heap as an empty DynamicArray
self._size =0
def heapsort(da: DynamicArray)-> None:
"""
TODO: Write this implementation
"""
n = da.length()
# Build the heap from the last non-leaf node
for i in range(n //2-1,-1,-1):
_percolate_down(da, i)
# Step 2: Extract elements one by one and place them at the end
for i in range(n -1,0,-1):
# Swap the root (maximum) with the last element
da.set_at_index(i, da.get_at_index(0)) # Swap root and last element
da.set_at_index(0, da.get_at_index(i)) # Move maximum to end
# Percolate down the root to restore the heap property
_percolate_down(da,0)
# 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 _percolate_down(da: 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:
left_child =2* parent +1
right_child =2* parent +2
smallest = parent
# Compare parent with left child
if left_child < da.length() and da.get_at_index(left_child)< da.get_at_index(smallest):
smallest = left_child
# Compare smallest so far with right child
if right_child < da.length() and da.get_at_index(right_child)< da.get_at_index(smallest):
smallest = right_child
# If the parent is already smaller than both children, we're done
if smallest == parent:
break
# Swap parent with the smallest child and continue
da.set_at_index(parent, da.get_at_index(smallest))
da.set_at_index(smallest, da.get_at_index(parent))
parent = smallest

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!