Heap Ready
size 8
type min
root 5
  • Comparing
  • Swapping
  • New / done
  • Removed
  • 3array index of the node
Array

Ready

Comparisons 0 Shifts 0
Ready

Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode

How it works

Every operation, in plain steps

These are exactly the steps the visualizer animates.

A tree without pointers

A heap is a COMPLETE binary tree: every level is full except maybe the last, which fills from the left. That means it fits in an array with no gaps and needs no pointers — the position of a value tells you where its parent and children are.

int heap[CAPACITY];
int size = 0;

// for the value at index i:
int parent(int i) { return (i - 1) / 2; }
int left(int i)   { return 2 * i + 1; }
int right(int i)  { return 2 * i + 2; }

// min-heap rule: heap[parent(i)] <= heap[i]

The heap rule

  1. Min-heap: every parent ≤ its children.
  2. Max-heap: every parent ≥ its children.
  3. So the root is the minimum (or maximum).
  4. Siblings are NOT in any order — it is not a BST.

Insert (sift up)

  1. Put the value in the first free spot (end of the array).
  2. Compare it with its parent.
  3. If it should be above the parent, swap.
  4. Repeat until it fits or reaches the root.

Extract (sift down)

  1. Remember the root — it is returned.
  2. Move the LAST value into the root.
  3. Swap it with its better child while it is out of order.
  4. The tree stays complete: no gaps.

Build a heap in O(n)

  1. Start from the last parent: index n/2 − 1.
  2. Sift each parent down, moving towards the root.
  3. Most nodes are near the bottom and move very little.
  4. Total work is O(n), not O(n log n).

Priority queue

  1. A queue where the most important item leaves first.
  2. Insert = heap insert, O(log n).
  3. Serve next = extract root, O(log n).
  4. Used in hospital triage, CPU scheduling, Dijkstra's algorithm.

Heap sort

  1. Build a max-heap from the array.
  2. Swap the root (maximum) to the end.
  3. Shrink the heap by one and sift down.
  4. See it step by step on the Sorting page.
Algorithm

Pseudocode

Language-independent steps. The visualizer shows the same logic in C, Python and Java.

1 INSERT(heap, value)
2 heap[size] ← value; i ← size; size ← size + 1
3 while i > 0 and heap[i] < heap[(i − 1) / 2]
4 swap heap[i], heap[(i − 1) / 2]
5 i ← (i − 1) / 2
Complexity

How fast is each operation?

n = number of values. The height of a heap is always ⌊log₂ n⌋ because the tree is complete, so sift-up and sift-down take at most O(log n) swaps.

Operation Best Average Worst Speed
Peek (min / max) O(1) O(1) O(1) Instant
Insert O(1) O(log n) O(log n) Grows with n
Extract min / max O(log n) O(log n) O(log n) Grows with n
Build heap (bottom-up) O(n) O(n) O(n) Grows with n
Search for any value O(1) O(n) O(n) Grows with n

Heap vs BST vs sorted array

Heap Balanced BST Sorted array
Get the minimum O(1) O(log n) O(1)
Remove the minimum O(log n) O(log n) O(n) — shift everything
Insert O(log n) O(log n) O(n)
Find any value O(n) O(log n) O(log n)
Memory Just an array Nodes + pointers Just an array

Array positions for index i

Relation Formula Example (i = 4)
Parent (i − 1) / 2 1
Left child 2i + 1 9
Right child 2i + 2 10
Last parent n / 2 − 1 n = 10 → 4
Practice

Check your understanding

Answer each question — you will see the explanation straight away.

0 / 6 correct
Question 1

In a min-heap, where is the smallest value?

Question 2

The value at index 5 of a heap array: what is the index of its parent?

Question 3

Where does a newly inserted value go first?

Question 4

After extracting the root, which value is moved into the root position?

Question 5

Is [3, 5, 4, 9, 6, 8] a valid min-heap?

Question 6

How long does it take to build a heap from n values with bottom-up heapify?

Assessment

Heaps quiz

Test yourself: 5 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.