A data structure is a collection of data values, the relationships among them, and the operations that can be performed on those values. It is the organizational layer that algorithms operate on.
Formal View
A data structure is an algebraic structure about data, defined by:
- A set of values — the data being stored
- Relations — how values are connected (e.g. ordering, parent-child, key-value)
- Operations — what you can do (insert, delete, search, traverse)
The choice of data structure determines the time and space complexity of the operations an algorithm needs.
Fundamental Structures
Arrays
- Fixed-size, contiguous memory
- random access by index
- insertion/deletion (requires shifting)
- Cache-friendly — spatial locality
Linked Lists
- Dynamic size, nodes connected by pointers
- insertion/deletion at known node
- access by position
- Poor cache locality
Hash Tables
- Key-value store via hash function
- average for insert, delete, lookup
- worst-case (hash collisions)
- No inherent ordering
Trees
| Variant | Search | Insert | Delete | Notes |
|---|---|---|---|---|
| BST (unbalanced) | worst | worst | worst | Degenerates on sorted input |
| AVL Tree | Height-balanced | |||
| Red-Black Tree | Used in Linux scheduler | |||
| B-Tree | Optimized for disk I/O | |||
| Heap | min/max access |
Graphs
General structure: vertices and edges .
- Adjacency matrix — edge lookup, space
- Adjacency list — space, better for sparse graphs
Choosing a Data Structure
The right structure depends on the dominant operations:
- Mostly lookups by key → hash table
- Ordered traversal + balanced ops → balanced BST
- Priority queue / scheduling → heap
- Graph traversal (BFS, DFS) → adjacency list
- Time-series / sliding window → deque or circular buffer
Related
- Algorithm — uses data structures to process inputs
- RAM Model of Computation — the machine model that determines operation costs
- Computational Problem — the problem a data structure helps solve