Basic Docs
cs-fundamentalsalgorithms

Data Structures

Core data structures: arrays, linked lists, trees, and hash maps.

Overview

Data structures organize and store data for efficient access and modification. Choosing the right structure is critical for algorithm performance — the same problem can be O(n) or O(1) depending on the choice.

Array
Contiguous memory, O(1) random access
Linked List
Nodes with pointers, O(1) insert/delete
Stack
LIFO order, push/pop operations
Queue
FIFO order, enqueue/dequeue operations
Tree
Hierarchical nodes, O(log n) depth operations
Hash Map
Key-value store, O(1) average lookup
Time Complexity Comparison (relative operations, n=100)
Array Access O(1)1
BST Search O(log n)7
LinkedList Search O(n)100
HashMap Lookup O(1)1

Arrays vs Linked Lists

Arrays offer fast random access but slow insertion in the middle. Linked lists flip the trade-off — O(1) insert/delete at a known node, but O(n) to reach it. Pick based on your dominant operation.

ArrayLinked List
Access: O(1)Access: O(n)
Insert/Delete middle: O(n)Insert/Delete at node: O(1)
Fixed or dynamic sizeAlways dynamically sized
Cache-friendly (contiguous)Cache-unfriendly (scattered)
[0]
[1]
[2]
[n]

Stacks & Queues

Stacks follow Last-In-First-Out (LIFO); queues follow First-In-First-Out (FIFO). Both are thin abstractions over arrays or linked lists — the difference is purely which end you remove from.

Push A
Push B
Push C
Pop → C (LIFO)
Stack & Queue in JavaScript
// Stack (LIFO)
const stack = [];
stack.push(1); stack.push(2); stack.push(3);
console.log(stack.pop());   // 3

// Queue (FIFO)
const queue = [];
queue.push(1); queue.push(2); queue.push(3);
console.log(queue.shift()); // 1

Trees & Graphs

Trees are hierarchical structures with a root and child nodes. Binary Search Trees keep elements sorted for O(log n) search; heaps maintain priority order for efficient min/max retrieval.

Binary Tree Anatomy
Root — topmost node, no parent
Parent / Child — directional relationship
Leaf — node with no children
Height — longest root-to-leaf path
Tree Node Levels
Root Node
Top-level node, entry point for all traversals
Internal Nodes
Have both parent and child relationships
Leaf Nodes
Terminal nodes with no children
1
Binary Tree
Each node has at most 2 children. Traversal orders: in-order, pre-order, post-order.
2
Binary Search Tree (BST)
Left child < parent < right child. Supports O(log n) search when the tree stays balanced.
3
Heap
Complete binary tree. Max-heap: parent ≥ children. Powers priority queues and heap sort.

Hash Maps

A hash map converts a key to an array index via a hash function, enabling O(1) average-case lookup, insert, and delete. Collisions occur when two keys map to the same index.

Key
Hash Function
Index
Value
Hash Map Usage
const map = new Map();
map.set("name", "Alice");
map.set("age", 30);
console.log(map.get("name")); // "Alice"
console.log(map.size);        // 2
!Collision Handling
When two keys hash to the same index, the map falls back to chaining (a linked list per bucket) or open addressing (probe for the next free slot). A well-designed hash function keeps collisions rare and lookups truly O(1).
Built: 4/8/2026, 12:01:11 PM