def setUp(self): self.heap = Heap()
class HeapTests(unittest.TestCase): def setUp(self): self.heap = Heap() def test_heap_insert_works(self): self.heap.insert(6) self.heap.insert(8) self.heap.insert(10) self.heap.insert(9) self.heap.insert(1) self.heap.insert(9) self.heap.insert(9) self.heap.insert(5) self.assertEqual(self.heap.storage, [10, 9, 9, 6, 1, 8, 9, 5]) def test_get_max_works(self): self.heap.insert(6) self.heap.insert(8) self.heap.insert(10) self.heap.insert(9) self.heap.insert(1) self.heap.insert(9) self.heap.insert(9) self.heap.insert(5) self.assertEqual(self.heap.get_size(), 8) self.assertEqual(self.heap.get_max(), 10) def test_get_max_after_delete(self): self.heap.insert(6) self.heap.insert(8) self.heap.insert(10) self.heap.insert(9) self.heap.insert(1) self.heap.insert(9) self.heap.insert(9) self.heap.insert(5) self.heap.delete() self.assertEqual(self.heap.get_max(), 9) self.heap.delete() self.assertEqual(self.heap.get_max(), 9) self.heap.delete() self.assertEqual(self.heap.get_max(), 9) self.heap.delete() self.assertEqual(self.heap.get_max(), 8) self.heap.delete() self.assertEqual(self.heap.get_max(), 6) def test_delete_elements_in_order(self): self.heap.insert(6) self.heap.insert(7) self.heap.insert(5) self.heap.insert(8) self.heap.insert(10) self.heap.insert(1) self.heap.insert(2) self.heap.insert(5) descending_order = [] while self.heap.get_size() > 0: descending_order.append(self.heap.delete()) self.assertEqual(descending_order, [10, 8, 7, 6, 5, 5, 2, 1])
class HeapTests(unittest.TestCase): def setUp(self): self.heap = Heap() def test_heap_insert_works(self): self.heap.insert(6) self.heap.insert(8) self.heap.insert(10) self.heap.insert(9) self.heap.insert(1) self.heap.insert(9) self.heap.insert(9) self.heap.insert(5) self.assertEqual(self.heap.storage, [10, 9, 9, 6, 1, 8, 9, 5]) def test_get_max_works(self): self.heap.insert(6) self.heap.insert(8) self.heap.insert(10) self.heap.insert(9) self.heap.insert(1) self.heap.insert(9) self.heap.insert(9) self.heap.insert(5) self.assertEqual(self.heap.get_size(), 8) self.assertEqual(self.heap.get_max(), 10) def test_get_max_after_delete(self): self.heap.insert(6) self.heap.insert(8) self.heap.insert(10) self.heap.insert(9) self.heap.insert(1) self.heap.insert(9) self.heap.insert(9) self.heap.insert(5) self.heap.delete() self.assertEqual(self.heap.get_max(), 9) self.heap.delete() self.assertEqual(self.heap.get_max(), 9) self.heap.delete() self.assertEqual(self.heap.get_max(), 9) self.heap.delete() self.assertEqual(self.heap.get_max(), 8) self.heap.delete() self.assertEqual(self.heap.get_max(), 6) def test_delete_elements_in_order(self): self.heap.insert(6) self.heap.insert(7) self.heap.insert(5) self.heap.insert(8) self.heap.insert(10) self.heap.insert(1) self.heap.insert(2) self.heap.insert(5) descending_order = [] print("Storage", self.heap.storage) while self.heap.get_size() > 0: descending_order.append(self.heap.delete()) self.assertEqual(descending_order, [10, 8, 7, 6, 5, 5, 2, 1]) def test_bubble_up_was_called(self): self.heap._bubble_up = MagicMock() self.heap.insert(5) self.assertTrue(self.heap._bubble_up.called) def test_sift_down_was_called(self): self.heap._sift_down = MagicMock() self.heap.insert(10) self.heap.insert(11) self.heap.delete() self.assertTrue(self.heap._sift_down.called)
def list_find_max_index(l): current_max = l[0] current_max_index = 0 for i in range(0, len(l)): if l[i] > current_max: current_max = l[i] current_max_index = i return current_max_index if __name__ == '__main__': n = 10000 l = [] h = Heap() # benchmark heap insertion vs list insertion # O(n) insertion for lists start_time = time.time() for i in range(0, n): l.append(i) end_time = time.time() print(f'list insertion runtime: {end_time - start_time} seconds') # O(n log n) insertion for heaps start_time = time.time() for i in range(0, n): h.insert(i) end_time = time.time()