%3C/text%3E%3C/svg%3E">
💻
💻 Tech & IT

Binary Heap Index Calculator

Results update as you type.

Your numbers

Understanding the binary heap index calculator — concept and principle

A binary heap is usually stored not as linked nodes but as a plain array, with the tree structure implied by the positions. This calculator takes a 0-based array index and returns its parent, its two children and its depth in the tree. It is handy when writing or debugging heapsort, priority queues and sift-up or sift-down code.

Heap node 5 with its parent and childrenIn a 0-based array heap, index 5 has parent ⌊(5−1)/2⌋ = 2, children 11 and 12, and depth ⌊log₂ 6⌋ = 2.The same heap as a tree and as an array01234567891011120123456789101112arrayparent 2⌊(5−1)/2⌋children 11, 122·5+1, 2·5+2depth 2⌊log₂ 6⌋
No pointers needed: index 5’s parent is 2 and its children are 11 and 12.

How it is calculated

A child index is valid only if it is smaller than the heap size; otherwise that child does not exist.

The principle

A heap is a complete binary tree: every level is full except possibly the last, which is filled from the left. Reading the nodes level by level, left to right, gives the array order. Level d holds 2ᵈ nodes and starts at index 2ᵈ − 1. The root's children take slots 1 and 2, and each later node's two children follow right after those of the node before it, so node i's children land at 2i + 1 and 2i + 2; reversing that gives the parent formula. No pointers are needed, which saves memory and keeps the data cache-friendly. With 1-based indexing, used in many textbooks, the formulas become parent ⌊i ÷ 2⌋ and children 2i and 2i + 1.

Worked example

  1. Input: i = 5.
  2. Parent: ⌊(5 − 1) ÷ 2⌋ = ⌊2⌋ = 2.
  3. Children: 2 × 5 + 1 = 11 and 2 × 5 + 2 = 12, shown as 11, 12.
  4. Depth: ⌊log₂ 6⌋ = ⌊2.585⌋ = 2.

Check: depth 2 holds indices 3 to 6, and index 5 is the left child of index 2, whose children are 5 and 6.

How Parent changes with Node index (0-based)How Parent changes with Node index012341.252.53.7556.257.510Example: 2Node index (0-based)
Each point is the calculator re-run with only “Node index (0-based)” changed; the gold point is the default example. Change the inputs above and this chart follows your numbers.
Node index (0-based)Parent
1.250
2.50
3.751
52
6.252
7.53
104

How to read the result

The depth tells you how many swaps a sift-up from this node can take at most. A heap of n items has height ⌊log₂ n⌋, so insert and remove-top run in O(log n). In a heap of size 12, index 5 has only a left child (11), because index 12 would be out of range. The last non-leaf node is at ⌊n ÷ 2⌋ − 1, where bottom-up heap construction starts.

Common mistakes

Keep in mind: The formulas apply to binary heaps only. A d-ary heap uses parent ⌊(i − 1) ÷ d⌋ and children d·i + 1 to d·i + d.

Frequently asked questions

Is a heap the same as a sorted array?

No. A heap only guarantees each parent is no larger (min-heap) or no smaller (max-heap) than its children; siblings are in no particular order.

How long does it take to build a heap from n items?

Bottom-up construction takes O(n), faster than inserting items one by one, which takes O(n log n).

Why use a heap for a priority queue?

It gives the minimum or maximum in O(1) and supports insert and removal in O(log n) with no extra memory.

Sources: J. W. J. Williams, "Algorithm 232: Heapsort", Communications of the ACM 7(6), 1964 · R. W. Floyd, "Algorithm 245: Treesort 3", Communications of the ACM 7(12), 1964 · T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein, Introduction to Algorithms, MIT Press (heapsort)

Checked 8 Nov 2026. Standard formulas and RFC/IEEE definitions, computed locally in your browser — nothing you type is sent anywhere.

💻 All tech & IT tools →🌍 Calculators by country →