data-structure-typed 2.5.2 → 2.6.0
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/.husky/pre-commit +3 -0
- package/CHANGELOG.md +3 -1
- package/MIGRATION.md +217 -0
- package/README.md +80 -8
- package/README_CN.md +569 -143
- package/SPECIFICATION.md +44 -14
- package/SPECIFICATION.zh-CN.md +44 -14
- package/dist/cjs/binary-tree.cjs +5841 -1678
- package/dist/cjs/graph.cjs +422 -14
- package/dist/cjs/hash.cjs +95 -7
- package/dist/cjs/heap.cjs +174 -16
- package/dist/cjs/index.cjs +7751 -2449
- package/dist/cjs/linked-list.cjs +443 -2
- package/dist/cjs/matrix.cjs +56 -0
- package/dist/cjs/priority-queue.cjs +172 -14
- package/dist/cjs/queue.cjs +435 -0
- package/dist/cjs/stack.cjs +103 -4
- package/dist/cjs/trie.cjs +106 -0
- package/dist/cjs-legacy/binary-tree.cjs +5933 -1772
- package/dist/cjs-legacy/graph.cjs +422 -14
- package/dist/cjs-legacy/hash.cjs +95 -7
- package/dist/cjs-legacy/heap.cjs +174 -16
- package/dist/cjs-legacy/index.cjs +8154 -2854
- package/dist/cjs-legacy/linked-list.cjs +443 -2
- package/dist/cjs-legacy/matrix.cjs +56 -0
- package/dist/cjs-legacy/priority-queue.cjs +172 -14
- package/dist/cjs-legacy/queue.cjs +435 -0
- package/dist/cjs-legacy/stack.cjs +103 -4
- package/dist/cjs-legacy/trie.cjs +106 -0
- package/dist/esm/binary-tree.mjs +5841 -1678
- package/dist/esm/graph.mjs +422 -14
- package/dist/esm/hash.mjs +95 -7
- package/dist/esm/heap.mjs +174 -16
- package/dist/esm/index.mjs +7751 -2449
- package/dist/esm/linked-list.mjs +443 -2
- package/dist/esm/matrix.mjs +56 -0
- package/dist/esm/priority-queue.mjs +172 -14
- package/dist/esm/queue.mjs +435 -0
- package/dist/esm/stack.mjs +103 -4
- package/dist/esm/trie.mjs +106 -0
- package/dist/esm-legacy/binary-tree.mjs +5933 -1772
- package/dist/esm-legacy/graph.mjs +422 -14
- package/dist/esm-legacy/hash.mjs +95 -7
- package/dist/esm-legacy/heap.mjs +174 -16
- package/dist/esm-legacy/index.mjs +8154 -2854
- package/dist/esm-legacy/linked-list.mjs +443 -2
- package/dist/esm-legacy/matrix.mjs +56 -0
- package/dist/esm-legacy/priority-queue.mjs +172 -14
- package/dist/esm-legacy/queue.mjs +435 -0
- package/dist/esm-legacy/stack.mjs +103 -4
- package/dist/esm-legacy/trie.mjs +106 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +17 -0
- package/dist/types/data-structures/base/linear-base.d.ts +6 -0
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +86 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +98 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +191 -15
- package/dist/types/data-structures/binary-tree/bst.d.ts +171 -3
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +136 -8
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +42 -0
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +1061 -167
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1232 -355
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +916 -194
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +1078 -141
- package/dist/types/data-structures/graph/directed-graph.d.ts +70 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +63 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +84 -6
- package/dist/types/data-structures/heap/heap.d.ts +140 -12
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +150 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +106 -1
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +126 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +56 -0
- package/dist/types/data-structures/queue/deque.d.ts +171 -0
- package/dist/types/data-structures/queue/queue.d.ts +97 -0
- package/dist/types/data-structures/stack/stack.d.ts +72 -2
- package/dist/types/data-structures/trie/trie.d.ts +84 -0
- package/dist/types/interfaces/binary-tree.d.ts +2 -3
- package/dist/umd/data-structure-typed.js +7784 -2484
- package/dist/umd/data-structure-typed.min.js +4 -4
- package/docs-site-docusaurus/docs/api/classes/AVLTree.md +188 -200
- package/docs-site-docusaurus/docs/api/classes/AVLTreeNode.md +11 -11
- package/docs-site-docusaurus/docs/api/classes/AbstractGraph.md +62 -62
- package/docs-site-docusaurus/docs/api/classes/BST.md +183 -195
- 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 +143 -155
- package/docs-site-docusaurus/docs/api/classes/BinaryTreeNode.md +13 -13
- package/docs-site-docusaurus/docs/api/classes/Deque.md +99 -85
- package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +73 -73
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +100 -70
- 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 +38 -38
- 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 +23 -23
- package/docs-site-docusaurus/docs/api/classes/LinearBase.md +48 -48
- package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +52 -52
- package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +46 -42
- package/docs-site-docusaurus/docs/api/classes/LinkedListNode.md +6 -6
- package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +74 -74
- package/docs-site-docusaurus/docs/api/classes/MapGraph.md +73 -73
- 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 +111 -59
- package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +200 -212
- package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +10 -10
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +75 -75
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedListNode.md +6 -6
- package/docs-site-docusaurus/docs/api/classes/SkipList.md +37 -37
- package/docs-site-docusaurus/docs/api/classes/Stack.md +42 -42
- package/docs-site-docusaurus/docs/api/classes/TreeMap.md +107 -36
- package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +43 -43
- package/docs-site-docusaurus/docs/api/classes/TreeSet.md +106 -35
- package/docs-site-docusaurus/docs/api/classes/Trie.md +43 -43
- package/docs-site-docusaurus/docs/api/classes/TrieNode.md +8 -8
- package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +72 -72
- package/docs-site-docusaurus/docs/guide/architecture.md +75 -7
- package/docs-site-docusaurus/docs/guide/concepts.md +53 -34
- package/docs-site-docusaurus/docs/guide/faq.md +53 -0
- package/docs-site-docusaurus/docs/guide/guides.md +8 -9
- package/docs-site-docusaurus/docs/guide/integrations.md +74 -177
- package/docs-site-docusaurus/docs/guide/overview.md +131 -17
- package/docs-site-docusaurus/src/pages/index.tsx +4 -0
- package/docs-site-docusaurus/typedoc.json +1 -0
- package/jest.integration.config.js +1 -2
- package/package.json +10 -7
- package/src/data-structures/base/iterable-element-base.ts +32 -0
- package/src/data-structures/base/linear-base.ts +11 -0
- package/src/data-structures/binary-tree/avl-tree.ts +88 -5
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +98 -0
- package/src/data-structures/binary-tree/binary-tree.ts +242 -81
- package/src/data-structures/binary-tree/bst.ts +173 -7
- package/src/data-structures/binary-tree/red-black-tree.ts +139 -15
- package/src/data-structures/binary-tree/segment-tree.ts +42 -0
- package/src/data-structures/binary-tree/tree-map.ts +948 -36
- package/src/data-structures/binary-tree/tree-multi-map.ts +893 -13
- package/src/data-structures/binary-tree/tree-multi-set.ts +761 -33
- package/src/data-structures/binary-tree/tree-set.ts +1260 -251
- 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 +100 -12
- package/src/data-structures/heap/heap.ts +149 -19
- package/src/data-structures/linked-list/doubly-linked-list.ts +178 -2
- package/src/data-structures/linked-list/singly-linked-list.ts +106 -1
- package/src/data-structures/linked-list/skip-linked-list.ts +126 -0
- package/src/data-structures/matrix/matrix.ts +56 -0
- package/src/data-structures/queue/deque.ts +187 -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 +84 -0
- package/src/interfaces/binary-tree.ts +1 -9
- 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
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
---
|
|
2
2
|
sidebar_label: "ARCHITECTURE"
|
|
3
|
-
description: "
|
|
4
|
-
title: "Architecture —
|
|
5
|
-
keywords: [data
|
|
3
|
+
description: "Design philosophy, V8 JIT optimizations, self-balancing strategies, and internal architecture of data-structure-typed."
|
|
4
|
+
title: "Architecture — Design & Implementation Details"
|
|
5
|
+
keywords: ["data structures architecture", "red black tree implementation", "v8 optimization", "typescript library design"]
|
|
6
6
|
---
|
|
7
|
-
|
|
8
7
|
# ARCHITECTURE
|
|
9
8
|
|
|
10
9
|
Understand why this library is designed the way it is, and how it works internally.
|
|
11
10
|
|
|
12
|
-
**[Back to README](
|
|
11
|
+
**[Back to README](/docs/guide/quick-start) • [See Performance](/guide/performance.md) • [Real Examples](/guide/guides.md)**
|
|
13
12
|
|
|
14
13
|
---
|
|
15
14
|
|
|
@@ -485,7 +484,7 @@ class TreeStructure<K, V> {
|
|
|
485
484
|
return this; // Chain continues!
|
|
486
485
|
}
|
|
487
486
|
|
|
488
|
-
reduce(reducer: (acc:
|
|
487
|
+
reduce<A>(reducer: (acc: A, v: V, k: K) => A, init: A): A {
|
|
489
488
|
// ... reduce logic ...
|
|
490
489
|
return result; // Terminal operation
|
|
491
490
|
}
|
|
@@ -597,6 +596,74 @@ const objectTree = new RedBlackTree<CustomObject>([], {
|
|
|
597
596
|
|
|
598
597
|
---
|
|
599
598
|
|
|
599
|
+
## Order-Statistic Tree Architecture
|
|
600
|
+
|
|
601
|
+
### How `enableOrderStatistic` Works
|
|
602
|
+
|
|
603
|
+
When enabled, every node maintains a `_count` field — the number of nodes in its subtree (including itself).
|
|
604
|
+
|
|
605
|
+
```
|
|
606
|
+
8 (count=5)
|
|
607
|
+
/ \
|
|
608
|
+
3 10 (count=1)
|
|
609
|
+
(count=3)
|
|
610
|
+
/ \
|
|
611
|
+
1 6 (count=1)
|
|
612
|
+
(count=1)
|
|
613
|
+
```
|
|
614
|
+
|
|
615
|
+
### Count Maintenance
|
|
616
|
+
|
|
617
|
+
Counts are updated in every mutation path:
|
|
618
|
+
|
|
619
|
+
- **Insert** (`set`/`add`): `_updateCountAlongPath` increments counts from new node to root
|
|
620
|
+
- **Delete**: `_updateCountAlongPath` decrements counts from deleted position to root
|
|
621
|
+
- **Rotations** (AVL: `_balanceLL/LR/RR/RL`, RBT: `_leftRotate/_rightRotate`): `_updateCount` recalculates after structural changes
|
|
622
|
+
- **Balanced rebuild** (`setMany`, `perfectlyBalance`): counts rebuilt during tree construction
|
|
623
|
+
|
|
624
|
+
### Rank Operations
|
|
625
|
+
|
|
626
|
+
All O(log n) by walking down the tree using counts:
|
|
627
|
+
|
|
628
|
+
- **`getByRank(k)`**: Start at root. If left subtree count > k, go left. If equal, return current. Otherwise subtract and go right.
|
|
629
|
+
- **`getRank(key)`**: Walk to key, accumulating left subtree counts. Returns count of elements preceding key in tree order.
|
|
630
|
+
- **`rangeByRank(start, end)`**: Combine `getByRank` to find boundaries, then in-order collect.
|
|
631
|
+
|
|
632
|
+
### Opt-in Design
|
|
633
|
+
|
|
634
|
+
Order-statistic is **opt-in** (`enableOrderStatistic: true`) because:
|
|
635
|
+
- Extra count maintenance on every mutation adds O(log n) overhead
|
|
636
|
+
- Most users don't need rank queries
|
|
637
|
+
- `_snapshotOptions` preserves the flag through `clone()` and `map()`
|
|
638
|
+
|
|
639
|
+
---
|
|
640
|
+
|
|
641
|
+
## Error Handling: `raise()`
|
|
642
|
+
|
|
643
|
+
### Unified Error Strategy
|
|
644
|
+
|
|
645
|
+
All error throwing goes through `raise(ErrorType, message)` in `src/common/error.ts`:
|
|
646
|
+
|
|
647
|
+
```typescript
|
|
648
|
+
raise(TypeError, ERR.comparatorRequired('TreeMap'));
|
|
649
|
+
raise(RangeError, ERR.indexOutOfBounds(index, length));
|
|
650
|
+
```
|
|
651
|
+
|
|
652
|
+
### Why `raise()` Instead of Direct `throw`
|
|
653
|
+
|
|
654
|
+
1. **Single chokepoint** — all errors flow through one function for consistent behavior
|
|
655
|
+
2. **Error message templates** — `ERR` object provides standardized, reusable messages
|
|
656
|
+
3. **Future extensibility** — can add logging, telemetry, or error transformation without touching call sites
|
|
657
|
+
|
|
658
|
+
### Error Categories
|
|
659
|
+
|
|
660
|
+
| Error Type | When | Example |
|
|
661
|
+
|------------|------|---------|
|
|
662
|
+
| `TypeError` | Invalid input type | NaN key, missing comparator, non-function callback |
|
|
663
|
+
| `RangeError` | Out of bounds | Index beyond array length, invalid rank |
|
|
664
|
+
|
|
665
|
+
---
|
|
666
|
+
|
|
600
667
|
## Summary: Design Checklist
|
|
601
668
|
|
|
602
669
|
- ✅ Unified API across all structures
|
|
@@ -606,7 +673,8 @@ const objectTree = new RedBlackTree<CustomObject>([], {
|
|
|
606
673
|
- ✅ V8 JIT-friendly code
|
|
607
674
|
- ✅ Memory-efficient algorithms
|
|
608
675
|
- ✅ Full TypeScript support
|
|
609
|
-
- ✅
|
|
676
|
+
- ✅ Order-statistic tree with opt-in `enableOrderStatistic`
|
|
677
|
+
- ✅ Unified error handling via `raise()` + `ERR` templates
|
|
610
678
|
|
|
611
679
|
---
|
|
612
680
|
|
|
@@ -1,18 +1,16 @@
|
|
|
1
1
|
---
|
|
2
|
-
keywords: [typescript data structures concepts, comparator, iterator protocol, generics, uniform API]
|
|
3
2
|
sidebar_label: "CONCEPTS"
|
|
4
|
-
description: "Core
|
|
3
|
+
description: "Core fundamentals and theory behind data-structure-typed. BST, balanced trees, heap, iterator protocol, and decision guide."
|
|
4
|
+
title: "Concepts — Core Fundamentals & Theory"
|
|
5
|
+
keywords: ["data structures concepts", "binary search tree", "balanced tree", "heap theory", "typescript data structures"]
|
|
5
6
|
---
|
|
6
|
-
keywords: [typescript data structures concepts, comparator, iterator protocol, generics, uniform API]
|
|
7
|
-
|
|
8
7
|
# CONCEPTS
|
|
9
8
|
|
|
10
9
|
This guide explains the foundational concepts behind data structures through plain language and practical understanding.
|
|
11
10
|
|
|
12
|
-
**👈 [Back to README](
|
|
11
|
+
**👈 [Back to README](/docs/guide/quick-start) • [API Docs](https://data-structure-typed-docs.vercel.app/) • [Real-World Guides](/guide/guides.md)**
|
|
13
12
|
|
|
14
13
|
---
|
|
15
|
-
keywords: [typescript data structures concepts, comparator, iterator protocol, generics, uniform API]
|
|
16
14
|
|
|
17
15
|
## Table of Contents
|
|
18
16
|
|
|
@@ -25,7 +23,6 @@ keywords: [typescript data structures concepts, comparator, iterator protocol, g
|
|
|
25
23
|
7. [Decision Guide](#-decision-guide-choose-the-right-data-structure)
|
|
26
24
|
|
|
27
25
|
---
|
|
28
|
-
keywords: [typescript data structures concepts, comparator, iterator protocol, generics, uniform API]
|
|
29
26
|
|
|
30
27
|
## The Big Three Concepts
|
|
31
28
|
|
|
@@ -82,7 +79,6 @@ A complete binary tree where parent always has priority over children.
|
|
|
82
79
|
**Perfect for**: Priority queues, heap sort
|
|
83
80
|
|
|
84
81
|
---
|
|
85
|
-
keywords: [typescript data structures concepts, comparator, iterator protocol, generics, uniform API]
|
|
86
82
|
|
|
87
83
|
## 🌍 Plain Language Explanations
|
|
88
84
|
|
|
@@ -108,7 +104,6 @@ For those who love understanding concepts through metaphors:
|
|
|
108
104
|
| **Matrix** | A 2D grid of numbers supporting standard linear algebra operations. | 2D grid transformations, linear algebra |
|
|
109
105
|
|
|
110
106
|
---
|
|
111
|
-
keywords: [typescript data structures concepts, comparator, iterator protocol, generics, uniform API]
|
|
112
107
|
|
|
113
108
|
## Iterator Protocol Design
|
|
114
109
|
|
|
@@ -170,7 +165,6 @@ for (const item of heap) console.log(item);
|
|
|
170
165
|
```
|
|
171
166
|
|
|
172
167
|
---
|
|
173
|
-
keywords: [typescript data structures concepts, comparator, iterator protocol, generics, uniform API]
|
|
174
168
|
|
|
175
169
|
## 🔗 Seamless Interoperability: Iterator Protocol Everywhere
|
|
176
170
|
|
|
@@ -186,7 +180,6 @@ Instead of forcing conversions between data structures, we made every structure
|
|
|
186
180
|
This is **zero friction** because you use the same mental model.
|
|
187
181
|
|
|
188
182
|
---
|
|
189
|
-
keywords: [typescript data structures concepts, comparator, iterator protocol, generics, uniform API]
|
|
190
183
|
|
|
191
184
|
## 🎁 All Array Methods Work Everywhere
|
|
192
185
|
|
|
@@ -258,7 +251,6 @@ const stats = {
|
|
|
258
251
|
| forEach | ✅ | ✅ | ✅ | ✅ | ✅ |
|
|
259
252
|
|
|
260
253
|
---
|
|
261
|
-
keywords: [typescript data structures concepts, comparator, iterator protocol, generics, uniform API]
|
|
262
254
|
|
|
263
255
|
## Why Not Just Use Native JavaScript?
|
|
264
256
|
|
|
@@ -341,45 +333,68 @@ const tree = new RedBlackTree(prices);
|
|
|
341
333
|
const inRange = tree.rangeSearch([30, 70]);
|
|
342
334
|
```
|
|
343
335
|
|
|
344
|
-
### Case 5:
|
|
336
|
+
### Case 5: Prefix Matching is Tedious
|
|
345
337
|
|
|
346
|
-
❌ Array
|
|
338
|
+
❌ Array.filter is O(n*m):
|
|
347
339
|
|
|
348
340
|
```javascript
|
|
349
|
-
const
|
|
350
|
-
|
|
351
|
-
|
|
341
|
+
const words = ['apple', 'app', 'apply', 'application'];
|
|
342
|
+
const matches = words.filter(w => w.startsWith('app'));
|
|
343
|
+
// For 1M words: checks 1M words ❌
|
|
352
344
|
```
|
|
353
345
|
|
|
354
|
-
✅
|
|
346
|
+
✅ Trie prefix matching is O(m + k):
|
|
355
347
|
|
|
356
348
|
```javascript
|
|
357
|
-
const
|
|
358
|
-
const
|
|
359
|
-
|
|
360
|
-
const top3 = tree.rangeByRank(tree.size - 3, tree.size - 1); // O(log n + 3)
|
|
349
|
+
const trie = new Trie(words);
|
|
350
|
+
const matches = trie.getWords('appl');
|
|
351
|
+
// O(5 + 4) = 9 operations ✅
|
|
361
352
|
```
|
|
362
353
|
|
|
363
|
-
### Case 6:
|
|
354
|
+
### Case 6: Finding the K-th Element or Rank
|
|
364
355
|
|
|
365
|
-
❌ Array
|
|
356
|
+
❌ Array requires sorting O(n log n):
|
|
366
357
|
|
|
367
358
|
```javascript
|
|
368
|
-
const
|
|
369
|
-
|
|
370
|
-
|
|
359
|
+
const scores = [85, 92, 78, 95, 88, 73, 99];
|
|
360
|
+
scores.sort((a, b) => b - a);
|
|
361
|
+
const thirdPlace = scores[2]; // Must re-sort after every insert
|
|
371
362
|
```
|
|
372
363
|
|
|
373
|
-
✅
|
|
364
|
+
✅ Order-Statistic Tree gives O(log n) rank queries:
|
|
374
365
|
|
|
375
366
|
```javascript
|
|
376
|
-
const
|
|
377
|
-
|
|
378
|
-
|
|
367
|
+
const tree = new RedBlackTree(scores.map(s => [s, null]), {
|
|
368
|
+
comparator: (a, b) => b - a,
|
|
369
|
+
enableOrderStatistic: true
|
|
370
|
+
});
|
|
371
|
+
tree.getByRank(2); // 3rd place — O(log n)
|
|
372
|
+
tree.getRank(92); // How many scores are higher? — O(log n)
|
|
373
|
+
tree.rangeByRank(0, 2); // Top 3 scores — O(log n + k)
|
|
374
|
+
// Insert new score — O(log n), no re-sorting needed
|
|
375
|
+
tree.set(91, null);
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
### Case 7: Pass Raw Objects Without Pre-Processing
|
|
379
|
+
|
|
380
|
+
❌ Array.map just to reshape data:
|
|
381
|
+
|
|
382
|
+
```javascript
|
|
383
|
+
const users = [{ id: 3, name: 'Charlie' }, { id: 1, name: 'Alice' }];
|
|
384
|
+
const entries = users.map(u => [u.id, u]); // Extra step
|
|
385
|
+
const map = new Map(entries);
|
|
386
|
+
```
|
|
387
|
+
|
|
388
|
+
✅ Pass raw objects directly with `toEntryFn`:
|
|
389
|
+
|
|
390
|
+
```javascript
|
|
391
|
+
const map = new TreeMap(users, {
|
|
392
|
+
toEntryFn: u => [u.id, u]
|
|
393
|
+
});
|
|
394
|
+
// No .map() needed — sorted by id automatically
|
|
379
395
|
```
|
|
380
396
|
|
|
381
397
|
---
|
|
382
|
-
keywords: [typescript data structures concepts, comparator, iterator protocol, generics, uniform API]
|
|
383
398
|
|
|
384
399
|
## 🎯 Decision Guide: Choose the Right Data Structure
|
|
385
400
|
|
|
@@ -415,6 +430,12 @@ Need range queries on an indexed sequence?
|
|
|
415
430
|
Only need prefix sums? → BinaryIndexedTree (simpler, less memory)
|
|
416
431
|
No → Continue
|
|
417
432
|
|
|
433
|
+
Need k-th element or rank queries?
|
|
434
|
+
↓
|
|
435
|
+
Yes → RedBlackTree / TreeMap / TreeSet with { enableOrderStatistic: true }
|
|
436
|
+
getByRank(k), getRank(key), rangeByRank(start, end) — all O(log n)
|
|
437
|
+
No → Continue
|
|
438
|
+
|
|
418
439
|
Need a sorted key-value map?
|
|
419
440
|
↓
|
|
420
441
|
Yes → TreeMap (guaranteed O(log n) via Red-Black Tree)
|
|
@@ -423,7 +444,6 @@ Need a sorted key-value map?
|
|
|
423
444
|
```
|
|
424
445
|
|
|
425
446
|
---
|
|
426
|
-
keywords: [typescript data structures concepts, comparator, iterator protocol, generics, uniform API]
|
|
427
447
|
|
|
428
448
|
## Next Steps
|
|
429
449
|
|
|
@@ -440,7 +460,6 @@ keywords: [typescript data structures concepts, comparator, iterator protocol, g
|
|
|
440
460
|
→ [See architecture details](/guide/architecture.md)
|
|
441
461
|
|
|
442
462
|
---
|
|
443
|
-
keywords: [typescript data structures concepts, comparator, iterator protocol, generics, uniform API]
|
|
444
463
|
|
|
445
464
|
**Related:**
|
|
446
465
|
|
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
---
|
|
2
|
+
sidebar_label: "FAQ"
|
|
3
|
+
sidebar_position: 7
|
|
4
|
+
description: "Frequently asked questions about data-structure-typed."
|
|
5
|
+
title: "FAQ — Frequently Asked Questions"
|
|
6
|
+
keywords: ["data-structure-typed faq", "typescript data structures questions"]
|
|
7
|
+
---
|
|
8
|
+
# FAQ
|
|
9
|
+
|
|
1
10
|
---
|
|
2
11
|
title: FAQ — Frequently Asked Questions
|
|
3
12
|
sidebar_label: "FAQ"description: Common questions about data-structure-typed — TreeMap in JavaScript, priority queues, rank queries, bundle size, and more.
|
|
@@ -148,6 +157,50 @@ Yes.
|
|
|
148
157
|
|
|
149
158
|
UMD bundle: ~143KB minified. `sideEffects: false` enables full tree-shaking with modern bundlers.
|
|
150
159
|
|
|
160
|
+
## Can I pass raw data without converting it first?
|
|
161
|
+
|
|
162
|
+
Yes. Three patterns depending on what you want to store:
|
|
163
|
+
|
|
164
|
+
```typescript
|
|
165
|
+
interface User {
|
|
166
|
+
id: number;
|
|
167
|
+
name: string;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const users: User[] = [
|
|
171
|
+
{ id: 3, name: 'Charlie' },
|
|
172
|
+
{ id: 1, name: 'Alice' },
|
|
173
|
+
{ id: 2, name: 'Bob' }
|
|
174
|
+
];
|
|
175
|
+
|
|
176
|
+
// 1. Extract a field — only that field is stored
|
|
177
|
+
const ids = new TreeSet<number, User>(
|
|
178
|
+
users,
|
|
179
|
+
{ toElementFn: u => u.id }
|
|
180
|
+
);
|
|
181
|
+
// [1, 2, 3]
|
|
182
|
+
|
|
183
|
+
// 2. Store full objects — sort by a field (raw data preserved!)
|
|
184
|
+
const fullSet = new TreeSet<User>(
|
|
185
|
+
users,
|
|
186
|
+
{ comparator: (a, b) => a.id - b.id }
|
|
187
|
+
);
|
|
188
|
+
// [{ id: 1, name: 'Alice' }, { id: 2, ... }, { id: 3, ... }]
|
|
189
|
+
|
|
190
|
+
// 3. Split into key-value — key for lookup, full object as value
|
|
191
|
+
const map = new TreeMap<number, User, User>(
|
|
192
|
+
users,
|
|
193
|
+
{ toEntryFn: u => [u.id, u] }
|
|
194
|
+
);
|
|
195
|
+
// map.get(1) → { id: 1, name: 'Alice' }
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
| I want to... | Use |
|
|
199
|
+
|---|---|
|
|
200
|
+
| Store only IDs/scores/prices | `toElementFn` |
|
|
201
|
+
| Store full objects, sorted by a field | `comparator` |
|
|
202
|
+
| Look up full objects by a key | `toEntryFn` |
|
|
203
|
+
|
|
151
204
|
## How do I build a leaderboard with this library?
|
|
152
205
|
|
|
153
206
|
```typescript
|
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
---
|
|
2
2
|
sidebar_label: "GUIDES"
|
|
3
|
-
description: "Real-world examples
|
|
4
|
-
title: "Guides —
|
|
5
|
-
keywords: [
|
|
3
|
+
description: "Real-world examples and production patterns for data-structure-typed."
|
|
4
|
+
title: "Guides — Real-World Examples & Production Patterns"
|
|
5
|
+
keywords: ["data-structure-typed examples", "typescript data structures patterns", "production code examples"]
|
|
6
6
|
---
|
|
7
|
-
|
|
8
7
|
# GUIDES
|
|
9
8
|
|
|
10
9
|
Production-ready code examples for common use cases. Learn by doing.
|
|
11
10
|
|
|
12
|
-
**[Back to README](
|
|
11
|
+
**[Back to README](/docs/guide/quick-start) • [API Docs](https://data-structure-typed-docs.vercel.app/) • [See INTEGRATIONS](/guide/integrations.md)**
|
|
13
12
|
|
|
14
13
|
---
|
|
15
14
|
|
|
@@ -148,7 +147,7 @@ If you insert keys in sorted or nearly-sorted order (timestamps, auto-increment
|
|
|
148
147
|
import { RedBlackTree } from 'data-structure-typed';
|
|
149
148
|
import type { RedBlackTreeNode } from 'data-structure-typed';
|
|
150
149
|
|
|
151
|
-
const tree = new RedBlackTree<number, number>(
|
|
150
|
+
const tree = new RedBlackTree<number, number>();
|
|
152
151
|
|
|
153
152
|
let hint: RedBlackTreeNode<number, number> | undefined;
|
|
154
153
|
for (let i = 0; i < 1_000_000; i++) {
|
|
@@ -172,7 +171,7 @@ Notes:
|
|
|
172
171
|
import { DoublyLinkedList } from 'data-structure-typed';
|
|
173
172
|
|
|
174
173
|
class LRUCache<K, V> {
|
|
175
|
-
private cache = new Map<K, { value: V; node:
|
|
174
|
+
private cache = new Map<K, { value: V; node: DoublyLinkedList<K> }>();
|
|
176
175
|
private order = new DoublyLinkedList<K>();
|
|
177
176
|
private readonly capacity: number;
|
|
178
177
|
|
|
@@ -220,7 +219,7 @@ console.log(cache.get('a')); // 'value1', 'a' is now most recent
|
|
|
220
219
|
cache.set('d', 'value4'); // Evicts 'b' (least recent)
|
|
221
220
|
```
|
|
222
221
|
|
|
223
|
-
### Example 2: Real-Time Leaderboard
|
|
222
|
+
### Example 2: Real-Time Leaderboard
|
|
224
223
|
|
|
225
224
|
```typescript
|
|
226
225
|
import { RedBlackTree } from 'data-structure-typed';
|
|
@@ -260,7 +259,7 @@ class Leaderboard {
|
|
|
260
259
|
return this.scores.getRank(this.players.get(playerId)!) + 1; // 1-based
|
|
261
260
|
}
|
|
262
261
|
|
|
263
|
-
// O(log n
|
|
262
|
+
// O(log n) — get k-th player by rank
|
|
264
263
|
getPlayerAt(rank: number): Player | undefined {
|
|
265
264
|
const key = this.scores.getByRank(rank - 1); // 0-indexed internally
|
|
266
265
|
return key !== undefined ? this.scores.get(key) : undefined;
|