Ensiklopedia VibeKoding: Data Structures: An Introduction.Ensiklopedia VibeKoding: Data Structures: An Introduction.
Programs = Data Structures + Algorithms. Previously, we learned how the CPU executes instructions and how the operating system manages resources. But the core objects that programs handle are data β user information, product lists, social relationships... How this data is organized in memory directly determines whether a program is fast or slow. You may have wondered: why do some programs process tens of thousands of records quickly while others freeze with just a few hundred? The answer often lies in the choice of data structures.Programs = Data Structures + Algorithms. Previously, we learned how the CPU executes instructions and how the operating system manages resources. But the core objects that programs handle are data β user information, product lists, social relationships... How this data is organized in memory directly determines whether a program is fast or slow. You may have wondered: why do some programs process tens of thousands of records quickly while others freeze with just a few hundred? The answer often lies in the choice of data structures.
What will you learn from this article?What will you learn from this article?
After completing this chapter, you will gain:After completing this chapter, you will gain:
| Chapter | Content | Core Concepts |
|---|---|---|
| Chapter 1 | Big Picture | Four major data structure categories, classification criteria |
| Chapter 2 | Linear Structures | Arrays, linked lists, stacks, queues |
| Chapter 3 | Hash Tables | Hash functions, collision handling, O(1) lookup |
| Chapter 4 | Tree Structures | Binary trees, file system trees, DOM trees |
| Chapter 5 | Graph Structures | Directed graphs, undirected graphs, traversal algorithms |
| Chapter 6 | Performance Comparison | Time complexity, space complexity |
| Chapter 7 | Selection Guide | Scenario analysis, decision flow |
------
Imagine you need to organize a pile of books:Imagine you need to organize a pile of books:
Different organizational methods result in vastly different book-finding efficiency. A data structure is the "organization method" for data β it determines how data is stored, found, and modified.Different organizational methods result in vastly different book-finding efficiency. A data structure is the "organization method" for data β it determines how data is stored, found, and modified.
All data structures can be categorized into four major types:All data structures can be categorized into four major types:
| Type | Data Relationship | Typical Examples | Real-life Analogy |
|---|---|---|---|
| Linear | One-to-one, arranged in a line | Arrays, linked lists, stacks, queues | Train cars, checkout lines |
| Hash | KeyβValue mapping | Hash tables, dictionaries, sets | Library index cards |
| Tree | One-to-many, hierarchical | Binary trees, B-trees, heaps | Family trees, folder structures |
| Graph | Many-to-many, networked | Directed graphs, undirected graphs | Subway maps, social networks |
Because there is no universal data structure. Each one is a trade-off between "lookup speed," "insertion speed," and "memory usage." Just as you wouldn't use a backpack to move furniture or a truck to deliver a single letter β choosing the right tool makes all the difference.Because there is no universal data structure. Each one is a trade-off between "lookup speed," "insertion speed," and "memory usage." Just as you wouldn't use a backpack to move furniture or a truck to deliver a single letter β choosing the right tool makes all the difference.
------
Linear structures are the most intuitive way to organize data β data items are arranged one after another, like train cars. But different "connection methods" and "operation endpoints" produce four variants, each with its own strengths.Linear structures are the most intuitive way to organize data β data items are arranged one after another, like train cars. But different "connection methods" and "operation endpoints" produce four variants, each with its own strengths.
Arrays and linked lists are the two most basic linear structures. Their core difference lies in memory layout:Arrays and linked lists are the two most basic linear structures. Their core difference lies in memory layout:
| Comparison | Array | Linked List |
|---|---|---|
| Memory layout | One continuous block | Scattered, connected by pointers |
| Access nth element | Calculate address directly, O(1) | Search from the head one by one, O(n) |
| Insert in the middle | Must shift all subsequent elements, O(n) | Just change two pointers, O(1) |
| Size | Fixed at creation | Can grow at any time |
| Real-life analogy | A row of numbered lockers | A chain of treasure hunt clues |
- Known data volume, frequent access by position β Array (e.g., student grade tables, pixel matrices) - Unknown data volume, frequent insertion/deletion β Linked list (e.g., playlists, undo history) - Not sure? β Start with an array. In most scenarios, arrays' cache-friendly nature provides greater performance advantages- Known data volume, frequent access by position β Array (e.g., student grade tables, pixel matrices) - Unknown data volume, frequent insertion/deletion β Linked list (e.g., playlists, undo history) - Not sure? β Start with an array. In most scenarios, arrays' cache-friendly nature provides greater performance advantages
Stacks and queues are essentially arrays or linked lists, just with restricted operation methods. It may seem like reduced functionality, but this restriction gives them specific purposes:Stacks and queues are essentially arrays or linked lists, just with restricted operation methods. It may seem like reduced functionality, but this restriction gives them specific purposes:
| Structure | Rule | Operations | Analogy | Where in Your Code? |
|---|---|---|---|---|
| Stack | Last In, First Out (LIFO) | push / pop | A stack of plates | Function call stack, browser back button, Ctrl+Z undo |
| Queue | First In, First Out (FIFO) | enqueue / dequeue | Waiting in line for tickets | Task scheduling, message queues, print queues |
Imagine a stack with only two operations β "place plate" and "remove plate." You'll never get the order wrong. Restriction brings certainty, and certainty brings reliability. The function call stack relies on "last in, first out" to ensure the most recently called function returns first. If random access to intermediate functions were allowed, programs would be chaotic.Imagine a stack with only two operations β "place plate" and "remove plate." You'll never get the order wrong. Restriction brings certainty, and certainty brings reliability. The function call stack relies on "last in, first out" to ensure the most recently called function returns first. If random access to intermediate functions were allowed, programs would be chaotic.
------
Linear structures aren't fast enough for lookups β arrays require O(n) traversal, and even sorted binary search is O(log n). Is there a structure that can achieve O(1) direct lookup? Yes β the hash table.Linear structures aren't fast enough for lookups β arrays require O(n) traversal, and even sorted binary search is O(log n). Is there a structure that can achieve O(1) direct lookup? Yes β the hash table.
The principle of hash tables is actually quite simple:The principle of hash tables is actually quite simple:
hash("apple") = 3)A hash function computes a number from the key (e.g., hash("apple") = 3)This is like a library's index system: instead of searching shelf by shelf, you check the index card to find the book's exact location.This is like a library's index system: instead of searching shelf by shelf, you check the index card to find the book's exact location.
Two different keys may compute the same index β this is called a hash collision. Like two books having the same index number pointing to the same location.Two different keys may compute the same index β this is called a hash collision. Like two books having the same index number pointing to the same location.
| Resolution Method | Principle | Analogy |
|---|---|---|
| Chaining | Store multiple values at the same position using a linked list | Put multiple books in the same cabinet |
| Open addressing | If there's a collision, look for the next empty slot | If the cabinet is full, use the adjacent one |
| Operation | Average Case | Worst Case (All Collisions) |
|---|---|---|
| Lookup | O(1) | O(n) |
| Insert | O(1) | O(n) |
| Delete | O(1) | O(n) |
When all keys map to the same index, the hash table degrades into a linked list and all operations become O(n). Prevention: choose a good hash function + dynamic resizing (expand when the load factor exceeds a threshold).When all keys map to the same index, the hash table degrades into a linked list and all operations become O(n). Prevention: choose a good hash function + dynamic resizing (expand when the load factor exceeds a threshold).
- JavaScript {} objects and Map β Hash table - Python dict β Hash table - Java HashMap β Hash table - Database indexes β Also use hashing at theεΊε± Every time you write user["name"] or map.get("key"), a hash table is working behind the scenes.- JavaScript {} objects and Map β Hash table - Python dict β Hash table - Java HashMap β Hash table - Database indexes β Also use hashing at theεΊε± Every time you write user["name"] or map.get("key"), a hash table is working behind the scenes.
------
Hash tables are fast for lookups, but data is unordered. If you need both fast lookup and ordered data, you need tree structures.Hash tables are fast for lookups, but data is unordered. If you need both fast lookup and ordered data, you need tree structures.
The core characteristic of a tree: each node can have multiple "children" but only one "parent" (except the root node). This one-to-many hierarchical relationship is everywhere in the real world.The core characteristic of a tree: each node can have multiple "children" but only one "parent" (except the root node). This one-to-many hierarchical relationship is everywhere in the real world.
A binary search tree has one simple but powerful rule: left is smaller, right is larger.A binary search tree has one simple but powerful rule: left is smaller, right is larger.
When searching, each comparison eliminates half the nodes, with time complexity O(log n). Like the number guessing game β "Is it bigger or smaller than 50?" "Bigger." "Bigger or smaller than 75?" β eliminating half each time.When searching, each comparison eliminates half the nodes, with time complexity O(log n). Like the number guessing game β "Is it bigger or smaller than 50?" "Bigger." "Bigger or smaller than 75?" β eliminating half each time.
Binary search trees have a problem: if data is inserted in order (1, 2, 3, 4, 5), the tree degenerates into a linked list and lookups return to O(n). Balanced trees avoid this by automatically adjusting the structure:Binary search trees have a problem: if data is inserted in order (1, 2, 3, 4, 5), the tree degenerates into a linked list and lookups return to O(n). Balanced trees avoid this by automatically adjusting the structure:
| Type | Balancing Strategy | Characteristics | Typical Applications |
|---|---|---|---|
| AVL Tree | Strict balance (height difference β€ 1) | Fastest lookups, slightly slower insertions/deletions | Scenarios requiring frequent lookups |
| Red-Black Tree | Approximate balance | Good overall performance | Java TreeMap, Linux kernel |
| B-Tree | Multi-way balance; one node stores multiple values | Reduces disk I/O | Database indexes |
- File system: Nested folders are tree structures - HTML DOM: ------ Trees can only represent "one-to-many" hierarchical relationships. But many real-world relationships are "many-to-many" β your friends also have friends, and there are multiple routes between cities. A structure where any node can potentially connect to any other node is a graph.Trees can only represent "one-to-many" hierarchical relationships. But many real-world relationships are "many-to-many" β your friends also have friends, and there are multiple routes between cities. A structure where any node can potentially connect to any other node is a graph. Graph traversal is more complex than linear structures because there may be cycles (AβBβCβA), requiring tracking of "visited" nodes:Graph traversal is more complex than linear structures because there may be cycles (AβBβCβA), requiring tracking of "visited" nodes: - Map navigation: Cities are nodes, roads are edges; navigation finds the shortest path in the graph - Social networks: Users are nodes, follows/friendships are edges; "People you may know" is graph algorithm recommendations - Package managers: npm/pip dependency relationships are directed graphs; ------ After learning so many data structures, how do their performances actually compare? The interactive comparison below will help you build intuition:After learning so many data structures, how do their performances actually compare? The interactive comparison below will help you build intuition: Core Performance Comparison Table:Core Performance Comparison Table: - O(1): Regardless of data volume, operation time is constant β fastest - O(log n): Doubling the data adds only one step β very fast - O(n): Doubling the data doubles the time β average - O(V+E): Depends on the number of vertices and edges β specific to graphs Note: These are all average cases. In the worst case, hash tables degrade to O(n), and binary search trees also degrade to O(n).- O(1): Regardless of data volume, operation time is constant β fastest - O(log n): Doubling the data adds only one step β very fast - O(n): Doubling the data doubles the time β average - O(V+E): Depends on the number of vertices and edges β specific to graphs Note: These are all average cases. In the worst case, hash tables degrade to O(n), and binary search trees also degrade to O(n). ------ After learning so many data structures, how do you choose when facing actual requirements? The key is to start from the requirements and ask yourself a few questions:After learning so many data structures, how do you choose when facing actual requirements? The key is to start from the requirements and ask yourself a few questions: Quick Decision Flow:Quick Decision Flow: - 80% of scenarios are fine with arrays and hash tables - Consider trees when you need ordering - Consider graphs when relationships are complex - Not sure? Start with the simplest and switch when you hit performance issues. Premature optimization is the root of all evil- 80% of scenarios are fine with arrays and hash tables - Consider trees when you need ordering - Consider graphs when relationships are complex - Not sure? Start with the simplest and switch when you hit performance issues. Premature optimization is the root of all evil ------ > Data structures are the skeleton of programs. Arrays are like a row of numbered lockers β fastest for retrieving items by position; linked lists are like a chain of treasure hunt clues β most flexible for insertions and deletions; hash tables are like a library index β fastest for finding things by name; trees are like family trees β expressing hierarchical relationships while maintaining order; graphs are like subway maps β expressing arbitrarily complex networked relationships. There's no best data structure, only the most appropriate one β the key is understanding the strengths and costs of each structure and making trade-offs based on actual requirements.> Data structures are the skeleton of programs. Arrays are like a row of numbered lockers β fastest for retrieving items by position; linked lists are like a chain of treasure hunt clues β most flexible for insertions and deletions; hash tables are like a library index β fastest for finding things by name; trees are like family trees β expressing hierarchical relationships while maintaining order; graphs are like subway maps β expressing arbitrarily complex networked relationships. There's no best data structure, only the most appropriate one β the key is understanding the strengths and costs of each structure and making trade-offs based on actual requirements. ------ ------ Now that you've mastered the core knowledge of data structures, you can continue learning:Now that you've mastered the core knowledge of data structures, you can continue learning: β β is a tree - Database indexes: B+ trees enable lookups across millions of records with only 3-4 disk reads - JSON/XML: Nested data formats are essentially trees- File system: Nested folders are tree structures - HTML DOM: β β is a tree - Database indexes: B+ trees enable lookups across millions of records with only 3-4 disk reads - JSON/XML: Nested data formats are essentially trees
5. Graph Structures: Networks of Complex Relationships5. Graph Structures: Networks of Complex Relationships
5.1 Three Types of Graphs5.1 Three Types of Graphs
Type Characteristics Analogy Typical Applications Undirected graph Edges have no direction; AβB equals BβA WeChat friends (mutual) Social networks, communication networks Directed graph Edges have direction; AβB is not the same as BβA Weibo follows (one-way) Web page links, dependency relationships Weighted graph Edges have weights (distance, cost, etc.) Highways between cities (with mileages) Map navigation, shortest path 5.2 Graph Traversal5.2 Graph Traversal
Traversal Method Strategy Analogy Use Cases BFS (Breadth-First) Visit all neighbors first, then neighbors' neighbors Ripples spreading in water Shortest path, level-order traversal DFS (Depth-First) Go as deep as possible on one path, backtrack when stuck Navigating a maze Path search, connectivity checking npm install performs topological sorting of the graph- Map navigation: Cities are nodes, roads are edges; navigation finds the shortest path in the graph - Social networks: Users are nodes, follows/friendships are edges; "People you may know" is graph algorithm recommendations - Package managers: npm/pip dependency relationships are directed graphs; npm install performs topological sorting of the graph6. Performance Comparison: One Table to See All Data Structures6. Performance Comparison: One Table to See All Data Structures
Data Structure Access Search Insert Delete Space Array O(1) O(n) O(n) O(n) O(n) Linked List O(n) O(n) O(1) O(1) O(n) Stack/Queue O(n) O(n) O(1) O(1) O(n) Hash Table β O(1) O(1) O(1) O(n) Binary Search Tree β O(log n) O(log n) O(log n) O(n) Graph β O(V+E) O(1) O(E) O(V+E) 7. Selection Guide: Data Structure Application Scenarios7. Selection Guide: Data Structure Application Scenarios
Your Need Recommended Structure Reason Fast access by position Array O(1) random access Frequent insertion/deletion in the middle Linked list O(1) insert/delete without moving elements Last in, first out (undo, recursion) Stack LIFO semantics naturally match First in, first out (task queue) Queue FIFO semantics naturally match Fast lookup by key Hash table O(1) average lookup Ordered data + fast lookup Binary search tree O(log n) lookup while maintaining order Complex many-to-many relationships Graph Can express connections between any nodes SummarySummary
Further ReadingFurther Reading
Topic Recommended Resources Data structure visualization [VisuAlgo](https://visualgo.net/) - Animated demonstrations of various data structures and algorithms Algorithms and data structures Grokking Algorithms by Aditya Bhargava β illustrated and beginner-friendly In-depth understanding Data Structures and Algorithm Analysis by Mark Allen Weiss Practice problems [LeetCode](https://leetcode.com/) - Practice categorized by data structure Next StepsNext Steps