data-structure-typed 2.2.2 → 2.2.3
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.
- package/CHANGELOG.md +1 -1
- package/README.md +311 -1687
- package/README_CN.md +509 -0
- package/SECURITY.md +962 -11
- package/SECURITY.zh-CN.md +966 -0
- package/SPECIFICATION.md +689 -30
- package/SPECIFICATION.zh-CN.md +715 -0
- package/SPONSOR.zh-CN.md +62 -0
- package/SPONSOR_POLISHED.md +62 -0
- package/benchmark/report.html +1 -1
- package/benchmark/report.json +215 -172
- package/dist/cjs/index.cjs +163 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +164 -0
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +163 -0
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +164 -0
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +96 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +103 -7
- package/dist/types/data-structures/binary-tree/bst.d.ts +156 -13
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +84 -35
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
- package/dist/types/data-structures/graph/directed-graph.d.ts +126 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +160 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +110 -27
- package/dist/types/data-structures/heap/heap.d.ts +107 -58
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +72 -404
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +121 -5
- package/dist/types/data-structures/queue/deque.d.ts +95 -67
- package/dist/types/data-structures/queue/queue.d.ts +90 -34
- package/dist/types/data-structures/stack/stack.d.ts +58 -40
- package/dist/types/data-structures/trie/trie.d.ts +109 -47
- package/dist/types/interfaces/binary-tree.d.ts +1 -0
- package/dist/umd/data-structure-typed.js +164 -0
- package/dist/umd/data-structure-typed.js.map +1 -1
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +3 -2
- package/src/data-structures/binary-tree/avl-tree.ts +96 -2
- package/src/data-structures/binary-tree/binary-tree.ts +117 -7
- package/src/data-structures/binary-tree/bst.ts +322 -13
- package/src/data-structures/binary-tree/red-black-tree.ts +84 -35
- package/src/data-structures/binary-tree/tree-multi-map.ts +2 -2
- package/src/data-structures/graph/directed-graph.ts +126 -1
- package/src/data-structures/graph/undirected-graph.ts +160 -1
- package/src/data-structures/hash/hash-map.ts +110 -27
- package/src/data-structures/heap/heap.ts +107 -58
- package/src/data-structures/linked-list/doubly-linked-list.ts +72 -404
- package/src/data-structures/linked-list/singly-linked-list.ts +121 -5
- package/src/data-structures/queue/deque.ts +95 -67
- package/src/data-structures/queue/queue.ts +90 -34
- package/src/data-structures/stack/stack.ts +58 -40
- package/src/data-structures/trie/trie.ts +109 -47
- package/src/interfaces/binary-tree.ts +2 -0
- package/test/performance/benchmark-runner.ts +14 -11
- package/test/performance/data-structures/binary-tree/avl-tree.test.ts +8 -8
- package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +8 -8
- package/test/performance/data-structures/binary-tree/binary-tree.test.ts +6 -6
- package/test/performance/data-structures/binary-tree/bst.test.ts +5 -5
- package/test/performance/data-structures/binary-tree/red-black-tree.test.ts +10 -10
- package/test/performance/reportor.ts +2 -1
- package/test/performance/single-suite-runner.ts +7 -4
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +117 -0
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +166 -0
- package/test/unit/data-structures/binary-tree/bst.test.ts +766 -8
- package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +89 -37
- package/test/unit/data-structures/graph/directed-graph.test.ts +133 -0
- package/test/unit/data-structures/graph/undirected-graph.test.ts +167 -0
- package/test/unit/data-structures/hash/hash-map.test.ts +149 -3
- package/test/unit/data-structures/heap/heap.test.ts +182 -47
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +118 -14
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +121 -0
- package/test/unit/data-structures/queue/deque.test.ts +98 -67
- package/test/unit/data-structures/queue/queue.test.ts +85 -51
- package/test/unit/data-structures/stack/stack.test.ts +142 -33
- package/test/unit/data-structures/trie/trie.test.ts +135 -39
- package/tsup.leetcode.config.js +99 -0
- package/typedoc.json +2 -1
- package/POSTS_zh-CN.md +0 -54
- package/README_zh-CN.md +0 -1208
- package/SPECIFICATION_zh-CN.md +0 -81
package/SPECIFICATION.md
CHANGED
|
@@ -1,56 +1,715 @@
|
|
|
1
|
+
# SPECIFICATION: data-structure-typed Library
|
|
1
2
|
|
|
2
|
-
|
|
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
|
-
|
|
8
|
+
---
|
|
5
9
|
|
|
6
|
-
##
|
|
10
|
+
## Table of Contents
|
|
7
11
|
|
|
8
|
-
|
|
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
|
-
|
|
25
|
+
---
|
|
11
26
|
|
|
12
|
-
|
|
27
|
+
## Specification Overview
|
|
13
28
|
|
|
14
|
-
-
|
|
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
|
-
###
|
|
31
|
+
### Design Goals
|
|
17
32
|
|
|
18
|
-
-
|
|
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
|
-
|
|
40
|
+
### Version Compatibility
|
|
21
41
|
|
|
22
|
-
|
|
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
|
-
|
|
47
|
+
---
|
|
25
48
|
|
|
26
|
-
|
|
49
|
+
## Supported Data Structures
|
|
27
50
|
|
|
28
|
-
|
|
51
|
+
### Linear Structures (20+ total)
|
|
29
52
|
|
|
30
|
-
|
|
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
|
-
|
|
61
|
+
### Tree Structures
|
|
33
62
|
|
|
34
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
82
|
+
### Specialized Structures
|
|
45
83
|
|
|
46
|
-
|
|
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
|
-
|
|
94
|
+
---
|
|
49
95
|
|
|
50
|
-
|
|
96
|
+
## API Standard
|
|
51
97
|
|
|
52
|
-
|
|
98
|
+
### Unified Linear Structure API
|
|
53
99
|
|
|
54
|
-
|
|
100
|
+
**All linear structures (Stack, Queue, Deque, LinkedList) support the same 4 core methods:**
|
|
55
101
|
|
|
56
|
-
|
|
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
|