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
package/SPECIFICATION.md CHANGED
@@ -1,56 +1,715 @@
1
+ # SPECIFICATION: data-structure-typed Library
1
2
 
2
- [NPM](https://www.npmjs.com/package/data-structure-typed)
3
+ **Version:** 2.0.4
4
+ **Last Updated:** January 2026
5
+ **Repository:** https://github.com/zrwusa/data-structure-typed
6
+ **API Docs:** https://data-structure-typed-docs.vercel.app
3
7
 
4
- [Github](https://github.com/zrwusa/data-structure-typed)
8
+ ---
5
9
 
6
- ## Pain Points Addressed
10
+ ## Table of Contents
7
11
 
8
- ### Enhancing the Performance of Simulated Data Structures in JS/TS
12
+ 1. [Specification Overview](#specification-overview)
13
+ 2. [Supported Data Structures](#supported-data-structures)
14
+ 3. [API Standard](#api-standard)
15
+ 4. [Type System Specification](#type-system-specification)
16
+ 5. [Performance Guarantees](#performance-guarantees)
17
+ 6. [Module System Support](#module-system-support)
18
+ 7. [Browser & Runtime Compatibility](#browser--runtime-compatibility)
19
+ 8. [Data Structure Contracts](#data-structure-contracts)
20
+ 9. [Iterator Protocol Compliance](#iterator-protocol-compliance)
21
+ 10. [Method Chaining Specification](#method-chaining-specification)
22
+ 11. [Error Handling](#error-handling)
23
+ 12. [Breaking Changes Policy](#breaking-changes-policy)
9
24
 
10
- - `Queue`: While many resort to using Arrays to simulate Queues and Deques, the time complexity of Array.shift is O(n). We have tackled this challenge by implementing a Queue and Deque with O(1) time complexity for enqueue and dequeue operations.
25
+ ---
11
26
 
12
- - `HashMap`: Opting for a pure HashMap over the built-in Map (technically a LinkedHashMap) can boost algorithmic speed. However, the performance is compromised due to the necessity of considering insertion order. We have independently implemented an optimized HashMap.
27
+ ## Specification Overview
13
28
 
14
- - `Stack`: In JS, simulating a Stack with an Array is acceptable, and its performance is on par with a genuine Stack.
29
+ **data-structure-typed** is a comprehensive TypeScript data structures library implementing 20+ production-ready data structures with guaranteed time/space complexity.
15
30
 
16
- ### Introducing Missing Native Data Structures in JS/TS
31
+ ### Design Goals
17
32
 
18
- - `Heap / Priority Queue`: Algorithms with O(log n) time complexity have been pivotal in improving efficiency since the dawn of computers. A Heap supports insertion, deletion, and search with O(log n) time complexity, coupled with the ability to obtain the minimum / maximum value in O(1) time.
33
+ - **Unified API**: Consistent interface across all linear structures (push, pop, shift, unshift)
34
+ - ✅ **Type Safety**: Full TypeScript generics with strict typing
35
+ - ✅ **Performance Guaranteed**: O(log n) for trees, O(1) for deque operations
36
+ - ✅ **Zero Friction**: Iterator protocol on all structures
37
+ - ✅ **Production Ready**: Comprehensive error handling and edge case coverage
38
+ - ✅ **Framework Agnostic**: Works in Node.js, browsers, and edge runtimes
19
39
 
20
- - `Red Black Tree`: Developers well-versed in databases, file systems, Linux virtual memory management, and network routing tables often have a nuanced understanding of Red-Black Trees. It stands out as the least operation-intensive among all balanced binary search trees, offering optimal performance balance in CRUD operations.
40
+ ### Version Compatibility
21
41
 
22
- - `Linked List`: In scenarios where insertion or deletion of elements with O(1) time complexity is required at a specific index in an ordered collection, JS lacks a provided data structure. Hence, the need for a LinkedList to implement this functionality.
42
+ | Version | Release Date | LTS Support | Node.js | TypeScript |
43
+ |---------|-------------|-------------|---------|-----------|
44
+ | 2.0.x | Jan 2025 | Until 2027 | 16+ | 5.0+ |
45
+ | 1.x.x | 2023-2024 | Until 2025 | 14+ | 4.5+ |
23
46
 
24
- - `Trie`: Efficient for fast string queries and space-efficient string storage, yet not commonly found in the standard libraries of most programming languages.
47
+ ---
25
48
 
26
- - `Graph`: This data structure is not commonly found in the standard libraries of most languages, making it a non-issue in JS.
49
+ ## Supported Data Structures
27
50
 
28
- ## Advantages
51
+ ### Linear Structures (20+ total)
29
52
 
30
- ### Performance:
53
+ | Structure | Category | Time Complexity | Space | Status |
54
+ |-----------|----------|-----------------|-------|--------|
55
+ | **Stack** | Linear | O(1) all ops | O(n) | ✅ Stable |
56
+ | **Queue** | Linear | O(1) all ops | O(n) | ✅ Stable |
57
+ | **Deque** | Linear | O(1) all ops | O(n) | ✅ Stable |
58
+ | **SinglyLinkedList** | Linear | O(n) avg access, O(1) insert* | O(n) | ✅ Stable |
59
+ | **DoublyLinkedList** | Linear | O(n) avg access, O(1) insert* | O(n) | ✅ Stable |
31
60
 
32
- - The performance of some ours data structures has surpassed JS's built-in data structures (`Queue`, `Deque`, `HashMap`), while most are comparable to or even surpass those in other languages. Some are still undergoing refinement (`Graph`, `AVL Tree`).
61
+ ### Tree Structures
33
62
 
34
- ### Uniformity
63
+ | Structure | Balance | Time Complexity | Guarantee | Status |
64
+ |-----------|---------|-----------------|-----------|--------|
65
+ | **BinaryTree** | N/A | O(n) avg | None | ✅ Stable |
66
+ | **BST** | None | O(log n) avg | O(n) worst | ✅ Stable |
67
+ | **RedBlackTree** | Yes | O(log n) | O(log n) | ✅ Stable |
68
+ | **AVLTree** | Yes | O(log n) | O(log n) | ✅ Stable |
69
+ | **TreeMultiMap** | Yes | O(log n) | O(log n) | ✅ Stable |
35
70
 
36
- - 1. Implementation or constraint of `forEach`, `filter`, `map`, `every`, `some`, `reduce`, `find`, `has`, `hasValue`, `get`, `print`, `isEmpty`, `clear`, `clone` methods in the base class.
37
- - 2. Use of generators to uniformly implement `[Symbol.iterator]`, `entries`, `keys`, `values`. Delaying iterator execution prevents performance loss and provides control during traversal.
38
- - 3. All deletion methods uniformly use the widely adopted `delete` in ES6, while addition methods uniformly use `add`. Compatibility with some specifications in Java.
39
- - 4. The first parameter for all constructors is data, and the second parameter is configuration, maintaining uniformity. The first parameter accepts any iterable type for seamless conversion between data structures.
40
- - 5. Uniform return types, for example, the `add` method consistently returns a boolean.
71
+ ### Priority Queues & Heaps
41
72
 
42
- ### Convenience and Simplicity of APIs
73
+ | Structure | Type | Time Complexity | Use Case | Status |
74
+ |-----------|------|-----------------|----------|--------|
75
+ | **Heap** | Generic | O(log n) add/remove | Heap operations | ✅ Stable |
76
+ | **MinHeap** | Min | O(log n) add/remove | Minimum priority | ✅ Stable |
77
+ | **MaxHeap** | Max | O(log n) add/remove | Maximum priority | ✅ Stable |
78
+ | **PriorityQueue** | Generic | O(log n) add/remove | Task scheduling | ✅ Stable |
79
+ | **MinPriorityQueue** | Min | O(log n) add/remove | Min priority tasks | ✅ Stable |
80
+ | **MaxPriorityQueue** | Max | O(log n) add/remove | Max priority tasks | ✅ Stable |
43
81
 
44
- - Inspired by ES6, Java, ESNext, TypeScript, Python, featuring methods like `forEach`, `filter`, `map`, `every`, `some`, `reduce`, `find`, `has`, `hasValue` and `get`.
82
+ ### Specialized Structures
45
83
 
46
- ### Use of Commonly Understood Industry Standard Naming
84
+ | Structure | Purpose | Time Complexity | Status |
85
+ |-----------|---------|-----------------|--------|
86
+ | **Trie** | Prefix search | O(m+k) | ✅ Stable |
87
+ | **HashMap** | Key-value lookup | O(1) avg | ✅ Stable |
88
+ | **Graph** | Base graph class | Varies | ✅ Stable |
89
+ | **DirectedGraph** | Directed graphs | O(V+E) | ✅ Stable |
90
+ | **UndirectedGraph** | Undirected graphs | O(V+E) | ✅ Stable |
91
+ | **SegmentTree** | Range queries | O(log n) | ⚠️ Beta |
92
+ | **BinaryIndexedTree** | Cumulative queries | O(log n) | ⚠️ Beta |
47
93
 
48
- - `enqueue`, `dequeue`, `push`, `pop`, `poll`, `push`, `unshift`, `shift`, `pop`, `isEmpty`, `clear`, `print`, `clone`.
94
+ ---
49
95
 
50
- ### Implementation of Customizable Features Whenever Possible
96
+ ## API Standard
51
97
 
52
- - Such as providing callback functions (lambda expressions) for all traversal methods.
98
+ ### Unified Linear Structure API
53
99
 
54
- ### Comprehensive Documentation
100
+ **All linear structures (Stack, Queue, Deque, LinkedList) support the same 4 core methods:**
55
101
 
56
- - The documentation not only explains the purpose of methods but also annotates time and space complexity across the entire series.
102
+ ```typescript
103
+ // Add operations
104
+ structure.push(element: T): number // Add to end, return length
105
+ structure.unshift(element: T): number // Add to start, return length
106
+
107
+ // Remove operations
108
+ structure.pop(): T | undefined // Remove from end
109
+ structure.shift(): T | undefined // Remove from start
110
+
111
+ // Peek operations (read without removing)
112
+ structure.peek(): T | undefined // View last element
113
+ structure.peekFirst(): T | undefined // View first element (Deque)
114
+ structure.peekLast(): T | undefined // View last element (Deque)
115
+
116
+ // Size and queries
117
+ structure.size: number // Element count
118
+ structure.isEmpty: boolean // Check if empty
119
+ ```
120
+
121
+ ### Tree & Map API
122
+
123
+ **All key-value structures (Trees, Maps) support:**
124
+
125
+ ```typescript
126
+ // Add/update
127
+ structure.set(key: K, value: V): this
128
+ structure.setMany(entries: [K, V][]): this
129
+
130
+ // Query
131
+ structure.get(key: K): V | undefined
132
+ structure.has(key: K): boolean
133
+ structure.size: number
134
+
135
+ // Remove
136
+ structure.delete(key: K): boolean
137
+ structure.clear(): void
138
+
139
+ // Iteration
140
+ structure.keys(): IterableIterator<K>
141
+ structure.values(): IterableIterator<V>
142
+ structure.entries(): IterableIterator<[K, V]>
143
+ ```
144
+
145
+ ### Heap & Priority Queue API
146
+
147
+ **All heaps and priority queues support:**
148
+
149
+ ```typescript
150
+ // Add/remove
151
+ heap.push(element: T): number // Add element
152
+ heap.add(element: T): number // Alias for push
153
+ heap.pop(): T | undefined // Remove highest/lowest priority
154
+ heap.poll(): T | undefined // Alias for pop
155
+
156
+ // Peek
157
+ heap.peek(): T | undefined // View highest/lowest without removing
158
+
159
+ // Query
160
+ heap.size: number
161
+ heap.isEmpty: boolean
162
+ ```
163
+
164
+ ### Array Methods (Available on All Structures)
165
+
166
+ **All structures support ES6+ array methods via iteration:**
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
+ ## Type System Specification
181
+
182
+ ### Generic Parameters
183
+
184
+ **Binary type parameters for key-value structures:**
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
+ // Example usage
193
+ const tree = new RedBlackTree<number, string>();
194
+ tree.set(1, 'Alice'); // Type-safe
195
+ tree.get(1); // Returns: string | undefined
196
+ ```
197
+
198
+ **Single type parameter for linear structures:**
199
+
200
+ ```typescript
201
+ class Stack<T> {
202
+ push(element: T): number
203
+ pop(): T | undefined
204
+ }
205
+
206
+ // Example usage
207
+ const stack = new Stack<number>();
208
+ stack.push(42); // ✅ Type-safe
209
+ stack.push('text'); // ❌ Type error
210
+ ```
211
+
212
+ ### Comparator Functions
213
+
214
+ **All ordered structures accept optional comparator:**
215
+
216
+ ```typescript
217
+ type Comparator<T> = (a: T, b: T) => number
218
+
219
+ // Return value semantics:
220
+ // < 0 means a comes before b (ascending)
221
+ // 0 means equal
222
+ // > 0 means a comes after b
223
+
224
+ const tree = new RedBlackTree<number>(
225
+ (a, b) => b - a // Descending order
226
+ );
227
+ ```
228
+
229
+ ### Iterator Protocol
230
+
231
+ **All structures implement Symbol.iterator:**
232
+
233
+ ```typescript
234
+ interface Structure<T> {
235
+ [Symbol.iterator](): Iterator<T>
236
+ }
237
+
238
+ // Enables:
239
+ [...structure] // Spread operator
240
+ for (const item of structure) {} // for...of loop
241
+ const [a, b] = structure // Destructuring
242
+ Array.from(structure) // Array conversion
243
+ new Set(structure) // Set constructor
244
+ ```
245
+
246
+ ---
247
+
248
+ ## Performance Guarantees
249
+
250
+ ### Time Complexity Guarantees
251
+
252
+ | Data Structure | Access | Search | Insert | Delete | Min |
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) | Varies |
264
+
265
+ *With pointer access; O(n) without
266
+ **Average case; worst case O(n) if unbalanced
267
+
268
+ ### Space Complexity
269
+
270
+ | Data Structure | Space | Notes |
271
+ |---|---|---|
272
+ | **Stack** | O(n) | One pointer per element |
273
+ | **Queue** | O(n) | Chunked optimized |
274
+ | **Deque** | O(n) | Bucket-based, minimal overhead |
275
+ | **LinkedList** | O(n) | Extra pointers (singly/doubly) |
276
+ | **BST/RedBlackTree/AVL** | O(n) | 2-3 pointers per node |
277
+ | **Heap** | O(n) | Array-based, tight packing |
278
+ | **Trie** | O(26n) | Per-character nodes |
279
+ | **Graph** | O(V+E) | Adjacency list representation |
280
+
281
+ ### Benchmark Targets
282
+
283
+ Library maintains 10-100x performance vs naive implementations:
284
+
285
+ - **Deque**: 484x faster than Array.shift() for 100K operations
286
+ - **RedBlackTree**: 308x faster than Array sort for 100K+1K operations
287
+ - **Trie**: O(m+k) vs O(n*m) for prefix search
288
+
289
+ Benchmarks run on: MacBook Pro 2018, Intel i7, 16GB RAM
290
+
291
+ ---
292
+
293
+ ## Module System Support
294
+
295
+ ### CommonJS
296
+
297
+ ```javascript
298
+ const { RedBlackTree, Deque } = require('data-structure-typed');
299
+ ```
300
+
301
+ ### ES Modules (ESM)
302
+
303
+ ```javascript
304
+ import { RedBlackTree, Deque } from 'data-structure-typed';
305
+ ```
306
+
307
+ ### TypeScript with Full Types
308
+
309
+ ```typescript
310
+ import { RedBlackTree } from 'data-structure-typed';
311
+ const tree = new RedBlackTree<number, string>();
312
+ ```
313
+
314
+ ### UMD (Browser Global)
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
+ ### Individual Packages
325
+
326
+ ```bash
327
+ npm install heap-typed deque-typed red-black-tree-typed
328
+ ```
329
+
330
+ ---
331
+
332
+ ## Browser & Runtime Compatibility
333
+
334
+ ### Minimum Versions
335
+
336
+ | Runtime | Version | Notes |
337
+ |---------|---------|-------|
338
+ | **Node.js** | 16+ | Active LTS |
339
+ | **TypeScript** | 5.0+ | Latest stable |
340
+ | **Chrome** | 90+ | Modern ES2020 support |
341
+ | **Firefox** | 88+ | Modern ES2020 support |
342
+ | **Safari** | 14+ | Modern ES2020 support |
343
+ | **Edge** | 90+ | Modern ES2020 support |
344
+
345
+ ### Built-in Globals Required
346
+
347
+ - Symbol.iterator (for iteration protocol)
348
+ - Map/Set (for some structures)
349
+ - WeakMap (not used currently)
350
+
351
+ ### Polyfill Requirements
352
+
353
+ **None required for modern environments.**
354
+
355
+ For IE11 compatibility: Manual implementation needed (not officially supported)
356
+
357
+ ### Tested Platforms
358
+
359
+ - ✅ Node.js 16, 18, 20, 21
360
+ - ✅ Deno 1.40+
361
+ - ✅ Bun 1.0+
362
+ - ✅ Chrome, Firefox, Safari (latest)
363
+ - ✅ Edge (latest)
364
+ - ✅ Browsers via CDN (jsDelivr, unpkg)
365
+
366
+ ---
367
+
368
+ ## Data Structure Contracts
369
+
370
+ ### Stack Contract
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: Last In, First Out
381
+ // Properties:
382
+ // - push() and pop() must be O(1)
383
+ // - Iteration order: LIFO (top to bottom)
384
+ ```
385
+
386
+ ### Queue Contract
387
+
388
+ ```typescript
389
+ interface Queue<T> {
390
+ push(element: T): number // Enqueue
391
+ shift(): T | undefined // Dequeue
392
+ peek(): T | undefined
393
+ size: number
394
+ }
395
+
396
+ // FIFO: First In, First Out
397
+ // Properties:
398
+ // - push() and shift() must be O(1)
399
+ // - Iteration order: FIFO (head to tail)
400
+ ```
401
+
402
+ ### Tree Map Contract
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
+ // Key Properties:
416
+ // - Keys iterated in sorted order
417
+ // - All ops O(log n) guaranteed
418
+ // - No duplicate keys
419
+ // - Maintains sort invariant
420
+ ```
421
+
422
+ ### Heap Contract
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
+ // Heap Properties:
434
+ // - MinHeap: parent <= children
435
+ // - MaxHeap: parent >= children
436
+ // - push() and pop() O(log n)
437
+ // - peek() O(1)
438
+ // - NOT sorted when iterated (only heap order)
439
+ ```
440
+
441
+ ### Graph Contract
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
+ // Graph Properties:
455
+ // - Directed: edges are one-way
456
+ // - Undirected: edges are bidirectional
457
+ // - Weighted: edges have values
458
+ ```
459
+
460
+ ---
461
+
462
+ ## Iterator Protocol Compliance
463
+
464
+ ### Specification Compliance
465
+
466
+ **All structures implement ES6 Iterator Protocol:**
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
+ ### Iteration Order Guarantees
484
+
485
+ | Structure | Iteration Order | Deterministic |
486
+ |-----------|-----------------|---------------|
487
+ | **Stack** | Top to bottom (LIFO) | ✅ Yes |
488
+ | **Queue** | Front to back (FIFO) | ✅ Yes |
489
+ | **Deque** | First to last | ✅ Yes |
490
+ | **LinkedList** | Head to tail | ✅ Yes |
491
+ | **BST/RBTree** | In-order (sorted) | ✅ Yes |
492
+ | **Heap** | Heap order (not sorted) | ✅ Yes |
493
+ | **Trie** | Lexicographic | ✅ Yes |
494
+ | **Graph** | BFS from start vertex | ✅ Yes |
495
+
496
+ ### Mutation During Iteration
497
+
498
+ **Behavior: Undefined (same as native Array)**
499
+
500
+ ```typescript
501
+ const tree = new RedBlackTree([1, 2, 3]);
502
+
503
+ // ❌ Do NOT mutate during iteration
504
+ for (const item of tree) {
505
+ if (item === 2) tree.delete(2); // Unsafe!
506
+ }
507
+
508
+ // ✅ Do collect mutations first
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
+ ## Method Chaining Specification
519
+
520
+ ### Chainable Operations
521
+
522
+ **Methods that return `this` support chaining:**
523
+
524
+ ```typescript
525
+ structure
526
+ .filter(predicate)
527
+ .map(mapper)
528
+ .reduce(reducer)
529
+ ```
530
+
531
+ **Map and filter return same type to maintain structure:**
532
+
533
+ ```typescript
534
+ const tree = new RedBlackTree([[1, 'a'], [2, 'b']]);
535
+
536
+ const result = tree
537
+ .filter((v, k) => k > 1) // Still RedBlackTree
538
+ .map((v, k) => [k, v.toUpperCase()]); // Still RedBlackTree
539
+
540
+ // Access tree methods still available
541
+ result.set(3, 'C');
542
+ ```
543
+
544
+ ### Terminal Operations
545
+
546
+ **Operations that break chain return final type:**
547
+
548
+ ```typescript
549
+ structure.reduce((acc, val) => acc + val, 0) // Returns number
550
+ structure.toArray() // Returns T[]
551
+ structure.find(pred) // Returns T | undefined
552
+ structure.some(pred) // Returns boolean
553
+ ```
554
+
555
+ ---
556
+
557
+ ## Error Handling
558
+
559
+ ### Error Types
560
+
561
+ **Library throws standard JavaScript errors:**
562
+
563
+ | Error Type | When | Example |
564
+ |---|---|---|
565
+ | **TypeError** | Type mismatch | Passing wrong type to generic |
566
+ | **RangeError** | Invalid index | Accessing out-of-bounds index |
567
+ | **Error** | Logic violation | Comparing incomparable items |
568
+
569
+ ### Error Behavior
570
+
571
+ **No silent failures:**
572
+
573
+ ```typescript
574
+ const tree = new RedBlackTree<number>();
575
+
576
+ // ✅ Throws TypeError if comparator fails
577
+ tree.set(null, 'value'); // TypeError: Cannot compare null
578
+
579
+ // ✅ Returns undefined for missing keys (safe)
580
+ tree.get(999); // undefined (not an error)
581
+
582
+ // ✅ No exceptions for empty operations
583
+ tree.pop(); // Returns undefined (not an error)
584
+ ```
585
+
586
+ ### Silent vs Loud
587
+
588
+ **Silent (no exception):**
589
+ - Missing key on `.get()`
590
+ - Pop/shift from empty structure
591
+ - Failed delete (key not found)
592
+
593
+ **Loud (throws exception):**
594
+ - Type mismatch in generic
595
+ - Comparison failures
596
+ - Invalid graph operations
597
+
598
+ ---
599
+
600
+ ## Breaking Changes Policy
601
+
602
+ ### Semantic Versioning
603
+
604
+ **Library follows SemVer 2.0:**
605
+
606
+ - **Major** (2.0.0): Breaking API changes
607
+ - **Minor** (2.1.0): New features, backward compatible
608
+ - **Patch** (2.0.1): Bug fixes, performance improvements
609
+
610
+ ### Deprecation Policy
611
+
612
+ **Deprecations announced 2 versions ahead:**
613
+
614
+ ```typescript
615
+ // v1.x
616
+ method() { /* old implementation */ }
617
+
618
+ // v1.x -> v2.0 (announcement release)
619
+ /**
620
+ * @deprecated Use newMethod() instead. Will be removed in v3.0
621
+ */
622
+ method() { /* old implementation */ }
623
+
624
+ // v3.0 (removal release)
625
+ // method() removed entirely
626
+ ```
627
+
628
+ ### Backward Compatibility Guarantee
629
+
630
+ **Within major version:**
631
+ - ✅ Public API guaranteed stable
632
+ - ✅ Existing code continues working
633
+ - ✅ Performance may improve
634
+ - ✅ New methods may be added
635
+ - ❌ Removed methods with deprecation notice
636
+
637
+ **Major version upgrades:**
638
+ - May include breaking changes
639
+ - Migration guide provided
640
+ - 6-month support overlap
641
+
642
+ ---
643
+
644
+ ## Compliance Checklist
645
+
646
+ ### API Compliance
647
+
648
+ - ✅ Unified method names across all linear structures
649
+ - ✅ Type-safe generics on all structures
650
+ - ✅ Iterator protocol on all structures
651
+ - ✅ Array methods (map/filter/reduce) on all structures
652
+ - ✅ Consistent error handling
653
+
654
+ ### Performance Compliance
655
+
656
+ - ✅ O(log n) guaranteed on all balanced trees
657
+ - ✅ O(1) guaranteed on Deque operations
658
+ - ✅ O(log n) heap add/remove operations
659
+ - ✅ Benchmarks published and maintained
660
+
661
+ ### Browser/Runtime Compliance
662
+
663
+ - ✅ CommonJS + ESM + UMD support
664
+ - ✅ TypeScript 5.0+ support
665
+ - ✅ Node.js 16+ support
666
+ - ✅ Modern browser support (ES2020)
667
+
668
+ ### Documentation Compliance
669
+
670
+ - ✅ Complete API documentation
671
+ - ✅ Real-world examples for all structures
672
+ - ✅ Performance benchmarks
673
+ - ✅ Framework integration guides
674
+
675
+ ---
676
+
677
+ ## Future Roadmap
678
+
679
+ ### Planned (v2.x)
680
+
681
+ - [ ] AVL Tree performance optimizations
682
+ - [ ] Segment Tree completion
683
+ - [ ] Binary Indexed Tree completion
684
+ - [ ] Skip List implementation
685
+ - [ ] Performance improvements for V8 JIT
686
+
687
+ ### Considering (v3.0+)
688
+
689
+ - [ ] Concurrent data structures
690
+ - [ ] Persistent (immutable) variants
691
+ - [ ] WebAssembly implementations for performance
692
+ - [ ] Java/Python ports
693
+
694
+ ### Won't Implement
695
+
696
+ - ❌ Weak references (design decision)
697
+ - ❌ Async iterators (synchronous library)
698
+ - ❌ Browser storage bindings (out of scope)
699
+
700
+ ---
701
+
702
+ ## Related Documentation
703
+
704
+ - **[SECURITY.md](./SECURITY.md)** - Security considerations and best practices
705
+ - **[CONCEPTS.md](./CONCEPTS.md)** - Fundamental concepts and theory
706
+ - **[REFERENCE.md](./REFERENCE.md)** - Complete API reference
707
+ - **[PERFORMANCE.md](./PERFORMANCE.md)** - Performance benchmarks
708
+ - **[GUIDES.md](./GUIDES.md)** - Real-world usage examples
709
+ - **[ARCHITECTURE.md](./ARCHITECTURE.md)** - Design decisions and internals
710
+
711
+ ---
712
+
713
+ **Last Updated:** January 2026
714
+ **Maintained By:** [@zrwusa](https://github.com/zrwusa)
715
+ **License:** MIT