Notice Period
visualization

Heap Operations

A tree in disguise: it's just an array with parent = (i-1)//2.

min-heap = [2, 5, 8, 9, 7] · push(3), push(1), then pop()step 1/17
0215283947
implicit tree (parent = (i-1)//2)
i=02 (root)i=15, parent i=0i=28, parent i=0i=39, parent i=1i=47, parent i=1

A min-heap is just this array: [2, 5, 8, 9, 7]. Index i's parent lives at (i-1)//2 — no pointers, the shape is implied.

pseudocode
def push(x): heap.append(x); i = len(heap)-1
while i > 0 and heap[i] < heap[(i-1)//2]:
swap(i, (i-1)//2); i = (i-1)//2
def pop(): top = heap[0]
heap[0] = heap.pop() # last leaf to root
while a child is smaller than heap[i]:
swap(i, smaller_child); i = smaller_child
return top