šŸ’» Computer Science · Data Structures

Data structure tricks that make storage click

Arrays, trees, graphs, stacks, and queues — demystified.

šŸ—‚ļø Data Structures

Memory tricks

Proven mnemonics — fast to learn, hard to forget.

Linked Lists
Linked List: O(1) insert at head, O(n) search — no random access
Linked Lists
Fast insertion, slow search — a chain of nodes
Each node stores data + pointer to next. No contiguous memory. Fast insert/delete at head. Must traverse to find element. Doubly linked: each node has prev and next pointers.
Binary Search Trees
BST: left < root < right → O(log n) average search
Binary Search Trees
The BST property enables fast search, insert, and delete
In a BST: left subtree < root < right subtree. O(log n) average for search/insert/delete. O(n) worst case when unbalanced. AVL and Red-Black trees self-balance.
Hash Tables
Hash Table: key → hash function → index → O(1) average lookup
Hash Tables
The data structure behind dictionaries, sets, and caches
Hash function converts key to array index. O(1) average for insert, delete, lookup. Collision handling: chaining (linked list) or open addressing. Python dict, Java HashMap.
Graphs and Graph Traversal
Graph: vertices + edges. BFS uses queue (level-by-level). DFS uses stack (depth-first).
Graphs and Graph Traversal
Graphs model networks — two fundamental ways to traverse them
Vertices (nodes) connected by edges. BFS (breadth-first search): uses queue, finds shortest path in unweighted graph. DFS (depth-first search): uses stack (or recursion), explores as far as possible before backtracking.
Arrays
Array: O(1) random access by index. O(n) insert/delete in middle. Fixed size (static) or dynamic.
Arrays
The most fundamental data structure — contiguous memory
Elements stored in contiguous memory → O(1) random access by index. Insert/delete at end: O(1). Insert/delete in middle: O(n) — must shift elements. Static array: fixed size. Dynamic array (ArrayList, Python list): doubles when full — amortized O(1) append.
Heaps
Heap: complete binary tree. Max-heap: parent ≄ children. Min-heap: parent ≤ children. O(log n) insert and extract.
Heaps
The data structure behind priority queues
Complete binary tree stored as array. Max-heap: parent always ≄ children → root is maximum. Min-heap: parent ≤ children → root is minimum. Insert: add at end, bubble up → O(log n). Extract max/min: swap root with last, remove, bubble down → O(log n). Heapsort: O(n log n).
Tries
Trie (prefix tree): efficient string storage and retrieval. Each node = one character. Used in autocomplete.
Tries
The data structure that powers autocomplete and spell-check
Each node represents one character. Root → complete words traced down the tree. Search/insert: O(m) where m = string length — independent of number of stored strings. Applications: autocomplete, spell-check, IP routing, DNA sequence matching. More memory than hash table but faster prefix lookups.
Balanced Binary Trees
AVL tree: self-balancing BST. Balance factor = height(left) - height(right) must be -1, 0, or 1.
Balanced Binary Trees
Self-balancing trees that guarantee O(log n) operations
BST degenerates to O(n) if elements inserted in sorted order (becomes a linked list). AVL tree: automatically rebalances via rotations when balance factor falls outside [-1, 0, 1]. Red-Black tree: less strictly balanced but faster insertions — used in Java TreeMap, C++ std::map.
Graph Representations
Adjacency matrix: O(1) edge lookup, O(V²) space. Adjacency list: O(V+E) space, better for sparse graphs.
Graph Representations
Two ways to store a graph — choose based on density
Adjacency matrix: 2D array, matrix[i][j]=1 if edge exists. O(1) edge lookup. O(V²) space — wastes space for sparse graphs. Adjacency list: array of lists, each vertex stores its neighbors. O(V+E) space. Better for sparse graphs (most real graphs). Slower edge lookup O(degree).
Matrix
O(1) edge lookup, O(V²) space — dense graphs
List
O(V+E) space — sparse graphs, most algorithms
Circular Buffer
Circular buffer (ring buffer): fixed-size queue using array with wraparound — efficient for streaming data
Circular Buffer
A queue that reuses space by wrapping around
Fixed-size array with head and tail pointers. Enqueue: add at tail, advance tail (mod size). Dequeue: remove at head, advance head. When tail reaches end, wraps to beginning. O(1) enqueue and dequeue. Used in: audio/video streaming buffers, OS I/O buffers, producer-consumer problems.
Sets
Set: unordered collection of unique elements. O(1) average for add/remove/contains using hashing.
Sets
The data structure for unique collections and membership testing
Set stores unique values — duplicates ignored. HashSet: O(1) average add/contains/remove, no ordering. TreeSet: O(log n), maintains sorted order. Useful for: removing duplicates from a list, membership testing, finding common elements (intersection), finding differences. Python set(), Java HashSet.