Home  Dsa   Should we u ...

Should we use array or linked list for storing tree data structure

Not all trees are created using arrays. Whether a tree is stored in an array or linked nodes depends on the type of tree and the operations you need. Let me explain clearly.


1️⃣ Complete Binary Trees / Heaps → Array

Index i:
Left child  = 2*i + 1
Right child = 2*i + 2
Parent      = (i-1)/2

✅ Advantages:


2️⃣ General Binary Trees → Linked Nodes

struct Node {
    int data;
    Node* left;
    Node* right;
};

✅ Advantages:


3️⃣ Summary Table

Tree TypeStorage MethodWhy
Complete Binary Tree / HeapArrayEasy index math, contiguous memory
Sparse or Unbalanced TreeLinked NodesAvoid wasted space, flexible shape
Binary Search Tree (BST)Linked NodesDynamic insertion/deletion
Trie / N-ary TreeLinked nodes / pointersEach node can have variable number of children

Key Takeaway


Published on: Oct 11, 2025, 11:40 PM  
 

Comments

Add your comment