Data Structures
These are the main data structures that show up in the algorithms we cover. Some have their own topic page; others are the input or the building block for techniques.
Data structures here include both the forms your input might take (array, graph, tree) and the structures we use while solving (stacks, heaps, hash maps, etc.).
For a list of typical input shapes only, see Typical input types on the overview.
Structures Used Across Algorithms
Section titled “Structures Used Across Algorithms”- Array / list — Contiguous, indexable sequence. Used in: two pointers, sliding window, binary search, prefix sum, sorting.
- Hash map / hash set — O(1) lookup/insert (amortized). Used in: sliding window (counts), subarray-sum problems, dynamic programming (memoization), duplicate checks.
- Stack — LIFO. Used in: iterative DFS, backtracking, parsing.
- Queue — FIFO. Used in: BFS, level-order traversal. In Python:
dequefromcollections. - Deque — Double-ended queue. Used in: BFS, some sliding-window problems.
- Graph — Vertices and edges; given as edge list, adjacency list (e.g. dict of neighbors), or adjacency matrix. Used in: BFS, DFS, Union Find, shortest paths.
- Tree — Often represented like a graph (pointers or parent array) or in array form (e.g. heap). Used in: DFS, BFS, divide and conquer, heaps.
Structures With Their Own Topic Page
Section titled “Structures With Their Own Topic Page”- Heap — Priority queue; min or max at the root. Used for “kth largest,” top K, merge k lists.
- Trie — Prefix tree for strings; each path spells a prefix. Used for autocomplete, word search, prefix matching.
- Union Find — Disjoint set union; merge sets and ask “same set?” Used for connected components, Kruskal’s MST, dynamic connectivity.
ASCII Diagrams
Section titled “ASCII Diagrams”Below are simple ASCII diagrams for how each structure is laid out or traversed.
1. Arrays
Section titled “1. Arrays”Index: 0 1 2 3 4 +-----+-----+-----+-----+-----+Array = | 10 | 20 | 30 | | | +-----+-----+-----+-----+-----+Contiguous memory layout:
[ A ][ B ][ C ][ D ][ E ]2. Linked Lists (Singly Linked List)
Section titled “2. Linked Lists (Singly Linked List)”Head | v+------+ +------+ +------+ +------+| 10 | -> | 20 | -> | 30 | -> | 40 | -> NULL+------+ +------+ +------+ +------+Node structure conceptually:
+-----------+| data | next ---->+-----------+3. Hash Maps
Section titled “3. Hash Maps”Key → value mapping:
"apple" -> 5"banana" -> 3"grape" -> 8Internal bucket representation (with hashing):
Index 0 -> NULL 1 -> ("banana", 3) 2 -> NULL 3 -> ("apple", 5) -> ("grape", 8) 4 -> NULLConceptually:
Key ----hash----> Bucket ----> Value4. Stacks (LIFO)
Section titled “4. Stacks (LIFO)” +------+Top -> | 30 | +------+ | 20 | +------+ | 10 | +------+After push(40):
+------+Top -> | 40 | +------+ | 30 | +------+ | 20 | +------+ | 10 | +------+5. Queues (FIFO)
Section titled “5. Queues (FIFO)”Front Rear | | v v+------+ +------+ +------+ +------+| 10 | -> | 20 | -> | 30 | -> | 40 |+------+ +------+ +------+ +------+Dequeue removes from front.
6. Trees (Binary Tree Example)
Section titled “6. Trees (Binary Tree Example)” 10 / \ 5 15 / \ \ 2 7 20General tree structure:
Root / | \ N1 N2 N3 / \ \ N4 N5 N67. Graphs
Section titled “7. Graphs”Undirected graph:
(A) ----- (B) | \ | | \ | (C) ---- (D)Directed graph:
(A) --> (B) ^ | | v(D) <-- (C)Adjacency list representation:
A: B, CB: DC: DD: AFor which Python type to use (list, dict, set, deque, heapq), see Python Data structures.