Intuition The ONE core idea
A heap is a tree that is short and wide (so nothing is ever far from the top) and obeys one local rule — every parent is more extreme than its children. Master that rule plus how we secretly store the tree in a flat list, and the whole of the parent topic falls out.
This page assumes nothing . Every squiggle, bracket and word the parent note used gets built here from the ground up, in an order where each idea rests on the one before it.
Before "heap" we need "tree". Forget computers for a second.
Definition Tree (the picture)
A tree is a bunch of circles (we call each a node ) joined by lines (edges ), arranged like a family chart hanging downward from a single top circle. No circle is ever joined back up into a loop.
The vocabulary the parent note throws around, all read off this one picture:
root — the single top node. Everything hangs from it. In the figure it's the amber circle.
child — a node directly below another, connected by one edge.
parent — the node directly above a child. Every node except the root has exactly one parent.
sibling — two nodes sharing the same parent (side by side).
leaf — a node with no children (the bottom fringe).
level (or depth) — how many edges down from the root you are. Root = level 0, its children = level 1, and so on.
height — the number of edges on the longest path from root down to a leaf. A short bushy tree has small height.
Intuition WHY a tree and not a plain list?
A list forces you to walk past everything one by one. A tree branches , so from the top you can reach many nodes in only a few hops. That branching is the seed of the "fast operations" the parent promises.
A binary tree is a tree where every node has at most two children: a left child and a right child . "Binary" literally means "two".
Left and right are positions , not sizes — a node can have only a left child, only a right, both, or neither.
The parent note says a heap is a complete binary tree whose last level fills left to right . Here is what that sentence means as a picture.
Definition Complete binary tree
A binary tree is complete when every level is completely full, except possibly the last, and the last level is filled from the left with no gaps .
Left tree (cyan): every full level, last level packed left → complete ✓.
Right tree (amber X): there's a hole on the left of the bottom row while something sits to its right → not complete ✗.
Intuition WHY does "complete" matter?
Two payoffs, both used constantly in the parent note:
No gaps → we can pour the nodes into a flat array with zero wasted cells (Section 5).
Guaranteed short → a full-except-last tree can't grow tall and skinny, so its height stays tiny (Section 6).
See Complete Binary Tree for more on this shape alone.
The parent writes A [ parent ] ≥ A [ child ] . Let's earn each piece.
a ≥ b reads "a is greater than or equal to b ". The open mouth of the sign faces the bigger side; the flat line under it adds "or equal". Picture: a sits at least as high as b .
a ≤ b reads "a is less than or equal to b " — the mirror image.
Definition Heap-order property
Pick any parent–child pair. In a max-heap the parent's value is ≥ the child's. In a min-heap the parent's value is ≤ the child's. This is checked for every such pair — one local rule, applied everywhere.
Intuition WHY only compare a parent with its own child?
Because that's all we need to guarantee the extreme is on top . If every parent beats its kids, then the root beats its kids, who beat their kids… so by relay the root beats everyone. We never once compare two siblings — that's why the parent note insists siblings are unordered , and why a heap is not a Binary Search Tree .
The parent suddenly writes things like [9, 5, 8, 1, 4] and A [ parent ] . Decode:
Definition Array and its notation
An array is a row of numbered boxes holding values, laid out contiguously in memory.
n = the number of boxes (the size ).
A [ i ] = "the value in box number i ". The number i is the index .
0-based indexing: the first box is box 0 , so a size-n array uses indices 0 , 1 , … , n − 1 .
Common mistake Off-by-one from 0-based vs 1-based
Some textbooks number from 1 . The parent note (and real code) uses 0-based . That single choice changes every index formula, so we lock it in now: first box = index 0.
This is the cleverest hidden move in the parent note. We write the tree into an array by reading it level by level, left to right (this reading order is called level-order ).
Because the tree is complete , level-order fills the array with no holes . Now the child/parent lines don't need to be stored — we can compute them.
The floor brackets ⌊ ⋅ ⌋ mean "round down to the nearest whole number" — e.g. ⌊ 2.7 ⌋ = 2 , ⌊ 5/2 ⌋ = 2 . We need it because half of an odd number isn't a whole box number, and box numbers must be whole.
Worked example Verify against the figure
Node at index 2 (value 8): left ( 2 ) = 2 ⋅ 2 + 1 = 5 , right ( 2 ) = 2 ⋅ 2 + 2 = 6 . Its parent: parent ( 5 ) = ⌊( 5 − 1 ) /2 ⌋ = ⌊ 2 ⌋ = 2 ✓ — matches back. Sanity trick: a child's parent should send you back where you came from.
Intuition WHY does this beat storing pointer links?
No pointer objects to allocate, and neighbours in the tree are neighbours in memory → the CPU cache loves it. This is exactly why heaps power an efficient Priority Queue and Heapsort .
The parent claims height = ⌊ log 2 n ⌋ and operations cost O ( log n ) . Two new symbols.
log 2 n in plain words
log 2 n answers: "how many times must I double 1 to reach (at least) n ?" Equivalently, "how many times can I halve n before hitting 1?" If n = 8 , doubling goes 1 → 2 → 4 → 8 = 3 steps, so log 2 8 = 3 .
Intuition WHY a log shows up in a complete tree
Each new full level doubles the number of nodes: 1, then 2, then 4, then 8… So a tree of height h holds up to 1 + 2 + 4 + ⋯ = 2 h + 1 − 1 nodes. Reading that backwards: to hold n nodes you only need about log 2 n levels. Doubling per level ⇒ log-many levels. A tree of a million nodes is only ~20 levels tall.
O ( log n ) is Big-O Notation — a coarse label meaning "grows like log n as n gets huge, ignoring constant factors." Since fixing a heap walks at most one root-to-leaf path (one path = height = log n hops), each insert/extract is O ( log n ) .
The parent's worked examples fling around "sift-down" and "sift-up". Name them once, precisely, so the later notes can lean on the terms:
Definition Sift-up and sift-down
sift-up (bubble-up): a value that is too extreme for its low position repeatedly swaps with its parent until the order rule holds. Used after insert (new leaf climbs).
sift-down (heapify): a value that is not extreme enough for its high position repeatedly swaps with its more-extreme child until the rule holds. Used after removing the root, and inside Build-Heap (Heapify) O(n) .
Both walk one vertical path, so both cost O ( log n ) — the same log from Section 6.
Node, edge, root, parent, child
Binary tree at most two children
Complete binary tree shape rule
Array level-order storage
Index formulas 2i+1 2i+2 floor
Log base 2 doubling per level
Priority Queue and Heapsort
Cover the right side; you're ready when each answer comes instantly.
What is the root of a tree? The single top node from which everything hangs (no parent).
What makes a binary tree complete ? Every level full except possibly the last, and the last filled left-to-right with no gaps.
Do siblings ever get compared in the heap-order rule? No — only each parent against its own children.
What does A [ i ] mean, and where does indexing start? The value in box number i ; 0-based, so the first box is index 0.
0-based children and parent of node i ? left = 2 i + 1 , right = 2 i + 2 , parent = ⌊( i − 1 ) /2 ⌋ .
What does ⌊ x ⌋ do? Rounds x down to the nearest whole number.
In words, what is log 2 n ? How many times you double 1 to reach n (or halve n to reach 1).
Why is a complete tree only about log 2 n levels tall? Each full level doubles the node count, so nodes grow exponentially while levels grow one at a time.
What does O ( log n ) mean here? Cost grows like the number of levels, because a repair walks one root-to-leaf path.
Difference between sift-up and sift-down? Sift-up climbs by swapping with the parent (after insert); sift-down descends by swapping with the more-extreme child (after removing root).