data-structure-typed 2.5.1 → 2.5.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 +5 -1
- package/MIGRATION.md +169 -0
- package/README.md +135 -23
- package/README_CN.md +551 -143
- package/SPECIFICATION.md +20 -14
- package/SPECIFICATION.zh-CN.md +20 -14
- package/dist/cjs/binary-tree.cjs +6460 -1591
- package/dist/cjs/graph.cjs +440 -20
- package/dist/cjs/hash.cjs +125 -22
- package/dist/cjs/heap.cjs +196 -47
- package/dist/cjs/index.cjs +8486 -2429
- package/dist/cjs/linked-list.cjs +456 -31
- package/dist/cjs/matrix.cjs +79 -9
- package/dist/cjs/priority-queue.cjs +193 -44
- package/dist/cjs/queue.cjs +391 -2
- package/dist/cjs/stack.cjs +92 -6
- package/dist/cjs/trie.cjs +122 -28
- package/dist/cjs-legacy/binary-tree.cjs +6484 -1612
- package/dist/cjs-legacy/graph.cjs +440 -20
- package/dist/cjs-legacy/hash.cjs +125 -22
- package/dist/cjs-legacy/heap.cjs +196 -47
- package/dist/cjs-legacy/index.cjs +8654 -2594
- package/dist/cjs-legacy/linked-list.cjs +456 -31
- package/dist/cjs-legacy/matrix.cjs +79 -9
- package/dist/cjs-legacy/priority-queue.cjs +193 -44
- package/dist/cjs-legacy/queue.cjs +391 -2
- package/dist/cjs-legacy/stack.cjs +92 -6
- package/dist/cjs-legacy/trie.cjs +122 -28
- package/dist/esm/binary-tree.mjs +6460 -1591
- package/dist/esm/graph.mjs +440 -20
- package/dist/esm/hash.mjs +125 -22
- package/dist/esm/heap.mjs +196 -47
- package/dist/esm/index.mjs +8486 -2430
- package/dist/esm/linked-list.mjs +456 -31
- package/dist/esm/matrix.mjs +79 -9
- package/dist/esm/priority-queue.mjs +193 -44
- package/dist/esm/queue.mjs +391 -2
- package/dist/esm/stack.mjs +92 -6
- package/dist/esm/trie.mjs +122 -28
- package/dist/esm-legacy/binary-tree.mjs +6484 -1612
- package/dist/esm-legacy/graph.mjs +440 -20
- package/dist/esm-legacy/hash.mjs +125 -22
- package/dist/esm-legacy/heap.mjs +196 -47
- package/dist/esm-legacy/index.mjs +8654 -2595
- package/dist/esm-legacy/linked-list.mjs +456 -31
- package/dist/esm-legacy/matrix.mjs +79 -9
- package/dist/esm-legacy/priority-queue.mjs +193 -44
- package/dist/esm-legacy/queue.mjs +391 -2
- package/dist/esm-legacy/stack.mjs +92 -6
- package/dist/esm-legacy/trie.mjs +122 -28
- package/dist/types/common/error.d.ts +9 -0
- package/dist/types/common/index.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +98 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +112 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +214 -13
- package/dist/types/data-structures/binary-tree/bst.d.ts +294 -3
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +155 -8
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +48 -0
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +1370 -323
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1329 -316
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +1116 -295
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +1330 -326
- package/dist/types/data-structures/graph/directed-graph.d.ts +80 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +72 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +95 -6
- package/dist/types/data-structures/heap/heap.d.ts +154 -12
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +143 -0
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +121 -1
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +144 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +64 -0
- package/dist/types/data-structures/queue/deque.d.ts +142 -0
- package/dist/types/data-structures/queue/queue.d.ts +109 -0
- package/dist/types/data-structures/stack/stack.d.ts +82 -2
- package/dist/types/data-structures/trie/trie.d.ts +96 -0
- package/dist/types/interfaces/binary-tree.d.ts +2 -3
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +5 -0
- package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +4 -0
- package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +4 -0
- package/dist/umd/data-structure-typed.js +8623 -2564
- package/dist/umd/data-structure-typed.min.js +5 -5
- package/docs-site-docusaurus/docs/api/classes/AVLTree.md +696 -194
- package/docs-site-docusaurus/docs/api/classes/AVLTreeNode.md +11 -11
- package/docs-site-docusaurus/docs/api/classes/AbstractGraph.md +71 -71
- package/docs-site-docusaurus/docs/api/classes/BST.md +639 -189
- package/docs-site-docusaurus/docs/api/classes/BSTNode.md +13 -13
- package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +15 -15
- package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +148 -160
- package/docs-site-docusaurus/docs/api/classes/BinaryTreeNode.md +13 -13
- package/docs-site-docusaurus/docs/api/classes/Deque.md +105 -91
- package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +82 -82
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +104 -74
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedListNode.md +8 -8
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +12 -12
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +1 -1
- package/docs-site-docusaurus/docs/api/classes/HashMap.md +51 -51
- package/docs-site-docusaurus/docs/api/classes/Heap.md +96 -85
- package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +25 -25
- package/docs-site-docusaurus/docs/api/classes/IterableEntryBase.md +33 -33
- package/docs-site-docusaurus/docs/api/classes/LinearBase.md +50 -50
- package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +55 -55
- package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +55 -55
- package/docs-site-docusaurus/docs/api/classes/LinkedListNode.md +6 -6
- package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +78 -78
- package/docs-site-docusaurus/docs/api/classes/MapGraph.md +82 -82
- package/docs-site-docusaurus/docs/api/classes/Matrix.md +31 -31
- package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +104 -89
- package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +104 -89
- package/docs-site-docusaurus/docs/api/classes/MinHeap.md +104 -89
- package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +104 -89
- package/docs-site-docusaurus/docs/api/classes/Navigator.md +5 -5
- package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +103 -88
- package/docs-site-docusaurus/docs/api/classes/Queue.md +112 -60
- package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +708 -206
- package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +10 -10
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +79 -79
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedListNode.md +6 -6
- package/docs-site-docusaurus/docs/api/classes/SkipList.md +44 -44
- package/docs-site-docusaurus/docs/api/classes/Stack.md +42 -42
- package/docs-site-docusaurus/docs/api/classes/TreeMap.md +236 -33
- package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +162 -46
- package/docs-site-docusaurus/docs/api/classes/TreeSet.md +232 -32
- package/docs-site-docusaurus/docs/api/classes/Trie.md +47 -47
- package/docs-site-docusaurus/docs/api/classes/TrieNode.md +8 -8
- package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +81 -81
- package/docs-site-docusaurus/docs/guide/architecture.md +75 -5
- package/docs-site-docusaurus/docs/guide/concepts.md +53 -3
- package/docs-site-docusaurus/docs/guide/faq.md +233 -0
- package/docs-site-docusaurus/docs/guide/guides.md +43 -58
- package/docs-site-docusaurus/docs/guide/installation.md +2 -0
- package/docs-site-docusaurus/docs/guide/integrations.md +75 -176
- package/docs-site-docusaurus/docs/guide/overview.md +132 -11
- package/docs-site-docusaurus/docs/guide/performance.md +2 -0
- package/docs-site-docusaurus/docs/guide/quick-start.md +31 -0
- package/docs-site-docusaurus/docs/guide/use-cases/_category_.json +6 -0
- package/docs-site-docusaurus/docs/guide/use-cases/array-sort-alternative.md +158 -0
- package/docs-site-docusaurus/docs/guide/use-cases/heap-vs-sorting.md +92 -0
- package/docs-site-docusaurus/docs/guide/use-cases/map-vs-treemap.md +151 -0
- package/docs-site-docusaurus/docs/guide/use-cases/priority-queue-typescript.md +113 -0
- package/docs-site-docusaurus/docs/guide/use-cases/treemap-javascript.md +151 -0
- package/docs-site-docusaurus/docusaurus.config.ts +1 -1
- package/docs-site-docusaurus/src/pages/index.tsx +55 -2
- package/docs-site-docusaurus/static/llms.txt +37 -0
- package/docs-site-docusaurus/typedoc.json +1 -0
- package/llms.txt +37 -0
- package/package.json +65 -56
- package/src/common/error.ts +19 -1
- package/src/common/index.ts +1 -1
- package/src/data-structures/base/iterable-element-base.ts +3 -2
- package/src/data-structures/binary-tree/avl-tree.ts +99 -5
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +102 -4
- package/src/data-structures/binary-tree/binary-tree.ts +239 -78
- package/src/data-structures/binary-tree/bst.ts +542 -13
- package/src/data-structures/binary-tree/red-black-tree.ts +155 -15
- package/src/data-structures/binary-tree/segment-tree.ts +42 -0
- package/src/data-structures/binary-tree/tree-map.ts +1223 -261
- package/src/data-structures/binary-tree/tree-multi-map.ts +939 -30
- package/src/data-structures/binary-tree/tree-multi-set.ts +746 -10
- package/src/data-structures/binary-tree/tree-set.ts +1018 -99
- package/src/data-structures/graph/abstract-graph.ts +2 -2
- package/src/data-structures/graph/directed-graph.ts +71 -1
- package/src/data-structures/graph/undirected-graph.ts +64 -1
- package/src/data-structures/hash/hash-map.ts +102 -16
- package/src/data-structures/heap/heap.ts +153 -23
- package/src/data-structures/heap/max-heap.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +139 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +106 -1
- package/src/data-structures/linked-list/skip-linked-list.ts +131 -5
- package/src/data-structures/matrix/matrix.ts +65 -9
- package/src/data-structures/priority-queue/max-priority-queue.ts +2 -2
- package/src/data-structures/queue/deque.ts +130 -0
- package/src/data-structures/queue/queue.ts +109 -0
- package/src/data-structures/stack/stack.ts +75 -5
- package/src/data-structures/trie/trie.ts +86 -2
- package/src/interfaces/binary-tree.ts +1 -9
- package/src/types/data-structures/binary-tree/bst.ts +1 -0
- package/src/types/data-structures/binary-tree/tree-map.ts +6 -0
- package/src/types/data-structures/binary-tree/tree-multi-set.ts +5 -0
- package/src/types/data-structures/binary-tree/tree-set.ts +5 -0
- package/.vitepress/cache/deps_temp_51f5f1b0/chunk-7OIKW5WK.js +0 -12984
- package/.vitepress/cache/deps_temp_51f5f1b0/package.json +0 -3
- package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vue_devtools-api.js +0 -4505
- package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vueuse_core.js +0 -9731
- package/.vitepress/cache/deps_temp_51f5f1b0/vue.js +0 -347
package/CHANGELOG.md
CHANGED
|
@@ -8,7 +8,11 @@ All notable changes to this project will be documented in this file.
|
|
|
8
8
|
- [Semantic Versioning](https://semver.org/spec/v2.0.0.html)
|
|
9
9
|
- [`auto-changelog`](https://github.com/CookPete/auto-changelog)
|
|
10
10
|
|
|
11
|
-
## [v2.5.
|
|
11
|
+
## [v2.5.3](https://github.com/zrwusa/data-structure-typed/compare/v2.5.3...main) (upcoming)
|
|
12
|
+
|
|
13
|
+
## [v2.5.3](https://github.com/zrwusa/data-structure-typed/compare/v2.5.1...v2.5.3) (31 March 2026)
|
|
14
|
+
|
|
15
|
+
## [v2.5.1](https://github.com/zrwusa/data-structure-typed/compare/v2.5.0...v2.5.1) (28 March 2026)
|
|
12
16
|
|
|
13
17
|
## [v2.5.0](https://github.com/zrwusa/data-structure-typed/compare/v2.4.3...v2.5.0) (27 March 2026)
|
|
14
18
|
|
package/MIGRATION.md
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
# Migration Guide
|
|
2
|
+
|
|
3
|
+
## Upgrading to v2.5.3
|
|
4
|
+
|
|
5
|
+
### Breaking Changes
|
|
6
|
+
|
|
7
|
+
#### 1. `HashMap/LinkedHashMap.set()` returns `this` instead of `boolean`
|
|
8
|
+
|
|
9
|
+
Aligns with the native JS `Map.set()` spec. Enables chaining.
|
|
10
|
+
|
|
11
|
+
```typescript
|
|
12
|
+
// Before
|
|
13
|
+
const success: boolean = map.set('key', 'value');
|
|
14
|
+
|
|
15
|
+
// After
|
|
16
|
+
map.set('key', 'value'); // returns this
|
|
17
|
+
const chain = map.set('a', 1).set('b', 2); // chaining works
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
#### 2. `BinaryTree/BST/AVL/RedBlackTree.delete()` returns `boolean` instead of `BinaryTreeDeleteResult[]`
|
|
21
|
+
|
|
22
|
+
Simplified to match standard container semantics.
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
// Before
|
|
26
|
+
const results: BinaryTreeDeleteResult[] = tree.delete(key);
|
|
27
|
+
const success = results.length > 0 && results[0].deleted !== undefined;
|
|
28
|
+
|
|
29
|
+
// After
|
|
30
|
+
const success: boolean = tree.delete(key); // true if found and removed
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
#### 3. `BST.deleteWhere()` returns `boolean` instead of `BinaryTreeDeleteResult[]`
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
// Before
|
|
37
|
+
const results: BinaryTreeDeleteResult[] = bst.deleteWhere(pred);
|
|
38
|
+
|
|
39
|
+
// After
|
|
40
|
+
const deleted: boolean = bst.deleteWhere(pred); // true if any match removed
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
#### 4. `Stack.deleteAt()` returns `E | undefined` instead of `boolean`
|
|
44
|
+
|
|
45
|
+
Consistent with `LinkedList.deleteAt()`, `Queue.deleteAt()`, `Deque.deleteAt()`.
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
// Before
|
|
49
|
+
const success: boolean = stack.deleteAt(2);
|
|
50
|
+
|
|
51
|
+
// After
|
|
52
|
+
const removed: E | undefined = stack.deleteAt(2); // returns removed element
|
|
53
|
+
if (removed !== undefined) { /* success */ }
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
#### 5. `LinkedHashMap.deleteAt()` returns `[K, V] | undefined` instead of `boolean`
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
// Before
|
|
60
|
+
const success: boolean = lhm.deleteAt(0);
|
|
61
|
+
|
|
62
|
+
// After
|
|
63
|
+
const entry: [K, V] | undefined = lhm.deleteAt(0);
|
|
64
|
+
if (entry) {
|
|
65
|
+
const [key, value] = entry;
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
#### 6. `FibonacciHeap.push()` returns `boolean` instead of `this`
|
|
70
|
+
|
|
71
|
+
Consistent with `Heap.add()`.
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
// Before
|
|
75
|
+
const heap: FibonacciHeap = fh.push(42); // returned this
|
|
76
|
+
|
|
77
|
+
// After
|
|
78
|
+
const success: boolean = fh.push(42); // returns true
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
#### 7. `refill()` removed from `BinaryTree` and `Heap`
|
|
82
|
+
|
|
83
|
+
Replace with `clear()` + `addMany()`/`setMany()`.
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
// Before (Heap)
|
|
87
|
+
heap.refill([5, 3, 1, 4]);
|
|
88
|
+
|
|
89
|
+
// After (Heap)
|
|
90
|
+
heap.clear();
|
|
91
|
+
heap.addMany([5, 3, 1, 4]);
|
|
92
|
+
|
|
93
|
+
// Before (BinaryTree)
|
|
94
|
+
tree.refill(entries);
|
|
95
|
+
|
|
96
|
+
// After (BinaryTree)
|
|
97
|
+
tree.clear();
|
|
98
|
+
tree.setMany(entries);
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
---
|
|
102
|
+
|
|
103
|
+
### Deprecations
|
|
104
|
+
|
|
105
|
+
These still work but will be removed in a future major version.
|
|
106
|
+
|
|
107
|
+
#### `Heap.poll()` → use `Heap.pop()`
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
// Deprecated
|
|
111
|
+
const top = heap.poll();
|
|
112
|
+
|
|
113
|
+
// Preferred
|
|
114
|
+
const top = heap.pop();
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
#### `Heap.deleteBy()` → use `Heap.deleteWhere()`
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
// Deprecated
|
|
121
|
+
heap.deleteBy(e => e.id === 42);
|
|
122
|
+
|
|
123
|
+
// Preferred
|
|
124
|
+
heap.deleteWhere(e => e.id === 42);
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
---
|
|
128
|
+
|
|
129
|
+
### New Features
|
|
130
|
+
|
|
131
|
+
#### `deleteWhere()` — conditional deletion
|
|
132
|
+
|
|
133
|
+
Available on: `TreeMap`, `TreeSet`, `TreeMultiMap`, `TreeMultiSet`, `DoublyLinkedList`, `Queue`, `Heap`
|
|
134
|
+
|
|
135
|
+
```typescript
|
|
136
|
+
// Delete first even number from queue
|
|
137
|
+
queue.deleteWhere((value, index) => value % 2 === 0);
|
|
138
|
+
|
|
139
|
+
// Delete entries where value > 100 from TreeMap
|
|
140
|
+
treeMap.deleteWhere((key, value) => value > 100);
|
|
141
|
+
|
|
142
|
+
// Delete keys > 50 from TreeSet
|
|
143
|
+
treeSet.deleteWhere((key) => key > 50);
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
#### `Queue.peek()` / `Deque.peek()`
|
|
147
|
+
|
|
148
|
+
Alias for the `first` getter. Familiar API for users coming from other languages.
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
const queue = new Queue([10, 20, 30]);
|
|
152
|
+
queue.peek(); // 10 (same as queue.first)
|
|
153
|
+
queue.first; // 10
|
|
154
|
+
|
|
155
|
+
const deque = new Deque([10, 20, 30]);
|
|
156
|
+
deque.peek(); // 10 (same as deque.first)
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
#### `Heap.pop()` — primary top-removal method
|
|
160
|
+
|
|
161
|
+
Consistent naming across all containers (`Stack.pop()`, `Deque.pop()`, `Heap.pop()`).
|
|
162
|
+
|
|
163
|
+
```typescript
|
|
164
|
+
const heap = new Heap([3, 1, 4], { comparator: (a, b) => a - b });
|
|
165
|
+
heap.pop(); // 1 (min element)
|
|
166
|
+
heap.pop(); // 3
|
|
167
|
+
heap.pop(); // 4
|
|
168
|
+
heap.pop(); // undefined
|
|
169
|
+
```
|
package/README.md
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
English | [简体中文](./README_CN.md)
|
|
4
4
|
|
|
5
|
-
A
|
|
5
|
+
A production-ready TypeScript data structures library featuring **Heap, Priority Queue, Deque, Trie, Graph, Red-Black Tree, TreeMap, TreeSet, SkipList, Segment Tree**, and more — with an API that feels as intuitive as JavaScript's native `Array`. Zero dependencies. Type-safe. Supports rank queries (`getRank`), positional access (`getByRank`), and range queries (`rangeByRank`).
|
|
6
6
|
|
|
7
|
-
**
|
|
7
|
+
> **Looking for a TreeMap or TreeSet in JavaScript?** Need a priority queue, an ordered set, or efficient rank/range queries? Tired of repeatedly sorting arrays after every insert? This library provides all of that with a unified, Array-like API.
|
|
8
8
|
|
|
9
9
|

|
|
10
10
|

|
|
@@ -14,7 +14,7 @@ A comprehensive TypeScript data structures library with production-ready impleme
|
|
|
14
14
|

|
|
15
15
|

|
|
16
16
|
|
|
17
|
-
**📦 [Installation](#-installation) • 🎮 [Playground](#-playground) • ⚡ [Quick Start](#-quick-start-30-seconds) • 📖 [Docs](#-documentation) • 📋 [API](https://data-structure-typed-docs.vercel.app/) • 💡 [Examples](./docs/GUIDES.md)**
|
|
17
|
+
**📦 [Installation](#-installation) • 🎮 [Playground](#-playground) • ⚡ [Quick Start](#-quick-start-30-seconds) • 📖 [Docs](#-documentation) • 📋 [API](https://data-structure-typed-docs.vercel.app/) • 💡 [Examples](./docs/GUIDES.md) • ❓ [FAQ](#-faq)**
|
|
18
18
|
|
|
19
19
|
---
|
|
20
20
|
|
|
@@ -28,6 +28,7 @@ A comprehensive TypeScript data structures library with production-ready impleme
|
|
|
28
28
|
6. [Key Features](#-key-features)
|
|
29
29
|
7. [Data Structures](#-data-structures-available)
|
|
30
30
|
8. [Documentation](#-documentation)
|
|
31
|
+
9. [FAQ](#-faq)
|
|
31
32
|
|
|
32
33
|
---
|
|
33
34
|
|
|
@@ -82,10 +83,10 @@ npm i avl-tree-typed bst-typed heap-typed
|
|
|
82
83
|
|
|
83
84
|
Try it instantly:
|
|
84
85
|
|
|
85
|
-
- [Node.js TypeScript](https://stackblitz.com/
|
|
86
|
-
- [Node.js JavaScript](https://stackblitz.com/
|
|
87
|
-
- [React TypeScript](https://stackblitz.com/
|
|
88
|
-
- [NestJS](https://stackblitz.com/
|
|
86
|
+
- [Node.js TypeScript](https://stackblitz.com/github/zrwusa/dst-playgrounds/tree/main/apps/nodejs-ts?file=src%2Findex.ts&title=data-structure-typed%20%E2%80%94%20Node.js%20TypeScript)
|
|
87
|
+
- [Node.js JavaScript](https://stackblitz.com/github/zrwusa/dst-playgrounds/tree/main/apps/nodejs-js?file=src%2Findex.js&title=data-structure-typed%20%E2%80%94%20Node.js%20JavaScript)
|
|
88
|
+
- [React TypeScript](https://stackblitz.com/github/zrwusa/dst-playgrounds/tree/main/apps/reactjs?file=src%2FApp.tsx&title=data-structure-typed%20%E2%80%94%20React%20Playground)
|
|
89
|
+
- [NestJS](https://stackblitz.com/github/zrwusa/dst-playgrounds/tree/main/apps/nestjs?file=src%2Fproduct%2Fservices%2Fproduct-price-index.service.ts&title=data-structure-typed%20%E2%80%94%20NestJS%20Product%20API)
|
|
89
90
|
|
|
90
91
|
---
|
|
91
92
|
|
|
@@ -215,7 +216,7 @@ const value = tree.get(1); // Type: string | undefined
|
|
|
215
216
|
|
|
216
217
|
### ✨ Zero Friction
|
|
217
218
|
|
|
218
|
-
Works everywhere. Spread it `[...]`, loop it `for..of`, convert it instantly.
|
|
219
|
+
Works everywhere. Spread it `[...]`, loop it `for..of`, convert it instantly. Pass raw data with `toEntryFn`/`toElementFn` — no pre-processing needed.
|
|
219
220
|
|
|
220
221
|
```javascript
|
|
221
222
|
// All data structures work with iterator protocol
|
|
@@ -224,8 +225,51 @@ const sorted = [...tree]; // Spread operator
|
|
|
224
225
|
for (const item of tree) {
|
|
225
226
|
} // for...of loop
|
|
226
227
|
const set = new Set(tree); // Set constructor
|
|
228
|
+
|
|
229
|
+
// Pass raw data directly
|
|
230
|
+
const map = new TreeMap(users, { toEntryFn: u => [u.id, u.name] });
|
|
227
231
|
```
|
|
228
232
|
|
|
233
|
+
### 🔄 Working with Raw Data
|
|
234
|
+
|
|
235
|
+
Got raw objects? Three ways to use them — pick based on what you want to store:
|
|
236
|
+
|
|
237
|
+
```typescript
|
|
238
|
+
interface User {
|
|
239
|
+
id: number;
|
|
240
|
+
name: string;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const users: User[] = [
|
|
244
|
+
{ id: 3, name: 'Charlie' },
|
|
245
|
+
{ id: 1, name: 'Alice' },
|
|
246
|
+
{ id: 2, name: 'Bob' }
|
|
247
|
+
];
|
|
248
|
+
|
|
249
|
+
// 1. Extract a field — store only that field
|
|
250
|
+
const ids = new TreeSet<number, User>(
|
|
251
|
+
users,
|
|
252
|
+
{ toElementFn: u => u.id }
|
|
253
|
+
);
|
|
254
|
+
// [1, 2, 3] — numbers only, original objects not kept
|
|
255
|
+
|
|
256
|
+
// 2. Store full objects — sort by a field
|
|
257
|
+
const fullSet = new TreeSet<User>(
|
|
258
|
+
users,
|
|
259
|
+
{ comparator: (a, b) => a.id - b.id }
|
|
260
|
+
);
|
|
261
|
+
// [{ id: 1, name: 'Alice' }, { id: 2, ... }, { id: 3, ... }]
|
|
262
|
+
|
|
263
|
+
// 3. Split into key-value — field as key, anything as value
|
|
264
|
+
const map = new TreeMap<number, User, User>(
|
|
265
|
+
users,
|
|
266
|
+
{ toEntryFn: u => [u.id, u] }
|
|
267
|
+
);
|
|
268
|
+
// map.get(1) → { id: 1, name: 'Alice' }
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
Works across all data structures — `toElementFn` for single-value types (Heap, Queue, Stack, LinkedList, Trie), `toEntryFn` for key-value types (TreeMap, HashMap, SkipList), and `comparator` for any sorted structure.
|
|
272
|
+
|
|
229
273
|
---
|
|
230
274
|
|
|
231
275
|
## 💡 When Should I Consider This Library?
|
|
@@ -237,6 +281,7 @@ const set = new Set(tree); // Set constructor
|
|
|
237
281
|
- Priority queues with fast position-based access
|
|
238
282
|
- Time-series data with range queries
|
|
239
283
|
- Red-Black Tree / Heap performance without learning new APIs
|
|
284
|
+
- **Pass raw objects directly** — no `.map()` pre-processing needed (unique to this library in JS/TS)
|
|
240
285
|
|
|
241
286
|
✅ **When your current code has:**
|
|
242
287
|
|
|
@@ -245,6 +290,7 @@ const set = new Set(tree); // Set constructor
|
|
|
245
290
|
- `Array.shift()` on large lists (queues)
|
|
246
291
|
- Custom sorting logic you repeat across files
|
|
247
292
|
- Map that needs to be ordered
|
|
293
|
+
- `.map()` calls just to reshape data before putting it in a collection
|
|
248
294
|
|
|
249
295
|
---
|
|
250
296
|
|
|
@@ -262,29 +308,43 @@ const leaderboard = new RedBlackTree<number, string>([
|
|
|
262
308
|
[92, 'Charlie']
|
|
263
309
|
], { comparator: (a, b) => b - a });
|
|
264
310
|
|
|
265
|
-
// Top-2 via lazy iterator — O(
|
|
311
|
+
// Top-2 via lazy iterator — O(k log n), no array copy
|
|
266
312
|
const iter = leaderboard.entries();
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
console.log(`${score}: ${player}`);
|
|
270
|
-
}
|
|
271
|
-
// Output: 100: Alice → 92: Charlie
|
|
313
|
+
const { value: [topScore, topPlayer] } = iter.next();
|
|
314
|
+
console.log(`${topScore}: ${topPlayer}`); // 100: Alice
|
|
272
315
|
|
|
273
316
|
// Update score — O(log n)
|
|
274
317
|
leaderboard.delete(85);
|
|
275
318
|
leaderboard.set(95, 'Bob');
|
|
276
319
|
|
|
277
|
-
// Top-k — O(k log n), no array copy needed
|
|
278
|
-
const top3: [number, string][] = [];
|
|
279
|
-
let score = leaderboard.getLeftMost(); // highest score
|
|
280
|
-
while (score !== undefined && top3.length < 3) {
|
|
281
|
-
top3.push([score, leaderboard.get(score)!]);
|
|
282
|
-
score = leaderboard.higher(score); // next in tree order
|
|
283
|
-
}
|
|
284
|
-
|
|
285
320
|
// Range query — players scoring 90~100, O(log n + k)
|
|
286
321
|
const scores90to100 = leaderboard.rangeSearch([90, 100]);
|
|
287
322
|
// [100, 95, 92] — automatically respects tree order
|
|
323
|
+
|
|
324
|
+
// For O(log n) top-k, rank, and pagination → see Order-Statistic Tree below
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
### Order-Statistic Tree (Rank Queries)
|
|
328
|
+
|
|
329
|
+
```typescript
|
|
330
|
+
import { RedBlackTree } from 'data-structure-typed';
|
|
331
|
+
|
|
332
|
+
const tree = new RedBlackTree<number, string>([
|
|
333
|
+
[100, 'Alice'], [85, 'Bob'], [92, 'Charlie'],
|
|
334
|
+
[78, 'Diana'], [95, 'Eve']
|
|
335
|
+
], { comparator: (a, b) => b - a, enableOrderStatistic: true });
|
|
336
|
+
|
|
337
|
+
// select(k) — find k-th element, O(log n)
|
|
338
|
+
tree.getByRank(0); // 100 (1st in tree order)
|
|
339
|
+
tree.getByRank(2); // 92 (3rd in tree order)
|
|
340
|
+
|
|
341
|
+
// rank(key) — count elements preceding key in tree order, O(log n)
|
|
342
|
+
tree.getRank(92); // 2 (2 elements before 92 in tree order)
|
|
343
|
+
|
|
344
|
+
// rangeByRank — pagination, O(log n + k)
|
|
345
|
+
tree.rangeByRank(0, 2); // [100, 95, 92] — top 3
|
|
346
|
+
|
|
347
|
+
// Also works with TreeMap, TreeSet, TreeMultiMap, TreeMultiSet
|
|
288
348
|
```
|
|
289
349
|
|
|
290
350
|
### Task Queue (Scheduling)
|
|
@@ -299,7 +359,7 @@ const taskQueue = new MaxPriorityQueue<{priority: number; task: string}>([], {
|
|
|
299
359
|
taskQueue.add({ priority: 5, task: 'Email' });
|
|
300
360
|
taskQueue.add({ priority: 9, task: 'Alert' }); // Instant priority handling
|
|
301
361
|
|
|
302
|
-
const nextTask = taskQueue.
|
|
362
|
+
const nextTask = taskQueue.pop(); // { priority: 9, task: 'Alert' }
|
|
303
363
|
```
|
|
304
364
|
|
|
305
365
|
### Fast Queue (FIFO)
|
|
@@ -646,3 +706,55 @@ docs/
|
|
|
646
706
|
---
|
|
647
707
|
|
|
648
708
|
**Ready to supercharge your TypeScript data structures? [Get started now →](#-quick-start-30-seconds)**
|
|
709
|
+
|
|
710
|
+
---
|
|
711
|
+
|
|
712
|
+
## ❓ FAQ
|
|
713
|
+
|
|
714
|
+
### Does JavaScript have a TreeMap or TreeSet?
|
|
715
|
+
|
|
716
|
+
Not natively. JavaScript's `Map` and `Set` are hash-based (unordered). This library provides `TreeMap` and `TreeSet` backed by Red-Black Trees — offering sorted iteration, `floor`/`ceiling`/`higher`/`lower` lookups, and `getRank`/`getByRank`/`rangeByRank` queries.
|
|
717
|
+
|
|
718
|
+
### When should I use a Heap instead of sorting an array?
|
|
719
|
+
|
|
720
|
+
When you need to repeatedly access the smallest or largest element. Sorting an array is O(n log n) every time; a Heap gives you O(log n) insert and O(1) access to the top element. Use `Heap`, `MinHeap`, or `MaxHeap` for priority queues, top-k problems, and scheduling.
|
|
721
|
+
|
|
722
|
+
### Does this library support rank and range queries?
|
|
723
|
+
|
|
724
|
+
Yes. Enable with `{ enableOrderStatistic: true }` on any tree-based structure (RedBlackTree, TreeMap, TreeSet, etc.):
|
|
725
|
+
- `getRank(key)` — how many elements precede this key in tree order
|
|
726
|
+
- `getByRank(k)` — get the element at position k
|
|
727
|
+
- `rangeByRank(start, end)` — get all elements between two positions
|
|
728
|
+
|
|
729
|
+
### Is it faster than native arrays for ordered operations?
|
|
730
|
+
|
|
731
|
+
For ordered insert + lookup: yes. Array insert into sorted position is O(n) (shift elements). Red-Black Tree insert is O(log n). For 10,000+ elements, the difference is significant. See [PERFORMANCE.md](./docs/PERFORMANCE.md) for benchmarks.
|
|
732
|
+
|
|
733
|
+
### Can I use this in React / Node.js / browser?
|
|
734
|
+
|
|
735
|
+
Yes. The library ships ESM, CJS, and UMD builds. It works in Node.js, browsers, React, Vue, Angular, Next.js, and any JavaScript runtime. Zero dependencies means no compatibility concerns.
|
|
736
|
+
|
|
737
|
+
### What data structures are included?
|
|
738
|
+
|
|
739
|
+
Heap, MinHeap, MaxHeap, Priority Queue, Deque, Queue, Stack, Linked List (Singly / Doubly), Red-Black Tree, AVL Tree, BST, TreeMap, TreeSet, TreeMultiMap, TreeMultiSet, SkipList, Trie, HashMap, Graph (Directed / Undirected), Segment Tree, Binary Indexed Tree (Fenwick Tree), Matrix. See [full list](#-data-structures-available).
|
|
740
|
+
|
|
741
|
+
### Is this library production-ready?
|
|
742
|
+
|
|
743
|
+
Yes. 2600+ tests, 99%+ code coverage, zero dependencies, and used in production. Every release passes typecheck, lint, and full test suite.
|
|
744
|
+
|
|
745
|
+
### How does this compare to js-sdsl or other libraries?
|
|
746
|
+
|
|
747
|
+
`data-structure-typed` offers more data structures (20+), a unified Array-like API across all structures, tree-shakeable subpath exports, and active maintenance. See [PERFORMANCE.md](./docs/PERFORMANCE.md) for benchmark comparisons.
|
|
748
|
+
|
|
749
|
+
### Can I pass raw data without converting it first?
|
|
750
|
+
|
|
751
|
+
Yes. Three patterns:
|
|
752
|
+
- **`toElementFn`** — extract a field, store only that (TreeSet, Heap, Queue, Stack, LinkedList, Trie)
|
|
753
|
+
- **`comparator`** — store full objects, sort by a field (all sorted structures)
|
|
754
|
+
- **`toEntryFn`** — split into key-value pairs (TreeMap, HashMap, SkipList)
|
|
755
|
+
|
|
756
|
+
See the [Raw Data section](#-working-with-raw-data) for examples.
|
|
757
|
+
|
|
758
|
+
### What is the bundle size?
|
|
759
|
+
|
|
760
|
+
UMD bundle: ~143KB minified. With subpath imports (e.g., `data-structure-typed/heap`), you only load what you use — as small as 18KB for Stack, 30KB for Heap. `sideEffects: false` enables full tree-shaking.
|