undirected-graph-typed 1.51.7 → 1.51.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 (55) hide show
  1. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +103 -74
  2. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +116 -93
  3. package/dist/data-structures/binary-tree/avl-tree.d.ts +82 -62
  4. package/dist/data-structures/binary-tree/avl-tree.js +90 -71
  5. package/dist/data-structures/binary-tree/binary-tree.d.ts +318 -233
  6. package/dist/data-structures/binary-tree/binary-tree.js +492 -392
  7. package/dist/data-structures/binary-tree/bst.d.ts +204 -251
  8. package/dist/data-structures/binary-tree/bst.js +256 -358
  9. package/dist/data-structures/binary-tree/rb-tree.d.ts +74 -85
  10. package/dist/data-structures/binary-tree/rb-tree.js +111 -119
  11. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +92 -76
  12. package/dist/data-structures/binary-tree/tree-multi-map.js +105 -93
  13. package/dist/data-structures/graph/abstract-graph.d.ts +10 -15
  14. package/dist/data-structures/graph/abstract-graph.js +10 -15
  15. package/dist/data-structures/hash/hash-map.d.ts +31 -38
  16. package/dist/data-structures/hash/hash-map.js +40 -55
  17. package/dist/data-structures/heap/heap.d.ts +1 -3
  18. package/dist/data-structures/queue/deque.d.ts +2 -3
  19. package/dist/data-structures/queue/deque.js +2 -3
  20. package/dist/data-structures/trie/trie.d.ts +1 -1
  21. package/dist/data-structures/trie/trie.js +1 -1
  22. package/dist/interfaces/binary-tree.d.ts +7 -7
  23. package/dist/types/common.d.ts +2 -3
  24. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +4 -3
  25. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +4 -3
  26. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +6 -5
  27. package/dist/types/data-structures/binary-tree/bst.d.ts +6 -5
  28. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +4 -3
  29. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +4 -3
  30. package/dist/types/utils/utils.d.ts +10 -1
  31. package/dist/utils/utils.d.ts +2 -1
  32. package/dist/utils/utils.js +27 -1
  33. package/package.json +2 -2
  34. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +142 -100
  35. package/src/data-structures/binary-tree/avl-tree.ts +109 -80
  36. package/src/data-structures/binary-tree/binary-tree.ts +556 -433
  37. package/src/data-structures/binary-tree/bst.ts +286 -375
  38. package/src/data-structures/binary-tree/rb-tree.ts +132 -125
  39. package/src/data-structures/binary-tree/tree-multi-map.ts +129 -102
  40. package/src/data-structures/graph/abstract-graph.ts +10 -10
  41. package/src/data-structures/hash/hash-map.ts +42 -49
  42. package/src/data-structures/heap/heap.ts +1 -1
  43. package/src/data-structures/queue/deque.ts +2 -2
  44. package/src/data-structures/queue/queue.ts +1 -1
  45. package/src/data-structures/trie/trie.ts +2 -2
  46. package/src/interfaces/binary-tree.ts +11 -9
  47. package/src/types/common.ts +2 -3
  48. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +4 -3
  49. package/src/types/data-structures/binary-tree/avl-tree.ts +4 -3
  50. package/src/types/data-structures/binary-tree/binary-tree.ts +7 -6
  51. package/src/types/data-structures/binary-tree/bst.ts +6 -5
  52. package/src/types/data-structures/binary-tree/rb-tree.ts +4 -3
  53. package/src/types/data-structures/binary-tree/tree-multi-map.ts +4 -3
  54. package/src/types/utils/utils.ts +14 -1
  55. package/src/utils/utils.ts +20 -1
@@ -9,18 +9,20 @@ import type {
9
9
  BinaryTreeDeleteResult,
10
10
  BSTNKeyOrNode,
11
11
  BTNCallback,
12
+ Comparable,
12
13
  IterationType,
13
14
  KeyOrNodeOrEntry,
15
+ RBTNColor,
14
16
  TreeMultiMapNested,
15
17
  TreeMultiMapNodeNested,
16
18
  TreeMultiMapOptions
17
19
  } from '../../types';
18
- import { RBTNColor } from '../../types';
20
+ import { BTNEntry } from '../../types';
19
21
  import { IBinaryTree } from '../../interfaces';
20
22
  import { RedBlackTree, RedBlackTreeNode } from './rb-tree';
21
23
 
22
24
  export class TreeMultiMapNode<
23
- K = any,
25
+ K extends Comparable,
24
26
  V = any,
25
27
  NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNodeNested<K, V>
26
28
  > extends RedBlackTreeNode<K, V, NODE> {
@@ -62,25 +64,29 @@ export class TreeMultiMapNode<
62
64
  }
63
65
 
64
66
  export class TreeMultiMap<
65
- K = any,
67
+ K extends Comparable,
66
68
  V = any,
69
+ R = BTNEntry<K, V>,
67
70
  NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNode<K, V, TreeMultiMapNodeNested<K, V>>,
68
- TREE extends TreeMultiMap<K, V, NODE, TREE> = TreeMultiMap<K, V, NODE, TreeMultiMapNested<K, V, NODE>>
71
+ TREE extends TreeMultiMap<K, V, R, NODE, TREE> = TreeMultiMap<K, V, R, NODE, TreeMultiMapNested<K, V, R, NODE>>
69
72
  >
70
- extends RedBlackTree<K, V, NODE, TREE>
71
- implements IBinaryTree<K, V, NODE, TREE> {
73
+ extends RedBlackTree<K, V, R, NODE, TREE>
74
+ implements IBinaryTree<K, V, R, NODE, TREE> {
72
75
  /**
73
- * The constructor function initializes a new instance of the TreeMultiMap class with optional
74
- * initial keys, nodes, or entries.
75
- * @param keysOrNodesOrEntries - The `keysOrNodesOrEntries` parameter is an iterable object that can
76
- * contain keys, nodes, or entries. It is used to initialize the TreeMultiMap with the provided keys,
77
- * nodes, or entries.
78
- * @param [options] - The `options` parameter is an optional object that can be passed to the
79
- * constructor. It allows you to customize the behavior of the `TreeMultiMap` instance.
76
+ * The constructor function initializes a TreeMultiMap object with optional initial data.
77
+ * @param keysOrNodesOrEntriesOrRawElements - The parameter `keysOrNodesOrEntriesOrRawElements` is an
78
+ * iterable that can contain keys, nodes, entries, or raw elements. It is used to initialize the
79
+ * TreeMultiMap with initial data.
80
+ * @param [options] - The `options` parameter is an optional object that can be used to customize the
81
+ * behavior of the `TreeMultiMap` constructor. It can include properties such as `compareKeys` and
82
+ * `compareValues`, which are functions used to compare keys and values respectively.
80
83
  */
81
- constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: TreeMultiMapOptions<K>) {
84
+ constructor(
85
+ keysOrNodesOrEntriesOrRawElements: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [],
86
+ options?: TreeMultiMapOptions<K, V, R>
87
+ ) {
82
88
  super([], options);
83
- if (keysOrNodesOrEntries) this.addMany(keysOrNodesOrEntries);
89
+ if (keysOrNodesOrEntriesOrRawElements) this.addMany(keysOrNodesOrEntriesOrRawElements);
84
90
  }
85
91
 
86
92
  protected _count = 0;
@@ -116,16 +122,15 @@ export class TreeMultiMap<
116
122
  /**
117
123
  * The function creates a new TreeMultiMapNode with the specified key, value, color, and count.
118
124
  * @param {K} key - The key parameter represents the key of the node being created. It is of type K,
119
- * which is a generic type representing the key type of the node.
120
- * @param {V} [value] - The `value` parameter represents the value associated with the key in the
121
- * node. It is an optional parameter, which means it can be omitted when calling the `createNode`
122
- * function. If provided, it should be of type `V`.
125
+ * which is a generic type representing the type of keys in the tree.
126
+ * @param {V} [value] - The `value` parameter is an optional parameter that represents the value
127
+ * associated with the key in the node. It is of type `V`, which can be any data type.
123
128
  * @param {RBTNColor} [color=BLACK] - The color parameter is used to specify the color of the node in
124
129
  * a Red-Black Tree. It can have two possible values: 'RED' or 'BLACK'. The default value is 'BLACK'.
125
130
  * @param {number} [count] - The `count` parameter represents the number of occurrences of a key in
126
131
  * the tree. It is an optional parameter and is used to keep track of the number of values associated
127
132
  * with a key in the tree.
128
- * @returns A new instance of the TreeMultiMapNode class is being returned.
133
+ * @returns A new instance of the TreeMultiMapNode class, casted as NODE.
129
134
  */
130
135
  override createNode(key: K, value?: V, color: RBTNColor = 'BLACK', count?: number): NODE {
131
136
  return new TreeMultiMapNode(key, value, count, color) as NODE;
@@ -134,64 +139,67 @@ export class TreeMultiMap<
134
139
  /**
135
140
  * The function creates a new instance of a TreeMultiMap with the specified options and returns it.
136
141
  * @param [options] - The `options` parameter is an optional object that contains additional
137
- * configuration options for creating the `TreeMultiMap`. It can include properties such as
138
- * `keyComparator`, `valueComparator`, `allowDuplicates`, etc.
142
+ * configuration options for creating the `TreeMultiMap`. It is of type `TreeMultiMapOptions<K, V,
143
+ * R>`.
139
144
  * @returns a new instance of the `TreeMultiMap` class, with the provided options merged with the
140
- * existing `iterationType` option. The returned value is casted as `TREE`.
145
+ * existing `iterationType` property. The returned value is casted as `TREE`.
141
146
  */
142
- override createTree(options?: TreeMultiMapOptions<K>): TREE {
143
- return new TreeMultiMap<K, V, NODE, TREE>([], {
147
+ override createTree(options?: TreeMultiMapOptions<K, V, R>): TREE {
148
+ return new TreeMultiMap<K, V, R, NODE, TREE>([], {
144
149
  iterationType: this.iterationType,
145
150
  ...options
146
151
  }) as TREE;
147
152
  }
148
153
 
149
154
  /**
150
- * The function `keyValueOrEntryToNode` takes a key, value, and count and returns a node if the input
151
- * is valid.
152
- * @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be of type `KeyOrNodeOrEntry<K, V,
153
- * NODE>`. It can accept three types of values:
154
- * @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
155
- * value associated with a key in a key-value pair.
156
- * @param [count=1] - The count parameter is an optional parameter that specifies the number of times
157
- * the key-value pair should be added to the node. If not provided, it defaults to 1.
158
- * @returns a NODE object or undefined.
155
+ * The function `keyValueOrEntryOrRawElementToNode` takes in a key, value, and count and returns a
156
+ * node based on the input.
157
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
158
+ * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
159
+ * @param {V} [value] - The `value` parameter is an optional value that represents the value
160
+ * associated with the key in the node. It is used when creating a new node or updating the value of
161
+ * an existing node.
162
+ * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
163
+ * times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
164
+ * @returns either a NODE object or undefined.
159
165
  */
160
- override keyValueOrEntryToNode(
161
- keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>,
166
+ override keyValueOrEntryOrRawElementToNode(
167
+ keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>,
162
168
  value?: V,
163
169
  count = 1
164
170
  ): NODE | undefined {
165
- let node: NODE | undefined;
166
- if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) {
167
- return;
168
- } else if (this.isNode(keyOrNodeOrEntry)) {
169
- node = keyOrNodeOrEntry;
170
- } else if (this.isEntry(keyOrNodeOrEntry)) {
171
- const [key, value] = keyOrNodeOrEntry;
172
- if (key === undefined || key === null) {
173
- return;
174
- } else {
175
- node = this.createNode(key, value, 'BLACK', count);
176
- }
177
- } else if (!this.isNode(keyOrNodeOrEntry)) {
178
- node = this.createNode(keyOrNodeOrEntry, value, 'BLACK', count);
179
- } else {
180
- return;
171
+ if (keyOrNodeOrEntryOrRawElement === undefined || keyOrNodeOrEntryOrRawElement === null) return;
172
+
173
+ if (this.isNode(keyOrNodeOrEntryOrRawElement)) return keyOrNodeOrEntryOrRawElement;
174
+
175
+ if (this.toEntryFn) {
176
+ const [key] = this.toEntryFn(keyOrNodeOrEntryOrRawElement as R);
177
+ if (key) return this.getNodeByKey(key);
181
178
  }
182
- return node;
179
+
180
+ if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
181
+ const [key, value] = keyOrNodeOrEntryOrRawElement;
182
+ if (key === undefined || key === null) return;
183
+ else return this.createNode(key, value, 'BLACK', count);
184
+ }
185
+
186
+ if (this.isKey(keyOrNodeOrEntryOrRawElement))
187
+ return this.createNode(keyOrNodeOrEntryOrRawElement, value, 'BLACK', count);
188
+
189
+ return;
183
190
  }
184
191
 
185
192
  /**
186
- * The function "isNode" checks if a given key, node, or entry is an instance of the TreeMultiMapNode
187
- * class.
188
- * @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be of type `KeyOrNodeOrEntry<K, V,
189
- * NODE>`.
190
- * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntry` is an instance
191
- * of the `TreeMultiMapNode` class.
193
+ * The function checks if the input is an instance of the TreeMultiMapNode class.
194
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
195
+ * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
196
+ * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
197
+ * an instance of the `TreeMultiMapNode` class.
192
198
  */
193
- override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE {
194
- return keyOrNodeOrEntry instanceof TreeMultiMapNode;
199
+ override isNode(
200
+ keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>
201
+ ): keyOrNodeOrEntryOrRawElement is NODE {
202
+ return keyOrNodeOrEntryOrRawElement instanceof TreeMultiMapNode;
195
203
  }
196
204
 
197
205
  /**
@@ -203,17 +211,20 @@ export class TreeMultiMap<
203
211
  * Time Complexity: O(log n)
204
212
  * Space Complexity: O(1)
205
213
  *
206
- * The function overrides the add method in TypeScript and adds a new node to the data structure.
207
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can accept three types of values:
214
+ * The function overrides the add method of a class and adds a new node to a data structure, updating
215
+ * the count and returning a boolean indicating success.
216
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
217
+ * `keyOrNodeOrEntryOrRawElement` parameter can accept one of the following types:
208
218
  * @param {V} [value] - The `value` parameter represents the value associated with the key in the
209
- * data structure.
219
+ * data structure. It is an optional parameter, so it can be omitted if not needed.
210
220
  * @param [count=1] - The `count` parameter represents the number of times the key-value pair should
211
- * be added to the data structure. By default, it is set to 1, meaning that the key-value pair will
212
- * be added once. However, you can specify a different value for `count` if you want to add
213
- * @returns a boolean value.
221
+ * be added to the data structure. By default, it is set to 1, meaning that if no value is provided
222
+ * for `count`, the key-value pair will be added once.
223
+ * @returns The method is returning a boolean value. It returns true if the addition of the new node
224
+ * was successful, and false otherwise.
214
225
  */
215
- override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V, count = 1): boolean {
216
- const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value, count);
226
+ override add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V, count = 1): boolean {
227
+ const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value, count);
217
228
  const orgCount = newNode?.count || 0;
218
229
  const isSuccessAdded = super.add(newNode);
219
230
 
@@ -234,20 +245,18 @@ export class TreeMultiMap<
234
245
  * Time Complexity: O(log n)
235
246
  * Space Complexity: O(1)
236
247
  *
237
- * The `delete` function in a TypeScript class is used to delete nodes from a binary tree based on a
238
- * given identifier, and it returns an array of results containing information about the deleted
239
- * nodes.
240
- * @param {ReturnType<C> | null | undefined} identifier - The identifier parameter is the value used
241
- * to identify the node to be deleted. It can be of any type that is returned by the callback
242
- * function. It can also be null or undefined if no node needs to be deleted.
243
- * @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as
244
- * input and returns a value of type `ReturnType<C>`. It is used to determine if a node matches the
245
- * identifier for deletion. If no callback is provided, the `_DEFAULT_CALLBACK` function is
246
- * used
247
- * @param [ignoreCount=false] - A boolean value indicating whether to ignore the count of the target
248
- * node when performing deletion. If set to true, the count of the target node will not be considered
249
- * and the node will be deleted regardless of its count. If set to false (default), the count of the
250
- * target node will be decremented
248
+ * The function `delete` is used to remove a node from a binary tree and fix the tree if necessary.
249
+ * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value or
250
+ * key that is used to identify the node that needs to be deleted from the binary tree. It can be of
251
+ * any type that is returned by the callback function `C`. It can also be `null` or `undefined` if
252
+ * the node to be deleted
253
+ * @param {C} callback - The `callback` parameter is a function that is used to determine the
254
+ * equality of nodes in the binary tree. It is optional and has a default value of
255
+ * `this._DEFAULT_CALLBACK`. The `callback` function is used to compare nodes when searching for a
256
+ * specific node or when performing other operations on the
257
+ * @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
258
+ * being deleted. If set to true, the count of the node will not be taken into account when deleting
259
+ * it. If set to false, the count of the node will be decremented by 1 before deleting it.
251
260
  * @returns an array of BinaryTreeDeleteResult<NODE> objects.
252
261
  */
253
262
  override delete<C extends BTNCallback<NODE>>(
@@ -371,10 +380,12 @@ export class TreeMultiMap<
371
380
  *
372
381
  * The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
373
382
  * tree using either a recursive or iterative approach.
374
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
375
- * type of iteration to use when building the balanced binary search tree. It can have two possible
376
- * values:
377
- * @returns a boolean value.
383
+ * @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
384
+ * specifies the type of iteration to use when building the balanced binary search tree. It has a
385
+ * default value of `this.iterationType`, which means it will use the iteration type specified by the
386
+ * `iterationType` property of the current object.
387
+ * @returns The function `perfectlyBalance` returns a boolean value. It returns `true` if the
388
+ * balancing operation is successful, and `false` if there are no nodes to balance.
378
389
  */
379
390
  override perfectlyBalance(iterationType: IterationType = this.iterationType): boolean {
380
391
  const sorted = this.dfs(node => node, 'IN'),
@@ -433,19 +444,27 @@ export class TreeMultiMap<
433
444
  }
434
445
 
435
446
  /**
436
- * The function swaps the properties of two nodes in a binary search tree.
437
- * @param srcNode - The source node that needs to be swapped with the destination node. It can be
438
- * either a key or a node object.
439
- * @param destNode - The `destNode` parameter is the node in the binary search tree where the
440
- * properties will be swapped with the `srcNode`.
447
+ * Time Complexity: O(1)
448
+ * Space Complexity: O(1)
449
+ */
450
+
451
+ /**
452
+ * Time Complexity: O(1)
453
+ * Space Complexity: O(1)
454
+ *
455
+ * The `_swapProperties` function swaps the properties (key, value, count, color) between two nodes
456
+ * in a binary search tree.
457
+ * @param {R | BSTNKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents the source node
458
+ * that will be swapped with the `destNode`. It can be either an instance of the `R` class or an
459
+ * instance of the `BSTNKeyOrNode<K, NODE>` class.
460
+ * @param {R | BSTNKeyOrNode<K, NODE>} destNode - The `destNode` parameter represents the destination
461
+ * node where the properties will be swapped with the source node.
441
462
  * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
442
- * If both `srcNode` and `destNode` are valid nodes, the method swaps their `key`, `value`, `count`,
443
- * and `color` properties. If the swapping is successful, the method returns the modified `destNode`.
444
- * If either `srcNode` or `destNode` is
463
+ * If either `srcNode` or `destNode` is undefined, it returns undefined.
445
464
  */
446
465
  protected override _swapProperties(
447
- srcNode: BSTNKeyOrNode<K, NODE>,
448
- destNode: BSTNKeyOrNode<K, NODE>
466
+ srcNode: R | BSTNKeyOrNode<K, NODE>,
467
+ destNode: R | BSTNKeyOrNode<K, NODE>
449
468
  ): NODE | undefined {
450
469
  srcNode = this.ensureNode(srcNode);
451
470
  destNode = this.ensureNode(destNode);
@@ -472,14 +491,22 @@ export class TreeMultiMap<
472
491
  }
473
492
 
474
493
  /**
494
+ * Time Complexity: O(1)
495
+ * Space Complexity: O(1)
496
+ */
497
+
498
+ /**
499
+ * Time Complexity: O(1)
500
+ * Space Complexity: O(1)
501
+ *
475
502
  * The function replaces an old node with a new node and updates the count property of the new node.
476
- * @param {NODE} oldNode - The `oldNode` parameter is of type `NODE` and represents the node that
477
- * needs to be replaced in the data structure.
478
- * @param {NODE} newNode - The `newNode` parameter is an object of type `NODE`.
503
+ * @param {NODE} oldNode - The `oldNode` parameter is the node that you want to replace in the data
504
+ * structure.
505
+ * @param {NODE} newNode - The `newNode` parameter is an instance of the `NODE` class.
479
506
  * @returns The method is returning the result of calling the `_replaceNode` method from the
480
- * superclass, after updating the `count` property of the `newNode` object.
507
+ * superclass, which is of type `NODE`.
481
508
  */
482
- protected _replaceNode(oldNode: NODE, newNode: NODE): NODE {
509
+ protected override _replaceNode(oldNode: NODE, newNode: NODE): NODE {
483
510
  newNode.count = oldNode.count + newNode.count;
484
511
  return super._replaceNode(oldNode, newNode);
485
512
  }
@@ -496,9 +496,9 @@ export abstract class AbstractGraph<
496
496
 
497
497
  /**
498
498
  * Dijkstra algorithm time: O(VE) space: O(VO + EO)
499
- * /
499
+ */
500
500
 
501
- /**
501
+ /**
502
502
  * Time Complexity: O(V^2 + E) - Quadratic time in the worst case (no heap optimization).
503
503
  * Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
504
504
  */
@@ -639,9 +639,9 @@ export abstract class AbstractGraph<
639
639
  * Dijkstra's algorithm is suitable for graphs with non-negative edge weights, whereas the Bellman-Ford algorithm and Floyd-Warshall algorithm can handle negative-weight edgeMap.
640
640
  * The time complexity of Dijkstra's algorithm and the Bellman-Ford algorithm depends on the size of the graph, while the time complexity of the Floyd-Warshall algorithm is O(VO^3), where VO is the number of nodes. For dense graphs, Floyd-Warshall might become slower.
641
641
  *
642
- * /
642
+ */
643
643
 
644
- /**
644
+ /**
645
645
  * Time Complexity: O((V + E) * log(V)) - Depends on the implementation (using a binary heap).
646
646
  * Space Complexity: O(V + E) - Depends on the implementation (using a binary heap).
647
647
  */
@@ -777,9 +777,9 @@ export abstract class AbstractGraph<
777
777
  * Time Complexity: O(V * E) - Quadratic time in the worst case (Bellman-Ford algorithm).
778
778
  * Space Complexity: O(V + E) - Depends on the implementation (Bellman-Ford algorithm).
779
779
  * one to rest pairs
780
- * /
780
+ */
781
781
 
782
- /**
782
+ /**
783
783
  * Time Complexity: O(V * E) - Quadratic time in the worst case (Bellman-Ford algorithm).
784
784
  * Space Complexity: O(V + E) - Depends on the implementation (Bellman-Ford algorithm).
785
785
  *
@@ -887,9 +887,9 @@ export abstract class AbstractGraph<
887
887
 
888
888
  /**
889
889
  * Dijkstra algorithm time: O(logVE) space: O(VO + EO)
890
- * /
890
+ */
891
891
 
892
- /**
892
+ /**
893
893
  * Dijkstra algorithm time: O(logVE) space: O(VO + EO)
894
894
  * Dijkstra's algorithm is used to find the shortest paths from a source node to all other nodes in a graph. Its basic idea is to repeatedly choose the node closest to the source node and update the distances of other nodes using this node as an intermediary. Dijkstra's algorithm requires that the edge weights in the graph are non-negative.
895
895
  */
@@ -907,9 +907,9 @@ export abstract class AbstractGraph<
907
907
  * Not support graph with negative weight cycle
908
908
  * all pairs
909
909
  * The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edgeMap, and it can simultaneously compute shortest paths between any two nodes.
910
- * /
910
+ */
911
911
 
912
- /**
912
+ /**
913
913
  * Time Complexity: O(V^3) - Cubic time (Floyd-Warshall algorithm).
914
914
  * Space Complexity: O(V^2) - Quadratic space (Floyd-Warshall algorithm).
915
915
  *
@@ -16,20 +16,21 @@ import { IterableEntryBase } from '../base';
16
16
  import { isWeakKey, rangeCheck } from '../../utils';
17
17
 
18
18
  /**
19
- * 1. Key-Value Pair Storage: HashMap stores key-value pairs. Each key maps to a value.
19
+ * 1. Key-Value Pair Storage: HashMap stores key-value pairs. Each key map to a value.
20
20
  * 2. Fast Lookup: It's used when you need to quickly find, insert, or delete entries based on a key.
21
- * 3. Unique Keys: Keys are unique. If you try to insert another entry with the same key, the old entry will be replaced by the new one.
21
+ * 3. Unique Keys: Keys are unique.
22
+ * If you try to insert another entry with the same key, the new one will replace the old entry.
22
23
  * 4. Unordered Collection: HashMap does not guarantee the order of entries, and the order may change over time.
23
24
  */
24
25
  export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K, V> {
25
26
  /**
26
27
  * The constructor function initializes a HashMap object with an optional initial collection and
27
28
  * options.
28
- * @param rawCollection - The `rawCollection` parameter is an iterable collection of elements of type
29
+ * @param entryOrRawElements - The `entryOrRawElements` parameter is an iterable collection of elements of a type
29
30
  * `T`. It is an optional parameter and its default value is an empty array `[]`.
30
31
  * @param [options] - The `options` parameter is an optional object that can contain two properties:
31
32
  */
32
- constructor(rawCollection: Iterable<R | [K, V]> = [], options?: HashMapOptions<K, V, R>) {
33
+ constructor(entryOrRawElements: Iterable<R | [K, V]> = [], options?: HashMapOptions<K, V, R>) {
33
34
  super();
34
35
  if (options) {
35
36
  const { hashFn, toEntryFn } = options;
@@ -40,8 +41,8 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
40
41
  this._toEntryFn = toEntryFn;
41
42
  }
42
43
  }
43
- if (rawCollection) {
44
- this.setMany(rawCollection);
44
+ if (entryOrRawElements) {
45
+ this.setMany(entryOrRawElements);
45
46
  }
46
47
  }
47
48
 
@@ -67,16 +68,7 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
67
68
  return this._objMap;
68
69
  }
69
70
 
70
- protected _toEntryFn: (rawElement: R) => [K, V] = (rawElement: R) => {
71
- if (this.isEntry(rawElement)) {
72
- // TODO, For performance optimization, it may be necessary to only inspect the first element traversed.
73
- return rawElement;
74
- } else {
75
- throw new Error(
76
- "If the provided rawCollection does not adhere to the [key, value] type format, the toEntryFn in the constructor's options parameter needs to specified."
77
- );
78
- }
79
- };
71
+ protected _toEntryFn?: (rawElement: R) => [K, V];
80
72
 
81
73
  /**
82
74
  * The function returns the value of the _toEntryFn property.
@@ -164,23 +156,24 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
164
156
  /**
165
157
  * The function `setMany` takes an iterable collection of objects, maps each object to a key-value
166
158
  * pair using a mapping function, and sets each key-value pair in the current object.
167
- * @param rawCollection - The `rawCollection` parameter is an iterable collection of elements of type
159
+ * @param entryOrRawElements - The `entryOrRawElements` parameter is an iterable collection of elements of a type
168
160
  * `T`.
169
161
  * @returns The `setMany` function is returning an array of booleans.
170
162
  */
171
- setMany(rawCollection: Iterable<R | [K, V]>): boolean[] {
163
+ setMany(entryOrRawElements: Iterable<R | [K, V]>): boolean[] {
172
164
  const results: boolean[] = [];
173
- for (const rawEle of rawCollection) {
174
- let key, value;
165
+ for (const rawEle of entryOrRawElements) {
166
+ let key: K | undefined, value: V | undefined;
175
167
  if (this.isEntry(rawEle)) {
176
168
  key = rawEle[0];
177
169
  value = rawEle[1];
178
- } else {
170
+ } else if (this.toEntryFn) {
179
171
  const item = this.toEntryFn(rawEle);
180
172
  key = item[0];
181
173
  value = item[1];
182
174
  }
183
- results.push(this.set(key, value));
175
+
176
+ if (key !== undefined && value !== undefined) results.push(this.set(key, value));
184
177
  }
185
178
  return results;
186
179
  }
@@ -383,14 +376,14 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
383
376
 
384
377
  /**
385
378
  * The constructor initializes a LinkedHashMap object with an optional raw collection and options.
386
- * @param rawCollection - The `rawCollection` parameter is an iterable collection of elements. It is
379
+ * @param entryOrRawElements - The `entryOrRawElements` parameter is an iterable collection of elements. It is
387
380
  * used to initialize the HashMapLinked instance with key-value pairs. Each element in the
388
- * `rawCollection` is converted to a key-value pair using the `toEntryFn` function (if provided) and
381
+ * `entryOrRawElements` is converted to a key-value pair using the `toEntryFn` function (if provided) and
389
382
  * then added to the HashMap
390
383
  * @param [options] - The `options` parameter is an optional object that can contain the following
391
384
  * properties:
392
385
  */
393
- constructor(rawCollection: Iterable<R> = [], options?: LinkedHashMapOptions<K, V, R>) {
386
+ constructor(entryOrRawElements: Iterable<R> = [], options?: LinkedHashMapOptions<K, V, R>) {
394
387
  super();
395
388
  this._sentinel = <HashMapLinkedNode<K, V>>{};
396
389
  this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
@@ -405,8 +398,8 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
405
398
  }
406
399
  }
407
400
 
408
- if (rawCollection) {
409
- for (const el of rawCollection) {
401
+ if (entryOrRawElements) {
402
+ for (const el of entryOrRawElements) {
410
403
  const [key, value] = this.toEntryFn(el);
411
404
  this.set(key, value);
412
405
  }
@@ -460,7 +453,7 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
460
453
  /**
461
454
  * The function returns the head node of a HashMapLinkedNode.
462
455
  * @returns The method `getHead()` is returning a `HashMapLinkedNode` object with key type `K` and
463
- * value type `V | undefined`.
456
+ * a value type `V | undefined`.
464
457
  */
465
458
  get head(): HashMapLinkedNode<K, V | undefined> {
466
459
  return this._head;
@@ -482,7 +475,7 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
482
475
  return rawElement;
483
476
  } else {
484
477
  throw new Error(
485
- "If the provided rawCollection does not adhere to the [key, value] type format, the toEntryFn in the constructor's options parameter needs to specified."
478
+ "If the provided entryOrRawElements does not adhere to the [key, value] type format, the toEntryFn in the constructor's options parameter needs to specified."
486
479
  );
487
480
  }
488
481
  };
@@ -590,7 +583,7 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
590
583
  node = this.objMap.get(hash);
591
584
 
592
585
  if (!node && isNewKey) {
593
- // Create new node
586
+ // Create a new node
594
587
  node = { key: <K>hash, value, prev: this.tail, next: this._sentinel };
595
588
  this.objMap.set(hash, node);
596
589
  } else if (node) {
@@ -630,13 +623,13 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
630
623
  * The function `setMany` takes an iterable collection, converts each element into a key-value pair
631
624
  * using a provided function, and sets each key-value pair in the current object, returning an array
632
625
  * of booleans indicating the success of each set operation.
633
- * @param rawCollection - The rawCollection parameter is an iterable collection of elements of type
626
+ * @param entryOrRawElements - The entryOrRawElements parameter is an iterable collection of elements of type
634
627
  * R.
635
628
  * @returns The `setMany` function returns an array of booleans.
636
629
  */
637
- setMany(rawCollection: Iterable<R>): boolean[] {
630
+ setMany(entryOrRawElements: Iterable<R>): boolean[] {
638
631
  const results: boolean[] = [];
639
- for (const rawEle of rawCollection) {
632
+ for (const rawEle of entryOrRawElements) {
640
633
  const [key, value] = this.toEntryFn(rawEle);
641
634
  results.push(this.set(key, value));
642
635
  }
@@ -692,9 +685,9 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
692
685
  /**
693
686
  * Time Complexity: O(n)
694
687
  * Space Complexity: O(1)
695
- * /
688
+ */
696
689
 
697
- /**
690
+ /**
698
691
  * Time Complexity: O(n)
699
692
  * Space Complexity: O(1)
700
693
  *
@@ -717,9 +710,9 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
717
710
  /**
718
711
  * Time Complexity: O(1)
719
712
  * Space Complexity: O(1)
720
- * /
713
+ */
721
714
 
722
- /**
715
+ /**
723
716
  * Time Complexity: O(1)
724
717
  * Space Complexity: O(1)
725
718
  *
@@ -764,9 +757,9 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
764
757
  /**
765
758
  * Time Complexity: O(n)
766
759
  * Space Complexity: O(1)
767
- * /
760
+ */
768
761
 
769
- /**
762
+ /**
770
763
  * Time Complexity: O(n)
771
764
  * Space Complexity: O(1)
772
765
  *
@@ -787,9 +780,9 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
787
780
  /**
788
781
  * Time Complexity: O(1)
789
782
  * Space Complexity: O(1)
790
- * /
783
+ */
791
784
 
792
- /**
785
+ /**
793
786
  * Time Complexity: O(1)
794
787
  * Space Complexity: O(1)
795
788
  *
@@ -814,9 +807,9 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
814
807
  /**
815
808
  * Time Complexity: O(1)
816
809
  * Space Complexity: O(1)
817
- * /
810
+ */
818
811
 
819
- /**
812
+ /**
820
813
  * Time Complexity: O(1)
821
814
  * Space Complexity: O(1)
822
815
  *
@@ -854,9 +847,9 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
854
847
  /**
855
848
  * Time Complexity: O(n)
856
849
  * Space Complexity: O(n)
857
- * /
850
+ */
858
851
 
859
- /**
852
+ /**
860
853
  * Time Complexity: O(n)
861
854
  * Space Complexity: O(n)
862
855
  *
@@ -886,9 +879,9 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
886
879
  /**
887
880
  * Time Complexity: O(n)
888
881
  * Space Complexity: O(n)
889
- * /
882
+ */
890
883
 
891
- /**
884
+ /**
892
885
  * Time Complexity: O(n)
893
886
  * Space Complexity: O(n)
894
887
  *
@@ -940,9 +933,9 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
940
933
  * Time Complexity: O(n)
941
934
  * Space Complexity: O(1)
942
935
  * where n is the number of entries in the LinkedHashMap.
943
- * /
936
+ */
944
937
 
945
- /**
938
+ /**
946
939
  * Time Complexity: O(n)
947
940
  * Space Complexity: O(1)
948
941
  * where n is the number of entries in the LinkedHashMap.