def test_add_element_to_maxheap(self): max_heap = MaxHeap(self.array) max_heap.add_element(13) self.assertEqual(6, max_heap.length()) self.assertEqual([13, 8, 11, 4, 3, 6], max_heap.elements()) self.assertEqual(13, max_heap.get_root_value()) self.assertEqual([13, 8, 11, 4, 3, 6], max_heap.elements())
def test_multiple_operations_maxheap(self): max_heap = MaxHeap(self.array) max_heap.add_element(5) self.assertEqual([11, 8, 6, 4, 3, 5], max_heap.elements()) self.assertEqual(11, max_heap.extract_root()) self.assertEqual([8, 5, 6, 4, 3], max_heap.elements()) max_heap.add_element([11, 13, 5, 6]) self.assertEqual([13, 6, 11, 5, 3, 6, 8, 4, 5], max_heap.elements()) self.assertEqual(13, max_heap.extract_root()) self.assertEqual(11, max_heap.extract_root()) self.assertEqual([8, 6, 6, 5, 3, 4, 5], max_heap.elements()) self.assertEqual([3, 4, 5, 5, 6, 6, 8], sorted(max_heap.elements()))
def test_max_heap_property(self): max_heap = MaxHeap(self.array) length = max_heap.length() elements = max_heap.elements() for i in range(0, length): current_value = elements[i] left_index = (2 * i) + 1 right_index = (2 * i) + 2 if left_index <= length - 1: self.assertGreaterEqual(current_value, elements[left_index]) if right_index <= length - 1: self.assertGreaterEqual(current_value, elements[right_index])
def test_max_heap_count_and_elements(self): max_heap = MaxHeap(self.array) self.assertEqual(5, max_heap.length()) self.assertEqual([11, 8, 6, 4, 3], max_heap.elements())
def test_extract_root_value_maxheap(self): max_heap = MaxHeap(self.array) self.assertEqual(11, max_heap.extract_root()) self.assertEqual([8, 4, 6, 3], max_heap.elements())
def test_delete_element_at_index_maxheap(self): max_heap = MaxHeap(self.array) max_heap.delete_element_at_index(2) self.assertEqual(4, max_heap.length()) self.assertEqual([11, 8, 3, 4], max_heap.elements())
def test_add_array_of_elements_to_maxheap(self): max_heap = MaxHeap(self.array) max_heap.add_element([1, 14, 7, 5]) self.assertEqual(9, max_heap.length()) self.assertEqual([14, 8, 11, 7, 3, 1, 6, 4, 5], max_heap.elements()) self.assertEqual(14, max_heap.get_root_value())