Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Data Structures Mastery Guide

How This Guide Is Organized

This is a multi-file textbook. Because doing every data structure justice (theory, internal working, complexities with why, C++ implementation, dry run, interview questions, practice problems) takes real space, each data structure gets its own chapter file. This file is the spine of the whole guide — the roadmap, the “which structure do I pick” decision engine, and the comparison tables you’ll come back to again and again.

Complete table of contents (all chapters delivered):

  • 00_roadmap_and_strategy.md ← you are here
  • 01_arrays.md
  • 02_strings.md
  • 03_linked_lists.md
  • 04_stack_and_queue.md
  • 05_heaps.md
  • 06_hash_tables.md
  • 07_binary_tree_and_bst.md
  • 08_avl_tree.md
  • 09_trie.md
  • 10_btree_overview.md
  • 11_graphs_part1_bfs_dfs.md
  • 12_topological_sort.md
  • 13_minimum_spanning_tree.md
  • 14_shortest_path.md
  • 15_segment_tree.md
  • 16_fenwick_tree.md
  • 17_disjoint_set_union.md
  • 18_skip_list.md
  • 19_bloom_filter.md
  • 20_lru_cache.md ← capstone

Part 1 — The Learning Roadmap

Prerequisites
│
├── Programming Fundamentals (variables, functions, pointers/references, memory model)
├── Big-O Analysis (time & space complexity, best/avg/worst case, amortized analysis)
├── Recursion (call stack, base case, recurrence relations)
│
├── Linear Data Structures
│   ├── Array               → foundation for everything; contiguous memory
│   ├── String               → array of chars + pattern algorithms
│   ├── Linked List          → breaks the "contiguous" assumption
│   ├── Stack                → LIFO discipline on top of array/list
│   └── Queue                → FIFO discipline on top of array/list
│
├── Hash-Based Structures
│   ├── Hash Table            → O(1) average lookup, needs arrays + linked lists (chaining)
│   ├── Hash Map              → key-value variant
│   └── Hash Set               → key-only variant
│
├── Trees
│   ├── Binary Tree            → hierarchical data, needs recursion mastery
│   ├── BST                    → ordered binary tree, enables O(log n) search
│   ├── AVL Tree                → self-balancing BST, guarantees O(log n)
│   ├── Heap                    → priority-ordered complete binary tree
│   ├── Trie                    → prefix tree, specialized for strings
│   └── B-Tree (Overview)        → disk-optimized generalization of BST
│
├── Graphs
│   ├── Representation (adjacency list/matrix) → needs arrays + linked lists + hash maps
│   ├── BFS                      → needs Queue
│   ├── DFS                      → needs Stack/recursion
│   ├── Topological Sort          → needs DFS + graph theory (DAGs)
│   ├── MST (Kruskal/Prim)         → needs DSU + Heap
│   └── Shortest Path (Dijkstra/Bellman-Ford) → needs Heap + Queue
│
└── Advanced Data Structures
    ├── Segment Tree                → needs Binary Tree + recursion, for range queries
    ├── Fenwick Tree (BIT)           → needs bit manipulation, lighter alternative to Segment Tree
    ├── Disjoint Set Union            → needs arrays, for connectivity/MST
    ├── Skip List                      → needs Linked List, probabilistic alternative to balanced trees
    ├── Bloom Filter (Overview)         → needs Hashing, probabilistic set membership
    └── LRU Cache                        → needs Hash Map + Doubly Linked List (capstone project)

Why this order?

StageWhy it comes herePrerequisitesLearning Outcome
Big-O & RecursionYou cannot reason about any data structure’s cost without Big-O, and trees/graphs are naturally recursiveBasic programmingAbility to analyze any algorithm’s cost and trace recursive calls
Linear structuresSimplest memory models; every later structure is built from these primitivesBig-O, RecursionUnderstand contiguous vs. linked memory, LIFO/FIFO discipline
HashingNeeds arrays (buckets) + linked lists (collision chains) already understoodArrays, Linked ListsUnderstand amortized O(1) and hash collision handling
TreesNeeds recursion fluency; introduces hierarchical (non-linear) thinkingRecursion, Linked ListsUnderstand branching structures, balancing, ordered traversal
GraphsGeneralizes trees (cycles allowed, no root); needs Queue/Stack/Heap from earlierTrees, Stack, Queue, HashingModel real-world networks; traversal and optimization algorithms
Advanced structuresComposed from everything above to solve specialized problems (range queries, connectivity, caching)Trees, Arrays, Hashing, DSU conceptsSolve competitive-programming-level and system-design-level problems

Suggested Timeline (self-paced, ~10-12 weeks at 1-2 hrs/day)

WeekFocusMini Project
1Big-O, Recursion, Arrays, StringsImplement dynamic array (vector) from scratch
2Linked List (singly, doubly, circular)Implement a text editor’s undo feature with a linked list
3Stack & QueueBalanced parentheses checker; browser history simulator
4Hash Table / Map / SetBuild your own HashMap with chaining + resizing
5Binary Tree, BSTIn-order/pre/post traversal visualizer
6AVL Tree, HeapBuild a priority-based task scheduler
7Trie, B-Tree overviewAutocomplete system
8Graph representation, BFS, DFSMaze solver
9Topological Sort, MST, Shortest PathCourse scheduler; network cost optimizer
10Segment Tree, Fenwick Tree, DSURange-sum query system; Kruskal’s MST with DSU
11Skip List, Bloom FilterProbabilistic membership tester
12LRU Cache (capstone) + full mock interviewsProduction-grade LRU cache with O(1) ops

Part 3 — Comparison Tables

Array vs Linked List

AspectArrayLinked List
Memory layoutContiguousScattered, linked via pointers
Access by indexO(1)O(n)
Insert/Delete at frontO(n) (shift elements)O(1)
Insert/Delete at endO(1) amortized (dynamic array)O(1) if tail pointer kept
Insert/Delete in middleO(n)O(n) to find + O(1) to link
Memory overheadLow (just data)Higher (data + pointer(s))
Cache performanceExcellent (locality)Poor (pointer chasing)
Use caseRandom access, fixed/known size rangesFrequent insert/delete, unknown size

Stack vs Queue

AspectStackQueue
DisciplineLIFO (Last In, First Out)FIFO (First In, First Out)
Core opspush, pop, peekenqueue, dequeue, front
Typical backing structureArray or Linked ListArray (circular) or Linked List
Real useFunction call stack, undo, backtrackingTask scheduling, BFS, print queue

Queue vs Deque

AspectQueueDeque (Double-Ended Queue)
Insertion/removal pointsRear (in), Front (out) onlyBoth ends
FlexibilityLowerHigher — can act as stack or queue
Use caseSimple FIFO tasksSliding window problems, palindrome checks

Hash Table vs Tree (BST)

AspectHash TableBST
Average search/insert/deleteO(1)O(log n)
Worst caseO(n) (bad hash/collisions)O(n) (unbalanced)
OrderingNo inherent orderSorted order maintained
Range queriesNot supported directlySupported efficiently
Memory overheadModerate (buckets + collision handling)Moderate (pointers per node)
Use caseFast lookup, no ordering neededSorted data, range queries

BST vs AVL vs Red-Black Tree

AspectBSTAVL TreeRed-Black Tree
Balance guaranteeNoneStrict (height diff ≤ 1)Loose (path length ≤ 2× shortest)
SearchO(log n) avg, O(n) worstO(log n) guaranteedO(log n) guaranteed
Insert/DeleteO(log n) avg, O(n) worstO(log n) but more rotationsO(log n), fewer rotations
Rotation frequencyN/AHigh (strict balance)Lower
Use caseSimple, small/random dataRead-heavy workloadsWrite-heavy workloads (e.g., Linux CFS, std::map)

Heap vs BST

AspectHeapBST
StructureComplete binary tree (array-backed)Not necessarily complete
OrderingParent ≤/≥ children only (weak order)Left < Root < Right (strict order)
Find min/maxO(1)O(log n) (leftmost/rightmost)
Find arbitrary elementO(n)O(log n)
Use casePriority queues, schedulingSorted traversal, range search

Trie vs Hash Map

AspectTrieHash Map
Key typeStrings (sequences)Any hashable type
Prefix searchO(L) — excellentNot supported directly
MemoryHigher (node per character)Lower typically
Ordered iterationLexicographic, naturalNot ordered
Use caseAutocomplete, spell-check, IP routingGeneral key-value lookup

BFS vs DFS

AspectBFSDFS
Data structure usedQueueStack (or recursion)
ExploresLevel by levelBranch by branch, deep first
Shortest path (unweighted)YesNo (not guaranteed)
Memory usageCan be high (wide graphs)Can be high (deep graphs)
Use caseShortest path, level-order, social network “degrees of separation”Cycle detection, topological sort, connected components

Segment Tree vs Fenwick Tree (BIT)

AspectSegment TreeFenwick Tree
Build timeO(n)O(n log n) or O(n) with tricks
Query/UpdateO(log n)O(log n)
Memory~4n~n
FlexibilitySupports range min/max/gcd/custom opsBest suited for prefix sums/frequency
Code complexityHigherLower, simpler to implement
Use caseComplex range queries with updatesSimple prefix-sum / frequency-count problems

Part 4 — Decision Guide: “Which Data Structure Should I Choose?”

START: What is your primary need?
│
├── Need fast lookup by key, don't care about order?
│   └── Hash Map / Hash Set  → O(1) average
│
├── Need data kept in sorted order AND fast lookup?
│   ├── Frequent reads, few writes → AVL Tree
│   └── Frequent writes → Red-Black Tree (or std::map in practice)
│
├── Need to repeatedly get the min/max element (priority)?
│   └── Heap (Priority Queue)  → O(log n) insert/extract-min
│
├── Need prefix-based search (autocomplete, dictionary)?
│   └── Trie
│
├── Need to process elements in the order they arrived?
│   └── Queue (FIFO)
│
├── Need to undo actions / match nested structures (parentheses, recursion)?
│   └── Stack (LIFO)
│
├── Need frequent insert/delete at arbitrary positions, size changes a lot?
│   └── Linked List (Doubly Linked if you need backward traversal)
│
├── Need random access by index, size relatively fixed?
│   └── Array / Dynamic Array (Vector)
│
├── Modeling relationships/connections (networks, maps, dependencies)?
│   ├── Need shortest path (unweighted) → BFS
│   ├── Need shortest path (weighted, non-negative) → Dijkstra (Heap-based)
│   ├── Need shortest path (negative weights allowed) → Bellman-Ford
│   ├── Need to detect cycles / order tasks by dependency → DFS + Topological Sort
│   └── Need minimum connections to link everything → MST (Kruskal + DSU, or Prim + Heap)
│
├── Need to answer "sum/min/max of range [L,R]" repeatedly, with updates?
│   ├── Simple prefix sums, point updates → Fenwick Tree (BIT)
│   └── Complex range queries (min, gcd, lazy propagation) → Segment Tree
│
├── Need to quickly check "are these two elements connected/grouped"?
│   └── Disjoint Set Union (Union-Find)
│
├── Need probabilistic "have I seen this before" with minimal memory, false positives OK?
│   └── Bloom Filter
│
└── Need a cache that evicts least-recently-used items in O(1)?
    └── LRU Cache (Hash Map + Doubly Linked List)

Reasoning behind key decisions:

  • Hash Map over BST for pure lookup: if you never need sorted order or range queries, paying O(log n) instead of O(1) is wasted cost.
  • Heap over sorted array for priority queues: maintaining a fully sorted array costs O(n) per insert; a heap only costs O(log n) because it only enforces partial order.
  • Fenwick Tree over Segment Tree when possible: simpler code, less memory — reach for Segment Tree only when you need operations Fenwick can’t express cleanly (range min/max, complex merges, lazy propagation).
  • DSU over BFS/DFS for connectivity queries: if you need repeated “are A and B connected” queries with a changing graph (edges added over time), DSU answers in near O(1) amortized; recomputing BFS/DFS each time would be far more expensive.

Part 6 — Master Cheat Sheet (Complexity Recap)

StructureAccessSearchInsertDeleteSpace
ArrayO(1)O(n)O(n)O(n)O(n)
Dynamic ArrayO(1)O(n)O(1) amortizedO(n)O(n)
Singly Linked ListO(n)O(n)O(1)*O(1)*O(n)
Doubly Linked ListO(n)O(n)O(1)*O(1)*O(n)
StackO(n)O(n)O(1)O(1)O(n)
QueueO(n)O(n)O(1)O(1)O(n)
Hash Table (avg)—O(1)O(1)O(1)O(n)
Hash Table (worst)—O(n)O(n)O(n)O(n)
BST (avg)O(log n)O(log n)O(log n)O(log n)O(n)
BST (worst)O(n)O(n)O(n)O(n)O(n)
AVL TreeO(log n)O(log n)O(log n)O(log n)O(n)
HeapO(1) min/maxO(n)O(log n)O(log n)O(n)
Trie—O(L)O(L)O(L)O(alphabet × nodes)
Segment Tree—O(log n)O(log n)O(log n)O(n)
Fenwick Tree—O(log n)O(log n)O(log n)O(n)
DSU (with path compression + union by rank)—~O(α(n))~O(α(n))—O(n)

*with pointer to node already in hand; O(n) if you must search first.

Memory tricks:

  • “Array = fast read, slow write in the middle.” “Linked List = slow read, fast write anywhere (once located).”
  • “Heap gives you the best item fast, not any item fast.”
  • “BST is sorted array + fast insert, at the cost of possible imbalance.”
  • “Trie trades memory for prefix-speed.”
  • “DSU: think of it as α(n) ≈ constant for all practical n — that’s why Union-Find is called ‘almost O(1)’.”

Interview Readiness Checklist (fill in as you complete each chapter)

  • Arrays — implemented dynamic array, solved 10+ problems
  • Strings — pattern matching (KMP/Z-function), solved 10+ problems
  • Linked Lists — reversal, cycle detection, merge, solved 10+ problems
  • Stack/Queue — monotonic stack, sliding window max, solved 10+ problems
  • Hashing — collision handling explained, solved 10+ problems
  • Trees — all traversals, balancing explained, solved 10+ problems
  • Heap — build-heap in O(n) explained, solved 10+ problems
  • Trie — autocomplete built, solved 5+ problems
  • Graphs — BFS/DFS/topo sort/MST/shortest path all implemented, solved 15+ problems
  • Advanced — Segment Tree, Fenwick, DSU implemented, solved 10+ problems
  • Capstone — LRU Cache built and can explain O(1) design in an interview

Next up: open 01_arrays.md for the full 15-section deep-dive chapter on Arrays, built to the exact template we’ll repeat for every remaining structure. Tell me which structure you want next (String, Linked List, Stack/Queue, Hash Table, Trees, Graphs, or Advanced) and I’ll build it to this same depth.