Skip to content
Part IA Lent Term

Heap Operations

All max-heap operations run in O(logn)O(\log n) time, where nn is the number of elements in the heap. Each operation works by traversing a single path from root to leaf (or leaf to root), bounded by the heap height log2n\lfloor \log_2 n \rfloor.

Max-Heapify (Bubble-Down)

Purpose: Restore the max-heap property at node ii, assuming the subtrees rooted at ii‘s children are already valid max-heaps.

Algorithm:

  1. Let l=2il = 2i, r=2i+1r = 2i + 1.
  2. Set largest to ii if A[i]A[l]A[i] \ge A[l] (or ll exceeds heap size), else to ll.
  3. If rr is within heap and A[r]>A[largest]A[r] > A[\text{largest}], set largest to rr.
  4. If largest i\ne i, swap A[i]A[i] and A[largest]A[\text{largest}], then recursively call Max-Heapify on largest.

Time: O(logn)O(\log n) — each swap moves down one level, at most the height of the heap.

Max-heapify: compare node with children, swap with larger child, recurse down

Walkthrough

Starting with a heap violation at the root: A=[4,14,10,8,7,9,3,2,1]A = [4, 14, 10, 8, 7, 9, 3, 2, 1] (node A[1]=4A[1] = 4, children 1414 and 1010):

StepCurrent nodeChildrenLargestAction
144 at index 11414, 10101414 at index 2Swap 4 and 14
244 at index 288, 7788 at index 4Swap 4 and 8
344 at index 422, 1144 at index 4No swap; done

Result: [14,8,10,4,7,9,3,2,1][14, 8, 10, 4, 7, 9, 3, 2, 1] — a valid max-heap.

Insert (Bubble-Up)

Purpose: Insert a new key into the heap.

Algorithm:

  1. Increment heap_size; place the new key at A[heap_size]A[\text{heap\_size}].
  2. Set j=heap_sizej = \text{heap\_size}.
  3. While j>0j > 0 and A[j]>A[parent(j)]A[j] > A[\text{parent}(j)]:
  4.  Swap A[j]A[j] and A[parent(j)]A[\text{parent}(j)].
  5. j=parent(j)j = \text{parent}(j).

Time: O(logn)O(\log n) — at most one swap per level up to the root.

Extract-Max

Purpose: Remove and return the largest element (the root).

Algorithm:

  1. If the heap is empty, error.
  2. Store A[1]A[1] (the maximum).
  3. Swap A[1]A[1] with A[heap_size]A[\text{heap\_size}].
  4. Decrement heap_size.
  5. Call Max-Heapify on the root (index 1).
  6. Return stored maximum.

Time: O(logn)O(\log n) — constant work plus one Max-Heapify call.

The swap with the last element preserves the structural property (only the rightmost bottom element can be removed without breaking completeness). The heap property is fixed by bubbling the new root downwards.

Increase-Key (Bubble-Up)

Purpose: Increase the value of a key at position ii, then restore the heap property by moving it upward.

Algorithm:

  1. If new value < current value, error.
  2. Set A[i]A[i] to new value.
  3. While i>1i > 1 and A[i]>A[parent(i)]A[i] > A[\text{parent}(i)]:
  4.  Swap A[i]A[i] and A[parent(i)]A[\text{parent}(i)].
  5. i=parent(i)i = \text{parent}(i).

Time: O(logn)O(\log n).

Max-Peek

Simply return A[1]A[1] in O(1)O(1) time. No modification to the heap.

Operations Summary

OperationPurposeTimeMethod
Max-HeapifyFix a single violationO(logn)O(\log n)Bubble down
InsertAdd new elementO(logn)O(\log n)Add at end, bubble up
Extract-MaxRemove and return maxO(logn)O(\log n)Swap with last, bubble down
Increase-KeyUpdate to larger valueO(logn)O(\log n)Update, bubble up
Max-PeekView maximumO(1)O(1)Read A[1]A[1]

All operations rely on the heap height being log2n\lfloor \log_2 n \rfloor, ensuring each upward or downward traversal touches at most O(logn)O(\log n) nodes.