コード例 #1
0
ファイル: exercise16_3_6.py プロジェクト: wojtask/CormenPy
def _min_heap_insert(A, z):
    A.heap_size = A.heap_size + 1
    i = A.heap_size
    A[i] = z
    while i > 1 and A[parent(i)].f > A[i].f:
        A[i], A[parent(i)] = A[parent(i)], A[i]
        i = parent(i)
コード例 #2
0
def _min_heap_insert(A, z):
    A.heap_size = A.heap_size + 1
    i = A.heap_size
    A[i] = z
    while i > 1 and A[parent(i)].f > A[i].f:
        A[i], A[parent(i)] = A[parent(i)], A[i]
        i = parent(i)
コード例 #3
0
ファイル: exercise6_5_8.py プロジェクト: wojtask/CormenPy
def _heap_decrease_pair_key(A, i, pair):
    if pair[0].key > A[i][0].key:
        raise RuntimeError('new key is larger than current key')
    A[i] = pair
    while i > 1 and A[parent(i)][0].key > A[i][0].key:
        A[i], A[parent(i)] = A[parent(i)], A[i]
        i = parent(i)
コード例 #4
0
def _heap_increase_element_key(A, i, x):
    if x.key < A[i].key:
        raise RuntimeError('new key is smaller than current key')
    A[i] = x
    while i > 1 and A[parent(i)].key < A[i].key:
        A[i], A[parent(i)] = A[parent(i)], A[i]
        i = parent(i)
コード例 #5
0
def heap_decrease_key(A, i, key):
    if key > A[i]:
        raise RuntimeError('new key is larger than current key')
    A[i] = key
    while i > 1 and A[parent(i)] > A[i]:
        A[i], A[parent(i)] = A[parent(i)], A[i]
        i = parent(i)
コード例 #6
0
def heap_increase_key(A, i, key):
    if key < A[i]:
        raise RuntimeError('new key is smaller than current key')
    A[i] = key
    while i > 1 and A[parent(i)] < A[i]:
        A[i], A[parent(i)] = A[parent(i)], A[i]
        i = parent(i)
コード例 #7
0
def max_heap_delete(A, i):
    A[i], A[A.heap_size] = A[A.heap_size], A[i]
    A.heap_size = A.heap_size - 1
    max_heapify(A, i)
    while i > 1 and A[parent(i)] < A[i]:
        A[i], A[parent(i)] = A[parent(i)], A[i]
        i = parent(i)
    return A[A.heap_size + 1]