data-structure-typed 2.2.2 → 2.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. package/CHANGELOG.md +3 -1
  2. package/README.md +355 -1672
  3. package/README_CN.md +509 -0
  4. package/SECURITY.md +962 -11
  5. package/SECURITY.zh-CN.md +966 -0
  6. package/SPECIFICATION.md +689 -30
  7. package/SPECIFICATION.zh-CN.md +715 -0
  8. package/SPONSOR.zh-CN.md +62 -0
  9. package/SPONSOR_POLISHED.md +62 -0
  10. package/benchmark/report.html +1 -1
  11. package/benchmark/report.json +215 -172
  12. package/dist/cjs/index.cjs +245 -72
  13. package/dist/cjs/index.cjs.map +1 -1
  14. package/dist/cjs-legacy/index.cjs +246 -72
  15. package/dist/cjs-legacy/index.cjs.map +1 -1
  16. package/dist/esm/index.mjs +245 -72
  17. package/dist/esm/index.mjs.map +1 -1
  18. package/dist/esm-legacy/index.mjs +246 -72
  19. package/dist/esm-legacy/index.mjs.map +1 -1
  20. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -2
  21. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -5
  22. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +98 -5
  23. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +103 -7
  24. package/dist/types/data-structures/binary-tree/bst.d.ts +202 -39
  25. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +86 -37
  26. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +4 -5
  27. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +7 -7
  28. package/dist/types/data-structures/graph/directed-graph.d.ts +126 -1
  29. package/dist/types/data-structures/graph/undirected-graph.d.ts +160 -1
  30. package/dist/types/data-structures/hash/hash-map.d.ts +110 -27
  31. package/dist/types/data-structures/heap/heap.d.ts +107 -58
  32. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +72 -404
  33. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +121 -5
  34. package/dist/types/data-structures/queue/deque.d.ts +95 -67
  35. package/dist/types/data-structures/queue/queue.d.ts +90 -34
  36. package/dist/types/data-structures/stack/stack.d.ts +58 -40
  37. package/dist/types/data-structures/trie/trie.d.ts +109 -47
  38. package/dist/types/interfaces/binary-tree.d.ts +1 -0
  39. package/dist/types/types/data-structures/binary-tree/bst.d.ts +5 -5
  40. package/dist/umd/data-structure-typed.js +246 -72
  41. package/dist/umd/data-structure-typed.js.map +1 -1
  42. package/dist/umd/data-structure-typed.min.js +3 -3
  43. package/dist/umd/data-structure-typed.min.js.map +1 -1
  44. package/package.json +3 -2
  45. package/src/data-structures/binary-tree/avl-tree-counter.ts +1 -2
  46. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +7 -8
  47. package/src/data-structures/binary-tree/avl-tree.ts +100 -7
  48. package/src/data-structures/binary-tree/binary-tree.ts +117 -7
  49. package/src/data-structures/binary-tree/bst.ts +431 -93
  50. package/src/data-structures/binary-tree/red-black-tree.ts +85 -37
  51. package/src/data-structures/binary-tree/tree-counter.ts +5 -7
  52. package/src/data-structures/binary-tree/tree-multi-map.ts +9 -10
  53. package/src/data-structures/graph/directed-graph.ts +126 -1
  54. package/src/data-structures/graph/undirected-graph.ts +160 -1
  55. package/src/data-structures/hash/hash-map.ts +110 -27
  56. package/src/data-structures/heap/heap.ts +107 -58
  57. package/src/data-structures/linked-list/doubly-linked-list.ts +72 -404
  58. package/src/data-structures/linked-list/singly-linked-list.ts +121 -5
  59. package/src/data-structures/queue/deque.ts +95 -67
  60. package/src/data-structures/queue/queue.ts +90 -34
  61. package/src/data-structures/stack/stack.ts +58 -40
  62. package/src/data-structures/trie/trie.ts +109 -47
  63. package/src/interfaces/binary-tree.ts +2 -0
  64. package/src/types/data-structures/binary-tree/bst.ts +5 -5
  65. package/test/performance/benchmark-runner.ts +14 -11
  66. package/test/performance/data-structures/binary-tree/avl-tree.test.ts +8 -8
  67. package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +8 -8
  68. package/test/performance/data-structures/binary-tree/binary-tree.test.ts +6 -6
  69. package/test/performance/data-structures/binary-tree/bst.test.ts +5 -5
  70. package/test/performance/data-structures/binary-tree/red-black-tree.test.ts +10 -10
  71. package/test/performance/reportor.ts +2 -1
  72. package/test/performance/single-suite-runner.ts +7 -4
  73. package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +2 -2
  74. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +117 -0
  75. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +166 -0
  76. package/test/unit/data-structures/binary-tree/bst.test.ts +771 -16
  77. package/test/unit/data-structures/binary-tree/overall.test.ts +2 -2
  78. package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +90 -38
  79. package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +2 -2
  80. package/test/unit/data-structures/graph/directed-graph.test.ts +133 -0
  81. package/test/unit/data-structures/graph/undirected-graph.test.ts +167 -0
  82. package/test/unit/data-structures/hash/hash-map.test.ts +149 -3
  83. package/test/unit/data-structures/heap/heap.test.ts +182 -47
  84. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +118 -14
  85. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +121 -0
  86. package/test/unit/data-structures/queue/deque.test.ts +98 -67
  87. package/test/unit/data-structures/queue/queue.test.ts +85 -51
  88. package/test/unit/data-structures/stack/stack.test.ts +142 -33
  89. package/test/unit/data-structures/trie/trie.test.ts +135 -39
  90. package/tsup.leetcode.config.js +99 -0
  91. package/typedoc.json +2 -1
  92. package/POSTS_zh-CN.md +0 -54
  93. package/README_zh-CN.md +0 -1208
  94. package/SPECIFICATION_zh-CN.md +0 -81
@@ -0,0 +1,715 @@
1
+ # 规范说明:data-structure-typed 库
2
+
3
+ **版本:** 2.0.4
4
+ **最后更新:** 2026年1月
5
+ **代码仓库:** https://github.com/zrwusa/data-structure-typed
6
+ **API 文档:** https://data-structure-typed-docs.vercel.app
7
+
8
+ ---
9
+
10
+ ## 目录
11
+
12
+ 1. [规范概述](#规范概述)
13
+ 2. [支持的数据结构](#支持的数据结构)
14
+ 3. [API 标准](#api-标准)
15
+ 4. [类型系统规范](#类型系统规范)
16
+ 5. [性能保证](#性能保证)
17
+ 6. [模块系统支持](#模块系统支持)
18
+ 7. [浏览器与运行时兼容性](#浏览器与运行时兼容性)
19
+ 8. [数据结构契约](#数据结构契约)
20
+ 9. [迭代器协议合规性](#迭代器协议合规性)
21
+ 10. [方法链式调用规范](#方法链式调用规范)
22
+ 11. [错误处理](#错误处理)
23
+ 12. [破坏性变更策略](#破坏性变更策略)
24
+
25
+ ---
26
+
27
+ ## 规范概述
28
+
29
+ **data-structure-typed** 是一个全面的 TypeScript 数据结构库,实现了 20 多种生产级数据结构,具有保证的时间/空间复杂度。
30
+
31
+ ### 设计目标
32
+
33
+ - ✅ **统一 API**:所有线性结构使用一致的接口(push、pop、shift、unshift)
34
+ - ✅ **类型安全**:完整的 TypeScript 泛型与严格类型
35
+ - ✅ **性能保证**:树结构 O(log n),双端队列操作 O(1)
36
+ - ✅ **零摩擦**:所有结构上的迭代器协议
37
+ - ✅ **生产就绪**:全面的错误处理和边缘情况覆盖
38
+ - ✅ **框架无关**:在 Node.js、浏览器和边缘运行时中工作
39
+
40
+ ### 版本兼容性
41
+
42
+ | 版本 | 发布日期 | LTS 支持 | Node.js | TypeScript |
43
+ |---------|-------------|-------------|---------|-----------|
44
+ | 2.0.x | 2025年1月 | 至2027年 | 16+ | 5.0+ |
45
+ | 1.x.x | 2023-2024 | 至2025年 | 14+ | 4.5+ |
46
+
47
+ ---
48
+
49
+ ## 支持的数据结构
50
+
51
+ ### 线性结构(共20+种)
52
+
53
+ | 结构 | 类别 | 时间复杂度 | 空间 | 状态 |
54
+ |-----------|----------|-----------------|-------|--------|
55
+ | **Stack(栈)** | 线性 | 所有操作 O(1) | O(n) | ✅ 稳定 |
56
+ | **Queue(队列)** | 线性 | 所有操作 O(1) | O(n) | ✅ 稳定 |
57
+ | **Deque(双端队列)** | 线性 | 所有操作 O(1) | O(n) | ✅ 稳定 |
58
+ | **SinglyLinkedList(单链表)** | 线性 | 平均访问 O(n),插入 O(1)* | O(n) | ✅ 稳定 |
59
+ | **DoublyLinkedList(双链表)** | 线性 | 平均访问 O(n),插入 O(1)* | O(n) | ✅ 稳定 |
60
+
61
+ ### 树结构
62
+
63
+ | 结构 | 平衡 | 时间复杂度 | 保证 | 状态 |
64
+ |-----------|---------|-----------------|-----------|--------|
65
+ | **BinaryTree(二叉树)** | 无 | 平均 O(n) | 无 | ✅ 稳定 |
66
+ | **BST(二叉搜索树)** | 无 | 平均 O(log n) | 最坏 O(n) | ✅ 稳定 |
67
+ | **RedBlackTree(红黑树)** | 是 | O(log n) | O(log n) | ✅ 稳定 |
68
+ | **AVLTree(AVL树)** | 是 | O(log n) | O(log n) | ✅ 稳定 |
69
+ | **TreeMultiMap(树多重映射)** | 是 | O(log n) | O(log n) | ✅ 稳定 |
70
+
71
+ ### 优先队列与堆
72
+
73
+ | 结构 | 类型 | 时间复杂度 | 使用场景 | 状态 |
74
+ |-----------|------|-----------------|----------|--------|
75
+ | **Heap(堆)** | 通用 | 添加/删除 O(log n) | 堆操作 | ✅ 稳定 |
76
+ | **MinHeap(最小堆)** | 最小 | 添加/删除 O(log n) | 最小优先级 | ✅ 稳定 |
77
+ | **MaxHeap(最大堆)** | 最大 | 添加/删除 O(log n) | 最大优先级 | ✅ 稳定 |
78
+ | **PriorityQueue(优先队列)** | 通用 | 添加/删除 O(log n) | 任务调度 | ✅ 稳定 |
79
+ | **MinPriorityQueue(最小优先队列)** | 最小 | 添加/删除 O(log n) | 最小优先级任务 | ✅ 稳定 |
80
+ | **MaxPriorityQueue(最大优先队列)** | 最大 | 添加/删除 O(log n) | 最大优先级任务 | ✅ 稳定 |
81
+
82
+ ### 专用结构
83
+
84
+ | 结构 | 用途 | 时间复杂度 | 状态 |
85
+ |-----------|---------|-----------------|--------|
86
+ | **Trie(前缀树)** | 前缀搜索 | O(m+k) | ✅ 稳定 |
87
+ | **HashMap(哈希映射)** | 键值查找 | 平均 O(1) | ✅ 稳定 |
88
+ | **Graph(图)** | 图基类 | 不定 | ✅ 稳定 |
89
+ | **DirectedGraph(有向图)** | 有向图 | O(V+E) | ✅ 稳定 |
90
+ | **UndirectedGraph(无向图)** | 无向图 | O(V+E) | ✅ 稳定 |
91
+ | **SegmentTree(线段树)** | 区间查询 | O(log n) | ⚠️ Beta |
92
+ | **BinaryIndexedTree(树状数组)** | 累积查询 | O(log n) | ⚠️ Beta |
93
+
94
+ ---
95
+
96
+ ## API 标准
97
+
98
+ ### 统一的线性结构 API
99
+
100
+ **所有线性结构(Stack、Queue、Deque、LinkedList)支持相同的 4 个核心方法:**
101
+
102
+ ```typescript
103
+ // 添加操作
104
+ structure.push(element: T): number // 添加到末尾,返回长度
105
+ structure.unshift(element: T): number // 添加到开头,返回长度
106
+
107
+ // 删除操作
108
+ structure.pop(): T | undefined // 从末尾删除
109
+ structure.shift(): T | undefined // 从开头删除
110
+
111
+ // 查看操作(读取但不删除)
112
+ structure.peek(): T | undefined // 查看最后一个元素
113
+ structure.peekFirst(): T | undefined // 查看第一个元素(Deque)
114
+ structure.peekLast(): T | undefined // 查看最后一个元素(Deque)
115
+
116
+ // 大小和查询
117
+ structure.size: number // 元素数量
118
+ structure.isEmpty: boolean // 检查是否为空
119
+ ```
120
+
121
+ ### 树和映射 API
122
+
123
+ **所有键值结构(树、映射)支持:**
124
+
125
+ ```typescript
126
+ // 添加/更新
127
+ structure.set(key: K, value: V): this
128
+ structure.setMany(entries: [K, V][]): this
129
+
130
+ // 查询
131
+ structure.get(key: K): V | undefined
132
+ structure.has(key: K): boolean
133
+ structure.size: number
134
+
135
+ // 删除
136
+ structure.delete(key: K): boolean
137
+ structure.clear(): void
138
+
139
+ // 迭代
140
+ structure.keys(): IterableIterator<K>
141
+ structure.values(): IterableIterator<V>
142
+ structure.entries(): IterableIterator<[K, V]>
143
+ ```
144
+
145
+ ### 堆和优先队列 API
146
+
147
+ **所有堆和优先队列支持:**
148
+
149
+ ```typescript
150
+ // 添加/删除
151
+ heap.push(element: T): number // 添加元素
152
+ heap.add(element: T): number // push 的别名
153
+ heap.pop(): T | undefined // 删除最高/最低优先级
154
+ heap.poll(): T | undefined // pop 的别名
155
+
156
+ // 查看
157
+ heap.peek(): T | undefined // 查看最高/最低优先级但不删除
158
+
159
+ // 查询
160
+ heap.size: number
161
+ heap.isEmpty: boolean
162
+ ```
163
+
164
+ ### 数组方法(所有结构上可用)
165
+
166
+ **所有结构通过迭代支持 ES6+ 数组方法:**
167
+
168
+ ```typescript
169
+ structure.map<U>(mapper: (v: V, k?: K, index?: number) => U): this
170
+ structure.filter(predicate: (v: V, k?: K, index?: number) => boolean): this
171
+ structure.reduce<U>(reducer: (acc: U, v: V, k?: K, i?: number) => U, init: U): U
172
+ structure.find(predicate: (v: V, k?: K, i?: number) => boolean): V | undefined
173
+ structure.some(predicate: (v: V, k?: K, i?: number) => boolean): boolean
174
+ structure.every(predicate: (v: V, k?: K, i?: number) => boolean): boolean
175
+ structure.forEach(callback: (v: V, k?: K, i?: number) => void): void
176
+ ```
177
+
178
+ ---
179
+
180
+ ## 类型系统规范
181
+
182
+ ### 泛型参数
183
+
184
+ **键值结构的二元类型参数:**
185
+
186
+ ```typescript
187
+ class RedBlackTree<K = number, V = any> {
188
+ set(key: K, value: V): this
189
+ get(key: K): V | undefined
190
+ }
191
+
192
+ // 使用示例
193
+ const tree = new RedBlackTree<number, string>();
194
+ tree.set(1, 'Alice'); // 类型安全
195
+ tree.get(1); // 返回:string | undefined
196
+ ```
197
+
198
+ **线性结构的单类型参数:**
199
+
200
+ ```typescript
201
+ class Stack<T> {
202
+ push(element: T): number
203
+ pop(): T | undefined
204
+ }
205
+
206
+ // 使用示例
207
+ const stack = new Stack<number>();
208
+ stack.push(42); // ✅ 类型安全
209
+ stack.push('text'); // ❌ 类型错误
210
+ ```
211
+
212
+ ### 比较器函数
213
+
214
+ **所有有序结构接受可选的比较器:**
215
+
216
+ ```typescript
217
+ type Comparator<T> = (a: T, b: T) => number
218
+
219
+ // 返回值语义:
220
+ // < 0 表示 a 在 b 前面(升序)
221
+ // 0 表示相等
222
+ // > 0 表示 a 在 b 后面
223
+
224
+ const tree = new RedBlackTree<number>(
225
+ (a, b) => b - a // 降序
226
+ );
227
+ ```
228
+
229
+ ### 迭代器协议
230
+
231
+ **所有结构实现 Symbol.iterator:**
232
+
233
+ ```typescript
234
+ interface Structure<T> {
235
+ [Symbol.iterator](): Iterator<T>
236
+ }
237
+
238
+ // 启用:
239
+ [...structure] // 展开运算符
240
+ for (const item of structure) {} // for...of 循环
241
+ const [a, b] = structure // 解构
242
+ Array.from(structure) // 数组转换
243
+ new Set(structure) // Set 构造函数
244
+ ```
245
+
246
+ ---
247
+
248
+ ## 性能保证
249
+
250
+ ### 时间复杂度保证
251
+
252
+ | 数据结构 | 访问 | 搜索 | 插入 | 删除 | 最小值 |
253
+ |---|---|---|---|---|---|
254
+ | **Stack** | - | - | O(1) | O(1) | ✅ |
255
+ | **Queue** | - | - | O(1) | O(1) | ✅ |
256
+ | **Deque** | - | - | O(1) | O(1) | ✅ |
257
+ | **LinkedList** | O(n) | O(n) | O(1)* | O(1)* | ✅ |
258
+ | **BST** | O(log n) | O(log n) | O(log n) | O(log n) | ⚠️** |
259
+ | **RedBlackTree** | O(log n) | O(log n) | O(log n) | O(log n) | ✅ |
260
+ | **AVLTree** | O(log n) | O(log n) | O(log n) | O(log n) | ✅ |
261
+ | **Heap** | O(n) | O(n) | O(log n) | O(log n) | ✅ |
262
+ | **Trie** | N/A | O(m) | O(m) | O(m) | ✅ |
263
+ | **Graph** | - | O(V+E) | O(1) | O(V+E) | 不定 |
264
+
265
+ *有指针访问时;无指针时为 O(n)
266
+ **平均情况;最坏情况下如果不平衡为 O(n)
267
+
268
+ ### 空间复杂度
269
+
270
+ | 数据结构 | 空间 | 备注 |
271
+ |---|---|---|
272
+ | **Stack** | O(n) | 每个元素一个指针 |
273
+ | **Queue** | O(n) | 分块优化 |
274
+ | **Deque** | O(n) | 基于桶,最小开销 |
275
+ | **LinkedList** | O(n) | 额外指针(单/双) |
276
+ | **BST/RedBlackTree/AVL** | O(n) | 每个节点 2-3 个指针 |
277
+ | **Heap** | O(n) | 基于数组,紧密打包 |
278
+ | **Trie** | O(26n) | 每个字符节点 |
279
+ | **Graph** | O(V+E) | 邻接表表示 |
280
+
281
+ ### 基准目标
282
+
283
+ 库保持比朴素实现快 10-100 倍的性能:
284
+
285
+ - **Deque**:对于 10 万次操作,比 Array.shift() 快 484 倍
286
+ - **RedBlackTree**:对于 10 万+1000 次操作,比 Array sort 快 308 倍
287
+ - **Trie**:前缀搜索 O(m+k) vs O(n*m)
288
+
289
+ 基准测试环境:MacBook Pro 2018,Intel i7,16GB RAM
290
+
291
+ ---
292
+
293
+ ## 模块系统支持
294
+
295
+ ### CommonJS
296
+
297
+ ```javascript
298
+ const { RedBlackTree, Deque } = require('data-structure-typed');
299
+ ```
300
+
301
+ ### ES 模块(ESM)
302
+
303
+ ```javascript
304
+ import { RedBlackTree, Deque } from 'data-structure-typed';
305
+ ```
306
+
307
+ ### 带完整类型的 TypeScript
308
+
309
+ ```typescript
310
+ import { RedBlackTree } from 'data-structure-typed';
311
+ const tree = new RedBlackTree<number, string>();
312
+ ```
313
+
314
+ ### UMD(浏览器全局变量)
315
+
316
+ ```html
317
+ <script src="https://cdn.jsdelivr.net/npm/data-structure-typed/dist/umd/data-structure-typed.min.js"></script>
318
+ <script>
319
+ const { Deque } = window.dataStructureTyped;
320
+ const deque = new Deque();
321
+ </script>
322
+ ```
323
+
324
+ ### 独立包
325
+
326
+ ```bash
327
+ npm install heap-typed deque-typed red-black-tree-typed
328
+ ```
329
+
330
+ ---
331
+
332
+ ## 浏览器与运行时兼容性
333
+
334
+ ### 最低版本
335
+
336
+ | 运行时 | 版本 | 备注 |
337
+ |---------|---------|-------|
338
+ | **Node.js** | 16+ | 活跃 LTS |
339
+ | **TypeScript** | 5.0+ | 最新稳定版 |
340
+ | **Chrome** | 90+ | 现代 ES2020 支持 |
341
+ | **Firefox** | 88+ | 现代 ES2020 支持 |
342
+ | **Safari** | 14+ | 现代 ES2020 支持 |
343
+ | **Edge** | 90+ | 现代 ES2020 支持 |
344
+
345
+ ### 需要的内置全局对象
346
+
347
+ - Symbol.iterator(用于迭代协议)
348
+ - Map/Set(用于某些结构)
349
+ - WeakMap(当前未使用)
350
+
351
+ ### Polyfill 要求
352
+
353
+ **现代环境无需 polyfill。**
354
+
355
+ 对于 IE11 兼容性:需要手动实现(不正式支持)
356
+
357
+ ### 测试平台
358
+
359
+ - ✅ Node.js 16、18、20、21
360
+ - ✅ Deno 1.40+
361
+ - ✅ Bun 1.0+
362
+ - ✅ Chrome、Firefox、Safari(最新版)
363
+ - ✅ Edge(最新版)
364
+ - ✅ 通过 CDN 的浏览器(jsDelivr、unpkg)
365
+
366
+ ---
367
+
368
+ ## 数据结构契约
369
+
370
+ ### 栈契约
371
+
372
+ ```typescript
373
+ interface Stack<T> {
374
+ push(element: T): number
375
+ pop(): T | undefined
376
+ peek(): T | undefined
377
+ size: number
378
+ }
379
+
380
+ // LIFO:后进先出
381
+ // 属性:
382
+ // - push() 和 pop() 必须是 O(1)
383
+ // - 迭代顺序:LIFO(从顶到底)
384
+ ```
385
+
386
+ ### 队列契约
387
+
388
+ ```typescript
389
+ interface Queue<T> {
390
+ push(element: T): number // 入队
391
+ shift(): T | undefined // 出队
392
+ peek(): T | undefined
393
+ size: number
394
+ }
395
+
396
+ // FIFO:先进先出
397
+ // 属性:
398
+ // - push() 和 shift() 必须是 O(1)
399
+ // - 迭代顺序:FIFO(从头到尾)
400
+ ```
401
+
402
+ ### 树映射契约
403
+
404
+ ```typescript
405
+ interface TreeMap<K, V> {
406
+ set(key: K, value: V): this
407
+ get(key: K): V | undefined
408
+ has(key: K): boolean
409
+ delete(key: K): boolean
410
+
411
+ keys(): IterableIterator<K>
412
+ values(): IterableIterator<V>
413
+ }
414
+
415
+ // 键属性:
416
+ // - 键按排序顺序迭代
417
+ // - 所有操作保证 O(log n)
418
+ // - 无重复键
419
+ // - 维护排序不变性
420
+ ```
421
+
422
+ ### 堆契约
423
+
424
+ ```typescript
425
+ interface Heap<T> {
426
+ push(element: T): number
427
+ pop(): T | undefined
428
+ peek(): T | undefined
429
+
430
+ size: number
431
+ }
432
+
433
+ // 堆属性:
434
+ // - MinHeap:父节点 <= 子节点
435
+ // - MaxHeap:父节点 >= 子节点
436
+ // - push() 和 pop() O(log n)
437
+ // - peek() O(1)
438
+ // - 迭代时不排序(仅堆顺序)
439
+ ```
440
+
441
+ ### 图契约
442
+
443
+ ```typescript
444
+ interface Graph<V, E = number> {
445
+ addVertex(vertex: V): boolean
446
+ addEdge(from: V, to: V, weight?: E): boolean
447
+ hasVertex(vertex: V): boolean
448
+ hasEdge(from: V, to: V): boolean
449
+
450
+ dijkstra(start: V, end?: V): any
451
+ topologicalSort(): V[]
452
+ }
453
+
454
+ // 图属性:
455
+ // - 有向:边是单向的
456
+ // - 无向:边是双向的
457
+ // - 加权:边有值
458
+ ```
459
+
460
+ ---
461
+
462
+ ## 迭代器协议合规性
463
+
464
+ ### 规范合规性
465
+
466
+ **所有结构实现 ES6 迭代器协议:**
467
+
468
+ ```typescript
469
+ interface Iterable<T> {
470
+ [Symbol.iterator](): Iterator<T>
471
+ }
472
+
473
+ interface Iterator<T> {
474
+ next(): IteratorResult<T>
475
+ }
476
+
477
+ interface IteratorResult<T> {
478
+ done: boolean
479
+ value?: T
480
+ }
481
+ ```
482
+
483
+ ### 迭代顺序保证
484
+
485
+ | 结构 | 迭代顺序 | 确定性 |
486
+ |-----------|-----------------|---------------|
487
+ | **Stack** | 从顶到底(LIFO) | ✅ 是 |
488
+ | **Queue** | 从前到后(FIFO) | ✅ 是 |
489
+ | **Deque** | 从第一个到最后一个 | ✅ 是 |
490
+ | **LinkedList** | 从头到尾 | ✅ 是 |
491
+ | **BST/RBTree** | 中序遍历(排序) | ✅ 是 |
492
+ | **Heap** | 堆顺序(不排序) | ✅ 是 |
493
+ | **Trie** | 字典序 | ✅ 是 |
494
+ | **Graph** | 从起始顶点 BFS | ✅ 是 |
495
+
496
+ ### 迭代期间的变更
497
+
498
+ **行为:未定义(与原生 Array 相同)**
499
+
500
+ ```typescript
501
+ const tree = new RedBlackTree([1, 2, 3]);
502
+
503
+ // ❌ 不要在迭代期间变更
504
+ for (const item of tree) {
505
+ if (item === 2) tree.delete(2); // 不安全!
506
+ }
507
+
508
+ // ✅ 首先收集变更
509
+ const toDelete = [];
510
+ for (const item of tree) {
511
+ if (item === 2) toDelete.push(item);
512
+ }
513
+ toDelete.forEach(item => tree.delete(item));
514
+ ```
515
+
516
+ ---
517
+
518
+ ## 方法链式调用规范
519
+
520
+ ### 可链式操作
521
+
522
+ **返回 `this` 的方法支持链式调用:**
523
+
524
+ ```typescript
525
+ structure
526
+ .filter(predicate)
527
+ .map(mapper)
528
+ .reduce(reducer)
529
+ ```
530
+
531
+ **map 和 filter 返回相同类型以维护结构:**
532
+
533
+ ```typescript
534
+ const tree = new RedBlackTree([[1, 'a'], [2, 'b']]);
535
+
536
+ const result = tree
537
+ .filter((v, k) => k > 1) // 仍然是 RedBlackTree
538
+ .map((v, k) => [k, v.toUpperCase()]); // 仍然是 RedBlackTree
539
+
540
+ // 树方法仍然可用
541
+ result.set(3, 'C');
542
+ ```
543
+
544
+ ### 终结操作
545
+
546
+ **打破链的操作返回最终类型:**
547
+
548
+ ```typescript
549
+ structure.reduce((acc, val) => acc + val, 0) // 返回 number
550
+ structure.toArray() // 返回 T[]
551
+ structure.find(pred) // 返回 T | undefined
552
+ structure.some(pred) // 返回 boolean
553
+ ```
554
+
555
+ ---
556
+
557
+ ## 错误处理
558
+
559
+ ### 错误类型
560
+
561
+ **库抛出标准 JavaScript 错误:**
562
+
563
+ | 错误类型 | 时机 | 示例 |
564
+ |---|---|---|
565
+ | **TypeError** | 类型不匹配 | 向泛型传递错误类型 |
566
+ | **RangeError** | 无效索引 | 访问越界索引 |
567
+ | **Error** | 逻辑违规 | 比较不可比较的项 |
568
+
569
+ ### 错误行为
570
+
571
+ **无静默失败:**
572
+
573
+ ```typescript
574
+ const tree = new RedBlackTree<number>();
575
+
576
+ // ✅ 如果比较器失败则抛出 TypeError
577
+ tree.set(null, 'value'); // TypeError: 无法比较 null
578
+
579
+ // ✅ 缺失键返回 undefined(安全)
580
+ tree.get(999); // undefined(不是错误)
581
+
582
+ // ✅ 空操作不抛出异常
583
+ tree.pop(); // 返回 undefined(不是错误)
584
+ ```
585
+
586
+ ### 静默 vs 响亮
587
+
588
+ **静默(无异常):**
589
+ - `.get()` 上缺失键
590
+ - 从空结构 pop/shift
591
+ - 删除失败(键未找到)
592
+
593
+ **响亮(抛出异常):**
594
+ - 泛型中的类型不匹配
595
+ - 比较失败
596
+ - 无效的图操作
597
+
598
+ ---
599
+
600
+ ## 破坏性变更策略
601
+
602
+ ### 语义化版本控制
603
+
604
+ **库遵循 SemVer 2.0:**
605
+
606
+ - **主版本**(2.0.0):破坏性 API 变更
607
+ - **次版本**(2.1.0):新功能,向后兼容
608
+ - **补丁**(2.0.1):错误修复,性能改进
609
+
610
+ ### 弃用策略
611
+
612
+ **提前 2 个版本宣布弃用:**
613
+
614
+ ```typescript
615
+ // v1.x
616
+ method() { /* 旧实现 */ }
617
+
618
+ // v1.x -> v2.0(宣布版本)
619
+ /**
620
+ * @deprecated 请改用 newMethod()。将在 v3.0 中删除
621
+ */
622
+ method() { /* 旧实现 */ }
623
+
624
+ // v3.0(删除版本)
625
+ // method() 完全删除
626
+ ```
627
+
628
+ ### 向后兼容性保证
629
+
630
+ **在主版本内:**
631
+ - ✅ 公共 API 保证稳定
632
+ - ✅ 现有代码继续工作
633
+ - ✅ 性能可能提高
634
+ - ✅ 可能添加新方法
635
+ - ❌ 带弃用通知删除的方法
636
+
637
+ **主版本升级:**
638
+ - 可能包括破坏性变更
639
+ - 提供迁移指南
640
+ - 6 个月支持重叠期
641
+
642
+ ---
643
+
644
+ ## 合规性检查清单
645
+
646
+ ### API 合规性
647
+
648
+ - ✅ 所有线性结构的统一方法名称
649
+ - ✅ 所有结构的类型安全泛型
650
+ - ✅ 所有结构的迭代器协议
651
+ - ✅ 所有结构的数组方法(map/filter/reduce)
652
+ - ✅ 一致的错误处理
653
+
654
+ ### 性能合规性
655
+
656
+ - ✅ 所有平衡树保证 O(log n)
657
+ - ✅ Deque 操作保证 O(1)
658
+ - ✅ 堆添加/删除操作 O(log n)
659
+ - ✅ 发布和维护基准测试
660
+
661
+ ### 浏览器/运行时合规性
662
+
663
+ - ✅ CommonJS + ESM + UMD 支持
664
+ - ✅ TypeScript 5.0+ 支持
665
+ - ✅ Node.js 16+ 支持
666
+ - ✅ 现代浏览器支持(ES2020)
667
+
668
+ ### 文档合规性
669
+
670
+ - ✅ 完整的 API 文档
671
+ - ✅ 所有结构的实际示例
672
+ - ✅ 性能基准测试
673
+ - ✅ 框架集成指南
674
+
675
+ ---
676
+
677
+ ## 未来路线图
678
+
679
+ ### 计划中(v2.x)
680
+
681
+ - [ ] AVL 树性能优化
682
+ - [ ] 线段树完成
683
+ - [ ] 树状数组完成
684
+ - [ ] 跳表实现
685
+ - [ ] V8 JIT 性能改进
686
+
687
+ ### 考虑中(v3.0+)
688
+
689
+ - [ ] 并发数据结构
690
+ - [ ] 持久化(不可变)变体
691
+ - [ ] WebAssembly 实现以提高性能
692
+ - [ ] Java/Python 移植
693
+
694
+ ### 不会实现
695
+
696
+ - ❌ 弱引用(设计决策)
697
+ - ❌ 异步迭代器(同步库)
698
+ - ❌ 浏览器存储绑定(超出范围)
699
+
700
+ ---
701
+
702
+ ## 相关文档
703
+
704
+ - **[SECURITY.md](./SECURITY.zh-CN.md)** - 安全考虑和最佳实践
705
+ - **[CONCEPTS.md](./CONCEPTS.md)** - 基本概念和理论
706
+ - **[REFERENCE.md](./REFERENCE.md)** - 完整 API 参考
707
+ - **[PERFORMANCE.md](./PERFORMANCE.md)** - 性能基准测试
708
+ - **[GUIDES.md](./GUIDES.md)** - 实际使用示例
709
+ - **[ARCHITECTURE.md](./ARCHITECTURE.md)** - 设计决策和内部实现
710
+
711
+ ---
712
+
713
+ **最后更新:** 2026年1月
714
+ **维护者:** [@zrwusa](https://github.com/zrwusa)
715
+ **许可证:** MIT