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
@@ -147,6 +147,13 @@ var Matrix = class _Matrix {
147
147
 
148
148
 
149
149
 
150
+
151
+
152
+
153
+
154
+
155
+
156
+
150
157
 
151
158
 
152
159
 
@@ -215,6 +222,13 @@ var Matrix = class _Matrix {
215
222
 
216
223
 
217
224
 
225
+
226
+
227
+
228
+
229
+
230
+
231
+
218
232
 
219
233
 
220
234
 
@@ -279,6 +293,13 @@ var Matrix = class _Matrix {
279
293
 
280
294
 
281
295
 
296
+
297
+
298
+
299
+
300
+
301
+
302
+
282
303
 
283
304
 
284
305
 
@@ -365,6 +386,13 @@ var Matrix = class _Matrix {
365
386
 
366
387
 
367
388
 
389
+
390
+
391
+
392
+
393
+
394
+
395
+
368
396
 
369
397
 
370
398
 
@@ -434,6 +462,13 @@ var Matrix = class _Matrix {
434
462
 
435
463
 
436
464
 
465
+
466
+
467
+
468
+
469
+
470
+
471
+
437
472
 
438
473
 
439
474
 
@@ -525,6 +560,13 @@ var Matrix = class _Matrix {
525
560
 
526
561
 
527
562
 
563
+
564
+
565
+
566
+
567
+
568
+
569
+
528
570
 
529
571
 
530
572
 
@@ -603,6 +645,13 @@ var Matrix = class _Matrix {
603
645
 
604
646
 
605
647
 
648
+
649
+
650
+
651
+
652
+
653
+
654
+
606
655
 
607
656
 
608
657
 
@@ -706,6 +755,13 @@ var Matrix = class _Matrix {
706
755
 
707
756
 
708
757
 
758
+
759
+
760
+
761
+
762
+
763
+
764
+
709
765
 
710
766
 
711
767
 
@@ -184,6 +184,35 @@ var IterableElementBase = class {
184
184
  for (const ele of this) if (ele === element) return true;
185
185
  return false;
186
186
  }
187
+ /**
188
+ * Check whether a value exists (Array-compatible alias for `has`).
189
+ * @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1).
190
+ * @param element - Element to search for (uses `===`).
191
+ * @returns `true` if found.
192
+ */
193
+ includes(element) {
194
+ return this.has(element);
195
+ }
196
+ /**
197
+ * Return an iterator of `[index, value]` pairs (Array-compatible).
198
+ * @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1) per step.
199
+ */
200
+ *entries() {
201
+ let index = 0;
202
+ for (const value of this) {
203
+ yield [index++, value];
204
+ }
205
+ }
206
+ /**
207
+ * Return an iterator of numeric indices (Array-compatible).
208
+ * @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1) per step.
209
+ */
210
+ *keys() {
211
+ let index = 0;
212
+ for (const _ of this) {
213
+ yield index++;
214
+ }
215
+ }
187
216
  /**
188
217
  * Reduces all elements to a single accumulated value.
189
218
  *
@@ -316,6 +345,13 @@ var Heap = class _Heap extends IterableElementBase {
316
345
 
317
346
 
318
347
 
348
+
349
+
350
+
351
+
352
+
353
+
354
+
319
355
 
320
356
 
321
357
 
@@ -372,7 +408,7 @@ var Heap = class _Heap extends IterableElementBase {
372
408
  }
373
409
  /**
374
410
  * Insert an element.
375
- * @remarks Time O(1) amortized, Space O(1)
411
+ * @remarks Time O(log N) amortized, Space O(1)
376
412
  * @param element - Element to insert.
377
413
  * @returns True.
378
414
 
@@ -402,6 +438,13 @@ var Heap = class _Heap extends IterableElementBase {
402
438
 
403
439
 
404
440
 
441
+
442
+
443
+
444
+
445
+
446
+
447
+
405
448
 
406
449
 
407
450
 
@@ -459,6 +502,13 @@ var Heap = class _Heap extends IterableElementBase {
459
502
 
460
503
 
461
504
 
505
+
506
+
507
+
508
+
509
+
510
+
511
+
462
512
 
463
513
 
464
514
 
@@ -522,7 +572,41 @@ var Heap = class _Heap extends IterableElementBase {
522
572
 
523
573
 
524
574
 
575
+
576
+
577
+
525
578
 
579
+
580
+
581
+
582
+ * @example
583
+ * // Heap with custom comparator (MaxHeap behavior)
584
+ * interface Task {
585
+ * id: number;
586
+ * priority: number;
587
+ * name: string;
588
+ * }
589
+ *
590
+ * // Custom comparator for max heap behavior (higher priority first)
591
+ * const tasks: Task[] = [
592
+ * { id: 1, priority: 5, name: 'Email' },
593
+ * { id: 2, priority: 3, name: 'Chat' },
594
+ * { id: 3, priority: 8, name: 'Alert' }
595
+ * ];
596
+ *
597
+ * const maxHeap = new Heap(tasks, {
598
+ * comparator: (a: Task, b: Task) => b.priority - a.priority
599
+ * });
600
+ *
601
+ * console.log(maxHeap.size); // 3;
602
+ *
603
+ * // Peek returns highest priority task
604
+ * const topTask = maxHeap.peek();
605
+ * console.log(topTask?.priority); // 8;
606
+ * console.log(topTask?.name); // 'Alert';
607
+ */
608
+ /**
609
+ * @deprecated Use `pop` instead. Will be removed in a future major version.
526
610
 
527
611
 
528
612
 
@@ -553,6 +637,14 @@ var Heap = class _Heap extends IterableElementBase {
553
637
  * console.log(topTask?.name); // 'Alert';
554
638
  */
555
639
  poll() {
640
+ return this.pop();
641
+ }
642
+ /**
643
+ * Remove and return the top element (min or max depending on comparator).
644
+ * @remarks Time O(log N) amortized, Space O(1)
645
+ * @returns The removed top element, or undefined if empty.
646
+ */
647
+ pop() {
556
648
  if (this.elements.length === 0) return;
557
649
  const value = this.elements[0];
558
650
  const last = this.elements.pop();
@@ -593,6 +685,13 @@ var Heap = class _Heap extends IterableElementBase {
593
685
 
594
686
 
595
687
 
688
+
689
+
690
+
691
+
692
+
693
+
694
+
596
695
 
597
696
 
598
697
 
@@ -693,6 +792,13 @@ var Heap = class _Heap extends IterableElementBase {
693
792
 
694
793
 
695
794
 
795
+
796
+
797
+
798
+
799
+
800
+
801
+
696
802
 
697
803
 
698
804
 
@@ -740,6 +846,13 @@ var Heap = class _Heap extends IterableElementBase {
740
846
 
741
847
 
742
848
 
849
+
850
+
851
+
852
+
853
+
854
+
855
+
743
856
 
744
857
 
745
858
 
@@ -757,16 +870,6 @@ var Heap = class _Heap extends IterableElementBase {
757
870
  clear() {
758
871
  this._elements = [];
759
872
  }
760
- /**
761
- * Replace the backing array and rebuild the heap.
762
- * @remarks Time O(N), Space O(N)
763
- * @param elements - Iterable used to refill the heap.
764
- * @returns Array of per-node results from fixing steps.
765
- */
766
- refill(elements) {
767
- this._elements = Array.from(elements);
768
- return this.fix();
769
- }
770
873
  /**
771
874
  * Check if an equal element exists in the heap.
772
875
  * @remarks Time O(N), Space O(1)
@@ -790,6 +893,13 @@ var Heap = class _Heap extends IterableElementBase {
790
893
 
791
894
 
792
895
 
896
+
897
+
898
+
899
+
900
+
901
+
902
+
793
903
 
794
904
 
795
905
 
@@ -837,6 +947,13 @@ var Heap = class _Heap extends IterableElementBase {
837
947
 
838
948
 
839
949
 
950
+
951
+
952
+
953
+
954
+
955
+
956
+
840
957
 
841
958
 
842
959
 
@@ -861,7 +978,7 @@ var Heap = class _Heap extends IterableElementBase {
861
978
  }
862
979
  if (index < 0) return false;
863
980
  if (index === 0) {
864
- this.poll();
981
+ this.pop();
865
982
  } else if (index === this.elements.length - 1) {
866
983
  this.elements.pop();
867
984
  } else {
@@ -871,13 +988,19 @@ var Heap = class _Heap extends IterableElementBase {
871
988
  }
872
989
  return true;
873
990
  }
991
+ /**
992
+ * @deprecated Use `deleteWhere` instead. Will be removed in a future major version.
993
+ */
994
+ deleteBy(predicate) {
995
+ return this.deleteWhere(predicate);
996
+ }
874
997
  /**
875
998
  * Delete the first element that matches a predicate.
876
999
  * @remarks Time O(N), Space O(1)
877
1000
  * @param predicate - Function (element, index, heap) → boolean.
878
1001
  * @returns True if an element was removed.
879
1002
  */
880
- deleteBy(predicate) {
1003
+ deleteWhere(predicate) {
881
1004
  let idx = -1;
882
1005
  for (let i = 0; i < this.elements.length; i++) {
883
1006
  if (predicate(this.elements[i], i, this)) {
@@ -887,7 +1010,7 @@ var Heap = class _Heap extends IterableElementBase {
887
1010
  }
888
1011
  if (idx < 0) return false;
889
1012
  if (idx === 0) {
890
- this.poll();
1013
+ this.pop();
891
1014
  } else if (idx === this.elements.length - 1) {
892
1015
  this.elements.pop();
893
1016
  } else {
@@ -930,6 +1053,13 @@ var Heap = class _Heap extends IterableElementBase {
930
1053
 
931
1054
 
932
1055
 
1056
+
1057
+
1058
+
1059
+
1060
+
1061
+
1062
+
933
1063
 
934
1064
 
935
1065
 
@@ -1010,6 +1140,13 @@ var Heap = class _Heap extends IterableElementBase {
1010
1140
 
1011
1141
 
1012
1142
 
1143
+
1144
+
1145
+
1146
+
1147
+
1148
+
1149
+
1013
1150
 
1014
1151
 
1015
1152
 
@@ -1063,6 +1200,13 @@ var Heap = class _Heap extends IterableElementBase {
1063
1200
 
1064
1201
 
1065
1202
 
1203
+
1204
+
1205
+
1206
+
1207
+
1208
+
1209
+
1066
1210
 
1067
1211
 
1068
1212
 
@@ -1115,6 +1259,13 @@ var Heap = class _Heap extends IterableElementBase {
1115
1259
 
1116
1260
 
1117
1261
 
1262
+
1263
+
1264
+
1265
+
1266
+
1267
+
1268
+
1118
1269
 
1119
1270
 
1120
1271
 
@@ -1174,6 +1325,13 @@ var Heap = class _Heap extends IterableElementBase {
1174
1325
 
1175
1326
 
1176
1327
 
1328
+
1329
+
1330
+
1331
+
1332
+
1333
+
1334
+
1177
1335
 
1178
1336
 
1179
1337