red-black-tree-typed 1.53.7 → 1.54.2

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 (119) hide show
  1. package/LICENSE +2 -2
  2. package/README.md +52 -0
  3. package/dist/common/index.js +5 -0
  4. package/dist/data-structures/base/iterable-entry-base.js +4 -4
  5. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +213 -0
  6. package/dist/data-structures/binary-tree/avl-tree-counter.js +407 -0
  7. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +71 -170
  8. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +133 -328
  9. package/dist/data-structures/binary-tree/avl-tree.d.ts +103 -69
  10. package/dist/data-structures/binary-tree/avl-tree.js +130 -70
  11. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +3 -0
  12. package/dist/data-structures/binary-tree/binary-indexed-tree.js +3 -0
  13. package/dist/data-structures/binary-tree/binary-tree.d.ts +268 -202
  14. package/dist/data-structures/binary-tree/binary-tree.js +311 -263
  15. package/dist/data-structures/binary-tree/bst.d.ts +193 -139
  16. package/dist/data-structures/binary-tree/bst.js +248 -164
  17. package/dist/data-structures/binary-tree/index.d.ts +3 -1
  18. package/dist/data-structures/binary-tree/index.js +3 -1
  19. package/dist/data-structures/binary-tree/red-black-tree.d.ts +286 -0
  20. package/dist/data-structures/binary-tree/{rb-tree.js → red-black-tree.js} +176 -107
  21. package/dist/data-structures/binary-tree/tree-counter.d.ts +212 -0
  22. package/dist/data-structures/binary-tree/tree-counter.js +444 -0
  23. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +78 -170
  24. package/dist/data-structures/binary-tree/tree-multi-map.js +145 -367
  25. package/dist/data-structures/graph/abstract-graph.js +2 -2
  26. package/dist/data-structures/graph/directed-graph.d.ts +3 -0
  27. package/dist/data-structures/graph/directed-graph.js +3 -0
  28. package/dist/data-structures/graph/map-graph.d.ts +3 -0
  29. package/dist/data-structures/graph/map-graph.js +3 -0
  30. package/dist/data-structures/graph/undirected-graph.d.ts +3 -0
  31. package/dist/data-structures/graph/undirected-graph.js +3 -0
  32. package/dist/data-structures/hash/hash-map.d.ts +31 -1
  33. package/dist/data-structures/hash/hash-map.js +35 -5
  34. package/dist/data-structures/heap/heap.d.ts +20 -3
  35. package/dist/data-structures/heap/heap.js +31 -11
  36. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +46 -11
  37. package/dist/data-structures/linked-list/doubly-linked-list.js +68 -21
  38. package/dist/data-structures/linked-list/singly-linked-list.d.ts +47 -11
  39. package/dist/data-structures/linked-list/singly-linked-list.js +73 -26
  40. package/dist/data-structures/linked-list/skip-linked-list.d.ts +3 -0
  41. package/dist/data-structures/linked-list/skip-linked-list.js +3 -0
  42. package/dist/data-structures/matrix/matrix.d.ts +3 -0
  43. package/dist/data-structures/matrix/matrix.js +3 -0
  44. package/dist/data-structures/matrix/navigator.d.ts +3 -0
  45. package/dist/data-structures/matrix/navigator.js +3 -0
  46. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +3 -0
  47. package/dist/data-structures/priority-queue/max-priority-queue.js +3 -0
  48. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +3 -0
  49. package/dist/data-structures/priority-queue/min-priority-queue.js +3 -0
  50. package/dist/data-structures/queue/deque.d.ts +37 -8
  51. package/dist/data-structures/queue/deque.js +73 -29
  52. package/dist/data-structures/queue/queue.d.ts +41 -1
  53. package/dist/data-structures/queue/queue.js +51 -9
  54. package/dist/data-structures/stack/stack.d.ts +27 -10
  55. package/dist/data-structures/stack/stack.js +39 -20
  56. package/dist/data-structures/trie/trie.d.ts +8 -7
  57. package/dist/data-structures/trie/trie.js +8 -7
  58. package/dist/index.d.ts +4 -4
  59. package/dist/index.js +4 -4
  60. package/dist/interfaces/binary-tree.d.ts +8 -8
  61. package/dist/types/data-structures/base/base.d.ts +1 -1
  62. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
  63. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -4
  64. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -3
  65. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -3
  66. package/dist/types/data-structures/binary-tree/bst.d.ts +4 -4
  67. package/dist/types/data-structures/binary-tree/index.d.ts +3 -1
  68. package/dist/types/data-structures/binary-tree/index.js +3 -1
  69. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +3 -0
  70. package/dist/types/data-structures/binary-tree/red-black-tree.js +2 -0
  71. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
  72. package/dist/types/data-structures/binary-tree/tree-counter.js +2 -0
  73. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -5
  74. package/package.json +3 -3
  75. package/src/common/index.ts +7 -1
  76. package/src/data-structures/base/iterable-entry-base.ts +4 -4
  77. package/src/data-structures/binary-tree/avl-tree-counter.ts +463 -0
  78. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +151 -370
  79. package/src/data-structures/binary-tree/avl-tree.ts +162 -105
  80. package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -0
  81. package/src/data-structures/binary-tree/binary-tree.ts +488 -416
  82. package/src/data-structures/binary-tree/bst.ts +326 -251
  83. package/src/data-structures/binary-tree/index.ts +3 -1
  84. package/src/data-structures/binary-tree/{rb-tree.ts → red-black-tree.ts} +219 -145
  85. package/src/data-structures/binary-tree/tree-counter.ts +504 -0
  86. package/src/data-structures/binary-tree/tree-multi-map.ts +159 -401
  87. package/src/data-structures/graph/abstract-graph.ts +2 -2
  88. package/src/data-structures/graph/directed-graph.ts +3 -0
  89. package/src/data-structures/graph/map-graph.ts +3 -0
  90. package/src/data-structures/graph/undirected-graph.ts +3 -0
  91. package/src/data-structures/hash/hash-map.ts +37 -7
  92. package/src/data-structures/heap/heap.ts +33 -10
  93. package/src/data-structures/linked-list/doubly-linked-list.ts +75 -21
  94. package/src/data-structures/linked-list/singly-linked-list.ts +80 -27
  95. package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
  96. package/src/data-structures/matrix/matrix.ts +3 -0
  97. package/src/data-structures/matrix/navigator.ts +3 -0
  98. package/src/data-structures/priority-queue/max-priority-queue.ts +3 -0
  99. package/src/data-structures/priority-queue/min-priority-queue.ts +3 -0
  100. package/src/data-structures/queue/deque.ts +72 -28
  101. package/src/data-structures/queue/queue.ts +50 -7
  102. package/src/data-structures/stack/stack.ts +39 -20
  103. package/src/data-structures/trie/trie.ts +8 -7
  104. package/src/index.ts +4 -4
  105. package/src/interfaces/binary-tree.ts +10 -21
  106. package/src/types/data-structures/base/base.ts +1 -1
  107. package/src/types/data-structures/binary-tree/avl-tree-counter.ts +3 -0
  108. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -6
  109. package/src/types/data-structures/binary-tree/avl-tree.ts +0 -5
  110. package/src/types/data-structures/binary-tree/binary-tree.ts +0 -5
  111. package/src/types/data-structures/binary-tree/bst.ts +6 -6
  112. package/src/types/data-structures/binary-tree/index.ts +3 -1
  113. package/src/types/data-structures/binary-tree/red-black-tree.ts +5 -0
  114. package/src/types/data-structures/binary-tree/tree-counter.ts +3 -0
  115. package/src/types/data-structures/binary-tree/tree-multi-map.ts +2 -7
  116. package/dist/data-structures/binary-tree/rb-tree.d.ts +0 -209
  117. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +0 -6
  118. package/src/types/data-structures/binary-tree/rb-tree.ts +0 -10
  119. /package/dist/types/data-structures/binary-tree/{rb-tree.js → avl-tree-counter.js} +0 -0
@@ -45,6 +45,9 @@ export class DirectedEdge<E = any> extends AbstractEdge<E> {
45
45
  }
46
46
  }
47
47
 
48
+ /**
49
+ *
50
+ */
48
51
  export class DirectedGraph<
49
52
  V = any,
50
53
  E = any,
@@ -40,6 +40,9 @@ export class MapEdge<E = any> extends DirectedEdge<E> {
40
40
  }
41
41
  }
42
42
 
43
+ /**
44
+ *
45
+ */
43
46
  export class MapGraph<
44
47
  V = any,
45
48
  E = any,
@@ -42,6 +42,9 @@ export class UndirectedEdge<E = number> extends AbstractEdge<E> {
42
42
  }
43
43
  }
44
44
 
45
+ /**
46
+ *
47
+ */
45
48
  export class UndirectedGraph<
46
49
  V = any,
47
50
  E = any,
@@ -97,6 +97,9 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
97
97
  }
98
98
 
99
99
  /**
100
+ * Time Complexity: O(1)
101
+ * Space Complexity: O(1)
102
+ *
100
103
  * The function checks if a given element is an array with exactly two elements.
101
104
  * @param {any} rawElement - The `rawElement` parameter is of type `any`, which means it can be any
102
105
  * data type.
@@ -107,6 +110,9 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
107
110
  }
108
111
 
109
112
  /**
113
+ * Time Complexity: O(1)
114
+ * Space Complexity: O(1)
115
+ *
110
116
  * The function checks if the size of an object is equal to zero and returns a boolean value.
111
117
  * @returns A boolean value indicating whether the size of the object is 0 or not.
112
118
  */
@@ -115,6 +121,9 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
115
121
  }
116
122
 
117
123
  /**
124
+ * Time Complexity: O(1)
125
+ * Space Complexity: O(1)
126
+ *
118
127
  * The clear() function resets the state of an object by clearing its internal store, object map, and
119
128
  * size.
120
129
  */
@@ -125,6 +134,9 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
125
134
  }
126
135
 
127
136
  /**
137
+ * Time Complexity: O(1)
138
+ * Space Complexity: O(1)
139
+ *
128
140
  * The `set` function adds a key-value pair to a map-like data structure, incrementing the size if
129
141
  * the key is not already present.
130
142
  * @param {K} key - The key parameter is the key used to identify the value in the data structure. It
@@ -150,6 +162,9 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
150
162
  }
151
163
 
152
164
  /**
165
+ * Time Complexity: O(k)
166
+ * Space Complexity: O(k)
167
+ *
153
168
  * The function `setMany` takes an iterable collection of objects, maps each object to a key-value
154
169
  * pair using a mapping function, and sets each key-value pair in the current object.
155
170
  * @param entryOrRawElements - The `entryOrRawElements` parameter is an iterable collection of elements of a type
@@ -175,6 +190,9 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
175
190
  }
176
191
 
177
192
  /**
193
+ * Time Complexity: O(1)
194
+ * Space Complexity: O(1)
195
+ *
178
196
  * The `get` function retrieves a value from a map based on a given key, either from an object map or
179
197
  * a string map.
180
198
  * @param {K} key - The `key` parameter is the key used to retrieve a value from the map. It can be
@@ -192,6 +210,9 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
192
210
  }
193
211
 
194
212
  /**
213
+ * Time Complexity: O(1)
214
+ * Space Complexity: O(1)
215
+ *
195
216
  * The `has` function checks if a given key exists in the `_objMap` or `_store` based on whether it
196
217
  * is an object key or not.
197
218
  * @param {K} key - The parameter "key" is of type K, which means it can be any type.
@@ -207,6 +228,9 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
207
228
  }
208
229
 
209
230
  /**
231
+ * Time Complexity: O(1)
232
+ * Space Complexity: O(1)
233
+ *
210
234
  * The `delete` function removes an element from a map-like data structure based on the provided key.
211
235
  * @param {K} key - The `key` parameter is the key of the element that you want to delete from the
212
236
  * data structure.
@@ -263,7 +287,7 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
263
287
  const resultMap = new HashMap<K, VM>();
264
288
  let index = 0;
265
289
  for (const [key, value] of this) {
266
- resultMap.set(key, callbackfn.call(thisArg, value, key, index++, this));
290
+ resultMap.set(key, callbackfn.call(thisArg, key, value, index++, this));
267
291
  }
268
292
  return resultMap;
269
293
  }
@@ -288,7 +312,7 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
288
312
  const filteredMap = new HashMap<K, V>();
289
313
  let index = 0;
290
314
  for (const [key, value] of this) {
291
- if (predicate.call(thisArg, value, key, index++, this)) {
315
+ if (predicate.call(thisArg, key, value, index++, this)) {
292
316
  filteredMap.set(key, value);
293
317
  }
294
318
  }
@@ -579,6 +603,9 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
579
603
  }
580
604
 
581
605
  /**
606
+ * Time Complexity: O(k)
607
+ * Space Complexity: O(k)
608
+ *
582
609
  * The function `setMany` takes an iterable collection, converts each element into a key-value pair
583
610
  * using a provided function, and sets each key-value pair in the current object, returning an array
584
611
  * of booleans indicating the success of each set operation.
@@ -605,6 +632,9 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
605
632
  }
606
633
 
607
634
  /**
635
+ * Time Complexity: O(1)
636
+ * Space Complexity: O(1)
637
+ *
608
638
  * The function checks if a given key exists in a map, using different logic depending on whether the
609
639
  * key is a weak key or not.
610
640
  * @param {K} key - The `key` parameter is the key that is being checked for existence in the map.
@@ -796,7 +826,7 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
796
826
  const filteredMap = new LinkedHashMap<K, V>();
797
827
  let index = 0;
798
828
  for (const [key, value] of this) {
799
- if (predicate.call(thisArg, value, key, index, this)) {
829
+ if (predicate.call(thisArg, key, value, index, this)) {
800
830
  filteredMap.set(key, value);
801
831
  }
802
832
  index++;
@@ -821,12 +851,12 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
821
851
  * @returns a new `LinkedHashMap` object with the values mapped according to the provided callback
822
852
  * function.
823
853
  */
824
- map<VM>(callback: EntryCallback<K, V, VM>, thisArg?: any): LinkedHashMap<K, VM> {
825
- const mappedMap = new LinkedHashMap<K, VM>();
854
+ map<MK, MV>(callback: EntryCallback<K, V, [MK, MV]>, thisArg?: any): LinkedHashMap<MK, MV> {
855
+ const mappedMap = new LinkedHashMap<MK, MV>();
826
856
  let index = 0;
827
857
  for (const [key, value] of this) {
828
- const newValue = callback.call(thisArg, value, key, index, this);
829
- mappedMap.set(key, newValue);
858
+ const [newKey, newValue] = callback.call(thisArg, key, value, index, this);
859
+ mappedMap.set(newKey, newValue);
830
860
  index++;
831
861
  }
832
862
  return mappedMap;
@@ -207,12 +207,7 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R, Heap<E, R>
207
207
  if (comparator) this._comparator = comparator;
208
208
  }
209
209
 
210
- if (elements) {
211
- for (const el of elements) {
212
- if (this.toElementFn) this.add(this.toElementFn(el as R));
213
- else this.add(el as E);
214
- }
215
- }
210
+ this.addMany(elements);
216
211
  }
217
212
 
218
213
  protected _elements: E[] = [];
@@ -254,14 +249,42 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R, Heap<E, R>
254
249
  * Time Complexity: O(log n)
255
250
  * Space Complexity: O(1)
256
251
  *
257
- * Insert an element into the heap and maintain the heap properties.
258
- * @param element - The element to be inserted.
252
+ * The add function pushes an element into an array and then triggers a bubble-up operation.
253
+ * @param {E} element - The `element` parameter represents the element that you want to add to the
254
+ * data structure.
255
+ * @returns The `add` method is returning a boolean value, which is the result of calling the
256
+ * `_bubbleUp` method with the index `this.elements.length - 1` as an argument.
259
257
  */
260
258
  add(element: E): boolean {
261
- this._elements.push(element);
259
+ this._elements.push(element as E);
262
260
  return this._bubbleUp(this.elements.length - 1);
263
261
  }
264
262
 
263
+ /**
264
+ * Time Complexity: O(k log n)
265
+ * Space Complexity: O(1)
266
+ *
267
+ * The `addMany` function iterates over elements and adds them to a collection, returning an array of
268
+ * boolean values indicating success or failure.
269
+ * @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the `addMany` method is
270
+ * an iterable containing elements of type `E` or `R`. The method iterates over each element in the
271
+ * iterable and adds them to the data structure. If a transformation function `_toElementFn` is
272
+ * provided, it transforms the element
273
+ * @returns The `addMany` method returns an array of boolean values indicating whether each element
274
+ * in the input iterable was successfully added to the data structure.
275
+ */
276
+ addMany(elements: Iterable<E> | Iterable<R>): boolean[] {
277
+ const ans: boolean[] = [];
278
+ for (const el of elements) {
279
+ if (this._toElementFn) {
280
+ ans.push(this.add(this._toElementFn(el as R)));
281
+ continue;
282
+ }
283
+ ans.push(this.add(el as E));
284
+ }
285
+ return ans;
286
+ }
287
+
265
288
  /**
266
289
  * Time Complexity: O(log n)
267
290
  * Space Complexity: O(1)
@@ -473,7 +496,7 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R, Heap<E, R>
473
496
  }
474
497
 
475
498
  /**
476
- * Time Complexity: O(n log n)
499
+ * Time Complexity: O(n)
477
500
  * Space Complexity: O(n)
478
501
  *
479
502
  * The `map` function creates a new heap by applying a callback function to each element of the
@@ -524,18 +524,15 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
524
524
  * `DoublyLinkedListOptions<E, R>`. It is an optional parameter that allows you to pass additional
525
525
  * configuration options to customize the behavior of the DoublyLinkedList.
526
526
  */
527
- constructor(elements: Iterable<E> | Iterable<R> = [], options?: DoublyLinkedListOptions<E, R>) {
527
+ constructor(
528
+ elements: Iterable<E> | Iterable<R> | Iterable<DoublyLinkedListNode<E>> = [],
529
+ options?: DoublyLinkedListOptions<E, R>
530
+ ) {
528
531
  super(options);
529
532
  this._head = undefined;
530
533
  this._tail = undefined;
531
534
  this._size = 0;
532
- if (elements) {
533
- for (const el of elements) {
534
- if (this.toElementFn) {
535
- this.push(this.toElementFn(el as R));
536
- } else this.push(el as E);
537
- }
538
- }
535
+ this.pushMany(elements);
539
536
  }
540
537
 
541
538
  protected _head: DoublyLinkedListNode<E> | undefined;
@@ -591,6 +588,19 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
591
588
  return this.tail?.value;
592
589
  }
593
590
 
591
+ /**
592
+ * Time Complexity: O(n)
593
+ * Space Complexity: O(n)
594
+ *
595
+ * The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
596
+ * given array.
597
+ * @param {E[]} data - The `data` parameter is an array of elements of type `E`.
598
+ * @returns The `fromArray` function returns a DoublyLinkedList object.
599
+ */
600
+ static fromArray<E>(data: E[]) {
601
+ return new DoublyLinkedList<E>(data);
602
+ }
603
+
594
604
  /**
595
605
  * Time Complexity: O(1)
596
606
  * Space Complexity: O(1)
@@ -700,6 +710,57 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
700
710
  return true;
701
711
  }
702
712
 
713
+ /**
714
+ * Time Complexity: O(k)
715
+ * Space Complexity: O(k)
716
+ *
717
+ * The function `pushMany` iterates over elements and pushes them into a data structure, applying a
718
+ * transformation function if provided.
719
+ * @param {Iterable<E> | Iterable<R> | Iterable<DoublyLinkedListNode<E>>} elements - The `elements`
720
+ * parameter in the `pushMany` function can accept an iterable containing elements of type `E`, `R`,
721
+ * or `DoublyLinkedListNode<E>`. The function iterates over each element in the iterable and pushes
722
+ * it onto the linked list. If a transformation function `to
723
+ * @returns The `pushMany` function is returning an array of boolean values (`ans`) which indicate
724
+ * the success or failure of pushing each element into the data structure.
725
+ */
726
+ pushMany(elements: Iterable<E> | Iterable<R> | Iterable<DoublyLinkedListNode<E>>) {
727
+ const ans: boolean[] = [];
728
+ for (const el of elements) {
729
+ if (this.toElementFn) {
730
+ ans.push(this.push(this.toElementFn(el as R)));
731
+ continue;
732
+ }
733
+ ans.push(this.push(el as E | DoublyLinkedListNode<E>));
734
+ }
735
+ return ans;
736
+ }
737
+
738
+ /**
739
+ * Time Complexity: O(k)
740
+ * Space Complexity: O(k)
741
+ *
742
+ * The function `unshiftMany` iterates through a collection of elements and adds them to the
743
+ * beginning of a Doubly Linked List, returning an array of boolean values indicating the success of
744
+ * each insertion.
745
+ * @param {Iterable<E> | Iterable<R> | Iterable<DoublyLinkedListNode<E>>} elements - The `elements`
746
+ * parameter in the `unshiftMany` function can accept an iterable containing elements of type `E`,
747
+ * `R`, or `DoublyLinkedListNode<E>`. The function iterates over each element in the iterable and
748
+ * performs an `unshift` operation on the doubly linked list
749
+ * @returns The `unshiftMany` function returns an array of boolean values indicating the success of
750
+ * each unshift operation performed on the elements passed as input.
751
+ */
752
+ unshiftMany(elements: Iterable<E> | Iterable<R> | Iterable<DoublyLinkedListNode<E>>) {
753
+ const ans: boolean[] = [];
754
+ for (const el of elements) {
755
+ if (this.toElementFn) {
756
+ ans.push(this.unshift(this.toElementFn(el as R)));
757
+ continue;
758
+ }
759
+ ans.push(this.unshift(el as E | DoublyLinkedListNode<E>));
760
+ }
761
+ return ans;
762
+ }
763
+
703
764
  /**
704
765
  * Time Complexity: O(n)
705
766
  * Space Complexity: O(1)
@@ -1182,6 +1243,12 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
1182
1243
  * Time Complexity: O(n)
1183
1244
  * Space Complexity: O(1)
1184
1245
  *
1246
+ * The function `countOccurrences` iterates through a doubly linked list and counts the occurrences
1247
+ * of a specified element or nodes that satisfy a given predicate.
1248
+ * @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementOrNode
1249
+ * - The `elementOrNode` parameter in the `countOccurrences` method can accept three types of values:
1250
+ * @returns The `countOccurrences` method returns the number of occurrences of the specified element,
1251
+ * node, or predicate function in the doubly linked list.
1185
1252
  */
1186
1253
  countOccurrences(elementOrNode: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)): number {
1187
1254
  const predicate = this._ensurePredicate(elementOrNode);
@@ -1198,19 +1265,6 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
1198
1265
  return count;
1199
1266
  }
1200
1267
 
1201
- /**
1202
- * Time Complexity: O(n)
1203
- * Space Complexity: O(n)
1204
- *
1205
- * The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
1206
- * given array.
1207
- * @param {E[]} data - The `data` parameter is an array of elements of type `E`.
1208
- * @returns The `fromArray` function returns a DoublyLinkedList object.
1209
- */
1210
- static fromArray<E>(data: E[]) {
1211
- return new DoublyLinkedList<E>(data);
1212
- }
1213
-
1214
1268
  /**
1215
1269
  * The function returns an iterator that iterates over the values of a linked list.
1216
1270
  */
@@ -59,18 +59,16 @@ export class SinglyLinkedListNode<E = any> {
59
59
  }
60
60
  }
61
61
 
62
+ /**
63
+ *
64
+ */
62
65
  export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R, SinglyLinkedList<E, R>> {
63
- constructor(elements: Iterable<E> | Iterable<R> = [], options?: SinglyLinkedListOptions<E, R>) {
66
+ constructor(
67
+ elements: Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>> = [],
68
+ options?: SinglyLinkedListOptions<E, R>
69
+ ) {
64
70
  super(options);
65
- if (elements) {
66
- for (const el of elements) {
67
- if (this.toElementFn) {
68
- this.push(this.toElementFn(el as R));
69
- } else {
70
- this.push(el as E);
71
- }
72
- }
73
- }
71
+ this.pushMany(elements);
74
72
  }
75
73
 
76
74
  protected _head: SinglyLinkedListNode<E> | undefined;
@@ -121,6 +119,23 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
121
119
  return this._size;
122
120
  }
123
121
 
122
+ /**
123
+ * Time Complexity: O(n)
124
+ * Space Complexity: O(n)
125
+ *
126
+ * The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
127
+ * array.
128
+ * @param {E[]} data - The `data` parameter is an array of elements of type `E`.
129
+ * @returns The `fromArray` function returns a `SinglyLinkedList` object.
130
+ */
131
+ static fromArray<E>(data: E[]) {
132
+ const singlyLinkedList = new SinglyLinkedList<E>();
133
+ for (const item of data) {
134
+ singlyLinkedList.push(item);
135
+ }
136
+ return singlyLinkedList;
137
+ }
138
+
124
139
  /**
125
140
  * Time Complexity: O(1)
126
141
  * Space Complexity: O(1)
@@ -211,6 +226,55 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
211
226
  return true;
212
227
  }
213
228
 
229
+ /**
230
+ * Time Complexity: O(k)
231
+ * Space Complexity: O(k)
232
+ *
233
+ * The function `pushMany` iterates over elements and pushes them into a data structure, applying a
234
+ * transformation function if provided.
235
+ * @param {Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>} elements - The `elements`
236
+ * parameter in the `pushMany` function can accept an iterable containing elements of type `E`, `R`,
237
+ * or `SinglyLinkedListNode<E>`.
238
+ * @returns The `pushMany` function returns an array of boolean values indicating whether each
239
+ * element was successfully pushed into the data structure.
240
+ */
241
+ pushMany(elements: Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>) {
242
+ const ans: boolean[] = [];
243
+ for (const el of elements) {
244
+ if (this.toElementFn) {
245
+ ans.push(this.push(this.toElementFn(el as R)));
246
+ continue;
247
+ }
248
+ ans.push(this.push(el as E | SinglyLinkedListNode<E>));
249
+ }
250
+ return ans;
251
+ }
252
+
253
+ /**
254
+ * Time Complexity: O(k)
255
+ * Space Complexity: O(k)
256
+ *
257
+ * The function `unshiftMany` iterates over elements and adds them to a data structure, optionally
258
+ * converting them using a provided function.
259
+ * @param {Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>} elements - The `elements`
260
+ * parameter in the `unshiftMany` function can accept an iterable containing elements of type `E`,
261
+ * `R`, or `SinglyLinkedListNode<E>`. The function iterates over each element in the iterable and
262
+ * performs an `unshift` operation on the linked list for each
263
+ * @returns The `unshiftMany` function is returning an array of boolean values, where each value
264
+ * represents the result of calling the `unshift` method on the current instance of the class.
265
+ */
266
+ unshiftMany(elements: Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>) {
267
+ const ans: boolean[] = [];
268
+ for (const el of elements) {
269
+ if (this.toElementFn) {
270
+ ans.push(this.unshift(this.toElementFn(el as R)));
271
+ continue;
272
+ }
273
+ ans.push(this.unshift(el as E | SinglyLinkedListNode<E>));
274
+ }
275
+ return ans;
276
+ }
277
+
214
278
  /**
215
279
  * Time Complexity: O(n)
216
280
  * Space Complexity: O(1)
@@ -399,6 +463,9 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
399
463
  }
400
464
 
401
465
  /**
466
+ * Time Complexity: O(1)
467
+ * Space Complexity: O(1)
468
+ *
402
469
  * The function checks if the length of a data structure is equal to zero and returns a boolean value indicating
403
470
  * whether it is empty or not.
404
471
  * @returns A boolean value indicating whether the length of the object is equal to 0.
@@ -408,6 +475,9 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
408
475
  }
409
476
 
410
477
  /**
478
+ * Time Complexity: O(1)
479
+ * Space Complexity: O(1)
480
+ *
411
481
  * The `clear` function resets the linked list by setting the head, tail, and length to undefined and 0 respectively.
412
482
  */
413
483
  clear(): void {
@@ -713,23 +783,6 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
713
783
  }
714
784
  }
715
785
 
716
- /**
717
- * Time Complexity: O(n)
718
- * Space Complexity: O(n)
719
- *
720
- * The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
721
- * array.
722
- * @param {E[]} data - The `data` parameter is an array of elements of type `E`.
723
- * @returns The `fromArray` function returns a `SinglyLinkedList` object.
724
- */
725
- static fromArray<E>(data: E[]) {
726
- const singlyLinkedList = new SinglyLinkedList<E>();
727
- for (const item of data) {
728
- singlyLinkedList.push(item);
729
- }
730
- return singlyLinkedList;
731
- }
732
-
733
786
  /**
734
787
  * The _isPredicate function in TypeScript checks if the input is a function that takes a
735
788
  * SinglyLinkedListNode as an argument and returns a boolean.
@@ -19,6 +19,9 @@ export class SkipListNode<K, V> {
19
19
  }
20
20
  }
21
21
 
22
+ /**
23
+ *
24
+ */
22
25
  export class SkipList<K, V> {
23
26
  /**
24
27
  * The constructor function initializes a SkipLinkedList object with optional options and elements.
@@ -7,6 +7,9 @@
7
7
  */
8
8
  import type { MatrixOptions } from '../../types';
9
9
 
10
+ /**
11
+ *
12
+ */
10
13
  export class Matrix {
11
14
  /**
12
15
  * The constructor function initializes a matrix object with the provided data and options, or with
@@ -25,6 +25,9 @@ export class Character {
25
25
  }
26
26
  }
27
27
 
28
+ /**
29
+ *
30
+ */
28
31
  export class Navigator<T = number> {
29
32
  onMove: (cur: [number, number]) => void;
30
33
  protected readonly _matrix: T[][];
@@ -8,6 +8,9 @@
8
8
  import type { Comparator, ElementCallback, PriorityQueueOptions } from '../../types';
9
9
  import { PriorityQueue } from './priority-queue';
10
10
 
11
+ /**
12
+ *
13
+ */
11
14
  export class MaxPriorityQueue<E = any, R = any> extends PriorityQueue<E, R> {
12
15
  /**
13
16
  * The constructor initializes a PriorityQueue with optional elements and options, including a
@@ -8,6 +8,9 @@
8
8
  import type { Comparator, ElementCallback, PriorityQueueOptions } from '../../types';
9
9
  import { PriorityQueue } from './priority-queue';
10
10
 
11
+ /**
12
+ *
13
+ */
11
14
  export class MinPriorityQueue<E = any, R = any> extends PriorityQueue<E, R> {
12
15
  /**
13
16
  * The constructor initializes a PriorityQueue with optional elements and options, including a