Topic 002 / Core
Data structures and algorithms: hash maps, trees, tries, heaps, queues
Chapter 1.1 · Programming, Data Structures, and Complexity
What and why
Every program (→ 1) stores its data in some shape, and the shape decided determines what the program can do quickly and what it can only do slowly. A hash map answers "do I have this?" in roughly constant time; a sorted list answers the same question in time that grows with size. This topic is a short, practical catalog of the five structures almost every later system component turns out to be a variant of. Builds on: → 1 (composite data).
Diagram
| Structure | Core operation & typical cost | Where it reappears later |
|---|---|---|
| Hash map | get/set, ~O(1) average | caches (→ 82), indexes, deduplication |
| Balanced tree | ordered insert/lookup/range, O(log n) | database indexes (→ 64) |
| Trie | prefix lookup, O(key length) | autocomplete, routing tables |
| Heap | insert / extract-min-or-max, O(log n) | schedulers, priority queues, top-k |
| Queue (FIFO) | enqueue/dequeue, O(1) | task buffers, message queues (→ 106) |
How it works
- Name the operation the code will do most often: exact lookup, ordered range, prefix match, "give me the smallest/largest," or "process in arrival order."
- Pick the structure whose core operation matches — a hash map for exact lookup, a tree for ranges, a trie for prefixes, a heap for priority, a queue for ordering.
- Check the cost of every other operation you still need occasionally — a hash map gives up ordering entirely; that trade-off must be acceptable, not accidental.
- Account for memory overhead — a hash map trades memory (the table itself, often half-empty) for its speed; a tree node carries pointers a plain array doesn't.
- Structures compose: a hash map plus a doubly linked list gives O(1) lookup and O(1) "move to front," which is exactly how an LRU cache is built (→ 86).
Practical example
Trace an LRU cache of capacity 2 (hash map of key → list node, plus a doubly linked list ordered most-to-least recently used):
put(1, A) list: [1] map: {1}
put(2, B) list: [2, 1] map: {1, 2}
get(1) list: [1, 2] map: {1, 2} (1 moved to front)
put(3, C) list: [3, 1] map: {1, 3} (2 evicted — least recently used)
Every step is a hash-map lookup (O(1)) plus a constant number of pointer moves in the list (O(1)) — never a scan of the whole structure, which is the entire point of pairing the two.
When to use it
- Use when: the dominant operation is clear — pick the structure built for it rather than the one you know best.
- Avoid when: you reach for a tree or a hash map "by default" without checking whether a plain sorted array (binary search, no pointers, less memory) already meets the need.
- Failure scenarios: a hash map with a poor hash function or no resizing degrades toward O(n) lookups; an unbalanced tree degrades toward a linked list, also O(n); a heap used where you need arbitrary-position access forces a full scan anyway.
- Simpler alternative: for small or rarely-changing collections, a sorted array with binary search is simpler to reason about than a tree, and often just as fast in practice.
Practice
(mechanism — build a tiny version; 20–30 min, no lab services, any language).
- Implement the LRU cache from the example above using a hash map and a doubly linked list; add a counter that increments once per pointer move or map operation, never once per element scanned.
- Run
get/put1,000 times against it at capacity 50 and at capacity 200 on a skewed key distribution (most calls hit the same 20 keys); confirm the operation counter stays flat regardless of how many total keys have been inserted. - Record the eviction count at each capacity and explain the difference.
Done when the counter shows O(1) work per call at both capacities, and the eviction counts match what the capacity and access pattern predict.
Key takeaway
Five structures cover most of what a system needs to store data in memory; the only real decision is which operation has to be fast. Done when you can: name the dominant operation of a given requirement and justify the structure chosen for it. Used next in → 64 (tree-based indexes), → 86 (eviction policies), → 106 (queues as the basis of messaging).
References
None — this topic makes no external claims.
Source directory