Data Structures · Heaps and Priority Queues
Array Representation, Index Arithmetic and Heap Shape Counts
Overview
Because a heap is a complete binary tree, it is stored in a plain array with no pointers. Parent and child positions are computed from the index alone.
Almost every numerical question on heaps (how many leaves, what is the height, where can the smallest key sit) comes from this index arithmetic, so it is worth knowing cold.
Key concepts
- 1-indexed convention (used in GATE, CLRS): the root is .
- 0-indexed convention: the root is .
- Always read which convention the question uses before computing.
- The array order is the level order (breadth-first, left to right) of the tree.
- Node is an internal node if (it has at least a left child). Otherwise it is a leaf.
- Heap size is tracked separately as
heap-sizearray length; heap sort shrinksheap-sizewhile the sorted part grows at the end of the same array.
How it works
Which indices are leaves? With 1-indexing, node has a child iff , i.e. .
- Internal nodes: indices .
- Leaves: indices . So there are leaves.
Height. Level (root is level 0) holds at most nodes. A complete tree with nodes has height (in edges) Number of nodes for a given height : at least (last level has one node) and at most (last level full).
Nodes at a given height. The number of nodes at height (leaves are height 0) is at most . This bound is what makes BuildHeap linear (see the lesson on building a heap).
Number of nodes with exactly one child. In a complete binary tree there is at most one such node: it exists iff is even, and it is node (whose only child is ).
Formulas
- Index of the last internal node: . Its position decides where
BuildHeapstarts. - Depth (level) of node (root depth 0): .
Worked examples
Example 1: relatives of node 7 (1-indexed). , , . If then , so node 7 is a leaf.
Example 2: leaves of a 10-node heap. Internal nodes: . Leaves: , so leaves. Node 5 has left child but no right child (): it is the single one-child node.
Example 3: height. . since . The height is 6 edges. A 100-node heap has levels 0 to 6 and 7 levels in all.
Example 4: converting conventions. In a 0-indexed array the element at index 4 has parent and children . In the 1-indexed view the same node is at index 5 with parent 2 and children 10, 11. Same tree, shifted labels.
Example 5: how deep can the 3rd largest key be in a max-heap? Every ancestor of a node holds a larger key. The 3rd largest key has exactly 2 larger keys, so it has at most 2 ancestors, i.e. depth .
In general the -th largest key of a max-heap lies at depth . In particular the 2nd largest key is a child of the root (depth 1), but it need not be the left child.
Example 6: positions of the smallest key in a max-heap. The minimum has no smaller key below it, so it has no children: it is a leaf. For that means indices (8 possible positions).
Comparison
| Quantity | 1-indexed | 0-indexed |
|---|---|---|
| Parent of | ||
| Left child | ||
| Right child | ||
| Last internal node | ||
| First leaf |
Revision summary
- Use bit thinking if it helps: in 1-indexed form,
left(i)is a left shift,right(i)is a left shift plus 1,parent(i)is a right shift. - Leaves occupy the second half of the array.
That is why half of all heap nodes are leaves and why heapifying them is free. - Height of a heap with nodes is in edges, and in levels.
Check which the question asks for. - The maximum of a max-heap is at index 1; the minimum is in .
Previous-year exam insights
- "In a heap of elements stored in an array, which positions can hold the smallest element?" The answer is the leaves: indices to .
- "What is the height of a heap with nodes?" .
Some options will give , which is wrong whenever is not a power of two.
Test a small case: has height 2 (), but . - Questions give an array and ask for the index of a given node's children or parent: apply the formulas for the stated indexing convention.