min-heap-typed 1.50.0 → 1.50.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/base/iterable-base.d.ts +114 -9
- package/dist/data-structures/base/iterable-base.js +143 -7
- package/dist/data-structures/binary-tree/avl-tree.d.ts +43 -46
- package/dist/data-structures/binary-tree/avl-tree.js +68 -71
- package/dist/data-structures/binary-tree/binary-tree.d.ts +244 -199
- package/dist/data-structures/binary-tree/binary-tree.js +484 -376
- package/dist/data-structures/binary-tree/bst.d.ts +54 -74
- package/dist/data-structures/binary-tree/bst.js +30 -71
- package/dist/data-structures/binary-tree/rb-tree.d.ts +78 -60
- package/dist/data-structures/binary-tree/rb-tree.js +84 -89
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +37 -56
- package/dist/data-structures/binary-tree/tree-multimap.js +64 -85
- package/dist/data-structures/graph/abstract-graph.d.ts +1 -0
- package/dist/data-structures/graph/abstract-graph.js +3 -0
- package/dist/data-structures/graph/directed-graph.d.ts +14 -0
- package/dist/data-structures/graph/directed-graph.js +26 -0
- package/dist/data-structures/graph/map-graph.d.ts +8 -0
- package/dist/data-structures/graph/map-graph.js +14 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +16 -0
- package/dist/data-structures/graph/undirected-graph.js +25 -0
- package/dist/data-structures/hash/hash-map.d.ts +121 -15
- package/dist/data-structures/hash/hash-map.js +160 -25
- package/dist/data-structures/heap/heap.d.ts +66 -6
- package/dist/data-structures/heap/heap.js +66 -6
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +67 -50
- package/dist/data-structures/linked-list/doubly-linked-list.js +70 -64
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +128 -103
- package/dist/data-structures/linked-list/singly-linked-list.js +130 -112
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +63 -36
- package/dist/data-structures/linked-list/skip-linked-list.js +63 -36
- package/dist/data-structures/matrix/matrix.d.ts +35 -4
- package/dist/data-structures/matrix/matrix.js +50 -11
- package/dist/data-structures/queue/deque.d.ts +49 -19
- package/dist/data-structures/queue/deque.js +101 -47
- package/dist/data-structures/queue/queue.d.ts +39 -5
- package/dist/data-structures/queue/queue.js +47 -5
- package/dist/data-structures/stack/stack.d.ts +16 -0
- package/dist/data-structures/stack/stack.js +22 -0
- package/dist/data-structures/trie/trie.d.ts +38 -1
- package/dist/data-structures/trie/trie.js +41 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +4 -3
- package/dist/types/utils/utils.d.ts +1 -0
- package/package.json +2 -2
- package/src/data-structures/base/iterable-base.ts +172 -19
- package/src/data-structures/binary-tree/avl-tree.ts +97 -97
- package/src/data-structures/binary-tree/binary-tree.ts +674 -671
- package/src/data-structures/binary-tree/bst.ts +89 -131
- package/src/data-structures/binary-tree/rb-tree.ts +127 -155
- package/src/data-structures/binary-tree/tree-multimap.ts +96 -112
- package/src/data-structures/graph/abstract-graph.ts +4 -0
- package/src/data-structures/graph/directed-graph.ts +30 -0
- package/src/data-structures/graph/map-graph.ts +15 -0
- package/src/data-structures/graph/undirected-graph.ts +28 -0
- package/src/data-structures/hash/hash-map.ts +175 -34
- package/src/data-structures/heap/heap.ts +66 -6
- package/src/data-structures/linked-list/doubly-linked-list.ts +72 -66
- package/src/data-structures/linked-list/singly-linked-list.ts +132 -114
- package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
- package/src/data-structures/matrix/matrix.ts +52 -12
- package/src/data-structures/queue/deque.ts +108 -49
- package/src/data-structures/queue/queue.ts +51 -5
- package/src/data-structures/stack/stack.ts +24 -0
- package/src/data-structures/trie/trie.ts +45 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +4 -3
- package/src/types/utils/utils.ts +2 -0
|
@@ -32,7 +32,7 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
32
32
|
* `T`. It is an optional parameter and its default value is an empty array `[]`.
|
|
33
33
|
* @param [options] - The `options` parameter is an optional object that can contain two properties:
|
|
34
34
|
*/
|
|
35
|
-
constructor(rawCollection: Iterable<R> = [], options?: HashMapOptions<K, V, R>) {
|
|
35
|
+
constructor(rawCollection: Iterable<R | [K, V]> = [], options?: HashMapOptions<K, V, R>) {
|
|
36
36
|
super();
|
|
37
37
|
if (options) {
|
|
38
38
|
const { hashFn, toEntryFn } = options;
|
|
@@ -59,24 +59,56 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
59
59
|
}
|
|
60
60
|
};
|
|
61
61
|
|
|
62
|
+
/**
|
|
63
|
+
* The function returns the value of the _toEntryFn property.
|
|
64
|
+
* @returns The function being returned is `this._toEntryFn`.
|
|
65
|
+
*/
|
|
62
66
|
get toEntryFn() {
|
|
63
67
|
return this._toEntryFn;
|
|
64
68
|
}
|
|
65
69
|
|
|
70
|
+
/**
|
|
71
|
+
* The hasFn function is a function that takes in an item and returns a boolean
|
|
72
|
+
* indicating whether the item is contained within the hash table.
|
|
73
|
+
*
|
|
74
|
+
* @return The hash function
|
|
75
|
+
*/
|
|
76
|
+
get hasFn() {
|
|
77
|
+
return this._hashFn;
|
|
78
|
+
}
|
|
79
|
+
|
|
66
80
|
protected _size = 0;
|
|
67
81
|
|
|
82
|
+
/**
|
|
83
|
+
* The function returns the size of an object.
|
|
84
|
+
* @returns The size of the object, which is a number.
|
|
85
|
+
*/
|
|
68
86
|
get size(): number {
|
|
69
87
|
return this._size;
|
|
70
88
|
}
|
|
71
89
|
|
|
90
|
+
/**
|
|
91
|
+
* The function checks if a given element is an array with exactly two elements.
|
|
92
|
+
* @param {any} rawElement - The `rawElement` parameter is of type `any`, which means it can be any
|
|
93
|
+
* data type.
|
|
94
|
+
* @returns a boolean value.
|
|
95
|
+
*/
|
|
72
96
|
isEntry(rawElement: any): rawElement is [K, V] {
|
|
73
97
|
return Array.isArray(rawElement) && rawElement.length === 2;
|
|
74
98
|
}
|
|
75
99
|
|
|
100
|
+
/**
|
|
101
|
+
* The function checks if the size of an object is equal to zero and returns a boolean value.
|
|
102
|
+
* @returns A boolean value indicating whether the size of the object is 0 or not.
|
|
103
|
+
*/
|
|
76
104
|
isEmpty(): boolean {
|
|
77
105
|
return this.size === 0;
|
|
78
106
|
}
|
|
79
107
|
|
|
108
|
+
/**
|
|
109
|
+
* The clear() function resets the state of an object by clearing its internal store, object map, and
|
|
110
|
+
* size.
|
|
111
|
+
*/
|
|
80
112
|
clear() {
|
|
81
113
|
this._store = {};
|
|
82
114
|
this._objMap.clear();
|
|
@@ -115,10 +147,18 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
115
147
|
* `T`.
|
|
116
148
|
* @returns The `setMany` function is returning an array of booleans.
|
|
117
149
|
*/
|
|
118
|
-
setMany(rawCollection: Iterable<R>): boolean[] {
|
|
150
|
+
setMany(rawCollection: Iterable<R | [K, V]>): boolean[] {
|
|
119
151
|
const results: boolean[] = [];
|
|
120
152
|
for (const rawEle of rawCollection) {
|
|
121
|
-
|
|
153
|
+
let key, value;
|
|
154
|
+
if (this.isEntry(rawEle)) {
|
|
155
|
+
key = rawEle[0];
|
|
156
|
+
value = rawEle[1];
|
|
157
|
+
} else {
|
|
158
|
+
const item = this.toEntryFn(rawEle);
|
|
159
|
+
key = item[0];
|
|
160
|
+
value = item[1];
|
|
161
|
+
}
|
|
122
162
|
results.push(this.set(key, value));
|
|
123
163
|
}
|
|
124
164
|
return results;
|
|
@@ -132,7 +172,7 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
132
172
|
* @returns The method `get(key: K)` returns a value of type `V` if the key exists in the `_objMap`
|
|
133
173
|
* or `_store`, otherwise it returns `undefined`.
|
|
134
174
|
*/
|
|
135
|
-
get(key: K): V | undefined {
|
|
175
|
+
override get(key: K): V | undefined {
|
|
136
176
|
if (this._isObjKey(key)) {
|
|
137
177
|
return this._objMap.get(key);
|
|
138
178
|
} else {
|
|
@@ -147,7 +187,7 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
147
187
|
* @param {K} key - The parameter "key" is of type K, which means it can be any type.
|
|
148
188
|
* @returns The `has` method is returning a boolean value.
|
|
149
189
|
*/
|
|
150
|
-
has(key: K): boolean {
|
|
190
|
+
override has(key: K): boolean {
|
|
151
191
|
if (this._isObjKey(key)) {
|
|
152
192
|
return this._objMap.has(key);
|
|
153
193
|
} else {
|
|
@@ -181,6 +221,17 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
181
221
|
}
|
|
182
222
|
}
|
|
183
223
|
|
|
224
|
+
/**
|
|
225
|
+
* The clone function creates a new HashMap with the same key-value pairs as
|
|
226
|
+
* this one. The clone function is useful for creating a copy of an existing
|
|
227
|
+
* HashMap, and then modifying that copy without affecting the original.
|
|
228
|
+
*
|
|
229
|
+
* @return A new hashmap with the same values as this one
|
|
230
|
+
*/
|
|
231
|
+
clone(): HashMap<K, V, R> {
|
|
232
|
+
return new HashMap<K, V, R>(this, { hashFn: this._hashFn, toEntryFn: this.toEntryFn });
|
|
233
|
+
}
|
|
234
|
+
|
|
184
235
|
/**
|
|
185
236
|
* Time Complexity: O(n)
|
|
186
237
|
* Space Complexity: O(n)
|
|
@@ -241,6 +292,14 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
241
292
|
return filteredMap;
|
|
242
293
|
}
|
|
243
294
|
|
|
295
|
+
/**
|
|
296
|
+
* The put function sets a value in a data structure using a specified key.
|
|
297
|
+
* @param {K} key - The key parameter is of type K, which represents the type of the key being passed
|
|
298
|
+
* to the function.
|
|
299
|
+
* @param {V} value - The value parameter represents the value that you want to associate with the
|
|
300
|
+
* specified key in the data structure.
|
|
301
|
+
* @returns The method is returning a boolean value.
|
|
302
|
+
*/
|
|
244
303
|
put(key: K, value: V): boolean {
|
|
245
304
|
return this.set(key, value);
|
|
246
305
|
}
|
|
@@ -288,33 +347,70 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
288
347
|
* 2. Based on Hash Table and Linked List: It combines the structures of a hash table and a linked list, using the hash table to ensure fast access, while maintaining the order of entries through the linked list.
|
|
289
348
|
* 3. Time Complexity: Similar to HashMap, LinkedHashMap offers constant-time performance for get and put operations in most cases.
|
|
290
349
|
*/
|
|
291
|
-
export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
350
|
+
export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K, V> {
|
|
292
351
|
protected _noObjMap: Record<string, HashMapLinkedNode<K, V | undefined>> = {};
|
|
293
352
|
protected _objMap = new WeakMap<object, HashMapLinkedNode<K, V | undefined>>();
|
|
294
353
|
protected _head: HashMapLinkedNode<K, V | undefined>;
|
|
295
354
|
protected _tail: HashMapLinkedNode<K, V | undefined>;
|
|
296
355
|
protected readonly _sentinel: HashMapLinkedNode<K, V | undefined>;
|
|
297
356
|
|
|
298
|
-
|
|
357
|
+
/**
|
|
358
|
+
* The constructor initializes a LinkedHashMap object with an optional raw collection and options.
|
|
359
|
+
* @param rawCollection - The `rawCollection` parameter is an iterable collection of elements. It is
|
|
360
|
+
* used to initialize the HashMapLinked instance with key-value pairs. Each element in the
|
|
361
|
+
* `rawCollection` is converted to a key-value pair using the `toEntryFn` function (if provided) and
|
|
362
|
+
* then added to the HashMap
|
|
363
|
+
* @param [options] - The `options` parameter is an optional object that can contain the following
|
|
364
|
+
* properties:
|
|
365
|
+
*/
|
|
366
|
+
constructor(rawCollection: Iterable<R> = [], options?: LinkedHashMapOptions<K, V, R>) {
|
|
299
367
|
super();
|
|
300
368
|
this._sentinel = <HashMapLinkedNode<K, V>>{};
|
|
301
369
|
this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
|
|
302
370
|
|
|
303
371
|
if (options) {
|
|
304
|
-
const { hashFn, objHashFn } = options;
|
|
372
|
+
const { hashFn, objHashFn, toEntryFn } = options;
|
|
305
373
|
if (hashFn) this._hashFn = hashFn;
|
|
306
374
|
if (objHashFn) this._objHashFn = objHashFn;
|
|
375
|
+
|
|
376
|
+
if (toEntryFn) {
|
|
377
|
+
this._toEntryFn = toEntryFn;
|
|
378
|
+
}
|
|
307
379
|
}
|
|
308
380
|
|
|
309
|
-
if (
|
|
310
|
-
for (const el of
|
|
311
|
-
this.
|
|
381
|
+
if (rawCollection) {
|
|
382
|
+
for (const el of rawCollection) {
|
|
383
|
+
const [key, value] = this.toEntryFn(el);
|
|
384
|
+
this.set(key, value);
|
|
312
385
|
}
|
|
313
386
|
}
|
|
314
387
|
}
|
|
315
388
|
|
|
389
|
+
protected _toEntryFn: (rawElement: R) => [K, V] = (rawElement: R) => {
|
|
390
|
+
if (this.isEntry(rawElement)) {
|
|
391
|
+
// TODO, For performance optimization, it may be necessary to only inspect the first element traversed.
|
|
392
|
+
return rawElement;
|
|
393
|
+
} else {
|
|
394
|
+
throw new Error(
|
|
395
|
+
"If the provided rawCollection does not adhere to the [key, value] type format, the toEntryFn in the constructor's options parameter needs to specified."
|
|
396
|
+
);
|
|
397
|
+
}
|
|
398
|
+
};
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* The function returns the value of the _toEntryFn property.
|
|
402
|
+
* @returns The function being returned is `this._toEntryFn`.
|
|
403
|
+
*/
|
|
404
|
+
get toEntryFn() {
|
|
405
|
+
return this._toEntryFn;
|
|
406
|
+
}
|
|
407
|
+
|
|
316
408
|
protected _size = 0;
|
|
317
409
|
|
|
410
|
+
/**
|
|
411
|
+
* The function returns the size of an object.
|
|
412
|
+
* @returns The size of the object.
|
|
413
|
+
*/
|
|
318
414
|
get size() {
|
|
319
415
|
return this._size;
|
|
320
416
|
}
|
|
@@ -425,7 +521,30 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
425
521
|
return true;
|
|
426
522
|
}
|
|
427
523
|
|
|
428
|
-
|
|
524
|
+
/**
|
|
525
|
+
* The function `setMany` takes an iterable collection, converts each element into a key-value pair
|
|
526
|
+
* using a provided function, and sets each key-value pair in the current object, returning an array
|
|
527
|
+
* of booleans indicating the success of each set operation.
|
|
528
|
+
* @param rawCollection - The rawCollection parameter is an iterable collection of elements of type
|
|
529
|
+
* R.
|
|
530
|
+
* @returns The `setMany` function returns an array of booleans.
|
|
531
|
+
*/
|
|
532
|
+
setMany(rawCollection: Iterable<R>): boolean[] {
|
|
533
|
+
const results: boolean[] = [];
|
|
534
|
+
for (const rawEle of rawCollection) {
|
|
535
|
+
const [key, value] = this.toEntryFn(rawEle);
|
|
536
|
+
results.push(this.set(key, value));
|
|
537
|
+
}
|
|
538
|
+
return results;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* The function checks if a given key exists in a map, using different logic depending on whether the
|
|
543
|
+
* key is a weak key or not.
|
|
544
|
+
* @param {K} key - The `key` parameter is the key that is being checked for existence in the map.
|
|
545
|
+
* @returns The method `has` is returning a boolean value.
|
|
546
|
+
*/
|
|
547
|
+
override has(key: K): boolean {
|
|
429
548
|
if (isWeakKey(key)) {
|
|
430
549
|
const hash = this._objHashFn(key);
|
|
431
550
|
return this._objMap.has(hash);
|
|
@@ -435,15 +554,6 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
435
554
|
}
|
|
436
555
|
}
|
|
437
556
|
|
|
438
|
-
setMany(entries: Iterable<[K, V]>): boolean[] {
|
|
439
|
-
const results: boolean[] = [];
|
|
440
|
-
for (const entry of entries) {
|
|
441
|
-
const [key, value] = entry;
|
|
442
|
-
results.push(this.set(key, value));
|
|
443
|
-
}
|
|
444
|
-
return results;
|
|
445
|
-
}
|
|
446
|
-
|
|
447
557
|
/**
|
|
448
558
|
* Time Complexity: O(1)
|
|
449
559
|
* Space Complexity: O(1)
|
|
@@ -457,7 +567,7 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
457
567
|
* property of the key. If the key is a string key, the value is retrieved from the `_noObjMap` object
|
|
458
568
|
* using the key itself. If the key is not found, `undefined` is
|
|
459
569
|
*/
|
|
460
|
-
get(key: K): V | undefined {
|
|
570
|
+
override get(key: K): V | undefined {
|
|
461
571
|
if (isWeakKey(key)) {
|
|
462
572
|
const hash = this._objHashFn(key);
|
|
463
573
|
const node = this._objMap.get(hash);
|
|
@@ -473,14 +583,14 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
473
583
|
* Time Complexity: O(n), where n is the index.
|
|
474
584
|
* Space Complexity: O(1)
|
|
475
585
|
*
|
|
476
|
-
* The function `
|
|
586
|
+
* The function `at` retrieves the key-value pair at a specified index in a linked list.
|
|
477
587
|
* @param {number} index - The index parameter is a number that represents the position of the
|
|
478
588
|
* element we want to retrieve from the data structure.
|
|
479
|
-
* @returns The method `
|
|
589
|
+
* @returns The method `at(index: number)` is returning an array containing the key-value pair at
|
|
480
590
|
* the specified index in the data structure. The key-value pair is represented as a tuple `[K, V]`,
|
|
481
591
|
* where `K` is the key and `V` is the value.
|
|
482
592
|
*/
|
|
483
|
-
|
|
593
|
+
at(index: number): V | undefined {
|
|
484
594
|
rangeCheck(index, 0, this._size - 1);
|
|
485
595
|
let node = this._head;
|
|
486
596
|
while (index--) {
|
|
@@ -532,7 +642,7 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
532
642
|
}
|
|
533
643
|
|
|
534
644
|
/**
|
|
535
|
-
* Time Complexity: O(n)
|
|
645
|
+
* Time Complexity: O(n)
|
|
536
646
|
* Space Complexity: O(1)
|
|
537
647
|
*
|
|
538
648
|
* The `deleteAt` function deletes a node at a specified index in a linked list.
|
|
@@ -561,6 +671,16 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
561
671
|
return this._size === 0;
|
|
562
672
|
}
|
|
563
673
|
|
|
674
|
+
/**
|
|
675
|
+
* The function checks if a given element is an array with exactly two elements.
|
|
676
|
+
* @param {any} rawElement - The `rawElement` parameter is of type `any`, which means it can be any
|
|
677
|
+
* data type.
|
|
678
|
+
* @returns a boolean value.
|
|
679
|
+
*/
|
|
680
|
+
isEntry(rawElement: any): rawElement is [K, V] {
|
|
681
|
+
return Array.isArray(rawElement) && rawElement.length === 2;
|
|
682
|
+
}
|
|
683
|
+
|
|
564
684
|
/**
|
|
565
685
|
* Time Complexity: O(1)
|
|
566
686
|
* Space Complexity: O(1)
|
|
@@ -573,6 +693,20 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
573
693
|
this._head = this._tail = this._sentinel.prev = this._sentinel.next = this._sentinel;
|
|
574
694
|
}
|
|
575
695
|
|
|
696
|
+
/**
|
|
697
|
+
* Time Complexity: O(n)
|
|
698
|
+
* Space Complexity: O(n)
|
|
699
|
+
*/
|
|
700
|
+
|
|
701
|
+
/**
|
|
702
|
+
* Time Complexity: O(n)
|
|
703
|
+
* Space Complexity: O(n)
|
|
704
|
+
*
|
|
705
|
+
* The `clone` function creates a new instance of a `LinkedHashMap` with the same key-value pairs as
|
|
706
|
+
* the original.
|
|
707
|
+
* @returns The `clone()` method is returning a new instance of `LinkedHashMap<K, V>` that is a clone
|
|
708
|
+
* of the original `LinkedHashMap` object.
|
|
709
|
+
*/
|
|
576
710
|
clone(): LinkedHashMap<K, V> {
|
|
577
711
|
const cloned = new LinkedHashMap<K, V>([], { hashFn: this._hashFn, objHashFn: this._objHashFn });
|
|
578
712
|
for (const entry of this) {
|
|
@@ -638,26 +772,33 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
638
772
|
}
|
|
639
773
|
|
|
640
774
|
/**
|
|
641
|
-
* Time Complexity: O(
|
|
642
|
-
* Space Complexity: O(
|
|
775
|
+
* Time Complexity: O(1)
|
|
776
|
+
* Space Complexity: O(1)
|
|
643
777
|
*/
|
|
644
778
|
|
|
779
|
+
/**
|
|
780
|
+
* Time Complexity: O(1)
|
|
781
|
+
* Space Complexity: O(1)
|
|
782
|
+
*
|
|
783
|
+
* The put function sets a value in a data structure using a specified key.
|
|
784
|
+
* @param {K} key - The key parameter is of type K, which represents the type of the key being passed
|
|
785
|
+
* to the function.
|
|
786
|
+
* @param {V} value - The value parameter represents the value that you want to associate with the
|
|
787
|
+
* specified key in the data structure.
|
|
788
|
+
* @returns The method is returning a boolean value.
|
|
789
|
+
*/
|
|
645
790
|
put(key: K, value: V): boolean {
|
|
646
791
|
return this.set(key, value);
|
|
647
792
|
}
|
|
648
793
|
|
|
649
|
-
/**
|
|
650
|
-
* Time Complexity: O(n)
|
|
651
|
-
* Space Complexity: O(n)
|
|
652
|
-
*/
|
|
653
|
-
|
|
654
794
|
protected _hashFn: (key: K) => string = (key: K) => String(key);
|
|
655
795
|
|
|
656
796
|
protected _objHashFn: (key: K) => object = (key: K) => <object>key;
|
|
657
797
|
|
|
658
798
|
/**
|
|
659
|
-
* Time Complexity: O(n)
|
|
799
|
+
* Time Complexity: O(n)
|
|
660
800
|
* Space Complexity: O(1)
|
|
801
|
+
* where n is the number of entries in the LinkedHashMap.
|
|
661
802
|
*
|
|
662
803
|
* The above function is an iterator that yields key-value pairs from a linked list.
|
|
663
804
|
*/
|
|
@@ -21,6 +21,16 @@ import { IterableElementBase } from '../base';
|
|
|
21
21
|
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
|
|
22
22
|
*/
|
|
23
23
|
export class Heap<E = any> extends IterableElementBase<E> {
|
|
24
|
+
/**
|
|
25
|
+
* The constructor initializes a heap data structure with optional elements and options.
|
|
26
|
+
* @param elements - The `elements` parameter is an iterable object that contains the initial
|
|
27
|
+
* elements to be added to the heap. It is an optional parameter and if not provided, the heap will
|
|
28
|
+
* be initialized as empty.
|
|
29
|
+
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
30
|
+
* configuration options for the heap. In this case, it is used to specify a custom comparator
|
|
31
|
+
* function for comparing elements in the heap. The comparator function is used to determine the
|
|
32
|
+
* order of elements in the heap.
|
|
33
|
+
*/
|
|
24
34
|
constructor(elements: Iterable<E> = [], options?: HeapOptions<E>) {
|
|
25
35
|
super();
|
|
26
36
|
|
|
@@ -45,12 +55,20 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
45
55
|
}
|
|
46
56
|
};
|
|
47
57
|
|
|
58
|
+
/**
|
|
59
|
+
* The function returns the value of the _comparator property.
|
|
60
|
+
* @returns The `_comparator` property is being returned.
|
|
61
|
+
*/
|
|
48
62
|
get comparator() {
|
|
49
63
|
return this._comparator;
|
|
50
64
|
}
|
|
51
65
|
|
|
52
66
|
protected _elements: E[] = [];
|
|
53
67
|
|
|
68
|
+
/**
|
|
69
|
+
* The function returns an array of elements.
|
|
70
|
+
* @returns The elements array is being returned.
|
|
71
|
+
*/
|
|
54
72
|
get elements(): E[] {
|
|
55
73
|
return this._elements;
|
|
56
74
|
}
|
|
@@ -81,12 +99,13 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
81
99
|
}
|
|
82
100
|
|
|
83
101
|
/**
|
|
84
|
-
* Time Complexity: O(log n)
|
|
102
|
+
* Time Complexity: O(log n)
|
|
85
103
|
* Space Complexity: O(1)
|
|
104
|
+
* where n is the number of elements in the heap.
|
|
86
105
|
*/
|
|
87
106
|
|
|
88
107
|
/**
|
|
89
|
-
* Time Complexity: O(log n)
|
|
108
|
+
* Time Complexity: O(log n)
|
|
90
109
|
* Space Complexity: O(1)
|
|
91
110
|
*
|
|
92
111
|
* Insert an element into the heap and maintain the heap properties.
|
|
@@ -98,12 +117,13 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
98
117
|
}
|
|
99
118
|
|
|
100
119
|
/**
|
|
101
|
-
* Time Complexity: O(log n)
|
|
120
|
+
* Time Complexity: O(log n)
|
|
102
121
|
* Space Complexity: O(1)
|
|
122
|
+
* where n is the number of elements in the heap.
|
|
103
123
|
*/
|
|
104
124
|
|
|
105
125
|
/**
|
|
106
|
-
* Time Complexity: O(log n)
|
|
126
|
+
* Time Complexity: O(log n)
|
|
107
127
|
* Space Complexity: O(1)
|
|
108
128
|
*
|
|
109
129
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
@@ -121,6 +141,9 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
121
141
|
}
|
|
122
142
|
|
|
123
143
|
/**
|
|
144
|
+
* Time Complexity: O(1)
|
|
145
|
+
* Space Complexity: O(1)
|
|
146
|
+
*
|
|
124
147
|
* Peek at the top element of the heap without removing it.
|
|
125
148
|
* @returns The top element or undefined if the heap is empty.
|
|
126
149
|
*/
|
|
@@ -391,6 +414,9 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
391
414
|
return mappedHeap;
|
|
392
415
|
}
|
|
393
416
|
|
|
417
|
+
/**
|
|
418
|
+
* The function `_getIterator` returns an iterable iterator for the elements in the class.
|
|
419
|
+
*/
|
|
394
420
|
protected* _getIterator(): IterableIterator<E> {
|
|
395
421
|
for (const element of this.elements) {
|
|
396
422
|
yield element;
|
|
@@ -458,6 +484,16 @@ export class FibonacciHeapNode<E> {
|
|
|
458
484
|
parent?: FibonacciHeapNode<E>;
|
|
459
485
|
marked: boolean;
|
|
460
486
|
|
|
487
|
+
/**
|
|
488
|
+
* The constructor function initializes an object with an element and a degree, and sets the marked
|
|
489
|
+
* property to false.
|
|
490
|
+
* @param {E} element - The "element" parameter represents the value or data that will be stored in
|
|
491
|
+
* the node of a data structure. It can be any type of data, such as a number, string, object, or
|
|
492
|
+
* even another data structure.
|
|
493
|
+
* @param [degree=0] - The degree parameter represents the degree of the element in a data structure
|
|
494
|
+
* called a Fibonacci heap. The degree of a node is the number of children it has. By default, the
|
|
495
|
+
* degree is set to 0 when a new node is created.
|
|
496
|
+
*/
|
|
461
497
|
constructor(element: E, degree = 0) {
|
|
462
498
|
this.element = element;
|
|
463
499
|
this.degree = degree;
|
|
@@ -466,6 +502,13 @@ export class FibonacciHeapNode<E> {
|
|
|
466
502
|
}
|
|
467
503
|
|
|
468
504
|
export class FibonacciHeap<E> {
|
|
505
|
+
/**
|
|
506
|
+
* The constructor function initializes a FibonacciHeap object with an optional comparator function.
|
|
507
|
+
* @param [comparator] - The `comparator` parameter is an optional argument that represents a
|
|
508
|
+
* function used to compare elements in the FibonacciHeap. If a comparator function is provided, it
|
|
509
|
+
* will be used to determine the order of elements in the heap. If no comparator function is
|
|
510
|
+
* provided, a default comparator function will be used.
|
|
511
|
+
*/
|
|
469
512
|
constructor(comparator?: Comparator<E>) {
|
|
470
513
|
this.clear();
|
|
471
514
|
this._comparator = comparator || this._defaultComparator;
|
|
@@ -477,24 +520,41 @@ export class FibonacciHeap<E> {
|
|
|
477
520
|
|
|
478
521
|
protected _root?: FibonacciHeapNode<E>;
|
|
479
522
|
|
|
523
|
+
/**
|
|
524
|
+
* The function returns the root node of a Fibonacci heap.
|
|
525
|
+
* @returns The method is returning either a FibonacciHeapNode object or undefined.
|
|
526
|
+
*/
|
|
480
527
|
get root(): FibonacciHeapNode<E> | undefined {
|
|
481
528
|
return this._root;
|
|
482
529
|
}
|
|
483
530
|
|
|
484
531
|
protected _size = 0;
|
|
485
532
|
|
|
533
|
+
/**
|
|
534
|
+
* The function returns the size of an object.
|
|
535
|
+
* @returns The size of the object, which is a number.
|
|
536
|
+
*/
|
|
486
537
|
get size(): number {
|
|
487
538
|
return this._size;
|
|
488
539
|
}
|
|
489
540
|
|
|
490
541
|
protected _min?: FibonacciHeapNode<E>;
|
|
491
542
|
|
|
543
|
+
/**
|
|
544
|
+
* The function returns the minimum node in a Fibonacci heap.
|
|
545
|
+
* @returns The method is returning the minimum node of the Fibonacci heap, which is of type
|
|
546
|
+
* `FibonacciHeapNode<E>`. If there is no minimum node, it will return `undefined`.
|
|
547
|
+
*/
|
|
492
548
|
get min(): FibonacciHeapNode<E> | undefined {
|
|
493
549
|
return this._min;
|
|
494
550
|
}
|
|
495
551
|
|
|
496
552
|
protected _comparator: Comparator<E>;
|
|
497
553
|
|
|
554
|
+
/**
|
|
555
|
+
* The function returns the comparator used for comparing elements.
|
|
556
|
+
* @returns The `_comparator` property of the object.
|
|
557
|
+
*/
|
|
498
558
|
get comparator(): Comparator<E> {
|
|
499
559
|
return this._comparator;
|
|
500
560
|
}
|
|
@@ -808,12 +868,12 @@ export class FibonacciHeap<E> {
|
|
|
808
868
|
}
|
|
809
869
|
|
|
810
870
|
/**
|
|
811
|
-
* Time Complexity: O(n log n)
|
|
871
|
+
* Time Complexity: O(n log n)
|
|
812
872
|
* Space Complexity: O(n)
|
|
813
873
|
*/
|
|
814
874
|
|
|
815
875
|
/**
|
|
816
|
-
* Time Complexity: O(n log n)
|
|
876
|
+
* Time Complexity: O(n log n)
|
|
817
877
|
* Space Complexity: O(n)
|
|
818
878
|
*
|
|
819
879
|
* Remove and return the top element (smallest or largest element) from the heap.
|