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
@@ -63,71 +63,6 @@ export class SinglyLinkedListNode<E = any> extends LinkedListNode<E> {
63
63
  * 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.
64
64
  * 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).
65
65
  * @example
66
- * // basic SinglyLinkedList creation and push operation
67
- * // Create a simple SinglyLinkedList with initial values
68
- * const list = new SinglyLinkedList([1, 2, 3, 4, 5]);
69
- *
70
- * // Verify the list maintains insertion order
71
- * console.log([...list]); // [1, 2, 3, 4, 5];
72
- *
73
- * // Check length
74
- * console.log(list.length); // 5;
75
- *
76
- * // Push a new element to the end
77
- * list.push(6);
78
- * console.log(list.length); // 6;
79
- * console.log([...list]); // [1, 2, 3, 4, 5, 6];
80
- * @example
81
- * // SinglyLinkedList pop and shift operations
82
- * const list = new SinglyLinkedList<number>([10, 20, 30, 40, 50]);
83
- *
84
- * // Pop removes from the end
85
- * const last = list.pop();
86
- * console.log(last); // 50;
87
- *
88
- * // Shift removes from the beginning
89
- * const first = list.shift();
90
- * console.log(first); // 10;
91
- *
92
- * // Verify remaining elements
93
- * console.log([...list]); // [20, 30, 40];
94
- * console.log(list.length); // 3;
95
- * @example
96
- * // SinglyLinkedList unshift and forward traversal
97
- * const list = new SinglyLinkedList<number>([20, 30, 40]);
98
- *
99
- * // Unshift adds to the beginning
100
- * list.unshift(10);
101
- * console.log([...list]); // [10, 20, 30, 40];
102
- *
103
- * // Access elements (forward traversal only for singly linked)
104
- * const second = list.at(1);
105
- * console.log(second); // 20;
106
- *
107
- * // SinglyLinkedList allows forward iteration only
108
- * const elements: number[] = [];
109
- * for (const item of list) {
110
- * elements.push(item);
111
- * }
112
- * console.log(elements); // [10, 20, 30, 40];
113
- *
114
- * console.log(list.length); // 4;
115
- * @example
116
- * // SinglyLinkedList filter and map operations
117
- * const list = new SinglyLinkedList<number>([1, 2, 3, 4, 5]);
118
- *
119
- * // Filter even numbers
120
- * const filtered = list.filter(value => value % 2 === 0);
121
- * console.log(filtered.length); // 2;
122
- *
123
- * // Map to double values
124
- * const doubled = list.map(value => value * 2);
125
- * console.log(doubled.length); // 5;
126
- *
127
- * // Use reduce to sum
128
- * const sum = list.reduce((acc, value) => acc + value, 0);
129
- * console.log(sum); // 15;
130
- * @example
131
66
  * // SinglyLinkedList for sequentially processed data stream
132
67
  * interface LogEntry {
133
68
  * timestamp: number;
@@ -244,6 +179,17 @@ export class SinglyLinkedListNode<E = any> extends LinkedListNode<E> {
244
179
  * editor.moveCursor(1);
245
180
  * editor.insert('a');
246
181
  * console.log(editor.getText()); // 'Haello';
182
+ * @example
183
+ * // Find first matching element
184
+ * const list = new SinglyLinkedList<number>([1, 2, 3, 4, 5]);
185
+ * console.log(list.find(n => n > 3)); // 4;
186
+ * console.log(list.find(n => n > 10)); // undefined;
187
+ * @example
188
+ * // Iterate over elements
189
+ * const list = new SinglyLinkedList<number>([10, 20, 30]);
190
+ * const result: number[] = [];
191
+ * list.forEach(n => result.push(n));
192
+ * console.log(result); // [10, 20, 30];
247
193
  */
248
194
  export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, SinglyLinkedListNode<E>> {
249
195
  protected _equals: (a: E, b: E) => boolean = (a, b) => Object.is(a, b);
@@ -350,6 +296,33 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
350
296
  * @remarks Time O(1), Space O(1)
351
297
  * @param elementOrNode - Element or node to append.
352
298
  * @returns True when appended.
299
+
300
+
301
+
302
+
303
+
304
+
305
+
306
+
307
+
308
+
309
+
310
+
311
+ * @example
312
+ * // basic SinglyLinkedList creation and push operation
313
+ * // Create a simple SinglyLinkedList with initial values
314
+ * const list = new SinglyLinkedList([1, 2, 3, 4, 5]);
315
+ *
316
+ * // Verify the list maintains insertion order
317
+ * console.log([...list]); // [1, 2, 3, 4, 5];
318
+ *
319
+ * // Check length
320
+ * console.log(list.length); // 5;
321
+ *
322
+ * // Push a new element to the end
323
+ * list.push(6);
324
+ * console.log(list.length); // 6;
325
+ * console.log([...list]); // [1, 2, 3, 4, 5, 6];
353
326
  */
354
327
 
355
328
  push(elementOrNode: E | SinglyLinkedListNode<E>): boolean {
@@ -369,6 +342,33 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
369
342
  * Remove and return the tail element.
370
343
  * @remarks Time O(N), Space O(1)
371
344
  * @returns Removed element or undefined.
345
+
346
+
347
+
348
+
349
+
350
+
351
+
352
+
353
+
354
+
355
+
356
+
357
+ * @example
358
+ * // SinglyLinkedList pop and shift operations
359
+ * const list = new SinglyLinkedList<number>([10, 20, 30, 40, 50]);
360
+ *
361
+ * // Pop removes from the end
362
+ * const last = list.pop();
363
+ * console.log(last); // 50;
364
+ *
365
+ * // Shift removes from the beginning
366
+ * const first = list.shift();
367
+ * console.log(first); // 10;
368
+ *
369
+ * // Verify remaining elements
370
+ * console.log([...list]); // [20, 30, 40];
371
+ * console.log(list.length); // 3;
372
372
  */
373
373
 
374
374
  pop(): E | undefined {
@@ -393,6 +393,23 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
393
393
  * Remove and return the head element.
394
394
  * @remarks Time O(1), Space O(1)
395
395
  * @returns Removed element or undefined.
396
+
397
+
398
+
399
+
400
+
401
+
402
+
403
+
404
+
405
+
406
+
407
+
408
+ * @example
409
+ * // Remove from the front
410
+ * const list = new SinglyLinkedList<number>([10, 20, 30]);
411
+ * console.log(list.shift()); // 10;
412
+ * console.log(list.length); // 2;
396
413
  */
397
414
 
398
415
  shift(): E | undefined {
@@ -409,6 +426,38 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
409
426
  * @remarks Time O(1), Space O(1)
410
427
  * @param elementOrNode - Element or node to prepend.
411
428
  * @returns True when prepended.
429
+
430
+
431
+
432
+
433
+
434
+
435
+
436
+
437
+
438
+
439
+
440
+
441
+ * @example
442
+ * // SinglyLinkedList unshift and forward traversal
443
+ * const list = new SinglyLinkedList<number>([20, 30, 40]);
444
+ *
445
+ * // Unshift adds to the beginning
446
+ * list.unshift(10);
447
+ * console.log([...list]); // [10, 20, 30, 40];
448
+ *
449
+ * // Access elements (forward traversal only for singly linked)
450
+ * const second = list.at(1);
451
+ * console.log(second); // 20;
452
+ *
453
+ * // SinglyLinkedList allows forward iteration only
454
+ * const elements: number[] = [];
455
+ * for (const item of list) {
456
+ * elements.push(item);
457
+ * }
458
+ * console.log(elements); // [10, 20, 30, 40];
459
+ *
460
+ * console.log(list.length); // 4;
412
461
  */
413
462
 
414
463
  unshift(elementOrNode: E | SinglyLinkedListNode<E>): boolean {
@@ -479,6 +528,24 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
479
528
  * @remarks Time O(N), Space O(1)
480
529
  * @param index - Zero-based index.
481
530
  * @returns Element or undefined.
531
+
532
+
533
+
534
+
535
+
536
+
537
+
538
+
539
+
540
+
541
+
542
+
543
+ * @example
544
+ * // Access element by index
545
+ * const list = new SinglyLinkedList<string>(['a', 'b', 'c', 'd']);
546
+ * console.log(list.at(0)); // 'a';
547
+ * console.log(list.at(2)); // 'c';
548
+ * console.log(list.at(3)); // 'd';
482
549
  */
483
550
 
484
551
  at(index: number): E | undefined {
@@ -506,6 +573,19 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
506
573
  * @remarks Time O(N), Space O(1)
507
574
  * @param index - Zero-based index.
508
575
  * @returns Node or undefined.
576
+
577
+
578
+
579
+
580
+
581
+
582
+
583
+
584
+
585
+ * @example
586
+ * // Get node at index
587
+ * const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
588
+ * console.log(list.getNodeAt(1)?.value); // 'b';
509
589
  */
510
590
 
511
591
  getNodeAt(index: number): SinglyLinkedListNode<E> | undefined {
@@ -520,6 +600,20 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
520
600
  * @remarks Time O(N), Space O(1)
521
601
  * @param index - Zero-based index.
522
602
  * @returns Removed element or undefined.
603
+
604
+
605
+
606
+
607
+
608
+
609
+
610
+
611
+
612
+ * @example
613
+ * // Remove by index
614
+ * const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
615
+ * list.deleteAt(1);
616
+ * console.log(list.toArray()); // ['a', 'c'];
523
617
  */
524
618
 
525
619
  deleteAt(index: number): E | undefined {
@@ -539,6 +633,20 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
539
633
  * @remarks Time O(N), Space O(1)
540
634
  * @param [elementOrNode] - Element or node to remove; if omitted/undefined, nothing happens.
541
635
  * @returns True if removed.
636
+
637
+
638
+
639
+
640
+
641
+
642
+
643
+
644
+
645
+ * @example
646
+ * // Remove first occurrence
647
+ * const list = new SinglyLinkedList<number>([1, 2, 3, 2]);
648
+ * list.delete(2);
649
+ * console.log(list.toArray()); // [1, 3, 2];
542
650
  */
543
651
 
544
652
  delete(elementOrNode: E | SinglyLinkedListNode<E> | undefined): boolean {
@@ -564,6 +672,20 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
564
672
  * @param index - Zero-based index.
565
673
  * @param newElementOrNode - Element or node to insert.
566
674
  * @returns True if inserted.
675
+
676
+
677
+
678
+
679
+
680
+
681
+
682
+
683
+
684
+ * @example
685
+ * // Insert at index
686
+ * const list = new SinglyLinkedList<number>([1, 3]);
687
+ * list.addAt(1, 2);
688
+ * console.log(list.toArray()); // [1, 2, 3];
567
689
  */
568
690
 
569
691
  addAt(index: number, newElementOrNode: E | SinglyLinkedListNode<E>): boolean {
@@ -597,6 +719,19 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
597
719
  * Check whether the list is empty.
598
720
  * @remarks Time O(1), Space O(1)
599
721
  * @returns True if length is 0.
722
+
723
+
724
+
725
+
726
+
727
+
728
+
729
+
730
+
731
+
732
+ * @example
733
+ * // Check empty
734
+ * console.log(new SinglyLinkedList().isEmpty()); // true;
600
735
  */
601
736
 
602
737
  isEmpty(): boolean {
@@ -607,6 +742,21 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
607
742
  * Remove all nodes and reset length.
608
743
  * @remarks Time O(N), Space O(1)
609
744
  * @returns void
745
+
746
+
747
+
748
+
749
+
750
+
751
+
752
+
753
+
754
+
755
+ * @example
756
+ * // Remove all
757
+ * const list = new SinglyLinkedList<number>([1, 2, 3]);
758
+ * list.clear();
759
+ * console.log(list.isEmpty()); // true;
610
760
  */
611
761
 
612
762
  clear(): void {
@@ -619,6 +769,23 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
619
769
  * Reverse the list in place.
620
770
  * @remarks Time O(N), Space O(1)
621
771
  * @returns This list.
772
+
773
+
774
+
775
+
776
+
777
+
778
+
779
+
780
+
781
+
782
+
783
+
784
+ * @example
785
+ * // Reverse the list in-place
786
+ * const list = new SinglyLinkedList<number>([1, 2, 3, 4]);
787
+ * list.reverse();
788
+ * console.log([...list]); // [4, 3, 2, 1];
622
789
  */
623
790
 
624
791
  reverse(): this {
@@ -827,6 +994,23 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
827
994
  * Deep clone this list (values are copied by reference).
828
995
  * @remarks Time O(N), Space O(N)
829
996
  * @returns A new list with the same element sequence.
997
+
998
+
999
+
1000
+
1001
+
1002
+
1003
+
1004
+
1005
+
1006
+
1007
+ * @example
1008
+ * // Deep copy
1009
+ * const list = new SinglyLinkedList<number>([1, 2, 3]);
1010
+ * const copy = list.clone();
1011
+ * copy.pop();
1012
+ * console.log(list.length); // 3;
1013
+ * console.log(copy.length); // 2;
830
1014
  */
831
1015
 
832
1016
  clone(): this {
@@ -841,6 +1025,33 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
841
1025
  * @param callback - Predicate (value, index, list) → boolean to keep value.
842
1026
  * @param [thisArg] - Value for `this` inside the callback.
843
1027
  * @returns A new list with kept values.
1028
+
1029
+
1030
+
1031
+
1032
+
1033
+
1034
+
1035
+
1036
+
1037
+
1038
+
1039
+
1040
+ * @example
1041
+ * // SinglyLinkedList filter and map operations
1042
+ * const list = new SinglyLinkedList<number>([1, 2, 3, 4, 5]);
1043
+ *
1044
+ * // Filter even numbers
1045
+ * const filtered = list.filter(value => value % 2 === 0);
1046
+ * console.log(filtered.length); // 2;
1047
+ *
1048
+ * // Map to double values
1049
+ * const doubled = list.map(value => value * 2);
1050
+ * console.log(doubled.length); // 5;
1051
+ *
1052
+ * // Use reduce to sum
1053
+ * const sum = list.reduce((acc, value) => acc + value, 0);
1054
+ * console.log(sum); // 15;
844
1055
  */
845
1056
 
846
1057
  filter(callback: ElementCallback<E, R, boolean>, thisArg?: any): this {
@@ -877,6 +1088,23 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
877
1088
  * @param [options] - Options for the output list (e.g., maxLen, toElementFn).
878
1089
  * @param [thisArg] - Value for `this` inside the callback.
879
1090
  * @returns A new SinglyLinkedList with mapped values.
1091
+
1092
+
1093
+
1094
+
1095
+
1096
+
1097
+
1098
+
1099
+
1100
+
1101
+
1102
+
1103
+ * @example
1104
+ * // Transform elements
1105
+ * const list = new SinglyLinkedList<number>([1, 2, 3]);
1106
+ * const doubled = list.map(n => n * 2);
1107
+ * console.log([...doubled]); // [2, 4, 6];
880
1108
  */
881
1109
 
882
1110
  map<EM, RM = any>(