LOGBOOK

HELP

Quiz Entry - updated: 2026.11.26

Which shape must a heap have, and what is its last node?

A heap of height h is a complete binary tree: every depth i from 0 to h − 1 is full with $2^i$ nodes, and the nodes at the bottom depth h are packed to the left. The last node is the rightmost node at depth h.

A ten-node heap: depths 0 to 2 full, depth 3 filled from the left, the last node ringed and the next free slot dashed

* Full levels above, the bottom level packed to the left: the last node is ringed, the next insertion slot is dashed. *

The shape rules say:

  • Full upper levels: depth 0 has 1 node, depth 1 has 2, depth 2 has 4, and in general depth i holds $2^i$ nodes, for every depth above the bottom one.
  • Bottom level filled from the left: at depth h the nodes sit as far left as possible, with no gaps between them.
  • At most one node with a single child, and that single child must be a left child. Any other node has either two children or none.
  • The last node is the rightmost node on the bottom level. Besides the usual reference to the root, a heap keeps a second reference to the last node (other trees don't need one), because inserting and removing both happen there.

Why insist on this shape? Because it makes the tree as short as possible for its number of nodes (height $O(\log n)$), and it means there is exactly one place where a new node can go (just after the last node) and exactly one node that can be removed without leaving a hole (the last node itself). Both heap operations are built around that.

A further consequence: a complete tree has no gaps in level order, so it can be stored in an array without wasting slots.

Go deeper:

  • doc Binary heap — the shape and heap properties together, with the insert and extract algorithms.

From Quiz: ADS / Priority Queues and Heaps | Updated: Nov 26, 2026