Пример #1
0
    def test_max_heap_insert(self):
        a = bh.Array([4, 1, 3, 2, 16, 9, 10, 14, 8, 7])
        bh.build_max_heap(a)

        pq.max_heap_insert(a, 20)
        b = bh.Array([20, 16, 10, 8, 14, 9, 3, 2, 4, 1, 7])

        self.assertEqual(a, b)
Пример #2
0
    def test_heap_increase_key(self):
        a = bh.Array([4, 1, 3, 2, 16, 9, 10, 14, 8, 7])
        bh.build_max_heap(a)

        pq.heap_increase_key(a, 8, 15)
        b = bh.Array([16, 15, 10, 14, 7, 9, 3, 2, 8, 1])

        self.assertEqual(a, b)
Пример #3
0
    def test_heapsort_twice(self):
        a = bh.Array([3, 2, 5, 6, 7, 2 ,1, 7])
        bh.heapsort(a)

        random.shuffle(a)

        bh.heapsort(a)
        b = bh.Array([1, 2, 2, 3, 5, 6, 7, 7])

        self.assertEqual(a, b)
Пример #4
0
    def test_heap_extract_max(self):
        a = bh.Array([3, 16, 7, 6])
        bh.build_max_heap(a)

        max_value = pq.heap_extract_max(a)

        self.assertEqual(max_value, 16)
        self.assertEqual(a, [7, 6, 3])
Пример #5
0
import binary_heap as bh
import priority_queue as pq

a = bh.Array([16, 4, 10, 14, 7, 9, 3, 2, 8, 1])

print("Testing the heap")
print(a)
bh.max_heapify(a, 1)

#print(a)
#bh.max_heapify(a, 3)

print(a)

print("Building a heap")

b = bh.Array([4, 1, 3, 2, 16, 9, 10, 14, 8, 7])
print(b)

bh.build_max_heap(b)
print(b)

print(pq.heap_extract_max(b))

print(b)
print(len(b))
print(b.heap_size)

#c = bh.Array([5,4,2,7,4,37,8,7])
#print(c)
#bh.heapsort(c)
Пример #6
0
    def test_max_heapify(self):
        a = bh.Array([16, 4, 10, 14, 7, 9, 3, 2, 8, 1])
        bh.max_heapify(a, 1)

        b = bh.Array([16, 14, 10, 8, 7, 9, 3, 2, 4, 1])
        self.assertEqual(a, b)
Пример #7
0
    def test_heapsort(self):
        a = bh.Array([3, 2, 5, 6, 7, 2 ,1, 7])
        bh.heapsort(a)

        b = bh.Array([1, 2, 2, 3, 5, 6, 7, 7])
        self.assertEqual(a, b)
Пример #8
0
    def test_build_max_heap(self):
        a = bh.Array([4, 1, 3, 2, 16, 9, 10, 14, 8, 7])
        bh.build_max_heap(a)

        b = bh.Array([16, 14, 10, 8, 7, 9, 3, 2, 4, 1])
        self.assertEqual(a, b)