avl-tree-typed 1.53.7 → 1.53.9

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 (68) hide show
  1. package/dist/common/index.js +5 -0
  2. package/dist/data-structures/base/iterable-entry-base.js +4 -4
  3. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +36 -17
  4. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +65 -36
  5. package/dist/data-structures/binary-tree/avl-tree.d.ts +12 -8
  6. package/dist/data-structures/binary-tree/avl-tree.js +19 -6
  7. package/dist/data-structures/binary-tree/binary-tree.d.ts +53 -40
  8. package/dist/data-structures/binary-tree/binary-tree.js +76 -72
  9. package/dist/data-structures/binary-tree/bst.d.ts +87 -52
  10. package/dist/data-structures/binary-tree/bst.js +111 -63
  11. package/dist/data-structures/binary-tree/index.d.ts +1 -1
  12. package/dist/data-structures/binary-tree/index.js +1 -1
  13. package/dist/data-structures/binary-tree/{rb-tree.d.ts → red-black-tree.d.ts} +83 -10
  14. package/dist/data-structures/binary-tree/{rb-tree.js → red-black-tree.js} +91 -44
  15. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +34 -18
  16. package/dist/data-structures/binary-tree/tree-multi-map.js +66 -40
  17. package/dist/data-structures/graph/abstract-graph.js +2 -2
  18. package/dist/data-structures/hash/hash-map.d.ts +31 -1
  19. package/dist/data-structures/hash/hash-map.js +35 -5
  20. package/dist/data-structures/heap/heap.d.ts +20 -3
  21. package/dist/data-structures/heap/heap.js +31 -11
  22. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +46 -11
  23. package/dist/data-structures/linked-list/doubly-linked-list.js +68 -21
  24. package/dist/data-structures/linked-list/singly-linked-list.d.ts +44 -11
  25. package/dist/data-structures/linked-list/singly-linked-list.js +70 -26
  26. package/dist/data-structures/queue/deque.d.ts +37 -8
  27. package/dist/data-structures/queue/deque.js +73 -29
  28. package/dist/data-structures/queue/queue.d.ts +41 -1
  29. package/dist/data-structures/queue/queue.js +51 -9
  30. package/dist/data-structures/stack/stack.d.ts +27 -10
  31. package/dist/data-structures/stack/stack.js +39 -20
  32. package/dist/data-structures/trie/trie.d.ts +8 -3
  33. package/dist/data-structures/trie/trie.js +8 -3
  34. package/dist/interfaces/binary-tree.d.ts +3 -4
  35. package/dist/types/data-structures/base/base.d.ts +1 -1
  36. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -3
  37. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -3
  38. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +2 -3
  39. package/dist/types/data-structures/binary-tree/bst.d.ts +3 -4
  40. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +4 -5
  41. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +4 -5
  42. package/package.json +2 -2
  43. package/src/common/index.ts +7 -1
  44. package/src/data-structures/base/iterable-entry-base.ts +4 -4
  45. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +82 -55
  46. package/src/data-structures/binary-tree/avl-tree.ts +32 -15
  47. package/src/data-structures/binary-tree/binary-tree.ts +89 -84
  48. package/src/data-structures/binary-tree/bst.ts +149 -97
  49. package/src/data-structures/binary-tree/index.ts +1 -1
  50. package/src/data-structures/binary-tree/{rb-tree.ts → red-black-tree.ts} +105 -55
  51. package/src/data-structures/binary-tree/tree-multi-map.ts +81 -51
  52. package/src/data-structures/graph/abstract-graph.ts +2 -2
  53. package/src/data-structures/hash/hash-map.ts +37 -7
  54. package/src/data-structures/heap/heap.ts +33 -10
  55. package/src/data-structures/linked-list/doubly-linked-list.ts +75 -21
  56. package/src/data-structures/linked-list/singly-linked-list.ts +77 -27
  57. package/src/data-structures/queue/deque.ts +72 -28
  58. package/src/data-structures/queue/queue.ts +50 -7
  59. package/src/data-structures/stack/stack.ts +39 -20
  60. package/src/data-structures/trie/trie.ts +8 -3
  61. package/src/interfaces/binary-tree.ts +3 -13
  62. package/src/types/data-structures/base/base.ts +1 -1
  63. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +2 -4
  64. package/src/types/data-structures/binary-tree/avl-tree.ts +2 -4
  65. package/src/types/data-structures/binary-tree/binary-tree.ts +3 -3
  66. package/src/types/data-structures/binary-tree/bst.ts +3 -5
  67. package/src/types/data-structures/binary-tree/rb-tree.ts +4 -6
  68. package/src/types/data-structures/binary-tree/tree-multi-map.ts +4 -6
@@ -9,15 +9,15 @@ import type {
9
9
  BinaryTreeDeleteResult,
10
10
  BSTNOptKeyOrNode,
11
11
  BTNRep,
12
+ EntryCallback,
12
13
  IterationType,
13
14
  OptNode,
14
15
  RBTNColor,
15
- TreeMultiMapNested,
16
16
  TreeMultiMapNodeNested,
17
17
  TreeMultiMapOptions
18
18
  } from '../../types';
19
19
  import { IBinaryTree } from '../../interfaces';
20
- import { RedBlackTree, RedBlackTreeNode } from './rb-tree';
20
+ import { RedBlackTree, RedBlackTreeNode } from './red-black-tree';
21
21
 
22
22
  export class TreeMultiMapNode<
23
23
  K = any,
@@ -65,11 +65,10 @@ export class TreeMultiMap<
65
65
  K = any,
66
66
  V = any,
67
67
  R = object,
68
- NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNode<K, V, TreeMultiMapNodeNested<K, V>>,
69
- TREE extends TreeMultiMap<K, V, R, NODE, TREE> = TreeMultiMap<K, V, R, NODE, TreeMultiMapNested<K, V, R, NODE>>
68
+ NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNode<K, V, TreeMultiMapNodeNested<K, V>>
70
69
  >
71
- extends RedBlackTree<K, V, R, NODE, TREE>
72
- implements IBinaryTree<K, V, R, NODE, TREE>
70
+ extends RedBlackTree<K, V, R, NODE>
71
+ implements IBinaryTree<K, V, R, NODE>
73
72
  {
74
73
  /**
75
74
  * The constructor function initializes a TreeMultiMap object with optional initial data.
@@ -135,53 +134,15 @@ export class TreeMultiMap<
135
134
  * @returns a new instance of the `TreeMultiMap` class, with the provided options merged with the
136
135
  * existing `iterationType` property. The returned value is casted as `TREE`.
137
136
  */
138
- override createTree(options?: TreeMultiMapOptions<K, V, R>): TREE {
139
- return new TreeMultiMap<K, V, R, NODE, TREE>([], {
137
+ // @ts-ignore
138
+ override createTree(options?: TreeMultiMapOptions<K, V, R>) {
139
+ return new TreeMultiMap<K, V, R, NODE>([], {
140
140
  iterationType: this.iterationType,
141
141
  isMapMode: this._isMapMode,
142
- extractComparable: this._extractComparable,
142
+ specifyComparable: this._specifyComparable,
143
143
  toEntryFn: this._toEntryFn,
144
144
  ...options
145
- }) as TREE;
146
- }
147
-
148
- /**
149
- * The function `keyValueNodeEntryRawToNodeAndValue` takes in a key, value, and count and returns a
150
- * node based on the input.
151
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
152
- * `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
153
- * @param {V} [value] - The `value` parameter is an optional value that represents the value
154
- * associated with the key in the node. It is used when creating a new node or updating the value of
155
- * an existing node.
156
- * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
157
- * times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
158
- * @returns either a NODE object or undefined.
159
- */
160
- override keyValueNodeEntryRawToNodeAndValue(
161
- keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
162
- value?: V,
163
- count = 1
164
- ): [NODE | undefined, V | undefined] {
165
- if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null) return [undefined, undefined];
166
-
167
- if (this.isNode(keyNodeEntryOrRaw)) return [keyNodeEntryOrRaw, value];
168
-
169
- if (this.isEntry(keyNodeEntryOrRaw)) {
170
- const [key, entryValue] = keyNodeEntryOrRaw;
171
- if (key === undefined || key === null) return [undefined, undefined];
172
- const finalValue = value ?? entryValue;
173
- if (this.isKey(key)) return [this.createNode(key, finalValue, 'BLACK', count), finalValue];
174
- }
175
-
176
- if (this.isRaw(keyNodeEntryOrRaw)) {
177
- const [key, entryValue] = this._toEntryFn!(keyNodeEntryOrRaw);
178
- const finalValue = value ?? entryValue;
179
- if (this.isKey(key)) return [this.createNode(key, finalValue, 'BLACK', count), finalValue];
180
- }
181
-
182
- if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value, 'BLACK', count), value];
183
-
184
- return [undefined, undefined];
145
+ });
185
146
  }
186
147
 
187
148
  /**
@@ -212,7 +173,7 @@ export class TreeMultiMap<
212
173
  * was successful, and false otherwise.
213
174
  */
214
175
  override add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V, count = 1): boolean {
215
- const [newNode, newValue] = this.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count);
176
+ const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count);
216
177
  const orgCount = newNode?.count || 0;
217
178
  const isSuccessAdded = super.add(newNode, newValue);
218
179
 
@@ -402,13 +363,82 @@ export class TreeMultiMap<
402
363
  * The function overrides the clone method to create a deep copy of a tree object.
403
364
  * @returns The `clone()` method is returning a cloned instance of the `TREE` object.
404
365
  */
405
- override clone(): TREE {
366
+ // @ts-ignore
367
+ override clone() {
406
368
  const cloned = this.createTree();
407
369
  this.bfs(node => cloned.add(node.key, undefined, node.count));
408
370
  if (this._isMapMode) cloned._store = this._store;
409
371
  return cloned;
410
372
  }
411
373
 
374
+ /**
375
+ * The `map` function in TypeScript overrides the default behavior to create a new TreeMultiMap with
376
+ * modified entries based on a provided callback.
377
+ * @param callback - The `callback` parameter is a function that will be called for each entry in the
378
+ * map. It takes four arguments:
379
+ * @param [options] - The `options` parameter in the `override map` function is of type
380
+ * `TreeMultiMapOptions<MK, MV, MR>`. This parameter allows you to provide additional configuration
381
+ * options when creating a new `TreeMultiMap` instance within the `map` function. These options could
382
+ * include things like
383
+ * @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
384
+ * the value of `this` when executing the `callback` function. It allows you to set the context
385
+ * (value of `this`) for the callback function when it is called within the `map` function. This
386
+ * @returns A new TreeMultiMap instance is being returned, which is populated with entries generated
387
+ * by the provided callback function.
388
+ */
389
+ // @ts-ignore
390
+ override map<MK, MV, MR>(
391
+ callback: EntryCallback<K, V | undefined, [MK, MV]>,
392
+ options?: TreeMultiMapOptions<MK, MV, MR>,
393
+ thisArg?: any
394
+ ) {
395
+ const newTree = new TreeMultiMap<MK, MV, MR>([], options);
396
+ let index = 0;
397
+ for (const [key, value] of this) {
398
+ newTree.add(callback.call(thisArg, key, value, index++, this));
399
+ }
400
+ return newTree;
401
+ }
402
+
403
+ /**
404
+ * The function `keyValueNodeEntryRawToNodeAndValue` takes in a key, value, and count and returns a
405
+ * node based on the input.
406
+ * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
407
+ * `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
408
+ * @param {V} [value] - The `value` parameter is an optional value that represents the value
409
+ * associated with the key in the node. It is used when creating a new node or updating the value of
410
+ * an existing node.
411
+ * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
412
+ * times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
413
+ * @returns either a NODE object or undefined.
414
+ */
415
+ protected override _keyValueNodeEntryRawToNodeAndValue(
416
+ keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
417
+ value?: V,
418
+ count = 1
419
+ ): [NODE | undefined, V | undefined] {
420
+ if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null) return [undefined, undefined];
421
+
422
+ if (this.isNode(keyNodeEntryOrRaw)) return [keyNodeEntryOrRaw, value];
423
+
424
+ if (this.isEntry(keyNodeEntryOrRaw)) {
425
+ const [key, entryValue] = keyNodeEntryOrRaw;
426
+ if (key === undefined || key === null) return [undefined, undefined];
427
+ const finalValue = value ?? entryValue;
428
+ if (this.isKey(key)) return [this.createNode(key, finalValue, 'BLACK', count), finalValue];
429
+ }
430
+
431
+ if (this.isRaw(keyNodeEntryOrRaw)) {
432
+ const [key, entryValue] = this._toEntryFn!(keyNodeEntryOrRaw);
433
+ const finalValue = value ?? entryValue;
434
+ if (this.isKey(key)) return [this.createNode(key, finalValue, 'BLACK', count), finalValue];
435
+ }
436
+
437
+ if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value, 'BLACK', count), value];
438
+
439
+ return [undefined, undefined];
440
+ }
441
+
412
442
  /**
413
443
  * Time Complexity: O(1)
414
444
  * Space Complexity: O(1)
@@ -947,7 +947,7 @@ export abstract class AbstractGraph<
947
947
  const filtered: [VertexKey, V | undefined][] = [];
948
948
  let index = 0;
949
949
  for (const [key, value] of this) {
950
- if (predicate.call(thisArg, value, key, index, this)) {
950
+ if (predicate.call(thisArg, key, value, index, this)) {
951
951
  filtered.push([key, value]);
952
952
  }
953
953
  index++;
@@ -972,7 +972,7 @@ export abstract class AbstractGraph<
972
972
  const mapped: T[] = [];
973
973
  let index = 0;
974
974
  for (const [key, value] of this) {
975
- mapped.push(callback.call(thisArg, value, key, index, this));
975
+ mapped.push(callback.call(thisArg, key, value, index, this));
976
976
  index++;
977
977
  }
978
978
  return mapped;
@@ -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
  */