data-structure-typed 2.5.2 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (162) hide show
  1. package/.husky/pre-commit +3 -0
  2. package/CHANGELOG.md +3 -1
  3. package/MIGRATION.md +217 -0
  4. package/README.md +80 -8
  5. package/README_CN.md +569 -143
  6. package/SPECIFICATION.md +44 -14
  7. package/SPECIFICATION.zh-CN.md +44 -14
  8. package/dist/cjs/binary-tree.cjs +5841 -1678
  9. package/dist/cjs/graph.cjs +422 -14
  10. package/dist/cjs/hash.cjs +95 -7
  11. package/dist/cjs/heap.cjs +174 -16
  12. package/dist/cjs/index.cjs +7751 -2449
  13. package/dist/cjs/linked-list.cjs +443 -2
  14. package/dist/cjs/matrix.cjs +56 -0
  15. package/dist/cjs/priority-queue.cjs +172 -14
  16. package/dist/cjs/queue.cjs +435 -0
  17. package/dist/cjs/stack.cjs +103 -4
  18. package/dist/cjs/trie.cjs +106 -0
  19. package/dist/cjs-legacy/binary-tree.cjs +5933 -1772
  20. package/dist/cjs-legacy/graph.cjs +422 -14
  21. package/dist/cjs-legacy/hash.cjs +95 -7
  22. package/dist/cjs-legacy/heap.cjs +174 -16
  23. package/dist/cjs-legacy/index.cjs +8154 -2854
  24. package/dist/cjs-legacy/linked-list.cjs +443 -2
  25. package/dist/cjs-legacy/matrix.cjs +56 -0
  26. package/dist/cjs-legacy/priority-queue.cjs +172 -14
  27. package/dist/cjs-legacy/queue.cjs +435 -0
  28. package/dist/cjs-legacy/stack.cjs +103 -4
  29. package/dist/cjs-legacy/trie.cjs +106 -0
  30. package/dist/esm/binary-tree.mjs +5841 -1678
  31. package/dist/esm/graph.mjs +422 -14
  32. package/dist/esm/hash.mjs +95 -7
  33. package/dist/esm/heap.mjs +174 -16
  34. package/dist/esm/index.mjs +7751 -2449
  35. package/dist/esm/linked-list.mjs +443 -2
  36. package/dist/esm/matrix.mjs +56 -0
  37. package/dist/esm/priority-queue.mjs +172 -14
  38. package/dist/esm/queue.mjs +435 -0
  39. package/dist/esm/stack.mjs +103 -4
  40. package/dist/esm/trie.mjs +106 -0
  41. package/dist/esm-legacy/binary-tree.mjs +5933 -1772
  42. package/dist/esm-legacy/graph.mjs +422 -14
  43. package/dist/esm-legacy/hash.mjs +95 -7
  44. package/dist/esm-legacy/heap.mjs +174 -16
  45. package/dist/esm-legacy/index.mjs +8154 -2854
  46. package/dist/esm-legacy/linked-list.mjs +443 -2
  47. package/dist/esm-legacy/matrix.mjs +56 -0
  48. package/dist/esm-legacy/priority-queue.mjs +172 -14
  49. package/dist/esm-legacy/queue.mjs +435 -0
  50. package/dist/esm-legacy/stack.mjs +103 -4
  51. package/dist/esm-legacy/trie.mjs +106 -0
  52. package/dist/types/data-structures/base/iterable-element-base.d.ts +17 -0
  53. package/dist/types/data-structures/base/linear-base.d.ts +6 -0
  54. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +86 -2
  55. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +98 -0
  56. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +191 -15
  57. package/dist/types/data-structures/binary-tree/bst.d.ts +171 -3
  58. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +136 -8
  59. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +42 -0
  60. package/dist/types/data-structures/binary-tree/tree-map.d.ts +1061 -167
  61. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1232 -355
  62. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +916 -194
  63. package/dist/types/data-structures/binary-tree/tree-set.d.ts +1078 -141
  64. package/dist/types/data-structures/graph/directed-graph.d.ts +70 -0
  65. package/dist/types/data-structures/graph/undirected-graph.d.ts +63 -0
  66. package/dist/types/data-structures/hash/hash-map.d.ts +84 -6
  67. package/dist/types/data-structures/heap/heap.d.ts +140 -12
  68. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +150 -2
  69. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +106 -1
  70. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +126 -0
  71. package/dist/types/data-structures/matrix/matrix.d.ts +56 -0
  72. package/dist/types/data-structures/queue/deque.d.ts +171 -0
  73. package/dist/types/data-structures/queue/queue.d.ts +97 -0
  74. package/dist/types/data-structures/stack/stack.d.ts +72 -2
  75. package/dist/types/data-structures/trie/trie.d.ts +84 -0
  76. package/dist/types/interfaces/binary-tree.d.ts +2 -3
  77. package/dist/umd/data-structure-typed.js +7784 -2484
  78. package/dist/umd/data-structure-typed.min.js +4 -4
  79. package/docs-site-docusaurus/docs/api/classes/AVLTree.md +188 -200
  80. package/docs-site-docusaurus/docs/api/classes/AVLTreeNode.md +11 -11
  81. package/docs-site-docusaurus/docs/api/classes/AbstractGraph.md +62 -62
  82. package/docs-site-docusaurus/docs/api/classes/BST.md +183 -195
  83. package/docs-site-docusaurus/docs/api/classes/BSTNode.md +13 -13
  84. package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +15 -15
  85. package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +143 -155
  86. package/docs-site-docusaurus/docs/api/classes/BinaryTreeNode.md +13 -13
  87. package/docs-site-docusaurus/docs/api/classes/Deque.md +99 -85
  88. package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +73 -73
  89. package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +100 -70
  90. package/docs-site-docusaurus/docs/api/classes/DoublyLinkedListNode.md +8 -8
  91. package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +12 -12
  92. package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +1 -1
  93. package/docs-site-docusaurus/docs/api/classes/HashMap.md +38 -38
  94. package/docs-site-docusaurus/docs/api/classes/Heap.md +96 -85
  95. package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +25 -25
  96. package/docs-site-docusaurus/docs/api/classes/IterableEntryBase.md +23 -23
  97. package/docs-site-docusaurus/docs/api/classes/LinearBase.md +48 -48
  98. package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +52 -52
  99. package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +46 -42
  100. package/docs-site-docusaurus/docs/api/classes/LinkedListNode.md +6 -6
  101. package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +74 -74
  102. package/docs-site-docusaurus/docs/api/classes/MapGraph.md +73 -73
  103. package/docs-site-docusaurus/docs/api/classes/Matrix.md +31 -31
  104. package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +104 -89
  105. package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +104 -89
  106. package/docs-site-docusaurus/docs/api/classes/MinHeap.md +104 -89
  107. package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +104 -89
  108. package/docs-site-docusaurus/docs/api/classes/Navigator.md +5 -5
  109. package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +103 -88
  110. package/docs-site-docusaurus/docs/api/classes/Queue.md +111 -59
  111. package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +200 -212
  112. package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +10 -10
  113. package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +75 -75
  114. package/docs-site-docusaurus/docs/api/classes/SinglyLinkedListNode.md +6 -6
  115. package/docs-site-docusaurus/docs/api/classes/SkipList.md +37 -37
  116. package/docs-site-docusaurus/docs/api/classes/Stack.md +42 -42
  117. package/docs-site-docusaurus/docs/api/classes/TreeMap.md +107 -36
  118. package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +43 -43
  119. package/docs-site-docusaurus/docs/api/classes/TreeSet.md +106 -35
  120. package/docs-site-docusaurus/docs/api/classes/Trie.md +43 -43
  121. package/docs-site-docusaurus/docs/api/classes/TrieNode.md +8 -8
  122. package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +72 -72
  123. package/docs-site-docusaurus/docs/guide/architecture.md +75 -7
  124. package/docs-site-docusaurus/docs/guide/concepts.md +53 -34
  125. package/docs-site-docusaurus/docs/guide/faq.md +53 -0
  126. package/docs-site-docusaurus/docs/guide/guides.md +8 -9
  127. package/docs-site-docusaurus/docs/guide/integrations.md +74 -177
  128. package/docs-site-docusaurus/docs/guide/overview.md +131 -17
  129. package/docs-site-docusaurus/src/pages/index.tsx +4 -0
  130. package/docs-site-docusaurus/typedoc.json +1 -0
  131. package/jest.integration.config.js +1 -2
  132. package/package.json +10 -7
  133. package/src/data-structures/base/iterable-element-base.ts +32 -0
  134. package/src/data-structures/base/linear-base.ts +11 -0
  135. package/src/data-structures/binary-tree/avl-tree.ts +88 -5
  136. package/src/data-structures/binary-tree/binary-indexed-tree.ts +98 -0
  137. package/src/data-structures/binary-tree/binary-tree.ts +242 -81
  138. package/src/data-structures/binary-tree/bst.ts +173 -7
  139. package/src/data-structures/binary-tree/red-black-tree.ts +139 -15
  140. package/src/data-structures/binary-tree/segment-tree.ts +42 -0
  141. package/src/data-structures/binary-tree/tree-map.ts +948 -36
  142. package/src/data-structures/binary-tree/tree-multi-map.ts +893 -13
  143. package/src/data-structures/binary-tree/tree-multi-set.ts +761 -33
  144. package/src/data-structures/binary-tree/tree-set.ts +1260 -251
  145. package/src/data-structures/graph/directed-graph.ts +71 -1
  146. package/src/data-structures/graph/undirected-graph.ts +64 -1
  147. package/src/data-structures/hash/hash-map.ts +100 -12
  148. package/src/data-structures/heap/heap.ts +149 -19
  149. package/src/data-structures/linked-list/doubly-linked-list.ts +178 -2
  150. package/src/data-structures/linked-list/singly-linked-list.ts +106 -1
  151. package/src/data-structures/linked-list/skip-linked-list.ts +126 -0
  152. package/src/data-structures/matrix/matrix.ts +56 -0
  153. package/src/data-structures/queue/deque.ts +187 -0
  154. package/src/data-structures/queue/queue.ts +109 -0
  155. package/src/data-structures/stack/stack.ts +75 -5
  156. package/src/data-structures/trie/trie.ts +84 -0
  157. package/src/interfaces/binary-tree.ts +1 -9
  158. package/.vitepress/cache/deps_temp_51f5f1b0/chunk-7OIKW5WK.js +0 -12984
  159. package/.vitepress/cache/deps_temp_51f5f1b0/package.json +0 -3
  160. package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vue_devtools-api.js +0 -4505
  161. package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vueuse_core.js +0 -9731
  162. package/.vitepress/cache/deps_temp_51f5f1b0/vue.js +0 -347
@@ -149,6 +149,13 @@ var _Matrix = class _Matrix {
149
149
 
150
150
 
151
151
 
152
+
153
+
154
+
155
+
156
+
157
+
158
+
152
159
 
153
160
 
154
161
 
@@ -217,6 +224,13 @@ var _Matrix = class _Matrix {
217
224
 
218
225
 
219
226
 
227
+
228
+
229
+
230
+
231
+
232
+
233
+
220
234
 
221
235
 
222
236
 
@@ -281,6 +295,13 @@ var _Matrix = class _Matrix {
281
295
 
282
296
 
283
297
 
298
+
299
+
300
+
301
+
302
+
303
+
304
+
284
305
 
285
306
 
286
307
 
@@ -368,6 +389,13 @@ var _Matrix = class _Matrix {
368
389
 
369
390
 
370
391
 
392
+
393
+
394
+
395
+
396
+
397
+
398
+
371
399
 
372
400
 
373
401
 
@@ -438,6 +466,13 @@ var _Matrix = class _Matrix {
438
466
 
439
467
 
440
468
 
469
+
470
+
471
+
472
+
473
+
474
+
475
+
441
476
 
442
477
 
443
478
 
@@ -529,6 +564,13 @@ var _Matrix = class _Matrix {
529
564
 
530
565
 
531
566
 
567
+
568
+
569
+
570
+
571
+
572
+
573
+
532
574
 
533
575
 
534
576
 
@@ -607,6 +649,13 @@ var _Matrix = class _Matrix {
607
649
 
608
650
 
609
651
 
652
+
653
+
654
+
655
+
656
+
657
+
658
+
610
659
 
611
660
 
612
661
 
@@ -711,6 +760,13 @@ var _Matrix = class _Matrix {
711
760
 
712
761
 
713
762
 
763
+
764
+
765
+
766
+
767
+
768
+
769
+
714
770
 
715
771
 
716
772
 
@@ -185,6 +185,35 @@ var _IterableElementBase = class _IterableElementBase {
185
185
  for (const ele of this) if (ele === element) return true;
186
186
  return false;
187
187
  }
188
+ /**
189
+ * Check whether a value exists (Array-compatible alias for `has`).
190
+ * @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1).
191
+ * @param element - Element to search for (uses `===`).
192
+ * @returns `true` if found.
193
+ */
194
+ includes(element) {
195
+ return this.has(element);
196
+ }
197
+ /**
198
+ * Return an iterator of `[index, value]` pairs (Array-compatible).
199
+ * @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1) per step.
200
+ */
201
+ *entries() {
202
+ let index = 0;
203
+ for (const value of this) {
204
+ yield [index++, value];
205
+ }
206
+ }
207
+ /**
208
+ * Return an iterator of numeric indices (Array-compatible).
209
+ * @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1) per step.
210
+ */
211
+ *keys() {
212
+ let index = 0;
213
+ for (const _ of this) {
214
+ yield index++;
215
+ }
216
+ }
188
217
  /**
189
218
  * Reduces all elements to a single accumulated value.
190
219
  *
@@ -325,6 +354,13 @@ var _Heap = class _Heap extends IterableElementBase {
325
354
 
326
355
 
327
356
 
357
+
358
+
359
+
360
+
361
+
362
+
363
+
328
364
 
329
365
 
330
366
 
@@ -382,7 +418,7 @@ var _Heap = class _Heap extends IterableElementBase {
382
418
  }
383
419
  /**
384
420
  * Insert an element.
385
- * @remarks Time O(1) amortized, Space O(1)
421
+ * @remarks Time O(log N) amortized, Space O(1)
386
422
  * @param element - Element to insert.
387
423
  * @returns True.
388
424
 
@@ -412,6 +448,13 @@ var _Heap = class _Heap extends IterableElementBase {
412
448
 
413
449
 
414
450
 
451
+
452
+
453
+
454
+
455
+
456
+
457
+
415
458
 
416
459
 
417
460
 
@@ -469,6 +512,13 @@ var _Heap = class _Heap extends IterableElementBase {
469
512
 
470
513
 
471
514
 
515
+
516
+
517
+
518
+
519
+
520
+
521
+
472
522
 
473
523
 
474
524
 
@@ -532,7 +582,41 @@ var _Heap = class _Heap extends IterableElementBase {
532
582
 
533
583
 
534
584
 
585
+
586
+
587
+
535
588
 
589
+
590
+
591
+
592
+ * @example
593
+ * // Heap with custom comparator (MaxHeap behavior)
594
+ * interface Task {
595
+ * id: number;
596
+ * priority: number;
597
+ * name: string;
598
+ * }
599
+ *
600
+ * // Custom comparator for max heap behavior (higher priority first)
601
+ * const tasks: Task[] = [
602
+ * { id: 1, priority: 5, name: 'Email' },
603
+ * { id: 2, priority: 3, name: 'Chat' },
604
+ * { id: 3, priority: 8, name: 'Alert' }
605
+ * ];
606
+ *
607
+ * const maxHeap = new Heap(tasks, {
608
+ * comparator: (a: Task, b: Task) => b.priority - a.priority
609
+ * });
610
+ *
611
+ * console.log(maxHeap.size); // 3;
612
+ *
613
+ * // Peek returns highest priority task
614
+ * const topTask = maxHeap.peek();
615
+ * console.log(topTask?.priority); // 8;
616
+ * console.log(topTask?.name); // 'Alert';
617
+ */
618
+ /**
619
+ * @deprecated Use `pop` instead. Will be removed in a future major version.
536
620
 
537
621
 
538
622
 
@@ -563,6 +647,14 @@ var _Heap = class _Heap extends IterableElementBase {
563
647
  * console.log(topTask?.name); // 'Alert';
564
648
  */
565
649
  poll() {
650
+ return this.pop();
651
+ }
652
+ /**
653
+ * Remove and return the top element (min or max depending on comparator).
654
+ * @remarks Time O(log N) amortized, Space O(1)
655
+ * @returns The removed top element, or undefined if empty.
656
+ */
657
+ pop() {
566
658
  if (this.elements.length === 0) return;
567
659
  const value = this.elements[0];
568
660
  const last = this.elements.pop();
@@ -603,6 +695,13 @@ var _Heap = class _Heap extends IterableElementBase {
603
695
 
604
696
 
605
697
 
698
+
699
+
700
+
701
+
702
+
703
+
704
+
606
705
 
607
706
 
608
707
 
@@ -703,6 +802,13 @@ var _Heap = class _Heap extends IterableElementBase {
703
802
 
704
803
 
705
804
 
805
+
806
+
807
+
808
+
809
+
810
+
811
+
706
812
 
707
813
 
708
814
 
@@ -750,6 +856,13 @@ var _Heap = class _Heap extends IterableElementBase {
750
856
 
751
857
 
752
858
 
859
+
860
+
861
+
862
+
863
+
864
+
865
+
753
866
 
754
867
 
755
868
 
@@ -767,16 +880,6 @@ var _Heap = class _Heap extends IterableElementBase {
767
880
  clear() {
768
881
  this._elements = [];
769
882
  }
770
- /**
771
- * Replace the backing array and rebuild the heap.
772
- * @remarks Time O(N), Space O(N)
773
- * @param elements - Iterable used to refill the heap.
774
- * @returns Array of per-node results from fixing steps.
775
- */
776
- refill(elements) {
777
- this._elements = Array.from(elements);
778
- return this.fix();
779
- }
780
883
  /**
781
884
  * Check if an equal element exists in the heap.
782
885
  * @remarks Time O(N), Space O(1)
@@ -800,6 +903,13 @@ var _Heap = class _Heap extends IterableElementBase {
800
903
 
801
904
 
802
905
 
906
+
907
+
908
+
909
+
910
+
911
+
912
+
803
913
 
804
914
 
805
915
 
@@ -847,6 +957,13 @@ var _Heap = class _Heap extends IterableElementBase {
847
957
 
848
958
 
849
959
 
960
+
961
+
962
+
963
+
964
+
965
+
966
+
850
967
 
851
968
 
852
969
 
@@ -871,7 +988,7 @@ var _Heap = class _Heap extends IterableElementBase {
871
988
  }
872
989
  if (index < 0) return false;
873
990
  if (index === 0) {
874
- this.poll();
991
+ this.pop();
875
992
  } else if (index === this.elements.length - 1) {
876
993
  this.elements.pop();
877
994
  } else {
@@ -881,13 +998,19 @@ var _Heap = class _Heap extends IterableElementBase {
881
998
  }
882
999
  return true;
883
1000
  }
1001
+ /**
1002
+ * @deprecated Use `deleteWhere` instead. Will be removed in a future major version.
1003
+ */
1004
+ deleteBy(predicate) {
1005
+ return this.deleteWhere(predicate);
1006
+ }
884
1007
  /**
885
1008
  * Delete the first element that matches a predicate.
886
1009
  * @remarks Time O(N), Space O(1)
887
1010
  * @param predicate - Function (element, index, heap) → boolean.
888
1011
  * @returns True if an element was removed.
889
1012
  */
890
- deleteBy(predicate) {
1013
+ deleteWhere(predicate) {
891
1014
  let idx = -1;
892
1015
  for (let i = 0; i < this.elements.length; i++) {
893
1016
  if (predicate(this.elements[i], i, this)) {
@@ -897,7 +1020,7 @@ var _Heap = class _Heap extends IterableElementBase {
897
1020
  }
898
1021
  if (idx < 0) return false;
899
1022
  if (idx === 0) {
900
- this.poll();
1023
+ this.pop();
901
1024
  } else if (idx === this.elements.length - 1) {
902
1025
  this.elements.pop();
903
1026
  } else {
@@ -940,6 +1063,13 @@ var _Heap = class _Heap extends IterableElementBase {
940
1063
 
941
1064
 
942
1065
 
1066
+
1067
+
1068
+
1069
+
1070
+
1071
+
1072
+
943
1073
 
944
1074
 
945
1075
 
@@ -1020,6 +1150,13 @@ var _Heap = class _Heap extends IterableElementBase {
1020
1150
 
1021
1151
 
1022
1152
 
1153
+
1154
+
1155
+
1156
+
1157
+
1158
+
1159
+
1023
1160
 
1024
1161
 
1025
1162
 
@@ -1073,6 +1210,13 @@ var _Heap = class _Heap extends IterableElementBase {
1073
1210
 
1074
1211
 
1075
1212
 
1213
+
1214
+
1215
+
1216
+
1217
+
1218
+
1219
+
1076
1220
 
1077
1221
 
1078
1222
 
@@ -1125,6 +1269,13 @@ var _Heap = class _Heap extends IterableElementBase {
1125
1269
 
1126
1270
 
1127
1271
 
1272
+
1273
+
1274
+
1275
+
1276
+
1277
+
1278
+
1128
1279
 
1129
1280
 
1130
1281
 
@@ -1184,6 +1335,13 @@ var _Heap = class _Heap extends IterableElementBase {
1184
1335
 
1185
1336
 
1186
1337
 
1338
+
1339
+
1340
+
1341
+
1342
+
1343
+
1344
+
1187
1345
 
1188
1346
 
1189
1347