I am trying to write a delete function for a heap. I am given a head class and heapify functions to work with, I just need to implement delete. I want to implement it in O(log n)
time. This is what I've tried:
from heap import *
class heap_delete(heap):
def delete(self, i):
self.A[i] = self.A[-1] #put the bottom rightmost element in i
self.min_heapify(i)
but when I run it with the provided test code, it says 'failed'. Reading the AssertionError
statement it seems that nothing is happening to the index I am trying to manipulate. Here is the heap code I am importing:
def parent(i):
return int(i/2)
def left(i)
return 2*i
def right(i):
return 2*i+1
class heap:
def __init__(self):
self.A = [None] #to make it 1 based, None is stuck at 0
def __getitem__(self, i):
return self.A[i]
def min_heapify(self, i):
l = left(i)
r = right(i)
smallest = i
if l <= self.heapsize and self.A[l] < self.A[i]:
smallest = l
if r <= self.heapsize and self.A[r] < self.A[smallest]:
smallest = r
if smallest != i:
self._swap(i, smallest)
self.min_heapify(smallest)
def _swap(self, index1, index2):
self.A[index1], self.A[index2] = self.A[index2], self.A[index1]
There are other functions, such as decrease_key, extract_min, and insert. I can add them to the post if necessary but I believe everything I need is here. The AssertionError
I am recieving is as follows:
self.assertEquals(h.A, [None])
AssertionError: Lists differ: [None, 5] != [None]
and
self.assertEquals(h.A, [None])
AssertionError: Lists differ: [None, 15] != [None]
the former AssertionError
is from the test function I am supposed to run my code with. It calls h.insert(5)
and h.delete(1)
, followed by self.assertEquals(h.A, [None])
. The ladder, also in the test function, calls insert
on 5,15,10,and 0 which is followed by h.delete(h.A.index(10))
and then the same assertEquals
statement as before.
From this I am lead to believe that my delete function isn't deleting anything. I have tried using del
and the _swap
provided but perhaps did not use them correctly. Again I am looking for O(log n) time so if anyone could point me in the right direction that would be great.