3.4.10 · D1Trees

Foundations — Heap — max-heap and min-heap properties

1,825 words8 min readBack to topic

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.


0. What is a "tree" at all?

Before "heap" we need "tree". Forget computers for a second.

Figure — Heap — max-heap and min-heap properties

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.

1. "Binary" — the fan-out is exactly two

Left and right are positions, not sizes — a node can have only a left child, only a right, both, or neither.


2. "Complete" — the shape rule (this is the parent's shape property)

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.

Figure — Heap — max-heap and min-heap properties
  • 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 ✗.

See Complete Binary Tree for more on this shape alone.


3. The heap-order rule and the symbols and

The parent writes . Let's earn each piece.

The comparison arrows

  • reads " is greater than or equal to ". The open mouth of the sign faces the bigger side; the flat line under it adds "or equal". Picture: sits at least as high as .
  • reads " is less than or equal to " — the mirror image.

The rule as a picture

Figure — Heap — max-heap and min-heap properties

4. Reading an array: what and mean

The parent suddenly writes things like [9, 5, 8, 1, 4] and . Decode:


5. The array trick — turning a tree into a flat row

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).

Figure — Heap — max-heap and min-heap properties

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. , . We need it because half of an odd number isn't a whole box number, and box numbers must be whole.


6. Logs, floors and why appears

The parent claims height and operations cost . Two new symbols.

The logarithm

Big-O

is Big-O Notation — a coarse label meaning "grows like as gets huge, ignoring constant factors." Since fixing a heap walks at most one root-to-leaf path (one path = height = hops), each insert/extract is .


7. The two repair moves the parent uses

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:

Both walk one vertical path, so both cost — the same log from Section 6.


Prerequisite map

Node, edge, root, parent, child

Binary tree at most two children

Complete binary tree shape rule

Comparison ge and le

Heap-order rule

Array level-order storage

Index formulas 2i+1 2i+2 floor

Log base 2 doubling per level

Height floor log2 n

Heap

Priority Queue and Heapsort


Equipment checklist

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 mean, and where does indexing start?
The value in box number ; 0-based, so the first box is index 0.
0-based children and parent of node ?
left , right , parent .
What does do?
Rounds down to the nearest whole number.
In words, what is ?
How many times you double 1 to reach (or halve to reach 1).
Why is a complete tree only about levels tall?
Each full level doubles the node count, so nodes grow exponentially while levels grow one at a time.
What does 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).