Data Structures19 sections · 729 units
Open in Course

Array Representation

No pointers needed

Heaps are stored as arrays, not as linked nodes.

For element at index ii:

  • Parent: (i1)/2\lfloor (i - 1) / 2 \rfloor
  • Left child: 2i+12i + 1
  • Right child: 2i+22i + 2

The heap [1,3,2,7,6,4,5][1, 3, 2, 7, 6, 4, 5] is stored exactly as written.

Index 00 is the root, indices 11 and 22 are its children, and so on. This compact representation has excellent cache performance and no pointer overhead.