(heap, pos)
Maxheap variant of _siftup
Maxheap variant of _siftup
Maxheap variant of _siftup
def _siftup_max(heap, pos):
    'Maxheap variant of _siftup'
    endpos = len(heap)
    startpos = pos
    newitem = heap[pos]
    # Bubble up the larger child until hitting a leaf.
    childpos = 2*pos + 1    # leftmost child position
    while childpos < endpos:
        # Set childpos to index of larger child.
        rightpos = childpos + 1
        if rightpos < endpos and not heap[rightpos] < heap[childpos]:
            childpos = rightpos
        # Move the larger child up.
        heap[pos] = heap[childpos]
        pos = childpos
        childpos = 2*pos + 1
    # The leaf at pos is empty now.  Put newitem there, and bubble it up
    # to its final resting place (by sifting its parents down).
    heap[pos] = newitem
    _siftdown_max(heap, startpos, pos)
def _siftup_max ( heap , pos ) : endpos = len ( heap ) startpos = pos newitem = heap [ pos ] # Bubble up the larger child until hitting a leaf. childpos = 2 * pos + 1 # leftmost child position while childpos < endpos : # Set childpos to index of larger child. rightpos = childpos + 1 if rightpos < endpos and not heap [ rightpos ] < heap [ childpos ] : childpos = rightpos # Move the larger child up. heap [ pos ] = heap [ childpos ] pos = childpos childpos = 2 * pos + 1 # The leaf at pos is empty now.  Put newitem there, and bubble it up # to its final resting place (by sifting its parents down). heap [ pos ] = newitem _siftdown_max ( heap , startpos , pos )