예제 #1
0
def test_0():
    # 0
    # 1 2 3
    # 4 5 6   7 8 9   10 11 12
    heap = MinHeap(*range(13), n=3)
    assert heap.height == 3
    assert heap.parent(0) is None
    assert heap.child(0, 0) == 1
    assert heap.child(0, 1) == 2
    assert heap.child(0, 2) == 3
    assert heap.first(0) == 1
    assert heap.last(0) == 3
    assert heap.parent(1) == 0
    assert heap.child(1, 0) == 4
    assert heap.child(1, 1) == 5
    assert heap.child(1, 2) == 6
    assert heap.first(1) == 4
    assert heap.last(1) == 6
    assert heap.parent(2) == 0
    assert heap.child(2, 0) == 7
    assert heap.child(2, 1) == 8
    assert heap.child(2, 2) == 9
    assert heap.first(2) == 7
    assert heap.last(2) == 9
    assert heap.parent(3) == 0
    assert heap.child(3, 0) == 10
    assert heap.child(3, 1) == 11
    assert heap.child(3, 2) == 12
    assert heap.first(3) == 10
    assert heap.last(3) == 12
    assert heap.parent(4) == 1
    assert heap.parent(5) == 1
    assert heap.parent(6) == 1
    assert heap.parent(7) == 2
    assert heap.parent(8) == 2
    assert heap.parent(9) == 2
    assert heap.parent(10) == 3
    assert heap.parent(11) == 3
    assert heap.parent(12) == 3
    assert heap.is_valid()
    assert tuple(heap.walk_up(4)) == ((1, 4), (0, 1))
    assert tuple(heap.walk_up(5)) == ((1, 5), (0, 1))
    assert tuple(heap.walk_up(6)) == ((1, 6), (0, 1))
    assert tuple(heap.walk_up(7)) == ((2, 7), (0, 2))
    assert tuple(heap.walk_up(12)) == ((3, 12), (0, 3))
    assert tuple(heap.walk_down()) == ((0, 1), (1, 4))
    heap.push(14)
    assert heap.is_valid()
    heap.push(6)
    assert heap.is_valid()
    assert heap.pop() == 0
    assert heap.is_valid()
    assert heap.pop() == 1
    assert heap.is_valid()
    assert heap.pop() == 2
    assert heap.is_valid()