clrs-algorithms

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

CLRS Data Structures & Algorithms Reference

基于CLRS的数据结构与算法参考资料

A comprehensive reference for data structures and algorithms based on "Introduction to Algorithms" (CLRS). This skill provides language-agnostic guidance with pseudocode examples that can be translated to any programming language.
这是基于《算法导论》(CLRS)的全面数据结构与算法参考资料。本技能提供与语言无关的指导,附带可转换为任意编程语言的伪代码示例。

When This Skill Activates

本技能的激活场景

This skill automatically activates when you:
  • Ask about or need to implement a data structure
  • Need to choose between data structures for a problem
  • Discuss time/space complexity trade-offs
  • Need algorithm implementations (sorting, searching, graph algorithms)
  • Mention specific structures: B-tree, heap, hash table, graph, etc.
当你进行以下操作时,本技能会自动激活:
  • 询问或需要实现某一数据结构
  • 需要为某个问题选择合适的数据结构
  • 讨论时间/空间复杂度的权衡
  • 需要算法实现(排序、查找、图算法等)
  • 提及特定结构:B-tree、heap、hash table、graph等

Quick Data Structure Reference

快速数据结构参考

Linear Structures

线性结构

StructureAccessSearchInsertDeleteUse When
ArrayO(1)O(n)O(n)O(n)Known size, index access
Dynamic ArrayO(1)O(n)O(1)*O(n)Unknown size, frequent append
Linked ListO(n)O(n)O(1)O(1)Frequent insert/delete
StackO(1)O(n)O(1)O(1)LIFO needed
QueueO(1)O(n)O(1)O(1)FIFO needed
DequeO(1)O(n)O(1)O(1)Both ends access
结构访问时间查找时间插入时间删除时间适用场景
ArrayO(1)O(n)O(n)O(n)已知数据规模,需索引访问
Dynamic ArrayO(1)O(n)O(1)*O(n)数据规模未知,需频繁追加元素
Linked ListO(n)O(n)O(1)O(1)需频繁插入/删除操作
StackO(1)O(n)O(1)O(1)需要后进先出(LIFO)操作
QueueO(1)O(n)O(1)O(1)需要先进先出(FIFO)操作
DequeO(1)O(n)O(1)O(1)需要访问两端元素

Trees

树结构

StructureSearchInsertDeleteUse When
Binary Search TreeO(log n)*O(log n)*O(log n)*Ordered data, frequent search
AVL TreeO(log n)O(log n)O(log n)Guaranteed balance needed
Red-Black TreeO(log n)O(log n)O(log n)Frequent inserts/deletes
B-TreeO(log n)O(log n)O(log n)Disk-based storage
TrieO(m)O(m)O(m)String/prefix operations
HeapO(1)/O(n)O(log n)O(log n)Priority queue needed
Splay TreeO(log n)*O(log n)*O(log n)*Self-adjusting, temporal locality
TreapO(log n)*O(log n)*O(log n)*Randomized balance, split/merge
Interval TreeO(log n)O(log n)O(log n)Interval overlap queries
Order-Statistic TreeO(log n)O(log n)O(log n)Rank/select queries
K-D TreeO(log n)*O(log n)*O(log n)*Multi-dimensional spatial data
结构查找时间插入时间删除时间适用场景
Binary Search TreeO(log n)*O(log n)*O(log n)*有序数据,需频繁查找
AVL TreeO(log n)O(log n)O(log n)需要保证树的平衡
Red-Black TreeO(log n)O(log n)O(log n)需频繁插入/删除操作
B-TreeO(log n)O(log n)O(log n)基于磁盘的存储场景
TrieO(m)O(m)O(m)字符串/前缀操作场景
HeapO(1)/O(n)O(log n)O(log n)需要优先队列
Splay TreeO(log n)*O(log n)*O(log n)*自调整结构,需时间局部性优化
TreapO(log n)*O(log n)*O(log n)*随机化平衡,需拆分/合并操作
Interval TreeO(log n)O(log n)O(log n)区间重叠查询场景
Order-Statistic TreeO(log n)O(log n)O(log n)排名/选择查询场景
K-D TreeO(log n)*O(log n)*O(log n)*多维空间数据场景

Hash-Based

哈希类结构

StructureSearchInsertDeleteUse When
Hash TableO(1)*O(1)*O(1)*Fast key-value lookup
Hash SetO(1)*O(1)*O(1)*Unique membership testing
Bloom FilterO(k)O(k)N/AProbabilistic membership
结构查找时间插入时间删除时间适用场景
Hash TableO(1)*O(1)*O(1)*快速键值对查找
Hash SetO(1)*O(1)*O(1)*唯一成员检测场景
Bloom FilterO(k)O(k)不支持概率型成员检测场景

Graphs

图结构

StructureSpaceAdd EdgeQuery EdgeUse When
Adjacency ListO(V+E)O(1)O(degree)Sparse graphs
Adjacency MatrixO(V²)O(1)O(1)Dense graphs
结构空间复杂度添加边操作查询边操作适用场景
Adjacency ListO(V+E)O(1)O(degree)稀疏图场景
Adjacency MatrixO(V²)O(1)O(1)稠密图场景

Graph Algorithms

图算法

AlgorithmTimeUse When
Network FlowO(VE²)Max flow, bipartite matching, min cut
Strongly Connected ComponentsO(V+E)Find SCCs, 2-SAT, dependency analysis
算法时间复杂度适用场景
Network FlowO(VE²)最大流、二分图匹配、最小割问题
Strongly Connected ComponentsO(V+E)查找强连通分量、2-SAT问题、依赖分析

Strings

字符串相关

StructureBuildSearchUse When
Suffix ArrayO(n log n)O(m log n)Space-efficient string matching
Suffix TreeO(n)O(m)Fast pattern matching, LCS
String AlgorithmsO(m)O(n)KMP, Rabin-Karp, Boyer-Moore, Aho-Corasick
结构构建时间查找时间适用场景
Suffix ArrayO(n log n)O(m log n)空间高效的字符串匹配
Suffix TreeO(n)O(m)快速模式匹配、最长公共子序列(LCS)
String AlgorithmsO(m)O(n)KMP、Rabin-Karp、Boyer-Moore、Aho-Corasick算法

Advanced

高级结构

StructureUse When
Skip ListProbabilistic balanced list
Disjoint SetUnion-find operations
Segment TreeRange queries with updates
Fenwick TreePrefix sums with updates
Fibonacci HeapDijkstra, Prim with O(1) decrease-key
Binomial HeapMergeable priority queue
van Emde Boas TreeInteger keys with O(log log u) operations
结构适用场景
Skip List概率型平衡列表
Disjoint Set并查集操作场景
Segment Tree带更新的范围查询
Fenwick Tree带更新的前缀和查询
Fibonacci HeapDijkstra、Prim算法中需O(1)时间的decrease-key操作
Binomial Heap可合并的优先队列
van Emde Boas Tree整数键场景,需O(log log u)时间复杂度的操作

Algorithms

算法

AlgorithmUse When
Sorting AlgorithmsQuickSort, MergeSort, HeapSort, RadixSort, and more
* = amortized or average case
算法适用场景
Sorting AlgorithmsQuickSort、MergeSort、HeapSort、RadixSort等排序算法
* = 均摊或平均情况

Decision Guides

决策指南

  • Which Data Structure Should I Use? - Decision guide by use case
  • Complexity Cheat Sheet - Quick reference for Big-O
  • Which Data Structure Should I Use? - 按使用场景划分的决策指南
  • Complexity Cheat Sheet - Big-O复杂度速查表

How to Use This Reference

本参考资料的使用方法

  1. Choosing a structure: Start with the decision guides
  2. Learning a structure: Read the full documentation with examples
  3. Quick reminder: Use the tables above for at-a-glance reference
  4. Implementation: Follow the pseudocode, adapt to your language
  1. 选择结构:从决策指南开始
  2. 学习结构:阅读完整文档及示例
  3. 快速查阅:使用上方表格进行即时参考
  4. 实现开发:遵循伪代码,适配你的编程语言

Language Translation Notes

语言转换说明

The pseudocode in this reference uses these conventions:
  • class
    for type definitions
  • function
    for methods/functions
  • ->
    for method calls on objects
  • //
    for comments
  • Type hints shown as
    name: Type
Translate to your language:
  • PHP:
    class
    ,
    function
    ,
    ->
    ,
    //
    , type hints in docblocks or PHP 8+
  • JavaScript/TypeScript:
    class
    ,
    function
    /arrow,
    .
    ,
    //
    , TS types
  • Python:
    class
    ,
    def
    ,
    .
    ,
    #
    , type hints
  • Java/C#: Direct mapping with
    new
    , generics

Based on concepts from "Introduction to Algorithms" by Cormen, Leiserson, Rivest, and Stein (CLRS), MIT Press.
本参考资料中的伪代码采用以下约定:
  • class
    用于类型定义
  • function
    用于方法/函数定义
  • ->
    用于对象的方法调用
  • //
    用于注释
  • 类型提示格式为
    name: Type
转换至各编程语言的说明:
  • PHP:使用
    class
    function
    ->
    //
    ,类型提示可放在文档块或使用PHP 8+的类型声明
  • JavaScript/TypeScript:使用
    class
    function
    /箭头函数、
    .
    //
    ,TypeScript可使用类型声明
  • Python:使用
    class
    def
    .
    #
    ,支持类型提示
  • Java/C#:直接映射,使用
    new
    、泛型等特性

基于Cormen、Leiserson、Rivest和Stein所著《算法导论》(CLRS,MIT出版社)中的概念。