data-structure-typed 2.4.4 → 2.5.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 (80) hide show
  1. package/CHANGELOG.md +22 -1
  2. package/README.md +34 -1
  3. package/dist/cjs/index.cjs +10639 -2151
  4. package/dist/cjs-legacy/index.cjs +10694 -2195
  5. package/dist/esm/index.mjs +10639 -2150
  6. package/dist/esm-legacy/index.mjs +10694 -2194
  7. package/dist/types/common/error.d.ts +23 -0
  8. package/dist/types/common/index.d.ts +1 -0
  9. package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
  10. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
  11. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
  12. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +439 -78
  13. package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
  14. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +217 -31
  15. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
  16. package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
  17. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
  18. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
  19. package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
  20. package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -0
  21. package/dist/types/data-structures/graph/directed-graph.d.ts +220 -47
  22. package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
  23. package/dist/types/data-structures/graph/undirected-graph.d.ts +218 -59
  24. package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
  25. package/dist/types/data-structures/heap/heap.d.ts +287 -99
  26. package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
  27. package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
  28. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
  29. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
  30. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
  31. package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
  32. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
  33. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
  34. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
  35. package/dist/types/data-structures/queue/deque.d.ts +313 -66
  36. package/dist/types/data-structures/queue/queue.d.ts +211 -42
  37. package/dist/types/data-structures/stack/stack.d.ts +174 -32
  38. package/dist/types/data-structures/trie/trie.d.ts +213 -43
  39. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
  40. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
  41. package/dist/types/types/data-structures/queue/deque.d.ts +6 -0
  42. package/dist/umd/data-structure-typed.js +10725 -2221
  43. package/dist/umd/data-structure-typed.min.js +4 -2
  44. package/package.json +5 -4
  45. package/src/common/error.ts +60 -0
  46. package/src/common/index.ts +2 -0
  47. package/src/data-structures/base/iterable-element-base.ts +2 -2
  48. package/src/data-structures/binary-tree/avl-tree.ts +146 -51
  49. package/src/data-structures/binary-tree/binary-indexed-tree.ts +317 -247
  50. package/src/data-structures/binary-tree/binary-tree.ts +567 -121
  51. package/src/data-structures/binary-tree/bst.ts +370 -37
  52. package/src/data-structures/binary-tree/red-black-tree.ts +328 -96
  53. package/src/data-structures/binary-tree/segment-tree.ts +378 -248
  54. package/src/data-structures/binary-tree/tree-map.ts +1411 -13
  55. package/src/data-structures/binary-tree/tree-multi-map.ts +1218 -215
  56. package/src/data-structures/binary-tree/tree-multi-set.ts +959 -69
  57. package/src/data-structures/binary-tree/tree-set.ts +1257 -15
  58. package/src/data-structures/graph/abstract-graph.ts +106 -1
  59. package/src/data-structures/graph/directed-graph.ts +233 -47
  60. package/src/data-structures/graph/map-graph.ts +59 -1
  61. package/src/data-structures/graph/undirected-graph.ts +308 -59
  62. package/src/data-structures/hash/hash-map.ts +254 -79
  63. package/src/data-structures/heap/heap.ts +305 -102
  64. package/src/data-structures/heap/max-heap.ts +48 -3
  65. package/src/data-structures/heap/min-heap.ts +59 -0
  66. package/src/data-structures/linked-list/doubly-linked-list.ts +303 -44
  67. package/src/data-structures/linked-list/singly-linked-list.ts +293 -65
  68. package/src/data-structures/linked-list/skip-linked-list.ts +707 -90
  69. package/src/data-structures/matrix/matrix.ts +433 -22
  70. package/src/data-structures/priority-queue/max-priority-queue.ts +59 -3
  71. package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
  72. package/src/data-structures/priority-queue/priority-queue.ts +60 -0
  73. package/src/data-structures/queue/deque.ts +358 -68
  74. package/src/data-structures/queue/queue.ts +223 -42
  75. package/src/data-structures/stack/stack.ts +184 -32
  76. package/src/data-structures/trie/trie.ts +227 -44
  77. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
  78. package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
  79. package/src/types/data-structures/queue/deque.ts +7 -0
  80. package/src/utils/utils.ts +4 -2
@@ -87,50 +87,6 @@ export class DoublyLinkedListNode<E = any> extends LinkedListNode<E> {
87
87
  * 4. High Efficiency in Insertion and Deletion: Adding or removing elements in a linked list does not require moving other elements, making these operations more efficient than in arrays.
88
88
  * Caution: Although our linked list classes provide methods such as at, setAt, addAt, and indexOf that are based on array indices, their time complexity, like that of the native Array.lastIndexOf, is 𝑂(𝑛). If you need to use these methods frequently, you might want to consider other data structures, such as Deque or Queue (designed for random access). Similarly, since the native Array.shift method has a time complexity of 𝑂(𝑛), using an array to simulate a queue can be inefficient. In such cases, you should use Queue or Deque, as these data structures leverage deferred array rearrangement, effectively reducing the average time complexity to 𝑂(1).
89
89
  * @example
90
- * // basic DoublyLinkedList creation and push operation
91
- * // Create a simple DoublyLinkedList with initial values
92
- * const list = new DoublyLinkedList([1, 2, 3, 4, 5]);
93
- *
94
- * // Verify the list maintains insertion order
95
- * console.log([...list]); // [1, 2, 3, 4, 5];
96
- *
97
- * // Check length
98
- * console.log(list.length); // 5;
99
- *
100
- * // Push a new element to the end
101
- * list.push(6);
102
- * console.log(list.length); // 6;
103
- * console.log([...list]); // [1, 2, 3, 4, 5, 6];
104
- * @example
105
- * // DoublyLinkedList pop and shift operations
106
- * const list = new DoublyLinkedList<number>([10, 20, 30, 40, 50]);
107
- *
108
- * // Pop removes from the end
109
- * const last = list.pop();
110
- * console.log(last); // 50;
111
- *
112
- * // Shift removes from the beginning
113
- * const first = list.shift();
114
- * console.log(first); // 10;
115
- *
116
- * // Verify remaining elements
117
- * console.log([...list]); // [20, 30, 40];
118
- * console.log(list.length); // 3;
119
- * @example
120
- * // DoublyLinkedList for...of iteration and map operation
121
- * const list = new DoublyLinkedList<number>([1, 2, 3, 4, 5]);
122
- *
123
- * // Iterate through list
124
- * const doubled = list.map(value => value * 2);
125
- * console.log(doubled.length); // 5;
126
- *
127
- * // Use for...of loop
128
- * const result: number[] = [];
129
- * for (const item of list) {
130
- * result.push(item);
131
- * }
132
- * console.log(result); // [1, 2, 3, 4, 5];
133
- * @example
134
90
  * // Browser history
135
91
  * const browserHistory = new DoublyLinkedList<string>();
136
92
  *
@@ -180,6 +136,16 @@ export class DoublyLinkedListNode<E = any> extends LinkedListNode<E> {
180
136
  * // Access entry (in real LRU, this would move it to end)
181
137
  * const foundEntry = [...cacheList].find(entry => entry.key === 'user:2');
182
138
  * console.log(foundEntry?.value); // 'Bob';
139
+ * @example
140
+ * // Find first matching element
141
+ * const list = new DoublyLinkedList<number>([5, 10, 15, 20]);
142
+ * console.log(list.find(n => n >= 12)); // 15;
143
+ * @example
144
+ * // Iterate over elements
145
+ * const list = new DoublyLinkedList<number>([1, 2, 3]);
146
+ * const sum: number[] = [];
147
+ * list.forEach(n => sum.push(n));
148
+ * console.log(sum); // [1, 2, 3];
183
149
  */
184
150
  export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, DoublyLinkedListNode<E>> {
185
151
  protected _equals: (a: E, b: E) => boolean = (a, b) => Object.is(a, b);
@@ -302,6 +268,33 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
302
268
  * @remarks Time O(1), Space O(1)
303
269
  * @param elementOrNode - Element or node to append.
304
270
  * @returns True when appended.
271
+
272
+
273
+
274
+
275
+
276
+
277
+
278
+
279
+
280
+
281
+
282
+
283
+ * @example
284
+ * // basic DoublyLinkedList creation and push operation
285
+ * // Create a simple DoublyLinkedList with initial values
286
+ * const list = new DoublyLinkedList([1, 2, 3, 4, 5]);
287
+ *
288
+ * // Verify the list maintains insertion order
289
+ * console.log([...list]); // [1, 2, 3, 4, 5];
290
+ *
291
+ * // Check length
292
+ * console.log(list.length); // 5;
293
+ *
294
+ * // Push a new element to the end
295
+ * list.push(6);
296
+ * console.log(list.length); // 6;
297
+ * console.log([...list]); // [1, 2, 3, 4, 5, 6];
305
298
  */
306
299
 
307
300
  push(elementOrNode: E | DoublyLinkedListNode<E>): boolean {
@@ -323,6 +316,33 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
323
316
  * Remove and return the tail element.
324
317
  * @remarks Time O(1), Space O(1)
325
318
  * @returns Removed element or undefined.
319
+
320
+
321
+
322
+
323
+
324
+
325
+
326
+
327
+
328
+
329
+
330
+
331
+ * @example
332
+ * // DoublyLinkedList pop and shift operations
333
+ * const list = new DoublyLinkedList<number>([10, 20, 30, 40, 50]);
334
+ *
335
+ * // Pop removes from the end
336
+ * const last = list.pop();
337
+ * console.log(last); // 50;
338
+ *
339
+ * // Shift removes from the beginning
340
+ * const first = list.shift();
341
+ * console.log(first); // 10;
342
+ *
343
+ * // Verify remaining elements
344
+ * console.log([...list]); // [20, 30, 40];
345
+ * console.log(list.length); // 3;
326
346
  */
327
347
 
328
348
  pop(): E | undefined {
@@ -343,6 +363,23 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
343
363
  * Remove and return the head element.
344
364
  * @remarks Time O(1), Space O(1)
345
365
  * @returns Removed element or undefined.
366
+
367
+
368
+
369
+
370
+
371
+
372
+
373
+
374
+
375
+
376
+
377
+
378
+ * @example
379
+ * // Remove from the front
380
+ * const list = new DoublyLinkedList<number>([10, 20, 30]);
381
+ * console.log(list.shift()); // 10;
382
+ * console.log(list.first); // 20;
346
383
  */
347
384
 
348
385
  shift(): E | undefined {
@@ -364,6 +401,23 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
364
401
  * @remarks Time O(1), Space O(1)
365
402
  * @param elementOrNode - Element or node to prepend.
366
403
  * @returns True when prepended.
404
+
405
+
406
+
407
+
408
+
409
+
410
+
411
+
412
+
413
+
414
+
415
+
416
+ * @example
417
+ * // Add to the front
418
+ * const list = new DoublyLinkedList<number>([2, 3]);
419
+ * list.unshift(1);
420
+ * console.log([...list]); // [1, 2, 3];
367
421
  */
368
422
 
369
423
  unshift(elementOrNode: E | DoublyLinkedListNode<E>): boolean {
@@ -418,6 +472,23 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
418
472
  * @remarks Time O(N), Space O(1)
419
473
  * @param index - Zero-based index.
420
474
  * @returns Element or undefined.
475
+
476
+
477
+
478
+
479
+
480
+
481
+
482
+
483
+
484
+
485
+
486
+
487
+ * @example
488
+ * // Access by index
489
+ * const list = new DoublyLinkedList<string>(['a', 'b', 'c']);
490
+ * console.log(list.at(1)); // 'b';
491
+ * console.log(list.at(2)); // 'c';
421
492
  */
422
493
 
423
494
  at(index: number): E | undefined {
@@ -432,6 +503,19 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
432
503
  * @remarks Time O(N), Space O(1)
433
504
  * @param index - Zero-based index.
434
505
  * @returns Node or undefined.
506
+
507
+
508
+
509
+
510
+
511
+
512
+
513
+
514
+
515
+ * @example
516
+ * // Get node at index
517
+ * const list = new DoublyLinkedList<string>(['a', 'b', 'c']);
518
+ * console.log(list.getNodeAt(1)?.value); // 'b';
435
519
  */
436
520
 
437
521
  getNodeAt(index: number): DoublyLinkedListNode<E> | undefined {
@@ -486,6 +570,20 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
486
570
  * @param index - Zero-based index.
487
571
  * @param newElementOrNode - Element or node to insert.
488
572
  * @returns True if inserted.
573
+
574
+
575
+
576
+
577
+
578
+
579
+
580
+
581
+
582
+ * @example
583
+ * // Insert at position
584
+ * const list = new DoublyLinkedList<number>([1, 3]);
585
+ * list.addAt(1, 2);
586
+ * console.log(list.toArray()); // [1, 2, 3];
489
587
  */
490
588
 
491
589
  addAt(index: number, newElementOrNode: E | DoublyLinkedListNode<E>): boolean {
@@ -575,6 +673,20 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
575
673
  * @remarks Time O(N), Space O(1)
576
674
  * @param index - Zero-based index.
577
675
  * @returns Removed element or undefined.
676
+
677
+
678
+
679
+
680
+
681
+
682
+
683
+
684
+
685
+ * @example
686
+ * // Remove by index
687
+ * const list = new DoublyLinkedList<string>(['a', 'b', 'c']);
688
+ * list.deleteAt(1);
689
+ * console.log(list.toArray()); // ['a', 'c'];
578
690
  */
579
691
 
580
692
  deleteAt(index: number): E | undefined {
@@ -596,6 +708,20 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
596
708
  * @remarks Time O(N), Space O(1)
597
709
  * @param [elementOrNode] - Element or node to remove.
598
710
  * @returns True if removed.
711
+
712
+
713
+
714
+
715
+
716
+
717
+
718
+
719
+
720
+ * @example
721
+ * // Remove first occurrence
722
+ * const list = new DoublyLinkedList<number>([1, 2, 3, 2]);
723
+ * list.delete(2);
724
+ * console.log(list.toArray()); // [1, 3, 2];
599
725
  */
600
726
 
601
727
  delete(elementOrNode: E | DoublyLinkedListNode<E> | undefined): boolean {
@@ -618,6 +744,19 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
618
744
  * Check whether the list is empty.
619
745
  * @remarks Time O(1), Space O(1)
620
746
  * @returns True if length is 0.
747
+
748
+
749
+
750
+
751
+
752
+
753
+
754
+
755
+
756
+
757
+ * @example
758
+ * // Check empty
759
+ * console.log(new DoublyLinkedList().isEmpty()); // true;
621
760
  */
622
761
 
623
762
  isEmpty(): boolean {
@@ -628,6 +767,21 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
628
767
  * Remove all nodes and reset length.
629
768
  * @remarks Time O(N), Space O(1)
630
769
  * @returns void
770
+
771
+
772
+
773
+
774
+
775
+
776
+
777
+
778
+
779
+
780
+ * @example
781
+ * // Remove all
782
+ * const list = new DoublyLinkedList<number>([1, 2]);
783
+ * list.clear();
784
+ * console.log(list.isEmpty()); // true;
631
785
  */
632
786
 
633
787
  clear(): void {
@@ -641,6 +795,20 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
641
795
  * @remarks Time O(N), Space O(1)
642
796
  * @param elementNodeOrPredicate - Element, node, or predicate to match.
643
797
  * @returns Matched value or undefined.
798
+
799
+
800
+
801
+
802
+
803
+
804
+
805
+
806
+
807
+ * @example
808
+ * // Search with predicate
809
+ * const list = new DoublyLinkedList<number>([10, 20, 30]);
810
+ * const found = list.search(node => node.value > 15);
811
+ * console.log(found); // 20;
644
812
  */
645
813
 
646
814
  search(
@@ -660,6 +828,21 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
660
828
  * @remarks Time O(N), Space O(1)
661
829
  * @param elementNodeOrPredicate - Element, node, or predicate to match.
662
830
  * @returns Matched value or undefined.
831
+
832
+
833
+
834
+
835
+
836
+
837
+
838
+
839
+
840
+ * @example
841
+ * // Find value scanning from tail
842
+ * const list = new DoublyLinkedList<number>([1, 2, 3, 4]);
843
+ * // getBackward scans from tail to head, returns first match
844
+ * const found = list.getBackward(node => node.value < 4);
845
+ * console.log(found); // 3;
663
846
  */
664
847
 
665
848
  getBackward(
@@ -678,6 +861,23 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
678
861
  * Reverse the list in place.
679
862
  * @remarks Time O(N), Space O(1)
680
863
  * @returns This list.
864
+
865
+
866
+
867
+
868
+
869
+
870
+
871
+
872
+
873
+
874
+
875
+
876
+ * @example
877
+ * // Reverse in-place
878
+ * const list = new DoublyLinkedList<number>([1, 2, 3]);
879
+ * list.reverse();
880
+ * console.log([...list]); // [3, 2, 1];
681
881
  */
682
882
 
683
883
  reverse(): this {
@@ -707,6 +907,22 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
707
907
  * Deep clone this list (values are copied by reference).
708
908
  * @remarks Time O(N), Space O(N)
709
909
  * @returns A new list with the same element sequence.
910
+
911
+
912
+
913
+
914
+
915
+
916
+
917
+
918
+
919
+
920
+ * @example
921
+ * // Deep copy
922
+ * const list = new DoublyLinkedList<number>([1, 2, 3]);
923
+ * const copy = list.clone();
924
+ * copy.pop();
925
+ * console.log(list.length); // 3;
710
926
  */
711
927
 
712
928
  clone(): this {
@@ -721,6 +937,23 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
721
937
  * @param callback - Predicate (value, index, list) → boolean to keep value.
722
938
  * @param [thisArg] - Value for `this` inside the callback.
723
939
  * @returns A new list with kept values.
940
+
941
+
942
+
943
+
944
+
945
+
946
+
947
+
948
+
949
+
950
+
951
+
952
+ * @example
953
+ * // Filter elements
954
+ * const list = new DoublyLinkedList<number>([1, 2, 3, 4, 5]);
955
+ * const evens = list.filter(n => n % 2 === 0);
956
+ * console.log([...evens]); // [2, 4];
724
957
  */
725
958
 
726
959
  filter(callback: ElementCallback<E, R, boolean>, thisArg?: any): this {
@@ -757,6 +990,32 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
757
990
  * @param [options] - Options for the output list (e.g., maxLen, toElementFn).
758
991
  * @param [thisArg] - Value for `this` inside the callback.
759
992
  * @returns A new DoublyLinkedList with mapped values.
993
+
994
+
995
+
996
+
997
+
998
+
999
+
1000
+
1001
+
1002
+
1003
+
1004
+
1005
+ * @example
1006
+ * // DoublyLinkedList for...of iteration and map operation
1007
+ * const list = new DoublyLinkedList<number>([1, 2, 3, 4, 5]);
1008
+ *
1009
+ * // Iterate through list
1010
+ * const doubled = list.map(value => value * 2);
1011
+ * console.log(doubled.length); // 5;
1012
+ *
1013
+ * // Use for...of loop
1014
+ * const result: number[] = [];
1015
+ * for (const item of list) {
1016
+ * result.push(item);
1017
+ * }
1018
+ * console.log(result); // [1, 2, 3, 4, 5];
760
1019
  */
761
1020
 
762
1021
  map<EM, RM>(