3.4.11 · D1Trees

Foundations — Heapify — bottom-up O(n) build

2,388 words11 min readBack to topic

This page assumes you know nothing. Every symbol the parent note (Heapify — bottom-up O(n) build) throws at you is built here from the ground up, in an order where each idea leans on the one before it. Read top to bottom.


1. The array — a row of numbered boxes

Before any "tree" talk, the raw material is an array: a straight row of storage boxes, each holding one number, each with an index (a position label).

Figure s01 — an array as numbered boxes. Seven values sit in a row; below each box is its index label i=0, 1, 2, …. The orange arrow points at box to show what A[2] names; the green and red captions mark the first box (index ) and the last box (index ). Use it to see that "value" and "position" are two different things.

Figure — Heapify — bottom-up O(n) build
  • ::: the count of items in the array (how many boxes).
  • ::: the value in box number .

The topic needs this because a heap is secretly just this array — no pointers, no tree objects, only boxes and index math.


2. The complete binary tree — reading the row as a triangle

Now the magic reinterpretation. We take that flat row and draw it as a triangle where the first box is at the top, the next two below it, the next four below those, and so on.

Figure s02 — the same row read as a tree. The top strip is the flat blue row of seven numbers; below it the identical seven numbers appear as circles wired into a triangle. Blue circles are internal nodes, green circles are leaves, each labelled with its index i=…. The orange arrow marks the root (index ). Notice the wires: index connects down to indices and ; index down to and — this is the index map of §3 drawn as lines. Nothing is copied — the triangle is just a way of looking at the same row.

Figure — Heapify — bottom-up O(n) build

3. The index map — the arithmetic that glues array to tree

Here is why the "no gaps" rule matters: it lets us jump between a node and its family using only arithmetic, no stored links. Before the formula, we need one symbol.

Now, with already in hand, the map:

Why the topic needs this: siftDown walks down the tree, so it must compute a node's children instantly — that's the / jump.


4. Which boxes are leaves? The start index

Let us actually solve "which is the last node that still has a child?" — the WHY behind :

Figure s03 — where the leaves begin. A 6-node tree: orange circles are internal nodes (), green circles are leaves (). The red arrow lands on index , the last node with a child — build-heap starts sifting here and skips everything to its right. Trace it: node 's left child would be , which still fits (); node 's would be , which falls off — so is a leaf.

Figure — Heapify — bottom-up O(n) build
  • last internal (non-leaf) node ::: index .
  • The topic starts heapify here because leaves are already valid heaps (nothing below them to violate anything), so sifting them is wasted work.

5. The heap property — the rule the whole game enforces

The symbol means "greater than or equal to"; means "less than or equal to." Picture water finding its level: in a max-heap the biggest value bubbles to the top; in a min-heap the smallest does. The root therefore always holds the extreme value — the reason heaps power a Priority Queue and Heapsort.


6. siftDown — pushing a misplaced value to its level

Why "down" and why upward processing? siftDown(i) only works if the stuff below is already tidy. Processing nodes from the last internal one up to the root guarantees that when we reach , its children were fixed earlier — a small bottom-up dynamic-programming order.


7. Height, and counting nodes per height

The parent measures height from the bottom to reason about total cost later.

Now the counting claim that later carries the whole linear-time proof — with its WHY spelled out:


8. Big-O — what "" and "" mean

Only now do we earn the cost-language used everywhere in the parent note.


Prerequisite map

Array with index i

Index map 2i+1 2i+2

Complete binary tree

Floor bracket rounds down

Last internal node floor n over 2 minus 1

Heap property parent bigger than child

siftDown

buildHeap bottom up

Height k and log base 2

Count n over 2 to the k plus 1

Total cost is O of n

Big-O growth rate


Equipment checklist

  • What does mean, and where does counting start? ::: The value in box ; indices start at , last is .
  • What is a complete binary tree? ::: A tree filled row by row, left to right, with no gaps — so it maps onto an array with no wasted boxes.
  • What does do? ::: Rounds down to the nearest whole number.
  • Given node , its left child, right child, and parent indices? ::: , , and .
  • Which index is the last internal (non-leaf) node, and why? ::: ; solving gives , so everything after it is a leaf.
  • What happens for or ? ::: The start index is , so the loop never runs — empty and single-node arrays are already valid heaps.
  • State the max-heap property. ::: Every parent both of its children (min-heap: parent children).
  • What must be true before calling siftDown(i)? ::: Both of 's children's subtrees are already valid heaps.
  • What does compute? ::: The power of that gives — roughly the tree's height in rows.
  • Roughly how many nodes sit at height , and why? ::: At most — the bottom row is ~half the tree and each step up halves the count, so tall nodes are exponentially rare.
  • What is the difference between and ? ::: A straight line vs a line bent slightly upward by the factor.