data-structure-typed 2.5.1 → 2.5.2
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 +3 -1
- package/README.md +75 -17
- package/dist/cjs/binary-tree.cjs +2723 -139
- package/dist/cjs/graph.cjs +192 -6
- package/dist/cjs/hash.cjs +63 -15
- package/dist/cjs/heap.cjs +93 -31
- package/dist/cjs/index.cjs +3514 -379
- package/dist/cjs/linked-list.cjs +237 -31
- package/dist/cjs/matrix.cjs +47 -9
- package/dist/cjs/priority-queue.cjs +92 -30
- package/dist/cjs/queue.cjs +176 -2
- package/dist/cjs/stack.cjs +48 -2
- package/dist/cjs/trie.cjs +78 -28
- package/dist/cjs-legacy/binary-tree.cjs +2725 -136
- package/dist/cjs-legacy/graph.cjs +192 -6
- package/dist/cjs-legacy/hash.cjs +63 -15
- package/dist/cjs-legacy/heap.cjs +93 -31
- package/dist/cjs-legacy/index.cjs +3389 -249
- package/dist/cjs-legacy/linked-list.cjs +237 -31
- package/dist/cjs-legacy/matrix.cjs +47 -9
- package/dist/cjs-legacy/priority-queue.cjs +92 -30
- package/dist/cjs-legacy/queue.cjs +176 -2
- package/dist/cjs-legacy/stack.cjs +48 -2
- package/dist/cjs-legacy/trie.cjs +78 -28
- package/dist/esm/binary-tree.mjs +2723 -139
- package/dist/esm/graph.mjs +192 -6
- package/dist/esm/hash.mjs +63 -15
- package/dist/esm/heap.mjs +93 -31
- package/dist/esm/index.mjs +3514 -380
- package/dist/esm/linked-list.mjs +237 -31
- package/dist/esm/matrix.mjs +47 -9
- package/dist/esm/priority-queue.mjs +92 -30
- package/dist/esm/queue.mjs +176 -2
- package/dist/esm/stack.mjs +48 -2
- package/dist/esm/trie.mjs +78 -28
- package/dist/esm-legacy/binary-tree.mjs +2725 -136
- package/dist/esm-legacy/graph.mjs +192 -6
- package/dist/esm-legacy/hash.mjs +63 -15
- package/dist/esm-legacy/heap.mjs +93 -31
- package/dist/esm-legacy/index.mjs +3389 -250
- package/dist/esm-legacy/linked-list.mjs +237 -31
- package/dist/esm-legacy/matrix.mjs +47 -9
- package/dist/esm-legacy/priority-queue.mjs +92 -30
- package/dist/esm-legacy/queue.mjs +176 -2
- package/dist/esm-legacy/stack.mjs +48 -2
- package/dist/esm-legacy/trie.mjs +78 -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 +48 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +56 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +102 -2
- package/dist/types/data-structures/binary-tree/bst.d.ts +195 -0
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +76 -0
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +24 -0
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +528 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +531 -6
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +435 -6
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +505 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +40 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +36 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +44 -0
- package/dist/types/data-structures/heap/heap.d.ts +56 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +68 -0
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +60 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +72 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +32 -0
- package/dist/types/data-structures/queue/deque.d.ts +60 -0
- package/dist/types/data-structures/queue/queue.d.ts +48 -0
- package/dist/types/data-structures/stack/stack.d.ts +40 -0
- package/dist/types/data-structures/trie/trie.d.ts +48 -0
- 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 +3404 -265
- package/dist/umd/data-structure-typed.min.js +5 -5
- package/docs-site-docusaurus/docs/api/classes/AVLTree.md +650 -136
- 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 +591 -129
- 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 +107 -107
- package/docs-site-docusaurus/docs/api/classes/BinaryTreeNode.md +13 -13
- package/docs-site-docusaurus/docs/api/classes/Deque.md +82 -82
- package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +82 -82
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +74 -74
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedListNode.md +8 -8
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +9 -9
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +1 -1
- package/docs-site-docusaurus/docs/api/classes/HashMap.md +47 -47
- package/docs-site-docusaurus/docs/api/classes/Heap.md +45 -45
- 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 +49 -49
- 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 +45 -45
- package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +45 -45
- package/docs-site-docusaurus/docs/api/classes/MinHeap.md +45 -45
- package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +45 -45
- package/docs-site-docusaurus/docs/api/classes/Navigator.md +5 -5
- package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +44 -44
- package/docs-site-docusaurus/docs/api/classes/Queue.md +60 -60
- package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +660 -146
- package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +10 -10
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +78 -78
- 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 +39 -39
- package/docs-site-docusaurus/docs/api/classes/TreeMap.md +165 -33
- package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +162 -46
- package/docs-site-docusaurus/docs/api/classes/TreeSet.md +161 -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 +2 -0
- package/docs-site-docusaurus/docs/guide/concepts.md +32 -1
- package/docs-site-docusaurus/docs/guide/faq.md +180 -0
- package/docs-site-docusaurus/docs/guide/guides.md +40 -54
- package/docs-site-docusaurus/docs/guide/installation.md +2 -0
- package/docs-site-docusaurus/docs/guide/integrations.md +2 -0
- package/docs-site-docusaurus/docs/guide/overview.md +7 -0
- 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 +51 -2
- package/docs-site-docusaurus/static/llms.txt +37 -0
- package/llms.txt +37 -0
- package/package.json +64 -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 +47 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +46 -4
- package/src/data-structures/binary-tree/binary-tree.ts +79 -4
- package/src/data-structures/binary-tree/bst.ts +441 -6
- package/src/data-structures/binary-tree/red-black-tree.ts +73 -0
- package/src/data-structures/binary-tree/segment-tree.ts +18 -0
- package/src/data-structures/binary-tree/tree-map.ts +434 -9
- package/src/data-structures/binary-tree/tree-multi-map.ts +426 -5
- package/src/data-structures/binary-tree/tree-multi-set.ts +350 -6
- package/src/data-structures/binary-tree/tree-set.ts +410 -8
- package/src/data-structures/graph/abstract-graph.ts +2 -2
- package/src/data-structures/graph/directed-graph.ts +30 -0
- package/src/data-structures/graph/undirected-graph.ts +27 -0
- package/src/data-structures/hash/hash-map.ts +35 -4
- package/src/data-structures/heap/heap.ts +46 -4
- package/src/data-structures/heap/max-heap.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +51 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +45 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +59 -5
- package/src/data-structures/matrix/matrix.ts +33 -9
- package/src/data-structures/priority-queue/max-priority-queue.ts +2 -2
- package/src/data-structures/queue/deque.ts +45 -0
- package/src/data-structures/queue/queue.ts +36 -0
- package/src/data-structures/stack/stack.ts +30 -0
- package/src/data-structures/trie/trie.ts +38 -2
- 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
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
---
|
|
2
2
|
sidebar_label: "ARCHITECTURE"
|
|
3
3
|
description: "Internal architecture: inheritance hierarchy, iterator protocol, memory layout, and algorithmic guarantees."
|
|
4
|
+
title: "Architecture — Inheritance, Iterators, Memory Layout"
|
|
5
|
+
keywords: [data structure library architecture, typescript inheritance, iterator protocol, memory layout, algorithmic guarantees]
|
|
4
6
|
---
|
|
5
7
|
|
|
6
8
|
# ARCHITECTURE
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
---
|
|
2
|
+
keywords: [typescript data structures concepts, comparator, iterator protocol, generics, uniform API]
|
|
2
3
|
sidebar_label: "CONCEPTS"
|
|
3
4
|
description: "Core concepts behind data-structure-typed: uniform API, iterators, generics, comparators, and the 5 design traits."
|
|
4
5
|
---
|
|
6
|
+
keywords: [typescript data structures concepts, comparator, iterator protocol, generics, uniform API]
|
|
5
7
|
|
|
6
8
|
# CONCEPTS
|
|
7
9
|
|
|
@@ -10,6 +12,7 @@ This guide explains the foundational concepts behind data structures through pla
|
|
|
10
12
|
**👈 [Back to README](/.md) • [API Docs](https://data-structure-typed-docs.vercel.app/) • [Real-World Guides](/guide/guides.md)**
|
|
11
13
|
|
|
12
14
|
---
|
|
15
|
+
keywords: [typescript data structures concepts, comparator, iterator protocol, generics, uniform API]
|
|
13
16
|
|
|
14
17
|
## Table of Contents
|
|
15
18
|
|
|
@@ -22,6 +25,7 @@ This guide explains the foundational concepts behind data structures through pla
|
|
|
22
25
|
7. [Decision Guide](#-decision-guide-choose-the-right-data-structure)
|
|
23
26
|
|
|
24
27
|
---
|
|
28
|
+
keywords: [typescript data structures concepts, comparator, iterator protocol, generics, uniform API]
|
|
25
29
|
|
|
26
30
|
## The Big Three Concepts
|
|
27
31
|
|
|
@@ -78,6 +82,7 @@ A complete binary tree where parent always has priority over children.
|
|
|
78
82
|
**Perfect for**: Priority queues, heap sort
|
|
79
83
|
|
|
80
84
|
---
|
|
85
|
+
keywords: [typescript data structures concepts, comparator, iterator protocol, generics, uniform API]
|
|
81
86
|
|
|
82
87
|
## 🌍 Plain Language Explanations
|
|
83
88
|
|
|
@@ -103,6 +108,7 @@ For those who love understanding concepts through metaphors:
|
|
|
103
108
|
| **Matrix** | A 2D grid of numbers supporting standard linear algebra operations. | 2D grid transformations, linear algebra |
|
|
104
109
|
|
|
105
110
|
---
|
|
111
|
+
keywords: [typescript data structures concepts, comparator, iterator protocol, generics, uniform API]
|
|
106
112
|
|
|
107
113
|
## Iterator Protocol Design
|
|
108
114
|
|
|
@@ -164,6 +170,7 @@ for (const item of heap) console.log(item);
|
|
|
164
170
|
```
|
|
165
171
|
|
|
166
172
|
---
|
|
173
|
+
keywords: [typescript data structures concepts, comparator, iterator protocol, generics, uniform API]
|
|
167
174
|
|
|
168
175
|
## 🔗 Seamless Interoperability: Iterator Protocol Everywhere
|
|
169
176
|
|
|
@@ -179,6 +186,7 @@ Instead of forcing conversions between data structures, we made every structure
|
|
|
179
186
|
This is **zero friction** because you use the same mental model.
|
|
180
187
|
|
|
181
188
|
---
|
|
189
|
+
keywords: [typescript data structures concepts, comparator, iterator protocol, generics, uniform API]
|
|
182
190
|
|
|
183
191
|
## 🎁 All Array Methods Work Everywhere
|
|
184
192
|
|
|
@@ -250,6 +258,7 @@ const stats = {
|
|
|
250
258
|
| forEach | ✅ | ✅ | ✅ | ✅ | ✅ |
|
|
251
259
|
|
|
252
260
|
---
|
|
261
|
+
keywords: [typescript data structures concepts, comparator, iterator protocol, generics, uniform API]
|
|
253
262
|
|
|
254
263
|
## Why Not Just Use Native JavaScript?
|
|
255
264
|
|
|
@@ -332,7 +341,26 @@ const tree = new RedBlackTree(prices);
|
|
|
332
341
|
const inRange = tree.rangeSearch([30, 70]);
|
|
333
342
|
```
|
|
334
343
|
|
|
335
|
-
### Case 5:
|
|
344
|
+
### Case 5: Finding the K-th Element Requires Sorting
|
|
345
|
+
|
|
346
|
+
❌ Array: sort + index is O(n log n):
|
|
347
|
+
|
|
348
|
+
```javascript
|
|
349
|
+
const scores = [85, 92, 78, 95, 88, 100, 73];
|
|
350
|
+
scores.sort((a, b) => a - b);
|
|
351
|
+
const median = scores[Math.floor(scores.length / 2)]; // re-sort on every update
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
✅ Order-statistic tree: O(log n) getByRank/getRank with live updates:
|
|
355
|
+
|
|
356
|
+
```javascript
|
|
357
|
+
const tree = new RedBlackTree(scores, { enableOrderStatistic: true });
|
|
358
|
+
const median = tree.getByRank(Math.floor(tree.size / 2)); // O(log n)
|
|
359
|
+
const rank = tree.getRank(92); // "how many scores below 92?" — O(log n)
|
|
360
|
+
const top3 = tree.rangeByRank(tree.size - 3, tree.size - 1); // O(log n + 3)
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
### Case 6: Prefix Matching is Tedious
|
|
336
364
|
|
|
337
365
|
❌ Array.filter is O(n*m):
|
|
338
366
|
|
|
@@ -351,6 +379,7 @@ const matches = trie.getWords('appl');
|
|
|
351
379
|
```
|
|
352
380
|
|
|
353
381
|
---
|
|
382
|
+
keywords: [typescript data structures concepts, comparator, iterator protocol, generics, uniform API]
|
|
354
383
|
|
|
355
384
|
## 🎯 Decision Guide: Choose the Right Data Structure
|
|
356
385
|
|
|
@@ -394,6 +423,7 @@ Need a sorted key-value map?
|
|
|
394
423
|
```
|
|
395
424
|
|
|
396
425
|
---
|
|
426
|
+
keywords: [typescript data structures concepts, comparator, iterator protocol, generics, uniform API]
|
|
397
427
|
|
|
398
428
|
## Next Steps
|
|
399
429
|
|
|
@@ -410,6 +440,7 @@ Need a sorted key-value map?
|
|
|
410
440
|
→ [See architecture details](/guide/architecture.md)
|
|
411
441
|
|
|
412
442
|
---
|
|
443
|
+
keywords: [typescript data structures concepts, comparator, iterator protocol, generics, uniform API]
|
|
413
444
|
|
|
414
445
|
**Related:**
|
|
415
446
|
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: FAQ — Frequently Asked Questions
|
|
3
|
+
sidebar_label: "FAQ"description: Common questions about data-structure-typed — TreeMap in JavaScript, priority queues, rank queries, bundle size, and more.
|
|
4
|
+
keywords: [typescript data structures, treemap javascript, priority queue typescript, sorted set javascript, rank query, faq]
|
|
5
|
+
sidebar_position: 7
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
# FAQ
|
|
9
|
+
|
|
10
|
+
## Does JavaScript have a TreeMap or TreeSet?
|
|
11
|
+
|
|
12
|
+
Not natively. JavaScript's `Map` and `Set` are hash-based and 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.
|
|
13
|
+
|
|
14
|
+
```typescript
|
|
15
|
+
import { TreeMap } from 'data-structure-typed';
|
|
16
|
+
|
|
17
|
+
const map = new TreeMap<number, string>();
|
|
18
|
+
map.set(3, 'c');
|
|
19
|
+
map.set(1, 'a');
|
|
20
|
+
map.set(2, 'b');
|
|
21
|
+
|
|
22
|
+
// Sorted iteration (by key)
|
|
23
|
+
for (const [key, value] of map) {
|
|
24
|
+
console.log(key, value); // 1 'a', 2 'b', 3 'c'
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// NavigableMap operations
|
|
28
|
+
map.floor(2.5); // [2, 'b'] — largest key ≤ 2.5
|
|
29
|
+
map.ceiling(1.5); // [2, 'b'] — smallest key ≥ 1.5
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## When should I use a Heap instead of sorting an array?
|
|
33
|
+
|
|
34
|
+
When you need to repeatedly access the smallest or largest element. Sorting an array is O(n log n) every time you add an element. A Heap gives you O(log n) insert and O(1) access to the top element.
|
|
35
|
+
|
|
36
|
+
**Use Heap when:**
|
|
37
|
+
- Building a priority queue or task scheduler
|
|
38
|
+
- Finding top-k elements from a stream
|
|
39
|
+
- Implementing Dijkstra's algorithm
|
|
40
|
+
- Any scenario where you repeatedly need the min/max
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
import { MinHeap } from 'data-structure-typed';
|
|
44
|
+
|
|
45
|
+
const tasks = new MinHeap<number>([5, 1, 3, 7, 2]);
|
|
46
|
+
tasks.poll(); // 1 (O(log n), not O(n log n))
|
|
47
|
+
tasks.add(0);
|
|
48
|
+
tasks.peek(); // 0
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Does this library support rank and range queries?
|
|
52
|
+
|
|
53
|
+
Yes. Enable with `{ enableOrderStatistic: true }` on any tree-based structure:
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
import { RedBlackTree } from 'data-structure-typed';
|
|
57
|
+
|
|
58
|
+
const tree = new RedBlackTree<number>([10, 20, 30, 40, 50], {
|
|
59
|
+
enableOrderStatistic: true
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
tree.getRank(30); // 2 — two elements precede 30 in tree order
|
|
63
|
+
tree.getByRank(0); // 10 — first element in tree order
|
|
64
|
+
tree.rangeByRank(1, 3); // [20, 30, 40] — positions 1 through 3
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Works with `TreeMap`, `TreeSet`, `TreeMultiMap`, and `TreeMultiSet` too.
|
|
68
|
+
|
|
69
|
+
## Is it faster than native arrays for ordered operations?
|
|
70
|
+
|
|
71
|
+
For ordered insert + lookup: **yes, significantly**.
|
|
72
|
+
|
|
73
|
+
| Operation | Sorted Array | Red-Black Tree |
|
|
74
|
+
|-----------|-------------|----------------|
|
|
75
|
+
| Insert (maintain order) | O(n) | O(log n) |
|
|
76
|
+
| Find by key | O(log n) | O(log n) |
|
|
77
|
+
| Find min/max | O(1) | O(log n) |
|
|
78
|
+
| Delete by key | O(n) | O(log n) |
|
|
79
|
+
| Get kth element | O(1) | O(log n) |
|
|
80
|
+
|
|
81
|
+
For 10,000+ elements, the O(n) insert cost of arrays becomes a bottleneck. Trees maintain O(log n) regardless of size.
|
|
82
|
+
|
|
83
|
+
See [PERFORMANCE.md](https://github.com/zrwusa/data-structure-typed/blob/main/docs/PERFORMANCE.md) for benchmark results.
|
|
84
|
+
|
|
85
|
+
## Can I use this in React / Node.js / browser?
|
|
86
|
+
|
|
87
|
+
Yes. The library ships ESM, CJS, and UMD builds. It works in:
|
|
88
|
+
|
|
89
|
+
- **Node.js** (any version supporting ES2015+)
|
|
90
|
+
- **Browsers** (via bundler or UMD script tag)
|
|
91
|
+
- **React / Next.js / Vue / Angular** (import normally)
|
|
92
|
+
- **Deno / Bun** (ESM compatible)
|
|
93
|
+
|
|
94
|
+
Zero dependencies means no compatibility concerns.
|
|
95
|
+
|
|
96
|
+
## What data structures are included?
|
|
97
|
+
|
|
98
|
+
| Category | Structures |
|
|
99
|
+
|----------|-----------|
|
|
100
|
+
| Trees | RedBlackTree, AVLTree, BST, TreeMap, TreeSet, TreeMultiMap, TreeMultiSet |
|
|
101
|
+
| Heaps | Heap, MinHeap, MaxHeap, MinPriorityQueue, MaxPriorityQueue |
|
|
102
|
+
| Queues | Queue, Deque |
|
|
103
|
+
| Lists | SinglyLinkedList, DoublyLinkedList, SkipList |
|
|
104
|
+
| Hashing | HashMap |
|
|
105
|
+
| Graphs | DirectedGraph, UndirectedGraph |
|
|
106
|
+
| Strings | Trie |
|
|
107
|
+
| Arrays | SegmentTree, BinaryIndexedTree (Fenwick Tree), Matrix |
|
|
108
|
+
| Basic | Stack |
|
|
109
|
+
|
|
110
|
+
## Is this library production-ready?
|
|
111
|
+
|
|
112
|
+
Yes.
|
|
113
|
+
|
|
114
|
+
- **2600+ tests**, 99%+ code coverage
|
|
115
|
+
- **Zero dependencies**
|
|
116
|
+
- **Type-safe** — full TypeScript generics
|
|
117
|
+
- **Actively maintained** — regular releases
|
|
118
|
+
- Every release passes typecheck, lint, and full test suite via CI
|
|
119
|
+
|
|
120
|
+
## How does this compare to js-sdsl?
|
|
121
|
+
|
|
122
|
+
| Feature | data-structure-typed | js-sdsl |
|
|
123
|
+
|---------|---------------------|---------|
|
|
124
|
+
| Data structures | 20+ | ~6 |
|
|
125
|
+
| API style | Unified Array-like | Mixed |
|
|
126
|
+
| Order-statistic (getRank/getByRank) | ✅ | ❌ |
|
|
127
|
+
| Tree-shaking subpaths | ✅ | ❌ |
|
|
128
|
+
| Maintenance | Active (2026) | Inactive |
|
|
129
|
+
| Bundle (full) | ~143KB min | ~45KB min |
|
|
130
|
+
|
|
131
|
+
`data-structure-typed` is broader and more actively maintained. js-sdsl is smaller if you only need a few structures.
|
|
132
|
+
|
|
133
|
+
## What is the bundle size?
|
|
134
|
+
|
|
135
|
+
| Import | Size (ESM) |
|
|
136
|
+
|--------|-----------|
|
|
137
|
+
| Full bundle | 598KB |
|
|
138
|
+
| `data-structure-typed/binary-tree` | 315KB |
|
|
139
|
+
| `data-structure-typed/graph` | 127KB |
|
|
140
|
+
| `data-structure-typed/linked-list` | 93KB |
|
|
141
|
+
| `data-structure-typed/queue` | 91KB |
|
|
142
|
+
| `data-structure-typed/heap` | 36KB |
|
|
143
|
+
| `data-structure-typed/priority-queue` | 30KB |
|
|
144
|
+
| `data-structure-typed/hash` | 29KB |
|
|
145
|
+
| `data-structure-typed/matrix` | 28KB |
|
|
146
|
+
| `data-structure-typed/trie` | 27KB |
|
|
147
|
+
| `data-structure-typed/stack` | 18KB |
|
|
148
|
+
|
|
149
|
+
UMD bundle: ~143KB minified. `sideEffects: false` enables full tree-shaking with modern bundlers.
|
|
150
|
+
|
|
151
|
+
## How do I build a leaderboard with this library?
|
|
152
|
+
|
|
153
|
+
```typescript
|
|
154
|
+
import { TreeMap } from 'data-structure-typed';
|
|
155
|
+
|
|
156
|
+
const leaderboard = new TreeMap<number, string>(
|
|
157
|
+
[[100, 'Alice'], [250, 'Bob'], [180, 'Charlie']],
|
|
158
|
+
{ comparator: (a, b) => b - a, enableOrderStatistic: true }
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
// Top 3 players (descending score order)
|
|
162
|
+
leaderboard.rangeByRank(0, 2);
|
|
163
|
+
// → [[250, 'Bob'], [180, 'Charlie'], [100, 'Alice']]
|
|
164
|
+
|
|
165
|
+
// What rank is score 180?
|
|
166
|
+
leaderboard.getRank(180); // 1 (0-indexed, second position)
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
## How do I build autocomplete with a Trie?
|
|
170
|
+
|
|
171
|
+
```typescript
|
|
172
|
+
import { Trie } from 'data-structure-typed';
|
|
173
|
+
|
|
174
|
+
const trie = new Trie(['apple', 'app', 'application', 'banana', 'band']);
|
|
175
|
+
|
|
176
|
+
trie.getWords('app'); // ['app', 'apple', 'application']
|
|
177
|
+
trie.getWords('ban'); // ['banana', 'band']
|
|
178
|
+
trie.hasPrefix('app'); // true
|
|
179
|
+
trie.has('apple'); // true
|
|
180
|
+
```
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
---
|
|
2
2
|
sidebar_label: "GUIDES"
|
|
3
3
|
description: "Real-world examples: leaderboards with RedBlackTree, task scheduling with PriorityQueue, autocomplete with Trie, and more."
|
|
4
|
+
title: "Guides — Leaderboards, Scheduling, Autocomplete"
|
|
5
|
+
keywords: [typescript leaderboard, task scheduler priority queue, autocomplete trie, rank query, range query, real world examples]
|
|
4
6
|
---
|
|
5
7
|
|
|
6
8
|
# GUIDES
|
|
@@ -218,11 +220,11 @@ console.log(cache.get('a')); // 'value1', 'a' is now most recent
|
|
|
218
220
|
cache.set('d', 'value4'); // Evicts 'b' (least recent)
|
|
219
221
|
```
|
|
220
222
|
|
|
221
|
-
### Example 2: Real-Time Leaderboard
|
|
223
|
+
### Example 2: Real-Time Leaderboard (Order-Statistic Tree)
|
|
222
224
|
|
|
223
225
|
```typescript
|
|
224
226
|
import { RedBlackTree } from 'data-structure-typed';
|
|
225
|
-
|
|
227
|
+
|
|
226
228
|
interface Player {
|
|
227
229
|
id: string;
|
|
228
230
|
name: string;
|
|
@@ -230,76 +232,58 @@ interface Player {
|
|
|
230
232
|
}
|
|
231
233
|
|
|
232
234
|
class Leaderboard {
|
|
235
|
+
// enableOrderStatistic gives O(log n) getByRank/getRank/rangeByRank
|
|
233
236
|
private scores = new RedBlackTree<number, Player>(
|
|
234
|
-
|
|
237
|
+
[],
|
|
238
|
+
{ comparator: (a, b) => b - a, enableOrderStatistic: true }
|
|
235
239
|
);
|
|
236
240
|
private players = new Map<string, number>(); // playerId → currentScore
|
|
237
241
|
|
|
238
242
|
updateScore(player: Player): void {
|
|
239
|
-
// Remove old score if exists
|
|
240
243
|
if (this.players.has(player.id)) {
|
|
241
|
-
|
|
242
|
-
this.scores.delete(oldScore);
|
|
244
|
+
this.scores.delete(this.players.get(player.id)!);
|
|
243
245
|
}
|
|
244
|
-
|
|
245
|
-
// Add new score
|
|
246
246
|
this.scores.set(player.score, player);
|
|
247
247
|
this.players.set(player.id, player.score);
|
|
248
248
|
}
|
|
249
249
|
|
|
250
|
-
// O(k
|
|
250
|
+
// O(k) — select by rank, no array copy
|
|
251
251
|
getTopN(n: number): Player[] {
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
const player = this.scores.get(key);
|
|
256
|
-
if (player) result.push(player);
|
|
257
|
-
key = this.scores.higher(key); // next in tree order
|
|
258
|
-
}
|
|
259
|
-
return result;
|
|
252
|
+
return this.scores.rangeByRank(0, n - 1)
|
|
253
|
+
.map(key => key !== undefined ? this.scores.get(key) : undefined)
|
|
254
|
+
.filter((p): p is Player => p !== undefined);
|
|
260
255
|
}
|
|
261
256
|
|
|
262
|
-
// O(log n
|
|
257
|
+
// O(log n) — direct rank lookup
|
|
263
258
|
getRank(playerId: string): number {
|
|
264
259
|
if (!this.players.has(playerId)) return -1;
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
return rank;
|
|
260
|
+
return this.scores.getRank(this.players.get(playerId)!) + 1; // 1-based
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// O(log n + k) — get k-th player by rank
|
|
264
|
+
getPlayerAt(rank: number): Player | undefined {
|
|
265
|
+
const key = this.scores.getByRank(rank - 1); // 0-indexed internally
|
|
266
|
+
return key !== undefined ? this.scores.get(key) : undefined;
|
|
273
267
|
}
|
|
274
268
|
|
|
275
|
-
// O(log n + k) —
|
|
269
|
+
// O(log n + k) — players around a given player
|
|
276
270
|
getAroundMe(playerId: string, range: number): Player[] {
|
|
277
271
|
if (!this.players.has(playerId)) return [];
|
|
278
|
-
const
|
|
279
|
-
const
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
const p = this.scores.get(key);
|
|
286
|
-
if (p) above.unshift(p);
|
|
287
|
-
key = this.scores.lower(key);
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
// Me + `range` players below me
|
|
291
|
-
result.push(...above);
|
|
292
|
-
const me = this.scores.get(myScore);
|
|
293
|
-
if (me) result.push(me);
|
|
294
|
-
|
|
295
|
-
key = this.scores.higher(myScore);
|
|
296
|
-
for (let i = 0; i < range && key !== undefined; i++) {
|
|
297
|
-
const p = this.scores.get(key);
|
|
298
|
-
if (p) result.push(p);
|
|
299
|
-
key = this.scores.higher(key);
|
|
300
|
-
}
|
|
272
|
+
const myRank = this.scores.getRank(this.players.get(playerId)!);
|
|
273
|
+
const start = Math.max(0, myRank - range);
|
|
274
|
+
const end = Math.min(this.scores.size - 1, myRank + range);
|
|
275
|
+
return this.scores.rangeByRank(start, end)
|
|
276
|
+
.map(key => key !== undefined ? this.scores.get(key) : undefined)
|
|
277
|
+
.filter((p): p is Player => p !== undefined);
|
|
278
|
+
}
|
|
301
279
|
|
|
302
|
-
|
|
280
|
+
// Pagination: show page N of the leaderboard
|
|
281
|
+
getPage(page: number, pageSize: number): Player[] {
|
|
282
|
+
const start = (page - 1) * pageSize;
|
|
283
|
+
const end = start + pageSize - 1;
|
|
284
|
+
return this.scores.rangeByRank(start, end)
|
|
285
|
+
.map(key => key !== undefined ? this.scores.get(key) : undefined)
|
|
286
|
+
.filter((p): p is Player => p !== undefined);
|
|
303
287
|
}
|
|
304
288
|
}
|
|
305
289
|
|
|
@@ -309,9 +293,11 @@ lb.updateScore({ id: '1', name: 'Alice', score: 1000 });
|
|
|
309
293
|
lb.updateScore({ id: '2', name: 'Bob', score: 900 });
|
|
310
294
|
lb.updateScore({ id: '3', name: 'Charlie', score: 950 });
|
|
311
295
|
|
|
312
|
-
console.log(lb.getTopN(2));
|
|
313
|
-
console.log(lb.getRank('2'));
|
|
314
|
-
console.log(lb.
|
|
296
|
+
console.log(lb.getTopN(2)); // Alice, Charlie
|
|
297
|
+
console.log(lb.getRank('2')); // 3 (Bob is 3rd)
|
|
298
|
+
console.log(lb.getPlayerAt(1)); // Alice (1st place)
|
|
299
|
+
console.log(lb.getAroundMe('3', 1)); // [Alice, Charlie, Bob]
|
|
300
|
+
console.log(lb.getPage(1, 2)); // [Alice, Charlie] (page 1, 2 per page)
|
|
315
301
|
```
|
|
316
302
|
|
|
317
303
|
### Example 3: Message Queue with Priorities
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
description: "Install data-structure-typed via npm, yarn, or pnpm. Supports tree-shaking with subpath imports for minimal bundle size."
|
|
3
|
+
title: "Installation — npm, yarn, pnpm"
|
|
4
|
+
sidebar_label: "INSTALLATION"keywords: [data-structure-typed install, npm data structures, typescript library install, tree shaking, subpath imports]
|
|
3
5
|
---
|
|
4
6
|
|
|
5
7
|
# Installation
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
---
|
|
2
2
|
sidebar_label: "INTEGRATIONS"
|
|
3
3
|
description: "Integrate data-structure-typed with React, Express, NestJS, and other frameworks. Production-ready patterns."
|
|
4
|
+
title: "Framework Integrations — React, Express, NestJS"
|
|
5
|
+
keywords: [data-structure-typed react, typescript data structures express, nestjs data structures, production patterns]
|
|
4
6
|
---
|
|
5
7
|
|
|
6
8
|
# INTEGRATIONS
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
---
|
|
2
2
|
sidebar_label: "OVERVIEW"
|
|
3
3
|
description: "Complete overview of all 20+ data structures: trees, heaps, graphs, queues, linked lists, hash maps, and more."
|
|
4
|
+
title: "Data Structures Overview — Trees, Heaps, Graphs, Queues"
|
|
5
|
+
keywords: [typescript data structures overview, red black tree, heap, priority queue, trie, graph, deque, treemap, treeset]
|
|
4
6
|
---
|
|
5
7
|
|
|
6
8
|
# OVERVIEW
|
|
@@ -147,6 +149,11 @@ rbTree.print()
|
|
|
147
149
|
// / \
|
|
148
150
|
// 1 4
|
|
149
151
|
|
|
152
|
+
// Order-Statistic mode — O(log n) rank queries
|
|
153
|
+
const ost = new RedBlackTree<number, string>([[1, 'A'], [2, 'B'], [3, 'C']], { enableOrderStatistic: true });
|
|
154
|
+
ost.getByRank(0); // 1 (smallest key)
|
|
155
|
+
ost.getRank(2); // 1 (one element before key 2)
|
|
156
|
+
ost.rangeByRank(0, 1); // [1, 2] (first two elements)
|
|
150
157
|
```
|
|
151
158
|
|
|
152
159
|
#### AVL Tree
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
---
|
|
2
2
|
sidebar_label: "PERFORMANCE"
|
|
3
3
|
description: "Benchmarks comparing data-structure-typed vs native Arrays, Maps, and C++ implementations. Includes methodology."
|
|
4
|
+
title: "Performance Benchmarks"
|
|
5
|
+
keywords: [typescript data structures performance, red black tree benchmark, heap benchmark, priority queue performance, javascript vs cpp]
|
|
4
6
|
---
|
|
5
7
|
|
|
6
8
|
# PERFORMANCE
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
description: "Get started with data-structure-typed in 30 seconds. Create trees, queues, and maps with a familiar JavaScript API."
|
|
3
|
+
title: "Quick Start — Get Running in 30 Seconds"
|
|
4
|
+
sidebar_label: "QUICK START"keywords: [data-structure-typed tutorial, typescript data structures getting started, treemap example, heap example]
|
|
3
5
|
---
|
|
4
6
|
|
|
5
7
|
# Quick Start
|
|
@@ -32,6 +34,35 @@ leaderboard.set(95, 'Bob');
|
|
|
32
34
|
const scores90to100 = leaderboard.rangeSearch([90, 100]);
|
|
33
35
|
```
|
|
34
36
|
|
|
37
|
+
## Order-Statistic Tree (Rank Queries)
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import { RedBlackTree } from 'data-structure-typed';
|
|
41
|
+
|
|
42
|
+
// Enable order-statistic for O(log n) rank operations
|
|
43
|
+
const tree = new RedBlackTree<number, string>([
|
|
44
|
+
[100, 'Alice'],
|
|
45
|
+
[85, 'Bob'],
|
|
46
|
+
[92, 'Charlie'],
|
|
47
|
+
[78, 'Diana'],
|
|
48
|
+
[95, 'Eve']
|
|
49
|
+
], { comparator: (a, b) => b - a, enableOrderStatistic: true });
|
|
50
|
+
|
|
51
|
+
// select(k) — find k-th element (0-indexed)
|
|
52
|
+
console.log(tree.getByRank(0)); // 100 (1st in tree order)
|
|
53
|
+
console.log(tree.getByRank(2)); // 92 (3rd in tree order)
|
|
54
|
+
|
|
55
|
+
// rank(key) — how many elements before this key?
|
|
56
|
+
console.log(tree.getRank(92)); // 2 (2 elements before 92 in tree order)
|
|
57
|
+
|
|
58
|
+
// rangeByRank(start, end) — pagination
|
|
59
|
+
console.log(tree.rangeByRank(0, 2)); // [100, 95, 92] — top 3
|
|
60
|
+
|
|
61
|
+
// Inverse property: getByRank(getRank(key)) === key
|
|
62
|
+
const k = tree.getRank(85);
|
|
63
|
+
console.log(tree.getByRank(k)); // 85
|
|
64
|
+
```
|
|
65
|
+
|
|
35
66
|
## Task Queue (Scheduling)
|
|
36
67
|
|
|
37
68
|
```typescript
|