Reading — step 1 of 5
Read
~1 min readHeap Property
Complete Binary Tree
Heaps are stored in arrays without explicit pointers because the tree is complete: every level filled left-to-right.
Index: 0 1 2 3 4 5 6 7 8 9 ...
|
root
| |
| left child of root
|
right child of root
For node at index i (0-indexed):
left = 2i + 1
right = 2i + 2
parent = (i - 1) // 2
This is the classic Eytzinger layout. Cache-friendly: parent and children are at predictable offsets, so prefetchers love it.
Insertion always at the END (next available slot). Removal of root: swap with the last element, shrink, then sift-down.
Memory: just a flat array. Heap with N entries uses N * 8 bytes (for 64-bit numbers). Compare to red-black tree which has 3 pointers per node (24 bytes overhead).
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…