๐ฒ A Quick Reminder: Whatโs a Binary Tree?
A binary tree looks something like this:
[A]
/ \
[B] [C]
/ \
[D] [E]
Here:
- A is the root node (the top of the tree).
- B and C are children of A.
- D and E are children of B.
When we draw it, it looks easy to understand. But computers donโt have โbranchesโ and โcirclesโ โ they only understand memory cells and pointers.
So we need a way to represent this structure in a way that the computer can handle efficiently.
There are two main ways to represent a binary tree in memory:
- Array Representation
- Linked Representation
Letโs explore both in a fun and easy way.
๐งฎ 1. Array Representation
Imagine storing a tree in an array โ just like keeping elements in a list where each position has a fixed index.
Hereโs how it works:
- The root node is stored at index 1 (not 0, to make math easy).
- For any node stored at position
i: - The left child is stored at position
2 * i - The right child is stored at position
2 * i + 1
Letโs see this in action.
๐ฟ Example Tree:
[A]
/ \
[B] [C]
/ \
[D] [E]
๐ง Array Representation:
| Index | Node |
|---|---|
| 1 | A |
| 2 | B |
| 3 | C |
| 4 | D |
| 5 | E |
Now letโs apply the formula:
- Root
Ais at index 1 - Left child of A โ 2 ร 1 = 2 โ B
- Right child of A โ 2 ร 1 + 1 = 3 โ C
- Left child of B โ 2 ร 2 = 4 โ D
- Right child of B โ 2 ร 2 + 1 = 5 โ E
Thatโs how the positions are calculated!
โ Pros:
- Simple and fast to access any node.
- Great for complete binary trees (where all levels are filled).
โ Cons:
- Wastes memory when the tree is not full.
(Because some indices in the array stay empty.)
๐ 2. Linked Representation
Now imagine building the tree using linked nodes, just like a linked list, but with two pointers instead of one.
Each node has three parts:
- Data (the value stored in the node)
- Left pointer (address of left child)
- Right pointer (address of right child)
Itโs like this:
-------------------------
| Data | Left | Right |
-------------------------
Each pointer connects one node to another, forming the full binary tree structure.
๐ฟ Example:
Letโs take the same tree again:
[A]
/ \
[B] [C]
/ \
[D] [E]
Now letโs imagine how it looks in memory:
+------+---------+---------+
| A | B(ptr) | C(ptr) |
+------+---------+---------+
/ \
+------+---------+---------+
| B | D(ptr) | E(ptr) |
+------+---------+---------+
/ \
+------+---------+---------+
| D | NULL | NULL |
+------+---------+---------+
+------+---------+---------+
| E | NULL | NULL |
+------+---------+---------+
+------+---------+---------+
| C | NULL | NULL |
+------+---------+---------+
Here:
- Each โptrโ is a memory address pointing to another node.
- NULL means thereโs no child in that direction.
So, the computer connects these pointers in memory to form the complete structure โ just like connecting pieces of a puzzle.
โ Pros:
- No wasted memory, even if the tree is incomplete.
- Easy to grow or shrink the tree dynamically.
- Suitable for all kinds of trees (not just complete ones).
โ Cons:
- Takes extra space for storing pointers.
- Slightly slower to access elements than array representation.
๐งพ Summary: Array vs Linked Representation
| Feature | Array Representation | Linked Representation |
|---|---|---|
| Storage | Uses array indexes | Uses pointers |
| Best for | Complete binary trees | Any kind of binary tree |
| Memory use | May waste space | Efficient memory use |
| Ease of insertion/deletion | Hard | Easy |
| Access time | Fast (using index) | Slower (need to follow links) |
๐ผ๏ธ Simple Diagram: Two Ways to Store a Binary Tree
Binary Tree:
[A]
/ \
[B] [C]
/ \
[D] [E]
-------------------------------
Array Representation:
Index: 1 2 3 4 5
Node : [A] [B] [C] [D] [E]
-------------------------------
Linked Representation:
[A]
/ \
[B] [C]
/ \
[D] [E]
(Each node has pointers: Left โ Right)