3.4.12 · D1Trees

Foundations — Heap operations — insert O(log n), extract-max - min O(log n), decrease-key

1,728 words8 min readBack to topic

Before you can trust the parent note's sift-up, parent(i)=⌊(i-1)/2⌋, and 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.


1. What an array actually is (index, A[i])

We count boxes starting at , not . So the first box is , the second is , 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.

the number of values currently stored — boxes through are in use.

2. A tree, a parent, a child, a root, 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.

If you want the full definition of the exact tree shape heaps use, see Complete Binary Tree.


3. The one bridge: array position ⇄ tree position

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 , its two children in boxes and , the next level in boxes , and so on.

Two new symbols just appeared — earn them before use:

Let's check the formula against the picture, covering both a left child and a right child:


4. The comparison symbols: , , ,


5. Swapping two boxes


6. Reading — what "cost" means

Two ideas hide inside . Split them.


7. How it all feeds the topic

Array and index A of i

Tree vocabulary root parent child leaf height

Index to family formula 2i+1 2i+2 floor

Floor brackets round down

Comparison ge le gt lt

Heap property parent dominates child

Swap two boxes

sift-up and sift-down

log base 2 counts halvings equals height

Big-O cost

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.


Equipment checklist

Cover the right side and test yourself. If any answer is fuzzy, re-read its section above before touching the parent note.

In , what is and where do we start counting?
is the box's position number; we start at (0-indexed), so the first box is .
What does stand for?
The count of values stored; boxes through 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.
, , .
What does do and why is it needed for parent?
Rounds down to the nearest whole number; it snaps both siblings' values onto their one shared parent index.
State the max-heap property using .
— every parent is at least as big as each child.
What does count, and how does that relate to a heap?
How many times you can halve 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 and work?
= fixed work regardless of (e.g. read ); = 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.