def test_remove(self): """ Test the removal of a key from the middle of the heap. Given the following heap: (4) / \ (4) (8) / \ / \ (9) (4) (12) (9) / \ (11) (13) """ data = [4, 4, 8, 9, 4, 12, 9, 11, 13] h = Heap(data) h.remove(2) self.assertTrue(Heap.is_heap(data), 'should preserve heap property') self.assertNotIn(8, h.data, 'the value corresponding to the index was removed')
def remove(self, vertex): """ Overrides parent method to remove a vertex by it's vertex not cost. Args vertex: str, name of the vertex to remove. """ index = map(itemgetter(0), self.data).index(vertex) return Heap.remove(self, index)
def test_remove_if_one_element(self): data = [3] h = Heap(data) h.remove(0) self.assertEqual(h.data, [], 'should remove the only elem in heap') self.assertTrue(Heap.is_heap(h.data), 'should maintain heap invariant')
def test_remove_if_index_is_last(self): data = [3, 4, 5] h = Heap(data) h.remove(2) self.assertEqual(h.data, [3, 4], 'should remove the last leaf') self.assertTrue(Heap.is_heap(h.data), 'should maintain heap invariant')
def test_remove_if_index_is_root(self): data = [3, 4, 5] h = Heap(data) h.remove(0) self.assertEqual(h.data, [4, 5], 'should remove the root') self.assertTrue(Heap.is_heap(h.data), 'should maintain heap invariant')
def test_remove_if_index_is_root(self): data = [3, 4, 5] h = Heap(data) h.remove(0) self.assertEqual(h.data, [4,5], 'should remove the root') self.assertTrue(Heap.is_heap(h.data), 'should maintain heap invariant')