1. Gyanastra
  2. Data Structures
  3. Heaps and Priority Queues
  4. Array Representation, Index Arithmetic and Heap Shape Counts
Free lesson, no account needed 9 min read 16 practice questions after it
Asked in past papers9 times
  • GATE CSE202520242022202020062001
  • UGC NET201720132011

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 A[1]A[1].
    • parent(i)=⌊i/2⌋\text{parent}(i) = \lfloor i/2 \rfloor
    • left(i)=2i\text{left}(i) = 2i
    • right(i)=2i+1\text{right}(i) = 2i + 1
  • 0-indexed convention: the root is A[0]A[0].
    • parent(i)=⌊(i−1)/2⌋\text{parent}(i) = \lfloor (i-1)/2 \rfloor
    • left(i)=2i+1\text{left}(i) = 2i + 1
    • right(i)=2i+2\text{right}(i) = 2i + 2 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 ii is an internal node if 2i≤n2i \le n (it has at least a left child). Otherwise it is a leaf.
  • Heap size is tracked separately as heap-size ≤\le array length; heap sort shrinks heap-size while the sorted part grows at the end of the same array.

How it works

Which indices are leaves? With 1-indexing, node ii has a child iff 2i≤n2i \le n, i.e. i≤⌊n/2⌋i \le \lfloor n/2 \rfloor.

  • Internal nodes: indices 1,2,…,⌊n/2⌋1, 2, \dots, \lfloor n/2 \rfloor.
  • Leaves: indices ⌊n/2⌋+1,…,n\lfloor n/2 \rfloor + 1, \dots, n. So there are n−⌊n/2⌋=⌈n/2⌉n - \lfloor n/2 \rfloor = \lceil n/2 \rceil leaves.

Height. Level dd (root is level 0) holds at most 2d2^d nodes. A complete tree with nn nodes has height (in edges) h=⌊log⁡2n⌋.h = \lfloor \log_2 n \rfloor. Number of nodes for a given height hh: at least 2h2^h (last level has one node) and at most 2h+1−12^{h+1} - 1 (last level full).

Nodes at a given height. The number of nodes at height kk (leaves are height 0) is at most ⌈n/2k+1⌉\lceil n / 2^{k+1} \rceil. 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 nn is even, and it is node n/2n/2 (whose only child is 2⋅(n/2)=n2 \cdot (n/2) = n).

Formulas

parent(i)=⌊i2⌋,left(i)=2i,right(i)=2i+1(1-indexed)\text{parent}(i)=\left\lfloor \tfrac{i}{2}\right\rfloor,\quad \text{left}(i)=2i,\quad \text{right}(i)=2i+1 \qquad (\text{1-indexed}) leaves=⌈n/2⌉,internal nodes=⌊n/2⌋,h=⌊log⁡2n⌋\text{leaves} = \lceil n/2 \rceil,\qquad \text{internal nodes} = \lfloor n/2 \rfloor,\qquad h = \lfloor \log_2 n \rfloor 2h≤n≤2h+1−12^h \le n \le 2^{h+1} - 1

  • Index of the last internal node: ⌊n/2⌋\lfloor n/2 \rfloor. Its position decides where BuildHeap starts.
  • Depth (level) of node ii (root depth 0): ⌊log⁡2i⌋\lfloor \log_2 i \rfloor.

Worked examples

Example 1: relatives of node 7 (1-indexed). parent(7)=3\text{parent}(7)=3, left(7)=14\text{left}(7)=14, right(7)=15\text{right}(7)=15. If n=12n = 12 then 14,15>n14, 15 > n, so node 7 is a leaf.

Example 2: leaves of a 10-node heap. Internal nodes: 1..51..5. Leaves: 6..106..10, so ⌈10/2⌉=5\lceil 10/2 \rceil = 5 leaves. Node 5 has left child 1010 but no right child (11>1011 > 10): it is the single one-child node.

Example 3: height. n=100n = 100. ⌊log⁡2100⌋=6\lfloor \log_2 100 \rfloor = 6 since 26=64≤100<1282^6 = 64 \le 100 < 128. 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 ⌊3/2⌋=1\lfloor 3/2 \rfloor = 1 and children 9,109, 10. 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 ≤2\le 2.
In general the kk-th largest key of a max-heap lies at depth ≤k−1\le k-1. 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 n=15n = 15 that means indices 8..158..15 (8 possible positions).

Comparison

Quantity1-indexed0-indexed
Parent of ii⌊i/2⌋\lfloor i/2 \rfloor⌊(i−1)/2⌋\lfloor (i-1)/2 \rfloor
Left child2i2i2i+12i+1
Right child2i+12i+12i+22i+2
Last internal node⌊n/2⌋\lfloor n/2 \rfloor⌊n/2⌋−1\lfloor n/2 \rfloor - 1
First leaf⌊n/2⌋+1\lfloor n/2 \rfloor + 1⌊n/2⌋\lfloor n/2 \rfloor

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 nn nodes is ⌊log⁡2n⌋\lfloor \log_2 n \rfloor in edges, and ⌊log⁡2n⌋+1\lfloor \log_2 n \rfloor + 1 in levels.
    Check which the question asks for.
  • The maximum of a max-heap is at index 1; the minimum is in {⌊n/2⌋+1,…,n}\{\lfloor n/2 \rfloor + 1, \dots, n\}.

Previous-year exam insights

  • "In a heap of nn elements stored in an array, which positions can hold the smallest element?" The answer is the leaves: indices ⌊n/2⌋+1\lfloor n/2 \rfloor + 1 to nn.
  • "What is the height of a heap with nn nodes?" ⌊log⁡2n⌋\lfloor \log_2 n \rfloor.
    Some options will give ⌈log⁡2n⌉\lceil \log_2 n \rceil, which is wrong whenever nn is not a power of two.
    Test a small case: n=5n = 5 has height 2 (⌊log⁡25⌋=2\lfloor \log_2 5 \rfloor = 2), but ⌈log⁡25⌉=3\lceil \log_2 5 \rceil = 3.
  • 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.
Pages 1–2 of 2