Exemplo n.º 1
0
def _min_heapify(A, i):
    l = left(i)
    r = right(i)
    if l <= A.heap_size and A[l].f < A[i].f:
        smallest = l
    else:
        smallest = i
    if r <= A.heap_size and A[r].f < A[smallest].f:
        smallest = r
    if smallest != i:
        A[i], A[smallest] = A[smallest], A[i]
        _min_heapify(A, smallest)
Exemplo n.º 2
0
def max_heapify(A, i):
    l = left(i)
    r = right(i)
    if l <= A.heap_size and A[l] > A[i]:
        largest = l
    else:
        largest = i
    if r <= A.heap_size and A[r] > A[largest]:
        largest = r
    if largest != i:
        A[i], A[largest] = A[largest], A[i]
        max_heapify(A, largest)
Exemplo n.º 3
0
def _min_heapify(A, i):
    l = left(i)
    r = right(i)
    if l <= A.heap_size and A[l].f < A[i].f:
        smallest = l
    else:
        smallest = i
    if r <= A.heap_size and A[r].f < A[smallest].f:
        smallest = r
    if smallest != i:
        A[i], A[smallest] = A[smallest], A[i]
        _min_heapify(A, smallest)
Exemplo n.º 4
0
def _min_heapify_pair(A, i):
    l = left(i)
    r = right(i)
    if l <= A.heap_size and A[l][0].key < A[i][0].key:
        smallest = l
    else:
        smallest = i
    if r <= A.heap_size and A[r][0].key < A[smallest][0].key:
        smallest = r
    if smallest != i:
        A[i], A[smallest] = A[smallest], A[i]
        _min_heapify_pair(A, smallest)
Exemplo n.º 5
0
def _max_heapify_element(A, i):
    l = left(i)
    r = right(i)
    if l <= A.heap_size and A[l].key > A[i].key:
        largest = l
    else:
        largest = i
    if r <= A.heap_size and A[r].key > A[largest].key:
        largest = r
    if largest != i:
        A[i], A[largest] = A[largest], A[i]
        _max_heapify_element(A, largest)
Exemplo n.º 6
0
def iterative_max_heapify(A, i):
    while True:
        l = left(i)
        r = right(i)
        if l <= A.heap_size and A[l] > A[i]:
            largest = l
        else:
            largest = i
        if r <= A.heap_size and A[r] > A[largest]:
            largest = r
        if largest == i:
            return
        A[i], A[largest] = A[largest], A[i]
        i = largest