avl-tree-typed 1.51.8 → 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.
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +104 -66
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +119 -87
- package/dist/data-structures/binary-tree/avl-tree.d.ts +80 -60
- package/dist/data-structures/binary-tree/avl-tree.js +78 -59
- package/dist/data-structures/binary-tree/binary-tree.d.ts +316 -224
- package/dist/data-structures/binary-tree/binary-tree.js +471 -361
- package/dist/data-structures/binary-tree/bst.d.ts +198 -200
- package/dist/data-structures/binary-tree/bst.js +215 -249
- package/dist/data-structures/binary-tree/rb-tree.d.ts +71 -72
- package/dist/data-structures/binary-tree/rb-tree.js +107 -98
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +90 -73
- package/dist/data-structures/binary-tree/tree-multi-map.js +105 -93
- package/dist/data-structures/graph/abstract-graph.d.ts +10 -15
- package/dist/data-structures/graph/abstract-graph.js +10 -15
- package/dist/data-structures/hash/hash-map.d.ts +31 -38
- package/dist/data-structures/hash/hash-map.js +40 -55
- package/dist/data-structures/queue/deque.d.ts +2 -3
- package/dist/data-structures/queue/deque.js +2 -3
- package/dist/data-structures/trie/trie.d.ts +1 -1
- package/dist/data-structures/trie/trie.js +1 -1
- package/dist/interfaces/binary-tree.d.ts +6 -6
- package/dist/types/common.d.ts +1 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +5 -4
- package/dist/types/data-structures/binary-tree/bst.d.ts +4 -4
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -3
- package/dist/utils/utils.js +3 -5
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +142 -92
- package/src/data-structures/binary-tree/avl-tree.ts +94 -66
- package/src/data-structures/binary-tree/binary-tree.ts +530 -398
- package/src/data-structures/binary-tree/bst.ts +251 -270
- package/src/data-structures/binary-tree/rb-tree.ts +121 -100
- package/src/data-structures/binary-tree/tree-multi-map.ts +125 -99
- package/src/data-structures/graph/abstract-graph.ts +10 -10
- package/src/data-structures/hash/hash-map.ts +42 -49
- package/src/data-structures/queue/deque.ts +2 -2
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/data-structures/trie/trie.ts +2 -2
- package/src/interfaces/binary-tree.ts +8 -7
- package/src/types/common.ts +1 -2
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +2 -2
- package/src/types/data-structures/binary-tree/avl-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/binary-tree.ts +5 -4
- package/src/types/data-structures/binary-tree/bst.ts +4 -4
- package/src/types/data-structures/binary-tree/rb-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -3
- package/src/utils/utils.ts +3 -3
|
@@ -17,6 +17,7 @@ import type {
|
|
|
17
17
|
TreeMultiMapNodeNested,
|
|
18
18
|
TreeMultiMapOptions
|
|
19
19
|
} from '../../types';
|
|
20
|
+
import { BTNEntry } from '../../types';
|
|
20
21
|
import { IBinaryTree } from '../../interfaces';
|
|
21
22
|
import { RedBlackTree, RedBlackTreeNode } from './rb-tree';
|
|
22
23
|
|
|
@@ -65,23 +66,27 @@ export class TreeMultiMapNode<
|
|
|
65
66
|
export class TreeMultiMap<
|
|
66
67
|
K extends Comparable,
|
|
67
68
|
V = any,
|
|
69
|
+
R = BTNEntry<K, V>,
|
|
68
70
|
NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNode<K, V, TreeMultiMapNodeNested<K, V>>,
|
|
69
|
-
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>>
|
|
70
72
|
>
|
|
71
|
-
extends RedBlackTree<K, V, NODE, TREE>
|
|
72
|
-
implements IBinaryTree<K, V, NODE, TREE> {
|
|
73
|
+
extends RedBlackTree<K, V, R, NODE, TREE>
|
|
74
|
+
implements IBinaryTree<K, V, R, NODE, TREE> {
|
|
73
75
|
/**
|
|
74
|
-
* The constructor function initializes a
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
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.
|
|
81
83
|
*/
|
|
82
|
-
constructor(
|
|
84
|
+
constructor(
|
|
85
|
+
keysOrNodesOrEntriesOrRawElements: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [],
|
|
86
|
+
options?: TreeMultiMapOptions<K, V, R>
|
|
87
|
+
) {
|
|
83
88
|
super([], options);
|
|
84
|
-
if (
|
|
89
|
+
if (keysOrNodesOrEntriesOrRawElements) this.addMany(keysOrNodesOrEntriesOrRawElements);
|
|
85
90
|
}
|
|
86
91
|
|
|
87
92
|
protected _count = 0;
|
|
@@ -117,16 +122,15 @@ export class TreeMultiMap<
|
|
|
117
122
|
/**
|
|
118
123
|
* The function creates a new TreeMultiMapNode with the specified key, value, color, and count.
|
|
119
124
|
* @param {K} key - The key parameter represents the key of the node being created. It is of type K,
|
|
120
|
-
* which is a generic type representing the
|
|
121
|
-
* @param {V} [value] - The `value` parameter
|
|
122
|
-
* node. It is
|
|
123
|
-
* 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.
|
|
124
128
|
* @param {RBTNColor} [color=BLACK] - The color parameter is used to specify the color of the node in
|
|
125
129
|
* a Red-Black Tree. It can have two possible values: 'RED' or 'BLACK'. The default value is 'BLACK'.
|
|
126
130
|
* @param {number} [count] - The `count` parameter represents the number of occurrences of a key in
|
|
127
131
|
* the tree. It is an optional parameter and is used to keep track of the number of values associated
|
|
128
132
|
* with a key in the tree.
|
|
129
|
-
* @returns A new instance of the TreeMultiMapNode class
|
|
133
|
+
* @returns A new instance of the TreeMultiMapNode class, casted as NODE.
|
|
130
134
|
*/
|
|
131
135
|
override createNode(key: K, value?: V, color: RBTNColor = 'BLACK', count?: number): NODE {
|
|
132
136
|
return new TreeMultiMapNode(key, value, count, color) as NODE;
|
|
@@ -135,64 +139,67 @@ export class TreeMultiMap<
|
|
|
135
139
|
/**
|
|
136
140
|
* The function creates a new instance of a TreeMultiMap with the specified options and returns it.
|
|
137
141
|
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
138
|
-
* configuration options for creating the `TreeMultiMap`. It
|
|
139
|
-
*
|
|
142
|
+
* configuration options for creating the `TreeMultiMap`. It is of type `TreeMultiMapOptions<K, V,
|
|
143
|
+
* R>`.
|
|
140
144
|
* @returns a new instance of the `TreeMultiMap` class, with the provided options merged with the
|
|
141
|
-
* existing `iterationType`
|
|
145
|
+
* existing `iterationType` property. The returned value is casted as `TREE`.
|
|
142
146
|
*/
|
|
143
|
-
override createTree(options?: TreeMultiMapOptions<K>): TREE {
|
|
144
|
-
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>([], {
|
|
145
149
|
iterationType: this.iterationType,
|
|
146
150
|
...options
|
|
147
151
|
}) as TREE;
|
|
148
152
|
}
|
|
149
153
|
|
|
150
154
|
/**
|
|
151
|
-
* The function `
|
|
152
|
-
*
|
|
153
|
-
* @param
|
|
154
|
-
*
|
|
155
|
-
* @param {V} [value] - The `value` parameter is an optional value
|
|
156
|
-
*
|
|
157
|
-
*
|
|
158
|
-
*
|
|
159
|
-
*
|
|
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.
|
|
160
165
|
*/
|
|
161
|
-
override
|
|
162
|
-
|
|
166
|
+
override keyValueOrEntryOrRawElementToNode(
|
|
167
|
+
keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>,
|
|
163
168
|
value?: V,
|
|
164
169
|
count = 1
|
|
165
170
|
): NODE | undefined {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
if (key === undefined || key === null) {
|
|
174
|
-
return;
|
|
175
|
-
} else {
|
|
176
|
-
node = this.createNode(key, value, 'BLACK', count);
|
|
177
|
-
}
|
|
178
|
-
} else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
179
|
-
node = this.createNode(keyOrNodeOrEntry, value, 'BLACK', count);
|
|
180
|
-
} else {
|
|
181
|
-
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);
|
|
182
178
|
}
|
|
183
|
-
|
|
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;
|
|
184
190
|
}
|
|
185
191
|
|
|
186
192
|
/**
|
|
187
|
-
* The function
|
|
188
|
-
*
|
|
189
|
-
*
|
|
190
|
-
*
|
|
191
|
-
*
|
|
192
|
-
* 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.
|
|
193
198
|
*/
|
|
194
|
-
override isNode(
|
|
195
|
-
|
|
199
|
+
override isNode(
|
|
200
|
+
keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>
|
|
201
|
+
): keyOrNodeOrEntryOrRawElement is NODE {
|
|
202
|
+
return keyOrNodeOrEntryOrRawElement instanceof TreeMultiMapNode;
|
|
196
203
|
}
|
|
197
204
|
|
|
198
205
|
/**
|
|
@@ -204,17 +211,20 @@ export class TreeMultiMap<
|
|
|
204
211
|
* Time Complexity: O(log n)
|
|
205
212
|
* Space Complexity: O(1)
|
|
206
213
|
*
|
|
207
|
-
* The function overrides the add method
|
|
208
|
-
*
|
|
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:
|
|
209
218
|
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
210
|
-
* data structure.
|
|
219
|
+
* data structure. It is an optional parameter, so it can be omitted if not needed.
|
|
211
220
|
* @param [count=1] - The `count` parameter represents the number of times the key-value pair should
|
|
212
|
-
* be added to the data structure. By default, it is set to 1, meaning that
|
|
213
|
-
*
|
|
214
|
-
* @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.
|
|
215
225
|
*/
|
|
216
|
-
override add(
|
|
217
|
-
const newNode = this.
|
|
226
|
+
override add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V, count = 1): boolean {
|
|
227
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value, count);
|
|
218
228
|
const orgCount = newNode?.count || 0;
|
|
219
229
|
const isSuccessAdded = super.add(newNode);
|
|
220
230
|
|
|
@@ -235,20 +245,18 @@ export class TreeMultiMap<
|
|
|
235
245
|
* Time Complexity: O(log n)
|
|
236
246
|
* Space Complexity: O(1)
|
|
237
247
|
*
|
|
238
|
-
* The `delete`
|
|
239
|
-
*
|
|
240
|
-
*
|
|
241
|
-
*
|
|
242
|
-
*
|
|
243
|
-
*
|
|
244
|
-
*
|
|
245
|
-
*
|
|
246
|
-
*
|
|
247
|
-
*
|
|
248
|
-
*
|
|
249
|
-
*
|
|
250
|
-
* and the node will be deleted regardless of its count. If set to false (default), the count of the
|
|
251
|
-
* 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.
|
|
252
260
|
* @returns an array of BinaryTreeDeleteResult<NODE> objects.
|
|
253
261
|
*/
|
|
254
262
|
override delete<C extends BTNCallback<NODE>>(
|
|
@@ -372,10 +380,12 @@ export class TreeMultiMap<
|
|
|
372
380
|
*
|
|
373
381
|
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
|
|
374
382
|
* tree using either a recursive or iterative approach.
|
|
375
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that
|
|
376
|
-
* type of iteration to use when building the balanced binary search tree. It
|
|
377
|
-
*
|
|
378
|
-
*
|
|
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.
|
|
379
389
|
*/
|
|
380
390
|
override perfectlyBalance(iterationType: IterationType = this.iterationType): boolean {
|
|
381
391
|
const sorted = this.dfs(node => node, 'IN'),
|
|
@@ -434,19 +444,27 @@ export class TreeMultiMap<
|
|
|
434
444
|
}
|
|
435
445
|
|
|
436
446
|
/**
|
|
437
|
-
*
|
|
438
|
-
*
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
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.
|
|
442
462
|
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
443
|
-
* If
|
|
444
|
-
* and `color` properties. If the swapping is successful, the method returns the modified `destNode`.
|
|
445
|
-
* If either `srcNode` or `destNode` is
|
|
463
|
+
* If either `srcNode` or `destNode` is undefined, it returns undefined.
|
|
446
464
|
*/
|
|
447
465
|
protected override _swapProperties(
|
|
448
|
-
srcNode: BSTNKeyOrNode<K, NODE>,
|
|
449
|
-
destNode: BSTNKeyOrNode<K, NODE>
|
|
466
|
+
srcNode: R | BSTNKeyOrNode<K, NODE>,
|
|
467
|
+
destNode: R | BSTNKeyOrNode<K, NODE>
|
|
450
468
|
): NODE | undefined {
|
|
451
469
|
srcNode = this.ensureNode(srcNode);
|
|
452
470
|
destNode = this.ensureNode(destNode);
|
|
@@ -473,14 +491,22 @@ export class TreeMultiMap<
|
|
|
473
491
|
}
|
|
474
492
|
|
|
475
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
|
+
*
|
|
476
502
|
* The function replaces an old node with a new node and updates the count property of the new node.
|
|
477
|
-
* @param {NODE} oldNode - The `oldNode` parameter is
|
|
478
|
-
*
|
|
479
|
-
* @param {NODE} newNode - The `newNode` parameter is an
|
|
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.
|
|
480
506
|
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
481
|
-
* superclass,
|
|
507
|
+
* superclass, which is of type `NODE`.
|
|
482
508
|
*/
|
|
483
|
-
protected _replaceNode(oldNode: NODE, newNode: NODE): NODE {
|
|
509
|
+
protected override _replaceNode(oldNode: NODE, newNode: NODE): NODE {
|
|
484
510
|
newNode.count = oldNode.count + newNode.count;
|
|
485
511
|
return super._replaceNode(oldNode, newNode);
|
|
486
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
|
|
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.
|
|
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
|
|
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(
|
|
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 (
|
|
44
|
-
this.setMany(
|
|
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
|
|
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
|
|
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(
|
|
163
|
+
setMany(entryOrRawElements: Iterable<R | [K, V]>): boolean[] {
|
|
172
164
|
const results: boolean[] = [];
|
|
173
|
-
for (const rawEle of
|
|
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
|
-
|
|
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
|
|
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
|
-
* `
|
|
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(
|
|
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 (
|
|
409
|
-
for (const el of
|
|
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
|
|
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
|
|
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(
|
|
630
|
+
setMany(entryOrRawElements: Iterable<R>): boolean[] {
|
|
638
631
|
const results: boolean[] = [];
|
|
639
|
-
for (const rawEle of
|
|
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.
|