def test_extract_min(self): """ Makes sure that the heap produces min values and that the heap property is preserved. 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) min_key = h.extract_min() self.assertEqual(min_key, 4, 'should extract the min value') self.assertTrue(Heap.is_heap(data), 'should still hold the heap property') min_key = h.extract_min() self.assertEqual(min_key, 4, 'should extract the min value') self.assertTrue(Heap.is_heap(data), 'should still hold the heap property') min_key = h.extract_min() self.assertEqual(min_key, 4, 'should extract the min value') self.assertTrue(Heap.is_heap(data), 'should still hold the heap property')
def test_static_is_heap(self): """ Tests static method is_heap if it can correctly verify if a list of elements preserves the heap property. """ good = [4, 4, 8, 9, 4, 12, 9, 11, 13] bad = [1,2,3,114,5,6,7,8,9,10] self.assertTrue(Heap.is_heap(good), 'should hold the heap property') self.assertFalse(Heap.is_heap(bad), 'should not hold the heap property')
def test_static_is_heap(self): """ Tests static method is_heap if it can correctly verify if a list of elements preserves the heap property. """ good = [4, 4, 8, 9, 4, 12, 9, 11, 13] bad = [1, 2, 3, 114, 5, 6, 7, 8, 9, 10] self.assertTrue(Heap.is_heap(good), 'should hold the heap property') self.assertFalse(Heap.is_heap(bad), 'should not hold the heap property')
def test_bubble_down(self): h = Heap([]) h.data = [3, 2, 4] new_index = h.bubble_down(0) self.assertTrue(Heap.is_heap(h.data), 'should maintain the heap prop') self.assertEqual(h.data, [2,3,4], 'should have reorganized the heap') self.assertEqual(new_index, 1, 'should return the correct new index')
def test_bubble_down(self): h = Heap([]) h.data = [3, 2, 4] new_index = h.bubble_down(0) self.assertTrue(Heap.is_heap(h.data), 'should maintain the heap prop') self.assertEqual(h.data, [2, 3, 4], 'should have reorganized the heap') self.assertEqual(new_index, 1, 'should return the correct new index')
def test_insert(self): """ Test that adding an element preserves the heap property. 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.insert(7) self.assertTrue(Heap.is_heap(h.data), 'should still be a heap') h.insert(10) self.assertTrue(Heap.is_heap(h.data), 'should still be a heap') h.insert(5) self.assertTrue(Heap.is_heap(h.data), 'should still be a heap')
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 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_static_heapify(self): data = [8,2,6,3,1,2,9,5,3,7,4] h = Heap.heapify(data) self.assertTrue(Heap.is_heap(data), 'should preserve heap property')
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_static_heapify(self): data = [8, 2, 6, 3, 1, 2, 9, 5, 3, 7, 4] h = Heap.heapify(data) self.assertTrue(Heap.is_heap(data), 'should preserve heap property')
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')