TREES, TRIES & HEAPS / ALGORITHM BRIEF

Heap and priority queue

A heap is a complete binary tree stored in an array that keeps the smallest (min-heap) or largest (max-heap) element at the root.

IntermediatePhase 04 / Topic 5 of 8Mental modelComplexityEdge cases
01

Overview

A heap is a complete binary tree stored in an array that keeps the smallest (min-heap) or largest (max-heap) element at the root. Children of index i live at 2i + 1 and 2i + 2, and the parent at floor((i - 1) / 2). Push and pop are O(log n); peeking at the root is O(1).

A priority queue is the abstract idea (always remove the highest-priority item) and a heap is how it is implemented. Heaps power Dijkstra's shortest path, Prim's MST, top K problems, running medians, merging sorted streams, and task schedulers.

A hospital emergency room

Patients are not treated in arrival order but by severity. When a new patient arrives, they are placed according to urgency, and the most critical patient is always seen next. The triage nurse does not fully sort everyone, only keeps the most urgent at the front.

02

When to use it

  • Repeatedly get the smallest or largest item while items are being added.
  • Top K, kth largest, or k closest.
  • Merging k sorted lists or streams.
  • Scheduling by priority or earliest deadline.
  • Greedy algorithms that always pick the cheapest next option (Dijkstra, Prim, Huffman).
03

Problem patterns it solves

Top K / kth element

Recognize it when: k largest, k smallest, kth largest in an array or stream.

  • 215. Kth Largest Element in an Array
  • 703. Kth Largest Element in a Stream
  • 973. K Closest Points to Origin
Two heaps

Recognize it when: running median, balance lower and upper halves.

  • 295. Find Median from Data Stream
  • 480. Sliding Window Median
  • 502. IPO
K-way merge

Recognize it when: merge several sorted sources.

  • 23. Merge k Sorted Lists
  • 378. Kth Smallest Element in a Sorted Matrix
  • 373. Find K Pairs with Smallest Sums
Greedy scheduling

Recognize it when: pick the most frequent / cheapest / earliest next.

  • 621. Task Scheduler
  • 1046. Last Stone Weight
  • 1834. Single-Threaded CPU
  • 767. Reorganize String
Shortest path and MST

Recognize it when: weighted graphs where you expand the cheapest frontier node.

  • 743. Network Delay Time
  • 1584. Min Cost to Connect All Points
  • 787. Cheapest Flights Within K Stops
04

Where it is used in real software

Operating system schedulers

Priority-based schedulers pick the highest-priority runnable process; timers are kept in heaps ordered by expiry time.

Network routing

OSPF routers run Dijkstra's algorithm with a priority queue to compute shortest paths.

Event-driven simulations and job queues

Delayed-job systems like Sidekiq scheduled sets and Java's DelayQueue release the job with the earliest run time first.

Compression

Huffman coding in ZIP and JPEG builds its tree by repeatedly merging the two least frequent symbols from a min-heap.

05

Key terms

Heap property
In a min-heap, every parent <= its children (root is the minimum).
Sift up (bubble up)
After push, swap the new element with its parent while it is smaller.
Sift down (heapify down)
After pop, move the last element to the root and swap it down with the smaller child.
Heapify
Build a heap from an array in O(n) by sifting down from the last parent to the root.
Complete binary tree
All levels full except possibly the last, which fills left to right; no gaps in the array.
06

Push and pop in a min-heap

  1. 1
    Push: append at the end

    Place the new value at index n to keep the tree complete.

  2. 2
    Push: sift up

    While the value is smaller than its parent at (i - 1) >> 1, swap them.

  3. 3
    Pop: take the root

    The minimum is at index 0.

  4. 4
    Pop: move the last element to the root

    This keeps the tree complete.

  5. 5
    Pop: sift down

    Swap with the smaller child until both children are larger or it becomes a leaf.

Push 1 into the min-heap [2, 4, 3, 7, 5]
Step 1 / 4
2
0
4
1
3
2
7
3
5
4

STEP 1Valid min-heap. Children of i are at 2i + 1 and 2i + 2: children of 2 are 4 and 3.

07

Pop from the min-heap [1, 4, 2, 7, 5, 3]

Remove the minimum and restore the heap

Step 1 / 4
StepArrayAction
1[1, 4, 2, 7, 5, 3]take root 1
2[3, 4, 2, 7, 5]move last element (3) to the root
3[2, 4, 3, 7, 5]children of 3 are 4 and 2; smaller is 2, swap
4[2, 4, 3, 7, 5]3 at index 2 has no children: done

NOWStep: 1 | Array: [1, 4, 2, 7, 5, 3] | Action: take root 1

Returned 1, and the new minimum 2 is at the root. The work is one path from root to leaf: O(log n).

08

Implementation

class PriorityQueue {  constructor(compare = (a, b) => a - b) {    this.heap = [];    this.compare = compare; // negative means a has higher priority  }  get size() { return this.heap.length; }  peek() { return this.heap[0]; }   push(value) {    const h = this.heap;    h.push(value);    let i = h.length - 1;    while (i > 0) {      const parent = (i - 1) >> 1;      if (this.compare(h[i], h[parent]) >= 0) break;      [h[i], h[parent]] = [h[parent], h[i]];      i = parent;    }  }   pop() {    const h = this.heap;    if (h.length === 0) return undefined;    const top = h[0];    const last = h.pop();    if (h.length > 0) {      h[0] = last;      let i = 0;      for (;;) {        const left = 2 * i + 1, right = left + 1;        let best = i;        if (left < h.length && this.compare(h[left], h[best]) < 0) best = left;        if (right < h.length && this.compare(h[right], h[best]) < 0) best = right;        if (best === i) break;        [h[i], h[best]] = [h[best], h[i]];        i = best;      }    }    return top;  }} // 295. Find Median from Data Stream with two heapsclass MedianFinder {  constructor() {    this.low = new PriorityQueue((a, b) => b - a); // max-heap: lower half    this.high = new PriorityQueue();                // min-heap: upper half  }  addNum(num) {    this.low.push(num);    this.high.push(this.low.pop());           // keep every low <= every high    if (this.high.size > this.low.size) this.low.push(this.high.pop());  }  findMedian() {    return this.low.size > this.high.size      ? this.low.peek()      : (this.low.peek() + this.high.peek()) / 2;  }}
09

Complexity and performance

peekO(1)

Root of the array.

push / popO(log n)

One root-to-leaf path.

Build heap (heapify)O(n)

Most nodes are near the bottom and move little.

Search arbitrary valueO(n)

Heaps are not sorted.

Heap sortO(n log n)

In place, not stable.

10

Trade-offs

Heap vs sorted array

A sorted array gives O(1) min but O(n) insert. A heap balances both at O(log n).

Heap vs balanced BST

A BST also gives min and max in O(log n) plus ordered iteration and arbitrary deletion, but with more memory and slower constants.

Lazy deletion

Heaps cannot delete arbitrary items efficiently. Mark items as removed and skip them when they reach the top.

11

Variants and related techniques

Indexed priority queue

Tracks each item's position so its priority can be decreased in O(log n); used in optimized Dijkstra.

d-ary heap

Each node has d children; shallower tree, faster pushes.

Fibonacci heap

O(1) amortized decrease-key; theoretical speedup for Dijkstra, rarely used in practice.

12

Common mistakes

  • Assuming Java's PriorityQueue is a max-heap.

    Fix: It is a min-heap by default; pass Collections.reverseOrder() for max.

  • Iterating a PriorityQueue expecting sorted order.

    Fix: Iteration order is the array order, not sorted. Poll repeatedly to get sorted output.

  • Using sort after each insert in JavaScript.

    Fix: That is O(n log n) per insert. Implement or import a binary heap.

  • Changing an element's priority in place.

    Fix: The heap will not re-order. Remove and re-insert, or push a new entry and lazily skip stale ones.

13

Interview questions

Why is building a heap O(n) rather than O(n log n)?

Sifting down from the bottom up, about half the nodes are leaves (no work), a quarter move one level, and so on. The sum n/4 x 1 + n/8 x 2 + ... converges to O(n).

How do you find a running median?

Keep a max-heap for the lower half and a min-heap for the upper half, balanced so their sizes differ by at most one. The median is the top of the larger heap, or the average of both tops.

14

Practice problems

ProblemDifficultyWhat it trains
1046. Last Stone WeightEasyMax-heap simulation.
703. Kth Largest Element in a StreamEasyMin-heap of size k.
215. Kth Largest Element in an ArrayMediumHeap vs quickselect.
621. Task SchedulerMediumGreedy with cooldown.
1834. Single-Threaded CPUMediumTwo orderings.
295. Find Median from Data StreamHardTwo heaps.
23. Merge k Sorted ListsHardK-way merge.