Understanding Dynamic Memory
Unlike static arrays which sit comfortably next to each other in memory, Linked Lists and Binary Trees are scattered across the Heap. This interactive guide visualizes how these structures are allocated, linked, and managed by the operating system.
Dynamic Allocation
Memory is not reserved in advance. It is requested at runtime (e.g., new Node()). The OS finds any available "free block" in the Heap.
// Returns a random address like 0x850
Non-Contiguous Storage
Because allocation is dynamic, nodes are scattered. We rely on Pointers to create logical order from physical chaos.
Node B @ 0x850 -> Next: 0x320
Visual Difference: Array vs. Linked Allocation
Static Array (Contiguous)
Neighbors in logic are neighbors in memory.
Linked Structure (Scattered)
Neighbors in logic are distant in memory.
Doubly Linked List (DLL) Simulator
Explore how nodes are allocated in the heap and linked via Next and Prev pointers.
Notice how the "Address" is random, but the pointers create the chain.
Controls
int data;
Node* next;
Node* prev;
};
// Heap Initialized. Waiting for allocation...
| Node Index | Memory Address | Prev Pointer | Data | Next Pointer |
|---|
Binary Tree Memory Map
Trees use similar allocation principles but branch out. Each node holds pointers to a Left Child and a Right Child.
Controls
Nodes are placed visually in a hierarchy for readability, but remember: in the Heap, they are scattered randomly!
int data;
TreeNode* left;
TreeNode* right;
};
// Tree Root empty.
Comparative Analysis
Memory Overhead Composition
A single node requires memory for both Data and Pointers. In a DLL, overhead is high (2 pointers per node).
Access vs. Insertion Efficiency
Linked structures sacrifice random access speed (O(n)) for efficient resizing/insertion (O(1) if loc known).
Structural Comparison
| Feature | Array | Doubly Linked List | Binary Tree |
|---|---|---|---|
| Memory Allocation | Static / Contiguous | Dynamic / Scattered | Dynamic / Scattered |
| Pointer Overhead | None (Index based) | High (Prev + Next) | High (Left + Right) |
| Resize Cost | High (Copy whole block) | Low (Update pointers) | Low (Update pointers) |