data-structure-typed 2.5.2 → 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.
Files changed (156) hide show
  1. package/CHANGELOG.md +3 -1
  2. package/MIGRATION.md +169 -0
  3. package/README.md +60 -6
  4. package/README_CN.md +551 -143
  5. package/SPECIFICATION.md +20 -14
  6. package/SPECIFICATION.zh-CN.md +20 -14
  7. package/dist/cjs/binary-tree.cjs +2417 -132
  8. package/dist/cjs/graph.cjs +248 -14
  9. package/dist/cjs/hash.cjs +62 -7
  10. package/dist/cjs/heap.cjs +103 -16
  11. package/dist/cjs/index.cjs +3046 -124
  12. package/dist/cjs/linked-list.cjs +219 -0
  13. package/dist/cjs/matrix.cjs +32 -0
  14. package/dist/cjs/priority-queue.cjs +101 -14
  15. package/dist/cjs/queue.cjs +215 -0
  16. package/dist/cjs/stack.cjs +44 -4
  17. package/dist/cjs/trie.cjs +44 -0
  18. package/dist/cjs-legacy/binary-tree.cjs +2406 -123
  19. package/dist/cjs-legacy/graph.cjs +248 -14
  20. package/dist/cjs-legacy/hash.cjs +62 -7
  21. package/dist/cjs-legacy/heap.cjs +103 -16
  22. package/dist/cjs-legacy/index.cjs +3105 -185
  23. package/dist/cjs-legacy/linked-list.cjs +219 -0
  24. package/dist/cjs-legacy/matrix.cjs +32 -0
  25. package/dist/cjs-legacy/priority-queue.cjs +101 -14
  26. package/dist/cjs-legacy/queue.cjs +215 -0
  27. package/dist/cjs-legacy/stack.cjs +44 -4
  28. package/dist/cjs-legacy/trie.cjs +44 -0
  29. package/dist/esm/binary-tree.mjs +2417 -132
  30. package/dist/esm/graph.mjs +248 -14
  31. package/dist/esm/hash.mjs +62 -7
  32. package/dist/esm/heap.mjs +103 -16
  33. package/dist/esm/index.mjs +3046 -124
  34. package/dist/esm/linked-list.mjs +219 -0
  35. package/dist/esm/matrix.mjs +32 -0
  36. package/dist/esm/priority-queue.mjs +101 -14
  37. package/dist/esm/queue.mjs +215 -0
  38. package/dist/esm/stack.mjs +44 -4
  39. package/dist/esm/trie.mjs +44 -0
  40. package/dist/esm-legacy/binary-tree.mjs +2406 -123
  41. package/dist/esm-legacy/graph.mjs +248 -14
  42. package/dist/esm-legacy/hash.mjs +62 -7
  43. package/dist/esm-legacy/heap.mjs +103 -16
  44. package/dist/esm-legacy/index.mjs +3105 -185
  45. package/dist/esm-legacy/linked-list.mjs +219 -0
  46. package/dist/esm-legacy/matrix.mjs +32 -0
  47. package/dist/esm-legacy/priority-queue.mjs +101 -14
  48. package/dist/esm-legacy/queue.mjs +215 -0
  49. package/dist/esm-legacy/stack.mjs +44 -4
  50. package/dist/esm-legacy/trie.mjs +44 -0
  51. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +50 -2
  52. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +56 -0
  53. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +116 -15
  54. package/dist/types/data-structures/binary-tree/bst.d.ts +99 -3
  55. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +79 -8
  56. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +24 -0
  57. package/dist/types/data-structures/binary-tree/tree-map.d.ts +520 -1
  58. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +489 -1
  59. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +393 -1
  60. package/dist/types/data-structures/binary-tree/tree-set.d.ts +500 -1
  61. package/dist/types/data-structures/graph/directed-graph.d.ts +40 -0
  62. package/dist/types/data-structures/graph/undirected-graph.d.ts +36 -0
  63. package/dist/types/data-structures/hash/hash-map.d.ts +51 -6
  64. package/dist/types/data-structures/heap/heap.d.ts +98 -12
  65. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +75 -0
  66. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +61 -1
  67. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +72 -0
  68. package/dist/types/data-structures/matrix/matrix.d.ts +32 -0
  69. package/dist/types/data-structures/queue/deque.d.ts +82 -0
  70. package/dist/types/data-structures/queue/queue.d.ts +61 -0
  71. package/dist/types/data-structures/stack/stack.d.ts +42 -2
  72. package/dist/types/data-structures/trie/trie.d.ts +48 -0
  73. package/dist/types/interfaces/binary-tree.d.ts +2 -3
  74. package/dist/umd/data-structure-typed.js +3105 -185
  75. package/dist/umd/data-structure-typed.min.js +4 -4
  76. package/docs-site-docusaurus/docs/api/classes/AVLTree.md +188 -200
  77. package/docs-site-docusaurus/docs/api/classes/AVLTreeNode.md +11 -11
  78. package/docs-site-docusaurus/docs/api/classes/AbstractGraph.md +62 -62
  79. package/docs-site-docusaurus/docs/api/classes/BST.md +183 -195
  80. package/docs-site-docusaurus/docs/api/classes/BSTNode.md +13 -13
  81. package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +15 -15
  82. package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +143 -155
  83. package/docs-site-docusaurus/docs/api/classes/BinaryTreeNode.md +13 -13
  84. package/docs-site-docusaurus/docs/api/classes/Deque.md +99 -85
  85. package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +73 -73
  86. package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +100 -70
  87. package/docs-site-docusaurus/docs/api/classes/DoublyLinkedListNode.md +8 -8
  88. package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +12 -12
  89. package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +1 -1
  90. package/docs-site-docusaurus/docs/api/classes/HashMap.md +38 -38
  91. package/docs-site-docusaurus/docs/api/classes/Heap.md +96 -85
  92. package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +25 -25
  93. package/docs-site-docusaurus/docs/api/classes/IterableEntryBase.md +23 -23
  94. package/docs-site-docusaurus/docs/api/classes/LinearBase.md +48 -48
  95. package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +52 -52
  96. package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +42 -42
  97. package/docs-site-docusaurus/docs/api/classes/LinkedListNode.md +6 -6
  98. package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +74 -74
  99. package/docs-site-docusaurus/docs/api/classes/MapGraph.md +73 -73
  100. package/docs-site-docusaurus/docs/api/classes/Matrix.md +31 -31
  101. package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +104 -89
  102. package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +104 -89
  103. package/docs-site-docusaurus/docs/api/classes/MinHeap.md +104 -89
  104. package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +104 -89
  105. package/docs-site-docusaurus/docs/api/classes/Navigator.md +5 -5
  106. package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +103 -88
  107. package/docs-site-docusaurus/docs/api/classes/Queue.md +111 -59
  108. package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +200 -212
  109. package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +10 -10
  110. package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +75 -75
  111. package/docs-site-docusaurus/docs/api/classes/SinglyLinkedListNode.md +6 -6
  112. package/docs-site-docusaurus/docs/api/classes/SkipList.md +37 -37
  113. package/docs-site-docusaurus/docs/api/classes/Stack.md +42 -42
  114. package/docs-site-docusaurus/docs/api/classes/TreeMap.md +107 -36
  115. package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +43 -43
  116. package/docs-site-docusaurus/docs/api/classes/TreeSet.md +106 -35
  117. package/docs-site-docusaurus/docs/api/classes/Trie.md +43 -43
  118. package/docs-site-docusaurus/docs/api/classes/TrieNode.md +8 -8
  119. package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +72 -72
  120. package/docs-site-docusaurus/docs/guide/architecture.md +75 -7
  121. package/docs-site-docusaurus/docs/guide/concepts.md +53 -34
  122. package/docs-site-docusaurus/docs/guide/faq.md +53 -0
  123. package/docs-site-docusaurus/docs/guide/guides.md +8 -9
  124. package/docs-site-docusaurus/docs/guide/integrations.md +74 -177
  125. package/docs-site-docusaurus/docs/guide/overview.md +131 -17
  126. package/docs-site-docusaurus/src/pages/index.tsx +4 -0
  127. package/docs-site-docusaurus/typedoc.json +1 -0
  128. package/package.json +7 -6
  129. package/src/data-structures/binary-tree/avl-tree.ts +52 -5
  130. package/src/data-structures/binary-tree/binary-indexed-tree.ts +56 -0
  131. package/src/data-structures/binary-tree/binary-tree.ts +167 -81
  132. package/src/data-structures/binary-tree/bst.ts +101 -7
  133. package/src/data-structures/binary-tree/red-black-tree.ts +82 -15
  134. package/src/data-structures/binary-tree/segment-tree.ts +24 -0
  135. package/src/data-structures/binary-tree/tree-map.ts +540 -3
  136. package/src/data-structures/binary-tree/tree-multi-map.ts +490 -2
  137. package/src/data-structures/binary-tree/tree-multi-set.ts +393 -1
  138. package/src/data-structures/binary-tree/tree-set.ts +520 -3
  139. package/src/data-structures/graph/directed-graph.ts +41 -1
  140. package/src/data-structures/graph/undirected-graph.ts +37 -1
  141. package/src/data-structures/hash/hash-map.ts +67 -12
  142. package/src/data-structures/heap/heap.ts +107 -19
  143. package/src/data-structures/linked-list/doubly-linked-list.ts +88 -0
  144. package/src/data-structures/linked-list/singly-linked-list.ts +61 -1
  145. package/src/data-structures/linked-list/skip-linked-list.ts +72 -0
  146. package/src/data-structures/matrix/matrix.ts +32 -0
  147. package/src/data-structures/queue/deque.ts +85 -0
  148. package/src/data-structures/queue/queue.ts +73 -0
  149. package/src/data-structures/stack/stack.ts +45 -5
  150. package/src/data-structures/trie/trie.ts +48 -0
  151. package/src/interfaces/binary-tree.ts +1 -9
  152. package/.vitepress/cache/deps_temp_51f5f1b0/chunk-7OIKW5WK.js +0 -12984
  153. package/.vitepress/cache/deps_temp_51f5f1b0/package.json +0 -3
  154. package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vue_devtools-api.js +0 -4505
  155. package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vueuse_core.js +0 -9731
  156. package/.vitepress/cache/deps_temp_51f5f1b0/vue.js +0 -347
@@ -1,15 +1,14 @@
1
1
  ---
2
2
  sidebar_label: "ARCHITECTURE"
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]
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](/.md) • [See Performance](/guide/performance.md) • [Real Examples](/guide/guides.md)**
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: any, v: V, k: K) => any, init: any) {
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
- - ✅ Production-ready error handling
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 concepts behind data-structure-typed: uniform API, iterators, generics, comparators, and the 5 design traits."
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](/.md) • [API Docs](https://data-structure-typed-docs.vercel.app/) • [Real-World Guides](/guide/guides.md)**
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: Finding the K-th Element Requires Sorting
336
+ ### Case 5: Prefix Matching is Tedious
345
337
 
346
- ❌ Array: sort + index is O(n log n):
338
+ ❌ Array.filter is O(n*m):
347
339
 
348
340
  ```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
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
- Order-statistic tree: O(log n) getByRank/getRank with live updates:
346
+ Trie prefix matching is O(m + k):
355
347
 
356
348
  ```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)
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: Prefix Matching is Tedious
354
+ ### Case 6: Finding the K-th Element or Rank
364
355
 
365
- ❌ Array.filter is O(n*m):
356
+ ❌ Array requires sorting O(n log n):
366
357
 
367
358
  ```javascript
368
- const words = ['apple', 'app', 'apply', 'application'];
369
- const matches = words.filter(w => w.startsWith('app'));
370
- // For 1M words: checks 1M words
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
- Trie prefix matching is O(m + k):
364
+ Order-Statistic Tree gives O(log n) rank queries:
374
365
 
375
366
  ```javascript
376
- const trie = new Trie(words);
377
- const matches = trie.getWords('appl');
378
- // O(5 + 4) = 9 operations ✅
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: 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]
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](/.md) • [API Docs](https://data-structure-typed-docs.vercel.app/) • [See INTEGRATIONS](/guide/integrations.md)**
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>([], { isMapMode: true });
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: any }>();
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 (Order-Statistic Tree)
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 + k) — get k-th player by rank
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;