Intuition The one core idea
A heap is just an ordinary array that we agree to read as a triangle-shaped family tree, where every parent is bigger than its two kids. That single agreement — plus a formula that turns a position number into "who is my parent, who are my children" — is the whole trick behind fast priority queues.
Before you can trust the parent note's sift-up, parent(i)=⌊(i-1)/2⌋, and O ( log n ) claims, you need to own every piece of notation it throws at you. We build them one at a time, from zero. Nothing is used before it is drawn.
Definition Array and index
An array is a row of numbered boxes in a computer's memory. Each box holds one value.
The index i is the box's position number. We write A [ i ] to mean "the value inside box number i of array A ."
We count boxes starting at 0 , not 1 . So the first box is A [ 0 ] , the second is A [ 1 ] , and so on. This is called 0-indexed , and it is the single most common source of confusion in the parent note — so pin it down now.
Intuition Why the topic needs this
A heap stores no arrows/pointers between family members. It stores only this flat row of boxes. Everything about "who is whose parent" has to be computed from index numbers alone. That is why the index i is the star of every heap formula.
n the number of values currently stored — boxes A [ 0 ] through A [ n − 1 ] are in use.
Definition Tree vocabulary
A tree is a picture of things connected in a branching family shape, no loops.
A node is one item (one circle in the picture).
The root is the single top node — nobody sits above it.
A child is a node hanging directly below another; that other is its parent .
A leaf is a node with no children (the bottom of a branch).
The height is the number of steps on the longest path from root down to a leaf.
In a binary tree, every node has at most two children — a left child and a right child. That is the only shape heaps use.
Intuition Why the topic needs this
The parent note's phrase "root-to-leaf path" is literally the height . Every heap operation only ever fixes things along one such path, and its length is what makes operations fast. So "height" is not decoration — it is the running time.
If you want the full definition of the exact tree shape heaps use, see Complete Binary Tree .
Here is the magic move. We lay the tree out into the array by reading it top-to-bottom, left-to-right. Root goes in box 0 , its two children in boxes 1 and 2 , the next level in boxes 3 , 4 , 5 , 6 , and so on.
Two new symbols just appeared — earn them before use:
Definition The floor brackets
⌊ ⌋
⌊ x ⌋ means "round x down to the nearest whole number." Picture a number line: you slide left until you hit an integer. ⌊ 3.9 ⌋ = 3 , ⌊ 2.0 ⌋ = 2 , ⌊ 0.5 ⌋ = 0 .
Why we need it: parents live one level up, and dividing a child's index by 2 lands between whole numbers for one of the two siblings. Flooring snaps both siblings back to their shared parent's exact box number.
Let's check the formula against the picture, covering both a left child and a right child:
Worked example Verify parent for both siblings
Left child i = 3 : ⌊ 2 3 − 1 ⌋ = ⌊ 1 ⌋ = 1 . ✓ parent is box 1.
Right child i = 4 : ⌊ 2 4 − 1 ⌋ = ⌊ 1.5 ⌋ = 1 . ✓ same parent, box 1.
See how the floor absorbed the .5 ? Without it, box 4 would claim a non-existent parent "1.5".
Common mistake The trap the parent note warns about
In 1-indexed textbooks the parent is i /2 . In our 0-indexed world that is wrong.
Test i = 2 : i /2 = 1 (wrong — box 2 is a child of the root, box 0), but ⌊( 2 − 1 ) /2 ⌋ = 0 ✓.
Definition Ordering symbols
a ≥ b reads "a is greater than or equal to b ."
a ≤ b reads "a is less than or equal to b ."
a > b and a < b are the strict versions (no "equal" allowed).
Intuition Why the topic needs this
The heap property is stated entirely with these. A max-heap demands A [ parent ( i )] ≥ A [ i ] — every parent is at least as big as each child. A min-heap flips it: A [ parent ( i )] ≤ A [ i ] . Which symbol appears tells you which primitive (sift-up vs sift-down) fixes a broken node. That is the whole logic of decrease-key.
Mnemonic Which way does the arrow-mouth eat?
The wide-open side of > always faces the bigger number, like a hungry mouth. 5 > 2 : mouth opens toward 5.
"Swap A [ i ] ↔ A [ j ] " means: exchange the two values so what was in box i is now in box j and vice-versa. In code this needs a temporary holder so nothing gets overwritten.
Intuition Why the topic needs this
sift-up and sift-down are nothing but a sequence of swaps that walk a misplaced value along one path until it sits correctly. Count the swaps and you have counted the cost.
Two ideas hide inside O ( log n ) . Split them.
log 2 n
log 2 n answers the question: "how many times can I halve n before reaching 1?" Equivalently, "2 to what power gives n ?"
Picture a full binary tree: each level doubles the number of nodes. So going from n nodes back up to the single root takes about log 2 n levels. That count is the tree's height.
Worked example Feel the logarithm
n = 8 : halve → 4 → 2 → 1. Three halvings, so log 2 8 = 3 . A heap of 8 items is 3 levels tall (below the root's own level), and any single path is ≤ 3 swaps long.
Definition Big-O notation
O ( ⋅ )
O ( f ( n )) is a promise about growth , ignoring constant factors and small terms. "O ( log n ) " means "the work grows no faster than the height of the tree as n grows." "O ( 1 ) " means "a fixed amount of work no matter how big n is" — like reading a single box.
Intuition Why the topic needs this
This is the entire payoff . Because each operation touches only one root-to-leaf path — of length log 2 n — insert/extract/decrease-key are O ( log n ) , while just peeking at the root A [ 0 ] is O ( 1 ) . Every timing claim in the parent note is this one geometric fact in disguise.
Tree vocabulary root parent child leaf height
Index to family formula 2i+1 2i+2 floor
Floor brackets round down
Heap property parent dominates child
log base 2 counts halvings equals height
Heap operations insert extract decrease-key
This foundation feeds straight into the parent Heap operations note , and from there into Priority Queue , Heapsort , Build-Heap (Heapify) , Dijkstra's Algorithm , and Prim's MST . Note that a heap is not a Binary Search Tree — it orders only parent-to-child, not left-to-right.
Cover the right side and test yourself. If any answer is fuzzy, re-read its section above before touching the parent note.
In A [ i ] , what is i and where do we start counting? i is the box's position number; we start at 0 (0-indexed), so the first box is A [ 0 ] .
What does n stand for? The count of values stored; boxes A [ 0 ] through A [ n − 1 ] are in use.
In a binary tree, what are the root, a leaf, and the height? Root = the single top node with no parent; leaf = a node with no children; height = number of steps on the longest root-to-leaf path.
Give the 0-indexed formulas for left child, right child, and parent. left ( i ) = 2 i + 1 , right ( i ) = 2 i + 2 , parent ( i ) = ⌊( i − 1 ) /2 ⌋ .
What does ⌊ x ⌋ do and why is it needed for parent? Rounds down to the nearest whole number; it snaps both siblings' 2 i − 1 values onto their one shared parent index.
State the max-heap property using ≥ . A [ parent ( i )] ≥ A [ i ] — every parent is at least as big as each child.
What does log 2 n count, and how does that relate to a heap? How many times you can halve n to reach 1; this equals the height of the tree, i.e. the length of one root-to-leaf path.
What is the difference between O ( 1 ) and O ( log n ) work? O ( 1 ) = fixed work regardless of n (e.g. read A [ 0 ] ); O ( log n ) = work grows like the tree height (e.g. walk one path of swaps).
Is a heap the same as a binary search tree? No — a heap orders only parent-vs-child; siblings and left-vs-right are unordered.