Exemplo n.º 1
0
    def test_add_to_heap_keeps_count(self):
        heap = Heap(0)
        number_total = 10
        for i in range(1, number_total):
            heap.add_to_heap(number_total)

        self.assertEqual(heap.num_in_heap, number_total)
Exemplo n.º 2
0
    def test_adding_after_pop_min_on_last_element(self):
        heap = Heap(1)
        _ = heap.pop_min()  # heap is now empty
        assert len(heap) == 0

        heap.add_to_heap(2)
        self.assertEqual(len(heap), 1)
Exemplo n.º 3
0
    def test_add_to_heap_keeps_balanced(self):
        heap = Heap(0)
        number_total = 25  # needs to be odd
        for i in range(1, number_total):
            heap.add_to_heap(number_total)

        number_per_child = (number_total - 1) / 2
        # 1 in main heap, (n - 1) / 2 in sub-heaps, for n total
        self.assertEqual(heap.left_child.num_in_heap, number_per_child)
        self.assertEqual(heap.right_child.num_in_heap, number_per_child)
Exemplo n.º 4
0
 def test_add_to_heap_keeps_ordered_when_added_high_to_low(self):
     low = 0
     high = 1
     heap = Heap(high)
     heap.add_to_heap(low)
     self.assertEqual(heap.value, low)