From the definition of binary heap in Wikipedia,
sift-up
is also called up-heap
operation, and sift-down
is called down-heap
.
So in heap (complete binary tree), up
means from leaf to root, and down
means from root to leaf.
But in python, it seems just opposite. I'm confused by meaning of siftup
and siftdown
, and misused when my first time.
Here is the python version implementation of _siftdown
and _siftup
in heapq
:
# 'heap' is a heap at all indices >= startpos, except possibly for pos. pos
# is the index of a leaf with a possibly out-of-order value. Restore the
# heap invariant.
def _siftdown(heap, startpos, pos):
newitem = heap[pos]
# Follow the path to the root, moving parents down until finding a place
# newitem fits.
while pos > startpos:
parentpos = (pos - 1) >> 1
parent = heap[parentpos]
if newitem < parent:
heap[pos] = parent
pos = parentpos
continue
break
heap[pos] = newitem
def _siftup(heap, pos):
endpos = len(heap)
startpos = pos
newitem = heap[pos]
# Bubble up the smaller child until hitting a leaf.
childpos = 2*pos + 1 # leftmost child position
while childpos < endpos:
# Set childpos to index of smaller child.
rightpos = childpos + 1
if rightpos < endpos and not heap[childpos] < heap[rightpos]:
childpos = rightpos
# Move the smaller 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(heap, startpos, pos)
Why opposite in python? I have confirmed in wiki and several other articles. Is there anything I missing or misunderstanding?
Thanks for reading, I really appreciate it to help me out. : )