data-structure-typed 2.5.0 → 2.5.1

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 (222) 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 +3 -1
  7. package/README.md +63 -26
  8. package/dist/cjs/binary-tree.cjs +23698 -0
  9. package/dist/cjs/graph.cjs +5236 -0
  10. package/dist/cjs/hash.cjs +1262 -0
  11. package/dist/cjs/heap.cjs +1540 -0
  12. package/dist/cjs/index.cjs +28244 -14795
  13. package/dist/cjs/linked-list.cjs +4370 -0
  14. package/dist/cjs/matrix.cjs +1042 -0
  15. package/dist/cjs/priority-queue.cjs +1314 -0
  16. package/dist/cjs/queue.cjs +4090 -0
  17. package/dist/cjs/stack.cjs +861 -0
  18. package/dist/cjs/trie.cjs +1173 -0
  19. package/dist/cjs-legacy/binary-tree.cjs +23730 -0
  20. package/dist/cjs-legacy/graph.cjs +5234 -0
  21. package/dist/cjs-legacy/hash.cjs +1262 -0
  22. package/dist/cjs-legacy/heap.cjs +1537 -0
  23. package/dist/cjs-legacy/index.cjs +28239 -14790
  24. package/dist/cjs-legacy/linked-list.cjs +4376 -0
  25. package/dist/cjs-legacy/matrix.cjs +1045 -0
  26. package/dist/cjs-legacy/priority-queue.cjs +1312 -0
  27. package/dist/cjs-legacy/queue.cjs +4088 -0
  28. package/dist/cjs-legacy/stack.cjs +861 -0
  29. package/dist/cjs-legacy/trie.cjs +1172 -0
  30. package/dist/esm/binary-tree.mjs +23683 -0
  31. package/dist/esm/graph.mjs +5223 -0
  32. package/dist/esm/hash.mjs +1259 -0
  33. package/dist/esm/heap.mjs +1534 -0
  34. package/dist/esm/index.mjs +28242 -14796
  35. package/dist/esm/linked-list.mjs +4363 -0
  36. package/dist/esm/matrix.mjs +1038 -0
  37. package/dist/esm/priority-queue.mjs +1310 -0
  38. package/dist/esm/queue.mjs +4086 -0
  39. package/dist/esm/stack.mjs +859 -0
  40. package/dist/esm/trie.mjs +1170 -0
  41. package/dist/esm-legacy/binary-tree.mjs +23715 -0
  42. package/dist/esm-legacy/graph.mjs +5221 -0
  43. package/dist/esm-legacy/hash.mjs +1259 -0
  44. package/dist/esm-legacy/heap.mjs +1531 -0
  45. package/dist/esm-legacy/index.mjs +28237 -14791
  46. package/dist/esm-legacy/linked-list.mjs +4369 -0
  47. package/dist/esm-legacy/matrix.mjs +1041 -0
  48. package/dist/esm-legacy/priority-queue.mjs +1308 -0
  49. package/dist/esm-legacy/queue.mjs +4084 -0
  50. package/dist/esm-legacy/stack.mjs +859 -0
  51. package/dist/esm-legacy/trie.mjs +1169 -0
  52. package/dist/types/data-structures/base/index.d.ts +1 -0
  53. package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
  54. package/dist/types/data-structures/base/linear-base.d.ts +3 -3
  55. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +240 -0
  56. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +280 -0
  57. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +502 -2
  58. package/dist/types/data-structures/binary-tree/bst.d.ts +481 -1
  59. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +380 -0
  60. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +120 -1
  61. package/dist/types/data-structures/binary-tree/tree-map.d.ts +2764 -384
  62. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2753 -353
  63. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2241 -321
  64. package/dist/types/data-structures/binary-tree/tree-set.d.ts +2554 -274
  65. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
  66. package/dist/types/data-structures/graph/directed-graph.d.ts +200 -0
  67. package/dist/types/data-structures/graph/undirected-graph.d.ts +180 -0
  68. package/dist/types/data-structures/hash/hash-map.d.ts +230 -10
  69. package/dist/types/data-structures/heap/heap.d.ts +280 -0
  70. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +343 -3
  71. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +303 -3
  72. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +362 -2
  73. package/dist/types/data-structures/matrix/matrix.d.ts +160 -0
  74. package/dist/types/data-structures/queue/deque.d.ts +304 -4
  75. package/dist/types/data-structures/queue/queue.d.ts +240 -0
  76. package/dist/types/data-structures/stack/stack.d.ts +200 -0
  77. package/dist/types/data-structures/trie/trie.d.ts +244 -4
  78. package/dist/types/interfaces/graph.d.ts +1 -1
  79. package/dist/types/types/common.d.ts +2 -2
  80. package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
  81. package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
  82. package/dist/types/types/utils/validate-type.d.ts +4 -4
  83. package/dist/umd/data-structure-typed.js +28022 -14573
  84. package/dist/umd/data-structure-typed.min.js +9 -3
  85. package/docs-site-docusaurus/README.md +41 -0
  86. package/docs-site-docusaurus/docs/api/README.md +52 -0
  87. package/docs-site-docusaurus/docs/api/classes/AVLTree.md +6130 -0
  88. package/docs-site-docusaurus/docs/api/classes/AVLTreeNode.md +282 -0
  89. package/docs-site-docusaurus/docs/api/classes/AbstractGraph.md +2266 -0
  90. package/docs-site-docusaurus/docs/api/classes/BST.md +5831 -0
  91. package/docs-site-docusaurus/docs/api/classes/BSTNode.md +333 -0
  92. package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +455 -0
  93. package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +4647 -0
  94. package/docs-site-docusaurus/docs/api/classes/BinaryTreeNode.md +331 -0
  95. package/docs-site-docusaurus/docs/api/classes/Deque.md +2767 -0
  96. package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +2999 -0
  97. package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +2685 -0
  98. package/docs-site-docusaurus/docs/api/classes/DoublyLinkedListNode.md +221 -0
  99. package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +253 -0
  100. package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +21 -0
  101. package/docs-site-docusaurus/docs/api/classes/HashMap.md +1333 -0
  102. package/docs-site-docusaurus/docs/api/classes/Heap.md +1881 -0
  103. package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +800 -0
  104. package/docs-site-docusaurus/docs/api/classes/IterableEntryBase.md +644 -0
  105. package/docs-site-docusaurus/docs/api/classes/LinearBase.md +1632 -0
  106. package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +1853 -0
  107. package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +1108 -0
  108. package/docs-site-docusaurus/docs/api/classes/LinkedListNode.md +156 -0
  109. package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +2824 -0
  110. package/docs-site-docusaurus/docs/api/classes/MapGraph.md +2929 -0
  111. package/docs-site-docusaurus/docs/api/classes/Matrix.md +1026 -0
  112. package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +1866 -0
  113. package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +1883 -0
  114. package/docs-site-docusaurus/docs/api/classes/MinHeap.md +1879 -0
  115. package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +1882 -0
  116. package/docs-site-docusaurus/docs/api/classes/Navigator.md +109 -0
  117. package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +1839 -0
  118. package/docs-site-docusaurus/docs/api/classes/Queue.md +2244 -0
  119. package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +6374 -0
  120. package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +372 -0
  121. package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +2897 -0
  122. package/docs-site-docusaurus/docs/api/classes/SinglyLinkedListNode.md +169 -0
  123. package/docs-site-docusaurus/docs/api/classes/SkipList.md +1229 -0
  124. package/docs-site-docusaurus/docs/api/classes/Stack.md +1573 -0
  125. package/docs-site-docusaurus/docs/api/classes/TreeMap.md +1257 -0
  126. package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +1475 -0
  127. package/docs-site-docusaurus/docs/api/classes/TreeSet.md +1117 -0
  128. package/docs-site-docusaurus/docs/api/classes/Trie.md +1708 -0
  129. package/docs-site-docusaurus/docs/api/classes/TrieNode.md +199 -0
  130. package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +2979 -0
  131. package/docs-site-docusaurus/docs/guide/_category_.json +6 -0
  132. package/docs-site-docusaurus/docs/guide/architecture.md +613 -0
  133. package/docs-site-docusaurus/docs/guide/concepts.md +420 -0
  134. package/docs-site-docusaurus/docs/guide/guides.md +611 -0
  135. package/docs-site-docusaurus/docs/guide/installation.md +60 -0
  136. package/docs-site-docusaurus/docs/guide/integrations.md +823 -0
  137. package/docs-site-docusaurus/docs/guide/overview.md +638 -0
  138. package/docs-site-docusaurus/docs/guide/performance.md +833 -0
  139. package/docs-site-docusaurus/docs/guide/quick-start.md +73 -0
  140. package/docs-site-docusaurus/docusaurus.config.ts +159 -0
  141. package/docs-site-docusaurus/fix-mdx-generics.mjs +75 -0
  142. package/docs-site-docusaurus/package-lock.json +18667 -0
  143. package/docs-site-docusaurus/package.json +50 -0
  144. package/docs-site-docusaurus/prefix-class-to-methods.mjs +48 -0
  145. package/docs-site-docusaurus/sidebars.ts +23 -0
  146. package/docs-site-docusaurus/sort-protected.mjs +87 -0
  147. package/docs-site-docusaurus/src/css/custom.css +96 -0
  148. package/docs-site-docusaurus/src/pages/index.module.css +13 -0
  149. package/docs-site-docusaurus/src/pages/index.tsx +71 -0
  150. package/docs-site-docusaurus/src/pages/markdown-page.md +7 -0
  151. package/docs-site-docusaurus/src/theme/TOCItems/index.tsx +34 -0
  152. package/docs-site-docusaurus/static/.nojekyll +0 -0
  153. package/docs-site-docusaurus/static/img/docusaurus-social-card.jpg +0 -0
  154. package/docs-site-docusaurus/static/img/docusaurus.png +0 -0
  155. package/docs-site-docusaurus/static/img/favicon.ico +0 -0
  156. package/docs-site-docusaurus/static/img/favicon.png +0 -0
  157. package/docs-site-docusaurus/static/img/logo-180.png +0 -0
  158. package/docs-site-docusaurus/static/img/logo.jpg +0 -0
  159. package/docs-site-docusaurus/static/img/logo.png +0 -0
  160. package/docs-site-docusaurus/static/img/logo.svg +1 -0
  161. package/docs-site-docusaurus/static/img/og-image.png +0 -0
  162. package/docs-site-docusaurus/static/img/undraw_docusaurus_mountain.svg +171 -0
  163. package/docs-site-docusaurus/static/img/undraw_docusaurus_react.svg +170 -0
  164. package/docs-site-docusaurus/static/img/undraw_docusaurus_tree.svg +40 -0
  165. package/docs-site-docusaurus/static/robots.txt +4 -0
  166. package/docs-site-docusaurus/typedoc.json +23 -0
  167. package/package.json +106 -10
  168. package/src/data-structures/base/index.ts +1 -0
  169. package/src/data-structures/base/iterable-entry-base.ts +8 -8
  170. package/src/data-structures/base/linear-base.ts +3 -3
  171. package/src/data-structures/binary-tree/avl-tree.ts +240 -0
  172. package/src/data-structures/binary-tree/binary-indexed-tree.ts +281 -1
  173. package/src/data-structures/binary-tree/binary-tree.ts +502 -2
  174. package/src/data-structures/binary-tree/bst.ts +481 -1
  175. package/src/data-structures/binary-tree/red-black-tree.ts +380 -0
  176. package/src/data-structures/binary-tree/segment-tree.ts +121 -2
  177. package/src/data-structures/binary-tree/tree-map.ts +2862 -482
  178. package/src/data-structures/binary-tree/tree-multi-map.ts +2963 -563
  179. package/src/data-structures/binary-tree/tree-multi-set.ts +2399 -479
  180. package/src/data-structures/binary-tree/tree-set.ts +2685 -405
  181. package/src/data-structures/graph/abstract-graph.ts +4 -4
  182. package/src/data-structures/graph/directed-graph.ts +200 -0
  183. package/src/data-structures/graph/undirected-graph.ts +180 -0
  184. package/src/data-structures/hash/hash-map.ts +235 -15
  185. package/src/data-structures/heap/heap.ts +280 -0
  186. package/src/data-structures/linked-list/doubly-linked-list.ts +343 -3
  187. package/src/data-structures/linked-list/singly-linked-list.ts +303 -3
  188. package/src/data-structures/linked-list/skip-linked-list.ts +362 -2
  189. package/src/data-structures/matrix/matrix.ts +161 -1
  190. package/src/data-structures/queue/deque.ts +305 -5
  191. package/src/data-structures/queue/queue.ts +240 -0
  192. package/src/data-structures/stack/stack.ts +200 -0
  193. package/src/data-structures/trie/trie.ts +245 -5
  194. package/src/interfaces/graph.ts +1 -1
  195. package/src/types/common.ts +2 -2
  196. package/src/types/data-structures/heap/heap.ts +1 -0
  197. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
  198. package/src/types/utils/validate-type.ts +4 -4
  199. package/vercel.json +6 -0
  200. package/dist/leetcode/avl-tree-counter.mjs +0 -2957
  201. package/dist/leetcode/avl-tree-multi-map.mjs +0 -2889
  202. package/dist/leetcode/avl-tree.mjs +0 -2720
  203. package/dist/leetcode/binary-tree.mjs +0 -1594
  204. package/dist/leetcode/bst.mjs +0 -2398
  205. package/dist/leetcode/deque.mjs +0 -683
  206. package/dist/leetcode/directed-graph.mjs +0 -1733
  207. package/dist/leetcode/doubly-linked-list.mjs +0 -709
  208. package/dist/leetcode/hash-map.mjs +0 -493
  209. package/dist/leetcode/heap.mjs +0 -542
  210. package/dist/leetcode/max-heap.mjs +0 -375
  211. package/dist/leetcode/max-priority-queue.mjs +0 -383
  212. package/dist/leetcode/min-heap.mjs +0 -363
  213. package/dist/leetcode/min-priority-queue.mjs +0 -371
  214. package/dist/leetcode/priority-queue.mjs +0 -363
  215. package/dist/leetcode/queue.mjs +0 -943
  216. package/dist/leetcode/red-black-tree.mjs +0 -2765
  217. package/dist/leetcode/singly-linked-list.mjs +0 -754
  218. package/dist/leetcode/stack.mjs +0 -217
  219. package/dist/leetcode/tree-counter.mjs +0 -3039
  220. package/dist/leetcode/tree-multi-map.mjs +0 -2913
  221. package/dist/leetcode/trie.mjs +0 -413
  222. package/dist/leetcode/undirected-graph.mjs +0 -1650
@@ -0,0 +1,420 @@
1
+ ---
2
+ sidebar_label: "CONCEPTS"
3
+ description: "Core concepts behind data-structure-typed: uniform API, iterators, generics, comparators, and the 5 design traits."
4
+ ---
5
+
6
+ # CONCEPTS
7
+
8
+ This guide explains the foundational concepts behind data structures through plain language and practical understanding.
9
+
10
+ **👈 [Back to README](/.md) • [API Docs](https://data-structure-typed-docs.vercel.app/) • [Real-World Guides](/guide/guides.md)**
11
+
12
+ ---
13
+
14
+ ## Table of Contents
15
+
16
+ 1. [The Big Three Concepts](#the-big-three-concepts)
17
+ 2. [Plain Language Explanations](#-plain-language-explanations)
18
+ 3. [Iterator Protocol Design](#iterator-protocol-design)
19
+ 4. [Seamless Interoperability](#-seamless-interoperability-iterator-protocol-everywhere)
20
+ 5. [All Array Methods Work Everywhere](#-all-array-methods-work-everywhere)
21
+ 6. [Why Not Just Use Native JavaScript?](#why-not-just-use-native-javascript)
22
+ 7. [Decision Guide](#-decision-guide-choose-the-right-data-structure)
23
+
24
+ ---
25
+
26
+ ## The Big Three Concepts
27
+
28
+ ### 1. **Binary Search Tree (BST)** — O(log n) search/insert/delete
29
+
30
+ Maintains sorted order by keeping all left children smaller and right children larger than each node.
31
+
32
+ ```javascript
33
+ // Property: For any node
34
+ // All left subtree values < node value
35
+ // All right subtree values > node value
36
+
37
+ // 5
38
+ // / \
39
+ // 3 8
40
+ // / \ \
41
+ // 1 4 9
42
+ ```
43
+
44
+ **Advantage**: Fast operations without pre-sorting
45
+ **Trade-off**: Unbalanced trees degrade to O(n)
46
+
47
+ ### 2. **Balanced Trees (AVL, Red-Black)** — Auto-rebalancing
48
+
49
+ Automatically reorganize themselves to maintain O(log n) guarantees even after insertions/deletions.
50
+
51
+ ```javascript
52
+ // Red-Black Tree: Color rules ensure balance
53
+ // AVL Tree: Height difference ≤ 1
54
+
55
+ // Both: Insert = O(log n), Delete = O(log n), Search = O(log n) always
56
+ ```
57
+
58
+ **Advantage**: Guaranteed O(log n) performance
59
+ **Cost**: Rebalancing overhead on every modification
60
+
61
+ ### 3. **Heap** — Parent-child priority relationships
62
+
63
+ A complete binary tree where parent always has priority over children.
64
+
65
+ ```javascript
66
+ // Max Heap: // Min Heap:
67
+ // 9 1
68
+ // / \ / \
69
+ // 7 8 2 3
70
+ // / \ / \
71
+ // 3 2 8 9
72
+
73
+ // Parent = 1.5x better than children
74
+ // Root always has best priority
75
+ ```
76
+
77
+ **Advantage**: Very fast to get highest/lowest priority
78
+ **Perfect for**: Priority queues, heap sort
79
+
80
+ ---
81
+
82
+ ## 🌍 Plain Language Explanations
83
+
84
+ For those who love understanding concepts through metaphors:
85
+
86
+ | Data Structure | Plain Language Definition | Example |
87
+ |------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------|
88
+ | **Linked List** | A line of bunnies, where each bunny holds the tail of the bunny in front of it. You want to find a bunny named Pablo, and you have to start searching from the first bunny. If it's not Pablo, you continue following that bunny's tail to the next one. So, you might need to search n times to find Pablo (O(n) time complexity). If you want to insert a bunny named Remi between Pablo and Vicky, it's very simple. You just need to let Vicky release Pablo's tail, let Remi hold Pablo's tail, and then let Vicky hold Remi's tail (O(1) time complexity). | To find bunny "Pablo", start from the first bunny and follow tails until found |
89
+ | **Array** | A line of numbered bunnies. If you want to find the bunny named Pablo, you can directly shout out Pablo's number 0680 (finding the element directly through array indexing, O(1) time complexity). However, if you don't know Pablo's number, you still need to search one by one (O(n) time complexity). Moreover, if you want to add a bunny named Vicky behind Pablo, you will need to renumber all the bunnies after Vicky (O(n) time complexity). | Finding element by index is instant, but inserting in the middle is slow |
90
+ | **Queue** | A line of numbered bunnies with a sticky note on the first bunny. For this line with a sticky note on the first bunny, whenever we want to remove a bunny from the front of the line, we only need to move the sticky note to the face of the next bunny without actually removing the bunny to avoid renumbering all the bunnies behind (removing from the front is also O(1) time complexity). For the tail of the line, we don't need to worry because each new bunny added to the tail is directly given a new number (O(1) time complexity) without needing to renumber all the previous bunnies. | Process items in FIFO order, efficiently from both ends |
91
+ | **Deque** | A line of grouped, numbered bunnies with a sticky note on the first bunny. For this line, we manage it by groups. Each time we remove a bunny from the front of the line, we only move the sticky note to the next bunny. This way, we don't need to renumber all the bunnies behind the first bunny each time a bunny is removed. Only when all members of a group are removed do we reassign numbers and regroup. The tail is handled similarly. This is a strategy of delaying and batching operations to offset the drawbacks of the Array data structure that requires moving all elements behind when inserting or deleting elements in the middle. | Efficient removal/insertion from both ends with batching optimization |
92
+ | **Stack** | A line of bunnies in a dead-end tunnel, where bunnies can only be removed from the tunnel entrance (end), and new bunnies can only be added at the entrance (end) as well. | Process items in LIFO order; undo/redo functionality |
93
+ | **Binary Tree** | A tree where each node has at most two children. | Hierarchical data organization |
94
+ | **Binary Search Tree** | A tree where all nodes in the left subtree are less than the node, and all nodes in the right subtree are greater than the node. Maintaining O(log n) for all operations. | Efficient search/insert/delete without re-sorting |
95
+ | **Red-Black Tree** | A self-balancing BST that automatically maintains balance through color-coding rules. | Used in Java TreeMap and maintains O(log n) guarantees |
96
+ | **AVL Tree** | A stricter self-balancing BST with stricter balance requirements than Red-Black trees. | Maximum search speed with slower insertions/deletions |
97
+ | **Heap** | A special binary tree stored in an array where parent always maintains priority relationship to children. | Efficient priority queue; heap sort |
98
+ | **Trie** | A tree of characters used for prefix-based searching. | Autocomplete, spell checking |
99
+ | **Graph** | A network of vertices (nodes) connected by edges. | Model relationships, networks |
100
+ | **SkipList** | A linked list with extra "express lanes" — higher levels skip over many nodes, giving probabilistic O(log n) like a balanced BST without rotations. | Sorted key-value store; simpler than Red-Black Tree, same average performance |
101
+ | **SegmentTree** | A binary tree where each node stores the aggregate (sum/min/max) of a range. Queries work by combining only the nodes that cover the target range. | Range sum/min/max queries with point updates; e.g. profit over date range |
102
+ | **BinaryIndexedTree** | A compact array where each cell stores partial sums using bit manipulation tricks. Much simpler than SegmentTree but only supports prefix sums. | Prefix sums, frequency counting, inversion counting |
103
+ | **Matrix** | A 2D grid of numbers supporting standard linear algebra operations. | 2D grid transformations, linear algebra |
104
+
105
+ ---
106
+
107
+ ## Iterator Protocol Design
108
+
109
+ ### The Hidden Superpower
110
+
111
+ Every single data structure in this library implements the **Iterator protocol**:
112
+
113
+ - ✅ Spread operator: `[...tree]`
114
+ - ✅ for...of loops: `for (const item of tree)`
115
+ - ✅ Destructuring: `const [a, b, c] = tree`
116
+ - ✅ Array.from(): `Array.from(tree)`
117
+ - ✅ Set/Map constructors: `new Set(tree)`
118
+
119
+ ### Iterator Support Comparison
120
+
121
+ | Feature | Array | Map | Set | Other Lib | data-structure-typed |
122
+ |----------------------|-------|------|-----|-----------|----------------------|
123
+ | Spread operator | ✅ | ❌/⚠️ | ✅ | ❌/⚠️ | ✅ |
124
+ | for...of loop | ✅ | ✅ | ✅ | ❌/⚠️ | ✅ |
125
+ | Destructuring | ✅ | ❌ | ❌ | ❌ | ✅ |
126
+ | Array.from() | ✅ | ❌/⚠️ | ❌ | ❌/⚠️ | ✅ |
127
+ | Set constructor | ✅ | ❌ | ✅ | ❌ | ✅ |
128
+ | **Full Integration** | ✅ | ⚠️ | ⚠️ | ⚠️ | **✅** |
129
+
130
+ ### Live Examples: Zero Friction Conversions
131
+
132
+ #### Example 1: Array to Tree to Array
133
+
134
+ ```javascript
135
+ const array = [64, 34, 25, 12, 22, 11, 90];
136
+ const rbTree = new RedBlackTree(array);
137
+ const sorted = [...rbTree.keys()];
138
+ console.log(sorted); // [11, 12, 22, 25, 34, 64, 90] ✅
139
+ ```
140
+
141
+ #### Example 2: Extract Keys and Values
142
+
143
+ ```javascript
144
+ const rbTree = new RedBlackTree([
145
+ [1, 'Alice'],
146
+ [2, 'Bob'],
147
+ [3, 'Charlie']
148
+ ]);
149
+
150
+ const allKeys = [...rbTree.keys()]; // [1, 2, 3]
151
+ const allValues = [...rbTree.values()]; // ['Alice', 'Bob', 'Charlie']
152
+ ```
153
+
154
+ #### Example 3: for...of on Any Structure
155
+
156
+ ```javascript
157
+ const tree = new RedBlackTree(entries);
158
+ const deque = new Deque(items);
159
+ const heap = new MaxHeap(items);
160
+
161
+ for (const entry of tree) console.log(entry);
162
+ for (const item of deque) console.log(item);
163
+ for (const item of heap) console.log(item);
164
+ ```
165
+
166
+ ---
167
+
168
+ ## 🔗 Seamless Interoperability: Iterator Protocol Everywhere
169
+
170
+ ### The Design Philosophy
171
+
172
+ Instead of forcing conversions between data structures, we made every structure speak the same language as JavaScript's native iterables. This means:
173
+
174
+ - You can pass any data structure to `Array.from()`
175
+ - You can destructure any data structure
176
+ - You can spread any data structure
177
+ - You can loop over any data structure with `for...of`
178
+
179
+ This is **zero friction** because you use the same mental model.
180
+
181
+ ---
182
+
183
+ ## 🎁 All Array Methods Work Everywhere
184
+
185
+ ### The Biggest Developer Joy: Array Methods, Everywhere
186
+
187
+ You know these methods. You use them every day. They work on **every data structure**:
188
+
189
+ #### Chain on Tree
190
+
191
+ ```typescript
192
+ const rbTree = new RedBlackTree([
193
+ [1, { name: 'Alice', age: 25 }],
194
+ [2, { name: 'Bob', age: 30 }],
195
+ [3, { name: 'Charlie', age: 28 }],
196
+ ]);
197
+
198
+ const result = rbTree
199
+ .filter((value, _key) => (value?.age ?? 0) > 26)
200
+ .map((value, key) => [key, { ...value, id: key }])
201
+ .reduce((sum, value) => sum + (value?.age ?? 0), 0);
202
+
203
+ console.log(result); // 58 ✅
204
+ ```
205
+
206
+ #### Chain on Heap
207
+
208
+ ```typescript
209
+ const minHeap = new Heap(
210
+ [
211
+ { priority: 5, task: 'Email' },
212
+ { priority: 3, task: 'Chat' },
213
+ { priority: 8, task: 'Alert' },
214
+ ],
215
+ { comparator: (a, b) => a.priority - b.priority }
216
+ );
217
+
218
+ const urgent = minHeap
219
+ .filter((value, _key) => value.priority > 4)
220
+ .map((value, _key) => value.task, {
221
+ comparator: (a, b) => a.localeCompare(b),
222
+ });
223
+
224
+ urgent.print(); // ['Alert', 'Email'] ✅
225
+ ```
226
+
227
+ #### Chain on Deque
228
+
229
+ ```typescript
230
+ const deque = new Deque([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
231
+
232
+ const stats = {
233
+ even: deque.filter((item) => item % 2 === 0).toArray(),
234
+ squared: deque.map((item) => item * item).toArray(),
235
+ hasLarge: deque.some((item) => item > 8),
236
+ sum: deque.reduce((acc, item) => acc + item, 0),
237
+ };
238
+ ```
239
+
240
+ ### Supported Methods Across All Structures
241
+
242
+ | Method | BinaryTrees | Heap | Deque | Graph | LinkedList |
243
+ |-------------|-------------|------|-------|-------|------------|
244
+ | map | ✅ | ✅ | ✅ | ✅ | ✅ |
245
+ | filter | ✅ | ✅ | ✅ | ✅ | ✅ |
246
+ | reduce | ✅ | ✅ | ✅ | ✅ | ✅ |
247
+ | find | ✅ | ✅ | ✅ | ✅ | ✅ |
248
+ | some/every | ✅ | ✅ | ✅ | ✅ | ✅ |
249
+ | keys/values | ✅ | ✅ | ✅ | ✅ | ✅ |
250
+ | forEach | ✅ | ✅ | ✅ | ✅ | ✅ |
251
+
252
+ ---
253
+
254
+ ## Why Not Just Use Native JavaScript?
255
+
256
+ ### Case 1: Map Doesn't Maintain Sorted Order
257
+
258
+ ❌ Map iteration is insertion order, not key order:
259
+
260
+ ```javascript
261
+ const map = new Map([[5, 'E'], [2, 'B'], [8, 'H'], [1, 'A']]);
262
+ for (const [key, value] of map) {
263
+ console.log(key); // 5, 2, 8, 1 (insertion order)
264
+ }
265
+ ```
266
+
267
+ ✅ RedBlackTree maintains sorted order automatically:
268
+
269
+ ```javascript
270
+ const tree = new RedBlackTree([[5, 'E'], [2, 'B'], [8, 'H'], [1, 'A']]);
271
+ for (const [key, value] of tree) {
272
+ console.log(key); // 1, 2, 5, 8 (key order) ✅
273
+ }
274
+ ```
275
+
276
+ ### Case 2: Array.shift is Too Slow
277
+
278
+ ❌ Array.shift is O(n):
279
+
280
+ ```javascript
281
+ const queue = [];
282
+ for (let i = 0; i < 100000; i++) queue.push(i);
283
+ for (let i = 0; i < 100000; i++) queue.shift();
284
+ // Time: 2829ms ❌
285
+ ```
286
+
287
+ ✅ Deque.shift is O(1):
288
+
289
+ ```javascript
290
+ const deque = new Deque();
291
+ for (let i = 0; i < 100000; i++) deque.push(i);
292
+ for (let i = 0; i < 100000; i++) deque.shift();
293
+ // Time: 5.83ms ✅
294
+ ```
295
+
296
+ ### Case 3: Maintaining Priority is Manual
297
+
298
+ ❌ Array requires re-sorting O(n log n):
299
+
300
+ ```javascript
301
+ const tasks = [];
302
+
303
+ function addTask(task) {
304
+ tasks.push(task);
305
+ tasks.sort((a, b) => b.priority - a.priority);
306
+ } // O(n² log n)
307
+ ```
308
+
309
+ ✅ PriorityQueue maintains priority O(log n):
310
+
311
+ ```javascript
312
+ const pq = new MaxPriorityQueue();
313
+
314
+ function addTask(task) {
315
+ pq.add(task); // O(log n)
316
+ } // O(n log n)
317
+ ```
318
+
319
+ ### Case 4: Range Queries are Tedious
320
+
321
+ ❌ Array.filter is O(n):
322
+
323
+ ```javascript
324
+ const prices = [10, 45, 23, 67, 89, 12, 54, 33, 78];
325
+ const inRange = prices.filter(p => p >= 30 && p <= 70);
326
+ ```
327
+
328
+ ✅ RedBlackTree range queries are O(log n + k):
329
+
330
+ ```javascript
331
+ const tree = new RedBlackTree(prices);
332
+ const inRange = tree.rangeSearch([30, 70]);
333
+ ```
334
+
335
+ ### Case 5: Prefix Matching is Tedious
336
+
337
+ ❌ Array.filter is O(n*m):
338
+
339
+ ```javascript
340
+ const words = ['apple', 'app', 'apply', 'application'];
341
+ const matches = words.filter(w => w.startsWith('app'));
342
+ // For 1M words: checks 1M words ❌
343
+ ```
344
+
345
+ ✅ Trie prefix matching is O(m + k):
346
+
347
+ ```javascript
348
+ const trie = new Trie(words);
349
+ const matches = trie.getWords('appl');
350
+ // O(5 + 4) = 9 operations ✅
351
+ ```
352
+
353
+ ---
354
+
355
+ ## 🎯 Decision Guide: Choose the Right Data Structure
356
+
357
+ ```
358
+ Need frequent head/tail operations?
359
+
360
+ Yes → Deque (O(1) shift/unshift)
361
+ No → Continue
362
+
363
+ Need sorted + fast queries?
364
+
365
+ Yes → RedBlackTree (O(log n) search)
366
+ No → Continue
367
+
368
+ Need priority handling?
369
+
370
+ Yes → PriorityQueue (O(log n) add)
371
+ No → Continue
372
+
373
+ Need prefix matching?
374
+
375
+ Yes → Trie (O(m + k) search)
376
+ No → Continue
377
+
378
+ Need graph algorithms?
379
+
380
+ Yes → DirectedGraph / UndirectedGraph
381
+ No → Continue
382
+
383
+ Need range queries on an indexed sequence?
384
+
385
+ Yes → SegmentTree (O(log n) query + update, supports sum/min/max/gcd/custom)
386
+ Only need prefix sums? → BinaryIndexedTree (simpler, less memory)
387
+ No → Continue
388
+
389
+ Need a sorted key-value map?
390
+
391
+ Yes → TreeMap (guaranteed O(log n) via Red-Black Tree)
392
+ Want simpler implementation, same API? → SkipList (O(log n) average, probabilistic)
393
+ No → Use Array
394
+ ```
395
+
396
+ ---
397
+
398
+ ## Next Steps
399
+
400
+ **Understand the basics?**
401
+ → [See real-world examples](/guide/guides.md)
402
+
403
+ **Want to use immediately?**
404
+ → [Full API Docs](https://data-structure-typed-docs.vercel.app/)
405
+
406
+ **Curious about performance?**
407
+ → [Read performance comparison](/guide/performance.md)
408
+
409
+ **Want to know how it's implemented?**
410
+ → [See architecture details](/guide/architecture.md)
411
+
412
+ ---
413
+
414
+ **Related:**
415
+
416
+ - [OVERVIEW.md](/guide/overview.md) - API / structures / methods
417
+ - [GUIDES.md](/guide/guides.md) - Leaderboard / LRU / Queue / real-world examples
418
+ - [ARCHITECTURE.md](/guide/architecture.md) - Design / JIT / internal abstractions
419
+ - [PERFORMANCE.md](/guide/performance.md) - Benchmarks / comparisons
420
+ - [INTEGRATIONS.md](/guide/integrations.md) - React / Nest / Express