def pop(heap):
x = len(heap) - 1
if x < 1: return None # is heap empty?
y = heap[0]
heap[0] = heap[x]
heap.pop()
a = 0
while True:
l = 2*a+1
r= 2*a + 2
if (l < x and heap[l] < heap[a]) or
(r < x and heap[r] < heap[a]):
if l < x: tmp = l
else: tmp = r
if heap[tmp] < heap[a]:
swap = heap[a]
heap[a] = heap[tmp]
heap[tmp]=swap
a = tmp
else:
break #exit loop lol
return y
is a the parent now?
left or right child?
wait… why tmp?
Static code + a 20-minute lecture
You're left mentally simulating how a while loop updates a pointer or how recursive calls stack up. It's exhausting — and it doesn't stick.
