data-structure-typed 2.5.0 → 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.
Files changed (246) hide show
  1. package/.vitepress/cache/deps_temp_51f5f1b0/chunk-7OIKW5WK.js +12984 -0
  2. package/.vitepress/cache/deps_temp_51f5f1b0/package.json +3 -0
  3. package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vue_devtools-api.js +4505 -0
  4. package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vueuse_core.js +9731 -0
  5. package/.vitepress/cache/deps_temp_51f5f1b0/vue.js +347 -0
  6. package/CHANGELOG.md +5 -1
  7. package/README.md +124 -29
  8. package/dist/cjs/binary-tree.cjs +26282 -0
  9. package/dist/cjs/graph.cjs +5422 -0
  10. package/dist/cjs/hash.cjs +1310 -0
  11. package/dist/cjs/heap.cjs +1602 -0
  12. package/dist/cjs/index.cjs +31257 -14673
  13. package/dist/cjs/linked-list.cjs +4576 -0
  14. package/dist/cjs/matrix.cjs +1080 -0
  15. package/dist/cjs/priority-queue.cjs +1376 -0
  16. package/dist/cjs/queue.cjs +4264 -0
  17. package/dist/cjs/stack.cjs +907 -0
  18. package/dist/cjs/trie.cjs +1223 -0
  19. package/dist/cjs-legacy/binary-tree.cjs +26319 -0
  20. package/dist/cjs-legacy/graph.cjs +5420 -0
  21. package/dist/cjs-legacy/hash.cjs +1310 -0
  22. package/dist/cjs-legacy/heap.cjs +1599 -0
  23. package/dist/cjs-legacy/index.cjs +31268 -14679
  24. package/dist/cjs-legacy/linked-list.cjs +4582 -0
  25. package/dist/cjs-legacy/matrix.cjs +1083 -0
  26. package/dist/cjs-legacy/priority-queue.cjs +1374 -0
  27. package/dist/cjs-legacy/queue.cjs +4262 -0
  28. package/dist/cjs-legacy/stack.cjs +907 -0
  29. package/dist/cjs-legacy/trie.cjs +1222 -0
  30. package/dist/esm/binary-tree.mjs +26267 -0
  31. package/dist/esm/graph.mjs +5409 -0
  32. package/dist/esm/hash.mjs +1307 -0
  33. package/dist/esm/heap.mjs +1596 -0
  34. package/dist/esm/index.mjs +31254 -14674
  35. package/dist/esm/linked-list.mjs +4569 -0
  36. package/dist/esm/matrix.mjs +1076 -0
  37. package/dist/esm/priority-queue.mjs +1372 -0
  38. package/dist/esm/queue.mjs +4260 -0
  39. package/dist/esm/stack.mjs +905 -0
  40. package/dist/esm/trie.mjs +1220 -0
  41. package/dist/esm-legacy/binary-tree.mjs +26304 -0
  42. package/dist/esm-legacy/graph.mjs +5407 -0
  43. package/dist/esm-legacy/hash.mjs +1307 -0
  44. package/dist/esm-legacy/heap.mjs +1593 -0
  45. package/dist/esm-legacy/index.mjs +31265 -14680
  46. package/dist/esm-legacy/linked-list.mjs +4575 -0
  47. package/dist/esm-legacy/matrix.mjs +1079 -0
  48. package/dist/esm-legacy/priority-queue.mjs +1370 -0
  49. package/dist/esm-legacy/queue.mjs +4258 -0
  50. package/dist/esm-legacy/stack.mjs +905 -0
  51. package/dist/esm-legacy/trie.mjs +1219 -0
  52. package/dist/types/common/error.d.ts +9 -0
  53. package/dist/types/common/index.d.ts +1 -1
  54. package/dist/types/data-structures/base/index.d.ts +1 -0
  55. package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
  56. package/dist/types/data-structures/base/linear-base.d.ts +3 -3
  57. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +288 -0
  58. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +336 -0
  59. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +618 -18
  60. package/dist/types/data-structures/binary-tree/bst.d.ts +676 -1
  61. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +456 -0
  62. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +144 -1
  63. package/dist/types/data-structures/binary-tree/tree-map.d.ts +3307 -399
  64. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3285 -360
  65. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2674 -325
  66. package/dist/types/data-structures/binary-tree/tree-set.d.ts +3072 -287
  67. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
  68. package/dist/types/data-structures/graph/directed-graph.d.ts +240 -0
  69. package/dist/types/data-structures/graph/undirected-graph.d.ts +216 -0
  70. package/dist/types/data-structures/hash/hash-map.d.ts +274 -10
  71. package/dist/types/data-structures/heap/heap.d.ts +336 -0
  72. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +411 -3
  73. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +363 -3
  74. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +434 -2
  75. package/dist/types/data-structures/matrix/matrix.d.ts +192 -0
  76. package/dist/types/data-structures/queue/deque.d.ts +364 -4
  77. package/dist/types/data-structures/queue/queue.d.ts +288 -0
  78. package/dist/types/data-structures/stack/stack.d.ts +240 -0
  79. package/dist/types/data-structures/trie/trie.d.ts +292 -4
  80. package/dist/types/interfaces/graph.d.ts +1 -1
  81. package/dist/types/types/common.d.ts +2 -2
  82. package/dist/types/types/data-structures/binary-tree/bst.d.ts +1 -0
  83. package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +5 -0
  84. package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +4 -0
  85. package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +4 -0
  86. package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
  87. package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
  88. package/dist/types/types/utils/validate-type.d.ts +4 -4
  89. package/dist/umd/data-structure-typed.js +31196 -14608
  90. package/dist/umd/data-structure-typed.min.js +11 -5
  91. package/docs-site-docusaurus/README.md +41 -0
  92. package/docs-site-docusaurus/docs/api/README.md +52 -0
  93. package/docs-site-docusaurus/docs/api/classes/AVLTree.md +6644 -0
  94. package/docs-site-docusaurus/docs/api/classes/AVLTreeNode.md +282 -0
  95. package/docs-site-docusaurus/docs/api/classes/AbstractGraph.md +2266 -0
  96. package/docs-site-docusaurus/docs/api/classes/BST.md +6293 -0
  97. package/docs-site-docusaurus/docs/api/classes/BSTNode.md +333 -0
  98. package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +455 -0
  99. package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +4647 -0
  100. package/docs-site-docusaurus/docs/api/classes/BinaryTreeNode.md +331 -0
  101. package/docs-site-docusaurus/docs/api/classes/Deque.md +2767 -0
  102. package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +2999 -0
  103. package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +2685 -0
  104. package/docs-site-docusaurus/docs/api/classes/DoublyLinkedListNode.md +221 -0
  105. package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +253 -0
  106. package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +21 -0
  107. package/docs-site-docusaurus/docs/api/classes/HashMap.md +1333 -0
  108. package/docs-site-docusaurus/docs/api/classes/Heap.md +1881 -0
  109. package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +800 -0
  110. package/docs-site-docusaurus/docs/api/classes/IterableEntryBase.md +644 -0
  111. package/docs-site-docusaurus/docs/api/classes/LinearBase.md +1632 -0
  112. package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +1853 -0
  113. package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +1108 -0
  114. package/docs-site-docusaurus/docs/api/classes/LinkedListNode.md +156 -0
  115. package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +2824 -0
  116. package/docs-site-docusaurus/docs/api/classes/MapGraph.md +2929 -0
  117. package/docs-site-docusaurus/docs/api/classes/Matrix.md +1026 -0
  118. package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +1866 -0
  119. package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +1883 -0
  120. package/docs-site-docusaurus/docs/api/classes/MinHeap.md +1879 -0
  121. package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +1882 -0
  122. package/docs-site-docusaurus/docs/api/classes/Navigator.md +109 -0
  123. package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +1839 -0
  124. package/docs-site-docusaurus/docs/api/classes/Queue.md +2244 -0
  125. package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +6888 -0
  126. package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +372 -0
  127. package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +2897 -0
  128. package/docs-site-docusaurus/docs/api/classes/SinglyLinkedListNode.md +169 -0
  129. package/docs-site-docusaurus/docs/api/classes/SkipList.md +1229 -0
  130. package/docs-site-docusaurus/docs/api/classes/Stack.md +1573 -0
  131. package/docs-site-docusaurus/docs/api/classes/TreeMap.md +1389 -0
  132. package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +1591 -0
  133. package/docs-site-docusaurus/docs/api/classes/TreeSet.md +1246 -0
  134. package/docs-site-docusaurus/docs/api/classes/Trie.md +1708 -0
  135. package/docs-site-docusaurus/docs/api/classes/TrieNode.md +199 -0
  136. package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +2979 -0
  137. package/docs-site-docusaurus/docs/guide/_category_.json +6 -0
  138. package/docs-site-docusaurus/docs/guide/architecture.md +615 -0
  139. package/docs-site-docusaurus/docs/guide/concepts.md +451 -0
  140. package/docs-site-docusaurus/docs/guide/faq.md +180 -0
  141. package/docs-site-docusaurus/docs/guide/guides.md +597 -0
  142. package/docs-site-docusaurus/docs/guide/installation.md +62 -0
  143. package/docs-site-docusaurus/docs/guide/integrations.md +825 -0
  144. package/docs-site-docusaurus/docs/guide/overview.md +645 -0
  145. package/docs-site-docusaurus/docs/guide/performance.md +835 -0
  146. package/docs-site-docusaurus/docs/guide/quick-start.md +104 -0
  147. package/docs-site-docusaurus/docs/guide/use-cases/_category_.json +6 -0
  148. package/docs-site-docusaurus/docs/guide/use-cases/array-sort-alternative.md +158 -0
  149. package/docs-site-docusaurus/docs/guide/use-cases/heap-vs-sorting.md +92 -0
  150. package/docs-site-docusaurus/docs/guide/use-cases/map-vs-treemap.md +151 -0
  151. package/docs-site-docusaurus/docs/guide/use-cases/priority-queue-typescript.md +113 -0
  152. package/docs-site-docusaurus/docs/guide/use-cases/treemap-javascript.md +151 -0
  153. package/docs-site-docusaurus/docusaurus.config.ts +159 -0
  154. package/docs-site-docusaurus/fix-mdx-generics.mjs +75 -0
  155. package/docs-site-docusaurus/package-lock.json +18667 -0
  156. package/docs-site-docusaurus/package.json +50 -0
  157. package/docs-site-docusaurus/prefix-class-to-methods.mjs +48 -0
  158. package/docs-site-docusaurus/sidebars.ts +23 -0
  159. package/docs-site-docusaurus/sort-protected.mjs +87 -0
  160. package/docs-site-docusaurus/src/css/custom.css +96 -0
  161. package/docs-site-docusaurus/src/pages/index.module.css +13 -0
  162. package/docs-site-docusaurus/src/pages/index.tsx +120 -0
  163. package/docs-site-docusaurus/src/pages/markdown-page.md +7 -0
  164. package/docs-site-docusaurus/src/theme/TOCItems/index.tsx +34 -0
  165. package/docs-site-docusaurus/static/.nojekyll +0 -0
  166. package/docs-site-docusaurus/static/img/docusaurus-social-card.jpg +0 -0
  167. package/docs-site-docusaurus/static/img/docusaurus.png +0 -0
  168. package/docs-site-docusaurus/static/img/favicon.ico +0 -0
  169. package/docs-site-docusaurus/static/img/favicon.png +0 -0
  170. package/docs-site-docusaurus/static/img/logo-180.png +0 -0
  171. package/docs-site-docusaurus/static/img/logo.jpg +0 -0
  172. package/docs-site-docusaurus/static/img/logo.png +0 -0
  173. package/docs-site-docusaurus/static/img/logo.svg +1 -0
  174. package/docs-site-docusaurus/static/img/og-image.png +0 -0
  175. package/docs-site-docusaurus/static/img/undraw_docusaurus_mountain.svg +171 -0
  176. package/docs-site-docusaurus/static/img/undraw_docusaurus_react.svg +170 -0
  177. package/docs-site-docusaurus/static/img/undraw_docusaurus_tree.svg +40 -0
  178. package/docs-site-docusaurus/static/llms.txt +37 -0
  179. package/docs-site-docusaurus/static/robots.txt +4 -0
  180. package/docs-site-docusaurus/typedoc.json +23 -0
  181. package/llms.txt +37 -0
  182. package/package.json +159 -55
  183. package/src/common/error.ts +19 -1
  184. package/src/common/index.ts +1 -1
  185. package/src/data-structures/base/index.ts +1 -0
  186. package/src/data-structures/base/iterable-element-base.ts +3 -2
  187. package/src/data-structures/base/iterable-entry-base.ts +8 -8
  188. package/src/data-structures/base/linear-base.ts +3 -3
  189. package/src/data-structures/binary-tree/avl-tree.ts +287 -0
  190. package/src/data-structures/binary-tree/binary-indexed-tree.ts +327 -5
  191. package/src/data-structures/binary-tree/binary-tree.ts +581 -6
  192. package/src/data-structures/binary-tree/bst.ts +922 -7
  193. package/src/data-structures/binary-tree/red-black-tree.ts +453 -0
  194. package/src/data-structures/binary-tree/segment-tree.ts +139 -2
  195. package/src/data-structures/binary-tree/tree-map.ts +3300 -495
  196. package/src/data-structures/binary-tree/tree-multi-map.ts +3384 -563
  197. package/src/data-structures/binary-tree/tree-multi-set.ts +2757 -493
  198. package/src/data-structures/binary-tree/tree-set.ts +3122 -440
  199. package/src/data-structures/graph/abstract-graph.ts +6 -6
  200. package/src/data-structures/graph/directed-graph.ts +230 -0
  201. package/src/data-structures/graph/undirected-graph.ts +207 -0
  202. package/src/data-structures/hash/hash-map.ts +270 -19
  203. package/src/data-structures/heap/heap.ts +326 -4
  204. package/src/data-structures/heap/max-heap.ts +2 -2
  205. package/src/data-structures/linked-list/doubly-linked-list.ts +394 -3
  206. package/src/data-structures/linked-list/singly-linked-list.ts +348 -3
  207. package/src/data-structures/linked-list/skip-linked-list.ts +421 -7
  208. package/src/data-structures/matrix/matrix.ts +194 -10
  209. package/src/data-structures/priority-queue/max-priority-queue.ts +2 -2
  210. package/src/data-structures/queue/deque.ts +350 -5
  211. package/src/data-structures/queue/queue.ts +276 -0
  212. package/src/data-structures/stack/stack.ts +230 -0
  213. package/src/data-structures/trie/trie.ts +283 -7
  214. package/src/interfaces/graph.ts +1 -1
  215. package/src/types/common.ts +2 -2
  216. package/src/types/data-structures/binary-tree/bst.ts +1 -0
  217. package/src/types/data-structures/binary-tree/tree-map.ts +6 -0
  218. package/src/types/data-structures/binary-tree/tree-multi-set.ts +5 -0
  219. package/src/types/data-structures/binary-tree/tree-set.ts +5 -0
  220. package/src/types/data-structures/heap/heap.ts +1 -0
  221. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
  222. package/src/types/utils/validate-type.ts +4 -4
  223. package/vercel.json +6 -0
  224. package/dist/leetcode/avl-tree-counter.mjs +0 -2957
  225. package/dist/leetcode/avl-tree-multi-map.mjs +0 -2889
  226. package/dist/leetcode/avl-tree.mjs +0 -2720
  227. package/dist/leetcode/binary-tree.mjs +0 -1594
  228. package/dist/leetcode/bst.mjs +0 -2398
  229. package/dist/leetcode/deque.mjs +0 -683
  230. package/dist/leetcode/directed-graph.mjs +0 -1733
  231. package/dist/leetcode/doubly-linked-list.mjs +0 -709
  232. package/dist/leetcode/hash-map.mjs +0 -493
  233. package/dist/leetcode/heap.mjs +0 -542
  234. package/dist/leetcode/max-heap.mjs +0 -375
  235. package/dist/leetcode/max-priority-queue.mjs +0 -383
  236. package/dist/leetcode/min-heap.mjs +0 -363
  237. package/dist/leetcode/min-priority-queue.mjs +0 -371
  238. package/dist/leetcode/priority-queue.mjs +0 -363
  239. package/dist/leetcode/queue.mjs +0 -943
  240. package/dist/leetcode/red-black-tree.mjs +0 -2765
  241. package/dist/leetcode/singly-linked-list.mjs +0 -754
  242. package/dist/leetcode/stack.mjs +0 -217
  243. package/dist/leetcode/tree-counter.mjs +0 -3039
  244. package/dist/leetcode/tree-multi-map.mjs +0 -2913
  245. package/dist/leetcode/trie.mjs +0 -413
  246. package/dist/leetcode/undirected-graph.mjs +0 -1650
@@ -0,0 +1,597 @@
1
+ ---
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]
6
+ ---
7
+
8
+ # GUIDES
9
+
10
+ Production-ready code examples for common use cases. Learn by doing.
11
+
12
+ **[Back to README](/.md) • [API Docs](https://data-structure-typed-docs.vercel.app/) • [See INTEGRATIONS](/guide/integrations.md)**
13
+
14
+ ---
15
+
16
+ ## Table of Contents
17
+
18
+ 1. [Design Patterns](#design-patterns)
19
+ 2. [Real-World Examples](#real-world-examples)
20
+ 3. [Common Mistakes](#common-mistakes)
21
+ 4. [Best Practices](#best-practices)
22
+
23
+ ---
24
+
25
+ ## Design Patterns
26
+
27
+ ### Pattern 1: Iterator Pattern (Zero Conversions)
28
+
29
+ ```typescript
30
+ import { RedBlackTree } from 'data-structure-typed';
31
+
32
+ // Problem: Need to work with sorted data in multiple ways
33
+ const scores = [95, 23, 67, 89, 12, 45];
34
+
35
+ // Solution: Use iterator protocol
36
+ const tree = new RedBlackTree(scores);
37
+
38
+ // Method 1: Spread operator
39
+ const sorted = [...tree.keys()];
40
+ console.log(sorted); // [12, 23, 45, 67, 89, 95]
41
+
42
+ // Method 2: for...of loop
43
+ for (const [score] of tree) {
44
+ console.log(score);
45
+ }
46
+
47
+ // Method 3: Destructuring
48
+ const [min, ...rest] = tree.keys();
49
+
50
+ // Method 4: Set constructor
51
+ const unique = new Set(tree.keys());
52
+
53
+ // Method 5: Array.from()
54
+ const array = Array.from(tree.keys());
55
+
56
+ // All work automatically - zero conversions!
57
+ ```
58
+
59
+ ### Pattern 2: Method Chaining (Stay on Structure)
60
+
61
+ ```typescript
62
+ import { RedBlackTree } from 'data-structure-typed';
63
+
64
+ const tree = new RedBlackTree([
65
+ [1, { name: 'Alice', score: 95 }],
66
+ [2, { name: 'Bob', score: 45 }],
67
+ [3, { name: 'Charlie', score: 87 }],
68
+ ]);
69
+
70
+ // Chain operations - structure maintained throughout
71
+ const result = tree
72
+ .filter((student, id) => (student?.score ?? 0) >= 50)
73
+ .map((student, id) => ([id, {
74
+ id,
75
+ name: student?.name,
76
+ passed: (student?.score ?? 0) >= 50
77
+ }]))
78
+ .reduce((summary, student) => {
79
+ summary.count++;
80
+ summary.names.push(student?.name ?? '');
81
+ return summary;
82
+ }, { count: 0, names: [] as string[] });
83
+
84
+ console.log(result);
85
+ // { count: 2, names: ['Alice', 'Charlie'] }
86
+ ```
87
+
88
+ ### Pattern 3: Seamless Structure Conversion
89
+
90
+ ```typescript
91
+ import { RedBlackTree, Deque, MaxHeap } from 'data-structure-typed';
92
+
93
+ const data = [64, 34, 25, 12, 22, 11, 90];
94
+
95
+ // Convert between structures instantly
96
+ const tree = new RedBlackTree(data);
97
+ const sorted = [...tree.keys()]; // [11, 12, 22, 25, 34, 64, 90]
98
+
99
+ const heap = new MaxHeap(sorted);
100
+ const byPriority = [...heap]; // [90, 64, 34, ...]
101
+
102
+ const deque = new Deque(byPriority);
103
+ const processed = deque.shift(); // Remove first - O(1)!
104
+
105
+ // No intermediate conversions, structure preserved when possible
106
+ ```
107
+
108
+ ### Pattern 4: Conditional Logic with Types
109
+
110
+ ```typescript
111
+ import { RedBlackTree } from 'data-structure-typed';
112
+
113
+ interface Product {
114
+ id: string;
115
+ name: string;
116
+ price: number;
117
+ }
118
+
119
+ class PriceIndex {
120
+ private index = new RedBlackTree<number, Product>([], {isMapMode: false});
121
+
122
+ addProduct(product: Product) {
123
+ this.index.set(product.price, product);
124
+ }
125
+
126
+ getByPriceRange(min: number, max: number) {
127
+ return this.index.rangeSearch([min, max], node => node.value)
128
+ }
129
+
130
+ getAffordable(budget: number) {
131
+ return this.index.rangeSearch([0, budget], node => node.value)
132
+ }
133
+ }
134
+
135
+ const index = new PriceIndex();
136
+ index.addProduct({id: '1', name: 'Laptop', price: 999});
137
+ index.addProduct({id: '2', name: 'Mouse', price: 25});
138
+
139
+ const affordable = index.getAffordable(100);
140
+ ```
141
+
142
+ ### Pattern 5: High-Throughput Insertions with Hints (RedBlackTree)
143
+
144
+ If you insert keys in sorted or nearly-sorted order (timestamps, auto-increment IDs, etc.),
145
+ `setWithHintNode()` can avoid repeated full root-to-leaf searches.
146
+
147
+ ```typescript
148
+ import { RedBlackTree } from 'data-structure-typed';
149
+ import type { RedBlackTreeNode } from 'data-structure-typed';
150
+
151
+ const tree = new RedBlackTree<number, number>([], { isMapMode: true });
152
+
153
+ let hint: RedBlackTreeNode<number, number> | undefined;
154
+ for (let i = 0; i < 1_000_000; i++) {
155
+ hint = tree.setWithHintNode(i, i, hint);
156
+ }
157
+
158
+ // tree.size === 1_000_000
159
+ ```
160
+
161
+ Notes:
162
+ - Pass the **last returned node** as the hint.
163
+ - If the hint is not valid for the key, the implementation safely falls back to normal `set()`.
164
+
165
+ ---
166
+
167
+ ## Real-World Examples
168
+
169
+ ### Example 1: LRU Cache
170
+
171
+ ```typescript
172
+ import { DoublyLinkedList } from 'data-structure-typed';
173
+
174
+ class LRUCache<K, V> {
175
+ private cache = new Map<K, { value: V; node: any }>();
176
+ private order = new DoublyLinkedList<K>();
177
+ private readonly capacity: number;
178
+
179
+ constructor(capacity: number) {
180
+ this.capacity = capacity;
181
+ }
182
+
183
+ get(key: K): V | null {
184
+ if (!this.cache.has(key)) return null;
185
+
186
+ const {value, node} = this.cache.get(key)!;
187
+
188
+ // Move to end (most recently used)
189
+ this.order.delete(node);
190
+ const newNode = this.order.push(key);
191
+ this.cache.set(key, {value, node: newNode});
192
+
193
+ return value;
194
+ }
195
+
196
+ set(key: K, value: V): void {
197
+ if (this.cache.has(key)) {
198
+ this.get(key); // Mark as recently used
199
+ this.cache.set(key, {value, node: this.cache.get(key)!.node});
200
+ return;
201
+ }
202
+
203
+ if (this.cache.size >= this.capacity) {
204
+ // Evict least recently used
205
+ const lru = this.order.shift();
206
+ if (lru) this.cache.delete(lru);
207
+ }
208
+
209
+ const node = this.order.push(key);
210
+ this.cache.set(key, {value, node});
211
+ }
212
+ }
213
+
214
+ // Usage
215
+ const cache = new LRUCache<string, string>(3);
216
+ cache.set('a', 'value1');
217
+ cache.set('b', 'value2');
218
+ cache.set('c', 'value3');
219
+ console.log(cache.get('a')); // 'value1', 'a' is now most recent
220
+ cache.set('d', 'value4'); // Evicts 'b' (least recent)
221
+ ```
222
+
223
+ ### Example 2: Real-Time Leaderboard (Order-Statistic Tree)
224
+
225
+ ```typescript
226
+ import { RedBlackTree } from 'data-structure-typed';
227
+
228
+ interface Player {
229
+ id: string;
230
+ name: string;
231
+ score: number;
232
+ }
233
+
234
+ class Leaderboard {
235
+ // enableOrderStatistic gives O(log n) getByRank/getRank/rangeByRank
236
+ private scores = new RedBlackTree<number, Player>(
237
+ [],
238
+ { comparator: (a, b) => b - a, enableOrderStatistic: true }
239
+ );
240
+ private players = new Map<string, number>(); // playerId → currentScore
241
+
242
+ updateScore(player: Player): void {
243
+ if (this.players.has(player.id)) {
244
+ this.scores.delete(this.players.get(player.id)!);
245
+ }
246
+ this.scores.set(player.score, player);
247
+ this.players.set(player.id, player.score);
248
+ }
249
+
250
+ // O(k) — select by rank, no array copy
251
+ getTopN(n: number): Player[] {
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);
255
+ }
256
+
257
+ // O(log n) — direct rank lookup
258
+ getRank(playerId: string): number {
259
+ if (!this.players.has(playerId)) return -1;
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;
267
+ }
268
+
269
+ // O(log n + k) — players around a given player
270
+ getAroundMe(playerId: string, range: number): Player[] {
271
+ if (!this.players.has(playerId)) return [];
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
+ }
279
+
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);
287
+ }
288
+ }
289
+
290
+ // Usage
291
+ const lb = new Leaderboard();
292
+ lb.updateScore({ id: '1', name: 'Alice', score: 1000 });
293
+ lb.updateScore({ id: '2', name: 'Bob', score: 900 });
294
+ lb.updateScore({ id: '3', name: 'Charlie', score: 950 });
295
+
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)
301
+ ```
302
+
303
+ ### Example 3: Message Queue with Priorities
304
+
305
+ ```typescript
306
+ import { Deque, MaxPriorityQueue } from 'data-structure-typed';
307
+
308
+ interface Message {
309
+ id: string;
310
+ content: string;
311
+ priority: number;
312
+ timestamp: number;
313
+ }
314
+
315
+ class PriorityMessageQueue {
316
+ private urgent = new Deque<Message>(); // Priority >= 8
317
+ private normal = new Deque<Message>(); // Priority 4-7
318
+ private low = new Deque<Message>(); // Priority < 4
319
+
320
+ enqueue(message: Message): void {
321
+ if (message.priority >= 8) {
322
+ this.urgent.push(message);
323
+ } else if (message.priority >= 4) {
324
+ this.normal.push(message);
325
+ } else {
326
+ this.low.push(message);
327
+ }
328
+ }
329
+
330
+ dequeue(): Message | null {
331
+ // Serve urgent first, then normal, then low
332
+ return (
333
+ this.urgent.shift() ||
334
+ this.normal.shift() ||
335
+ this.low.shift() ||
336
+ null
337
+ );
338
+ }
339
+
340
+ length(): number {
341
+ return this.urgent.length + this.normal.length + this.low.length;
342
+ }
343
+ }
344
+
345
+ // Usage
346
+ const queue = new PriorityMessageQueue();
347
+ queue.enqueue({ id: '1', content: 'Normal task', priority: 5, timestamp: Date.now() });
348
+ queue.enqueue({ id: '2', content: 'Urgent task', priority: 9, timestamp: Date.now() });
349
+ queue.enqueue({ id: '3', content: 'Low task', priority: 1, timestamp: Date.now() });
350
+
351
+ while (queue.length() > 0) {
352
+ const msg = queue.dequeue();
353
+ console.log(msg?.id); // 2, 1, 3 (urgent first)
354
+ }
355
+ ```
356
+
357
+ ### Example 4: Task Scheduler
358
+
359
+ ```typescript
360
+ interface Task {
361
+ id: string;
362
+ action: () => Promise<void>;
363
+ priority: number;
364
+ scheduledTime: number;
365
+ }
366
+
367
+ class TaskScheduler {
368
+ private queue = new MaxPriorityQueue<Task>([], {
369
+ comparator: (a, b) => a.priority - b.priority
370
+ });
371
+ private running = false;
372
+
373
+ scheduleTask(task: Task): void {
374
+ this.queue.add(task);
375
+ if (!this.running) void this.start();
376
+ }
377
+
378
+ private async start(): Promise<void> {
379
+ this.running = true;
380
+
381
+ while (!this.queue.isEmpty()) {
382
+ const task = this.queue.poll();
383
+ if (!task) break;
384
+
385
+ try {
386
+ console.log(`Executing task ${task.id}`);
387
+ await task.action();
388
+ } catch (error) {
389
+ console.error(`Task ${task.id} failed:`, error);
390
+ }
391
+ }
392
+
393
+ this.running = false;
394
+ }
395
+ }
396
+
397
+ // Usage
398
+ const scheduler = new TaskScheduler();
399
+ scheduler.scheduleTask({
400
+ id: '1',
401
+ action: async () => console.log('High priority'),
402
+ priority: 10,
403
+ scheduledTime: Date.now()
404
+ });
405
+ scheduler.scheduleTask({
406
+ id: '2',
407
+ action: async () => console.log('Low priority'),
408
+ priority: 1,
409
+ scheduledTime: Date.now()
410
+ });
411
+ ```
412
+
413
+ ### Example 5: Search Index with Autocomplete
414
+
415
+ ```typescript
416
+ import {Trie} from 'data-structure-typed';
417
+
418
+ class SearchIndex {
419
+ private trie = new Trie();
420
+ private documents = new Map<string, string[]>();
421
+
422
+ indexDocument(docId: string, content: string): void {
423
+ const words = content.toLowerCase().split(/\s+/);
424
+
425
+ for (const word of words) {
426
+ // Insert word into trie
427
+ if (!this.trie.has(word)) {
428
+ this.trie.add(word);
429
+ }
430
+
431
+ // Track which documents contain this word
432
+ if (!this.documents.has(word)) {
433
+ this.documents.set(word, []);
434
+ }
435
+ this.documents.get(word)!.push(docId);
436
+ }
437
+ }
438
+
439
+ autocomplete(prefix: string): string[] {
440
+ const words = this.trie.getWords(prefix);
441
+ return words.filter((_word, index) => index < 10); // Top 10
442
+ }
443
+
444
+ search(query: string): string[] {
445
+ const word = query.toLowerCase();
446
+ return this.documents.get(word) || [];
447
+ }
448
+ }
449
+
450
+ // Usage
451
+ const index = new SearchIndex();
452
+ index.indexDocument('doc1', 'apple application');
453
+ index.indexDocument('doc2', 'app store');
454
+
455
+ console.log(index.autocomplete('app')); // ['apple', 'application', 'app']
456
+ console.log(index.search('apple')); // ['doc1']
457
+ ```
458
+
459
+ ---
460
+
461
+ ## Common Mistakes
462
+
463
+ ### Mistake 1: Forgetting to Convert Back
464
+
465
+ ```typescript
466
+ // ❌ Wrong
467
+ const tree = new RedBlackTree([5, 2, 8]);
468
+ const doubled = tree.map((_v, k) => [k * 2, undefined]); // Still a tree!
469
+ JSON.stringify(doubled); // Error or wrong output
470
+ ```
471
+
472
+ ```typescript
473
+ // ✅ Right
474
+ const tree = new RedBlackTree([5, 2, 8]);
475
+ const doubled = tree.map((_v, k) => [k * 2, undefined]);
476
+ JSON.stringify([...doubled.keys()]); // [4, 10, 16]
477
+ ```
478
+
479
+ ### Mistake 2: Mutating During Iteration
480
+
481
+ ```typescript
482
+ // ❌ Wrong
483
+ const tree = new RedBlackTree([1, 2, 3, 4, 5, 6]);
484
+ for (const [key, _value] of tree) {
485
+ if (key % 2 === 0) tree.delete(key); // Avoid this!
486
+ }
487
+ ```
488
+
489
+ ```typescript
490
+ // ✅ Right
491
+ const tree = new RedBlackTree([1, 2, 3, 4, 5, 6]);
492
+ const toDelete = [];
493
+ for (const [key, _value] of tree) {
494
+ if (key % 2 === 0) toDelete.push(key);
495
+ }
496
+ toDelete.forEach(v => tree.delete(v));
497
+ ```
498
+
499
+ ```typescript
500
+ // ✅ Perfect
501
+ const tree = new RedBlackTree([1, 2, 3, 4, 5, 6]);
502
+ tree.deleteWhere((node) => node.key % 2 === 0);
503
+ ```
504
+
505
+ ### Mistake 3: Wrong Data Structure Choice
506
+
507
+ ```typescript
508
+ // ❌ Wrong - using Array for sorted data with updates
509
+ const scores = [];
510
+ function addScore(score) {
511
+ scores.push(score);
512
+ scores.sort((a, b) => b - a); // O(n log n) every time!
513
+ }
514
+ ```
515
+
516
+ ```typescript
517
+ // ✅ Right - using RedBlackTree
518
+ const scores = new RedBlackTree();
519
+ function addScore(score) {
520
+ scores.set(score, true); // O(log n), auto-sorted
521
+ }
522
+ ```
523
+
524
+ ---
525
+
526
+ ## Best Practices
527
+
528
+ ### 1. Choose Structure Early
529
+
530
+ ```typescript
531
+ // ❌ Bad: Convert later
532
+ function processScores(scores: number[]) {
533
+ const sorted = [...scores].sort((a, b) => b - a);
534
+ return new RedBlackTree(sorted).filter(x => x > 50); // Redundant
535
+ }
536
+ ```
537
+
538
+ ```typescript
539
+ // ✅ Good: Decide upfront
540
+ function processScores(scores: number[]) {
541
+ const tree = new RedBlackTree(scores); // Already sorted
542
+ return tree.filter(x => x > 50).map((_v,k) => [k * 1.1, undefined]);
543
+ }
544
+ ```
545
+
546
+ ### 2. Batch Operations When Possible
547
+
548
+ ```typescript
549
+ // ❌ Bad: Individual inserts
550
+ const tree = new RedBlackTree();
551
+ for (const item of largeDataset) {
552
+ tree.set(item.id, item); // Rebalances each time
553
+ }
554
+ ```
555
+
556
+ ```typescript
557
+ // ✅ Good: Bulk insert
558
+ const tree = new RedBlackTree(largeDataset);
559
+ ```
560
+
561
+ ### 3. Use Type Safety
562
+
563
+ ```typescript
564
+ // ❌ Bad: No type checking
565
+ const tree = new RedBlackTree();
566
+ tree.set(1, { id: 1, name: 'Alice' });
567
+ ```
568
+
569
+ ```typescript
570
+ // ✅ Good: Full type inference
571
+ const tree = new RedBlackTree<number, User>();
572
+ tree.set(1, { id: 1, name: 'Alice' });
573
+ ```
574
+
575
+ ### 4. Chain When Possible
576
+
577
+ ```typescript
578
+ // ❌ Bad: Convert unnecessarily
579
+ const result = [...tree]
580
+ .filter(x => x > 10)
581
+ .map(x => x * 2)
582
+ .reduce((sum, x) => sum + x, 0);
583
+ ```
584
+
585
+ ```typescript
586
+ // ✅ Good: Stay on structure
587
+ const result = tree
588
+ .filter(v => v > 10)
589
+ .map(v => [(v ?? 0) * 2, undefined])
590
+ .reduce((sum, v) => sum + (v ?? 0), 0);
591
+ ```
592
+
593
+ ---
594
+
595
+ **Need more?** Check [INTEGRATIONS.md](/guide/integrations.md) for framework examples.
596
+
597
+ **Performance questions?** See [PERFORMANCE.md](/guide/performance.md).
@@ -0,0 +1,62 @@
1
+ ---
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]
5
+ ---
6
+
7
+ # Installation
8
+
9
+ ## Package Manager
10
+
11
+ ::: code-group
12
+
13
+ ```bash [npm]
14
+ npm i data-structure-typed
15
+ ```
16
+
17
+ ```bash [yarn]
18
+ yarn add data-structure-typed
19
+ ```
20
+
21
+ ```bash [pnpm]
22
+ pnpm add data-structure-typed
23
+ ```
24
+
25
+ :::
26
+
27
+ ## Subpath Imports
28
+
29
+ Import only the category you need for smaller bundles:
30
+
31
+ ```typescript
32
+ import { RedBlackTree, TreeMap } from 'data-structure-typed/binary-tree';
33
+ import { Deque, Queue } from 'data-structure-typed/queue';
34
+ import { HashMap } from 'data-structure-typed/hash';
35
+ import { Heap, MinHeap } from 'data-structure-typed/heap';
36
+ import { Trie } from 'data-structure-typed/trie';
37
+ import { Stack } from 'data-structure-typed/stack';
38
+ ```
39
+
40
+ ::: tip Tree-Shaking
41
+ With `"sideEffects": false` and modern bundlers (Vite, Webpack 5, Rollup), even the full import `from 'data-structure-typed'` will tree-shake unused structures. Subpath imports give you explicit control and faster IDE autocomplete.
42
+ :::
43
+
44
+ ## Individual Packages
45
+
46
+ Standalone packages are also available:
47
+
48
+ ```bash
49
+ npm i avl-tree-typed bst-typed heap-typed
50
+ ```
51
+
52
+ ## CDN
53
+
54
+ ```html
55
+ <script src="https://unpkg.com/data-structure-typed/dist/umd/data-structure-typed.min.js"></script>
56
+ ```
57
+
58
+ ## Requirements
59
+
60
+ - **Node.js** ≥ 16
61
+ - **TypeScript** ≥ 4.8 (optional, for type support)
62
+ - **Browser**: Any modern browser (ES2018+)