Kazi Rahamatullah
Kazi Rahamatullah
AboutProjectsBlogContact
UsesBooks
ResumeView CV
Kazi Rahamatullah

© Copyright 2026 Kazi Rahamatullah

AboutProjectsBlogBooksUses
Twitter/XGitHubProduct HuntCodeSandbox
Back to Blog
DSATreesHeaps

Tree Array Implementation — Heap Storage Visualized

Store a complete binary tree in a 1-indexed array — parent/child index formulas, interactive visualizer, and heap use cases.

Jul 4, 20263 min read

Introduction

Array implementation stores a complete binary tree in a 1-indexed array — no pointers, just index math. Used by binary heaps, segment trees, and priority queues.

Quick index

#Section
1Array layout
2Interactive visualizer
3Full implementation
4When to use

1. Array layout

plaintext
Tree:       4
           / \
          2   6
         / \ / \
        1  3 5  7
 
Array: [∅, 4, 2, 6, 1, 3, 5, 7]
Index:  0  1  2  3  4  5  6  7

Index 0 is unused — root lives at index 1.

Back to index


2. Interactive visualizer

Tree Array Implementation

Store a complete binary tree in an array — O(1) parent/child via index arithmetic.

1-indexed array

[0]∅
[1]4
[2]2
[3]6
[4]1
[5]3
[6]5
[7]7
parent(i)=⌊i/2⌋ · left(i)=2i · right(i)=2i+1

Complete binary tree stored in a 1-indexed array — index 0 unused.

Array representation

// 1-indexed array for complete binary tree
const tree = [null, 4, 2, 6, 1, 3, 5, 7]
// index 1 = root (4)
// index 2 = left child of 4 (2)
// index 3 = right child of 4 (6)

No pointers — parent/child computed by index math.

Root index

1

Space

O(n)

Used in

Heaps

Back to index


3. Full implementation

js
class ArrayBinaryTree {
  constructor(values) {
    // 1-indexed: index 0 unused
    this.tree = [null, ...values]
  }
 
  parent(i) {
    return Math.floor(i / 2)
  }
  left(i) {
    return 2 * i
  }
  right(i) {
    return 2 * i + 1
  }
 
  get(i) {
    return this.tree[i] ?? null
  }
 
  set(i, val) {
    this.tree[i] = val
  }
 
  root() {
    return this.tree[1] ?? null
  }
 
  height() {
    let h = 0
    for (let i = this.tree.length - 1; i >= 1; i--) {
      if (this.tree[i] != null) {
        h = Math.floor(Math.log2(i)) + 1
        break
      }
    }
    return h
  }
}

Function calls with output

js
// ── Demo calls — output shown on the right ──
const t = new ArrayBinaryTree([4, 2, 6, 1, 3, 5, 7])
 
t.root() // → 4
t.get(t.left(1)) // → 2  (left child of root)
t.get(t.right(1)) // → 6  (right child of root)
t.get(t.parent(5)) // → 3  (parent of index 5)
t.height() // → 3

Back to index


4. When to use

ScenarioWhy array
Binary heapComplete tree, O(1) parent/child
Segment treeFixed-size complete tree
Priority queueHeap array backing
Memory efficiencyNo pointer overhead

Warning

Array implementation only works well for complete or nearly complete trees. Sparse trees waste array space — use pointer-based nodes instead.

Back to index


Summary

1-indexed arrays map cleanly to complete binary trees — O(1) navigation via parent, left, right formulas.

Next reads: Binary Search Trees · Binary Trees

Share this article

XLinkedInFacebook
Kazi Rahamatullah

Written by

Kazi Rahamatullah

FullStack Developer

X / TwitterGitHubLinkedIn

Subscribe to my newsletter

Stay up to date and get notified when I share new contents.

No spam ever, unsubscribe anytime