undirected-graph-typed 1.51.9 → 1.52.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/index.d.ts +2 -1
- package/dist/data-structures/base/index.js +2 -1
- package/dist/data-structures/base/iterable-element-base.d.ts +171 -0
- package/dist/data-structures/base/iterable-element-base.js +225 -0
- package/dist/data-structures/base/{iterable-base.d.ts → iterable-entry-base.d.ts} +4 -147
- package/dist/data-structures/base/{iterable-base.js → iterable-entry-base.js} +12 -189
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +13 -13
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +6 -6
- package/dist/data-structures/binary-tree/avl-tree.d.ts +13 -13
- package/dist/data-structures/binary-tree/avl-tree.js +6 -6
- package/dist/data-structures/binary-tree/binary-tree.d.ts +99 -99
- package/dist/data-structures/binary-tree/binary-tree.js +56 -54
- package/dist/data-structures/binary-tree/bst.d.ts +37 -45
- package/dist/data-structures/binary-tree/bst.js +19 -27
- package/dist/data-structures/binary-tree/rb-tree.d.ts +10 -10
- package/dist/data-structures/binary-tree/rb-tree.js +6 -6
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +12 -12
- package/dist/data-structures/binary-tree/tree-multi-map.js +5 -5
- package/dist/data-structures/graph/directed-graph.js +2 -1
- package/dist/data-structures/hash/hash-map.d.ts +2 -2
- package/dist/data-structures/heap/heap.d.ts +43 -114
- package/dist/data-structures/heap/heap.js +59 -127
- package/dist/data-structures/heap/max-heap.d.ts +50 -4
- package/dist/data-structures/heap/max-heap.js +76 -10
- package/dist/data-structures/heap/min-heap.d.ts +51 -5
- package/dist/data-structures/heap/min-heap.js +68 -11
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +22 -28
- package/dist/data-structures/linked-list/doubly-linked-list.js +26 -28
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +22 -25
- package/dist/data-structures/linked-list/singly-linked-list.js +29 -26
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +50 -4
- package/dist/data-structures/priority-queue/max-priority-queue.js +79 -10
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +51 -5
- package/dist/data-structures/priority-queue/min-priority-queue.js +71 -11
- package/dist/data-structures/priority-queue/priority-queue.d.ts +50 -4
- package/dist/data-structures/priority-queue/priority-queue.js +70 -1
- package/dist/data-structures/queue/deque.d.ts +27 -18
- package/dist/data-structures/queue/deque.js +43 -21
- package/dist/data-structures/queue/queue.d.ts +26 -29
- package/dist/data-structures/queue/queue.js +47 -38
- package/dist/data-structures/stack/stack.d.ts +17 -22
- package/dist/data-structures/stack/stack.js +25 -24
- package/dist/data-structures/trie/trie.d.ts +18 -13
- package/dist/data-structures/trie/trie.js +26 -15
- package/dist/interfaces/binary-tree.d.ts +4 -4
- package/dist/types/common.d.ts +1 -22
- package/dist/types/data-structures/base/base.d.ts +5 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -3
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +20 -4
- package/dist/types/data-structures/binary-tree/bst.d.ts +5 -3
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +2 -3
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -3
- package/dist/types/data-structures/heap/heap.d.ts +3 -2
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +2 -1
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/types/data-structures/queue/deque.d.ts +4 -2
- package/dist/types/data-structures/queue/queue.d.ts +4 -1
- package/dist/types/data-structures/stack/stack.d.ts +2 -1
- package/dist/types/data-structures/trie/trie.d.ts +3 -2
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +2 -1
- package/src/data-structures/base/iterable-element-base.ts +250 -0
- package/src/data-structures/base/{iterable-base.ts → iterable-entry-base.ts} +26 -217
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +26 -26
- package/src/data-structures/binary-tree/avl-tree.ts +21 -21
- package/src/data-structures/binary-tree/binary-tree.ts +166 -161
- package/src/data-structures/binary-tree/bst.ts +61 -68
- package/src/data-structures/binary-tree/rb-tree.ts +19 -19
- package/src/data-structures/binary-tree/tree-multi-map.ts +19 -19
- package/src/data-structures/graph/abstract-graph.ts +15 -14
- package/src/data-structures/graph/directed-graph.ts +9 -7
- package/src/data-structures/graph/undirected-graph.ts +7 -6
- package/src/data-structures/hash/hash-map.ts +8 -8
- package/src/data-structures/heap/heap.ts +72 -153
- package/src/data-structures/heap/max-heap.ts +88 -13
- package/src/data-structures/heap/min-heap.ts +78 -15
- package/src/data-structures/linked-list/doubly-linked-list.ts +33 -33
- package/src/data-structures/linked-list/singly-linked-list.ts +38 -30
- package/src/data-structures/priority-queue/max-priority-queue.ts +94 -13
- package/src/data-structures/priority-queue/min-priority-queue.ts +84 -15
- package/src/data-structures/priority-queue/priority-queue.ts +81 -4
- package/src/data-structures/queue/deque.ts +53 -28
- package/src/data-structures/queue/queue.ts +61 -44
- package/src/data-structures/stack/stack.ts +32 -27
- package/src/data-structures/trie/trie.ts +34 -19
- package/src/interfaces/binary-tree.ts +4 -5
- package/src/types/common.ts +2 -24
- package/src/types/data-structures/base/base.ts +14 -6
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +2 -3
- package/src/types/data-structures/binary-tree/avl-tree.ts +2 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +24 -5
- package/src/types/data-structures/binary-tree/bst.ts +9 -3
- package/src/types/data-structures/binary-tree/rb-tree.ts +2 -3
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +2 -3
- package/src/types/data-structures/graph/abstract-graph.ts +8 -8
- package/src/types/data-structures/heap/heap.ts +4 -1
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +3 -1
- package/src/types/data-structures/linked-list/singly-linked-list.ts +3 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/types/data-structures/queue/deque.ts +6 -1
- package/src/types/data-structures/queue/queue.ts +5 -1
- package/src/types/data-structures/stack/stack.ts +3 -1
- package/src/types/data-structures/trie/trie.ts +3 -1
- package/src/types/utils/utils.ts +4 -4
|
@@ -1,6 +1,14 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { EntryCallback, ReduceEntryCallback } from '../../types';
|
|
2
2
|
|
|
3
3
|
export abstract class IterableEntryBase<K = any, V = any> {
|
|
4
|
+
// protected constructor(options?: IterableEntryBaseOptions<K, V, R>) {
|
|
5
|
+
// if (options) {
|
|
6
|
+
// const { toEntryFn } = options;
|
|
7
|
+
// if (typeof toEntryFn === 'function') this._toEntryFn = toEntryFn
|
|
8
|
+
// else throw new TypeError('toEntryFn must be a function type');
|
|
9
|
+
// }
|
|
10
|
+
// }
|
|
11
|
+
|
|
4
12
|
/**
|
|
5
13
|
* Time Complexity: O(n)
|
|
6
14
|
* Space Complexity: O(1)
|
|
@@ -8,6 +16,16 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
8
16
|
|
|
9
17
|
abstract get size(): number;
|
|
10
18
|
|
|
19
|
+
// protected _toEntryFn?: (rawElement: R) => BTNEntry<K, V>;
|
|
20
|
+
//
|
|
21
|
+
// /**
|
|
22
|
+
// * The function returns the value of the _toEntryFn property.
|
|
23
|
+
// * @returns The function being returned is `this._toEntryFn`.
|
|
24
|
+
// */
|
|
25
|
+
// get toEntryFn() {
|
|
26
|
+
// return this._toEntryFn;
|
|
27
|
+
// }
|
|
28
|
+
|
|
11
29
|
/**
|
|
12
30
|
* Time Complexity: O(n)
|
|
13
31
|
* Space Complexity: O(1)
|
|
@@ -17,7 +35,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
17
35
|
* allows the function to accept any number of arguments as an array. In this case, the `args`
|
|
18
36
|
* parameter is used to pass any additional arguments to the `_getIterator` method.
|
|
19
37
|
*/
|
|
20
|
-
*
|
|
38
|
+
*[Symbol.iterator](...args: any[]): IterableIterator<[K, V]> {
|
|
21
39
|
yield* this._getIterator(...args);
|
|
22
40
|
}
|
|
23
41
|
|
|
@@ -32,7 +50,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
32
50
|
* The function returns an iterator that yields key-value pairs from the object, where the value can
|
|
33
51
|
* be undefined.
|
|
34
52
|
*/
|
|
35
|
-
*
|
|
53
|
+
*entries(): IterableIterator<[K, V | undefined]> {
|
|
36
54
|
for (const item of this) {
|
|
37
55
|
yield item;
|
|
38
56
|
}
|
|
@@ -48,7 +66,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
48
66
|
*
|
|
49
67
|
* The function returns an iterator that yields the keys of a data structure.
|
|
50
68
|
*/
|
|
51
|
-
*
|
|
69
|
+
*keys(): IterableIterator<K> {
|
|
52
70
|
for (const item of this) {
|
|
53
71
|
yield item[0];
|
|
54
72
|
}
|
|
@@ -64,7 +82,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
64
82
|
*
|
|
65
83
|
* The function returns an iterator that yields the values of a collection.
|
|
66
84
|
*/
|
|
67
|
-
*
|
|
85
|
+
*values(): IterableIterator<V> {
|
|
68
86
|
for (const item of this) {
|
|
69
87
|
yield item[1];
|
|
70
88
|
}
|
|
@@ -285,221 +303,12 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
285
303
|
* Time Complexity: O(n)
|
|
286
304
|
* Space Complexity: O(n)
|
|
287
305
|
*/
|
|
288
|
-
print(): void {
|
|
289
|
-
console.log([...this]);
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
abstract isEmpty(): boolean;
|
|
293
|
-
|
|
294
|
-
abstract clear(): void;
|
|
295
|
-
|
|
296
|
-
abstract clone(): any;
|
|
297
|
-
|
|
298
|
-
abstract map(...args: any[]): any;
|
|
299
|
-
|
|
300
|
-
abstract filter(...args: any[]): any;
|
|
301
|
-
|
|
302
|
-
protected abstract _getIterator(...args: any[]): IterableIterator<[K, V]>;
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
export abstract class IterableElementBase<E = any, C = any> {
|
|
306
|
-
abstract get size(): number;
|
|
307
|
-
|
|
308
|
-
/**
|
|
309
|
-
* Time Complexity: O(n)
|
|
310
|
-
* Space Complexity: O(1)
|
|
311
|
-
*/
|
|
312
|
-
/**
|
|
313
|
-
* Time Complexity: O(n)
|
|
314
|
-
* Space Complexity: O(1)
|
|
315
|
-
*
|
|
316
|
-
* The function is an implementation of the Symbol.iterator method that returns an IterableIterator.
|
|
317
|
-
* @param {any[]} args - The `args` parameter in the code snippet represents a rest parameter. It
|
|
318
|
-
* allows the function to accept any number of arguments as an array. In this case, the `args`
|
|
319
|
-
* parameter is used to pass any number of arguments to the `_getIterator` method.
|
|
320
|
-
*/
|
|
321
|
-
* [Symbol.iterator](...args: any[]): IterableIterator<E> {
|
|
322
|
-
yield* this._getIterator(...args);
|
|
323
|
-
}
|
|
324
306
|
|
|
325
307
|
/**
|
|
326
308
|
* Time Complexity: O(n)
|
|
327
309
|
* Space Complexity: O(n)
|
|
328
|
-
*/
|
|
329
|
-
/**
|
|
330
|
-
* Time Complexity: O(n)
|
|
331
|
-
* Space Complexity: O(n)
|
|
332
|
-
*
|
|
333
|
-
* The function returns an iterator that yields all the values in the object.
|
|
334
|
-
*/
|
|
335
|
-
* values(): IterableIterator<E> {
|
|
336
|
-
for (const item of this) {
|
|
337
|
-
yield item;
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
/**
|
|
342
|
-
* Time Complexity: O(n)
|
|
343
|
-
* Space Complexity: O(1)
|
|
344
|
-
*/
|
|
345
|
-
/**
|
|
346
|
-
* Time Complexity: O(n)
|
|
347
|
-
* Space Complexity: O(1)
|
|
348
|
-
*
|
|
349
|
-
* The `every` function checks if every element in the array satisfies a given predicate.
|
|
350
|
-
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
351
|
-
* the current element being processed, its index, and the array it belongs to. It should return a
|
|
352
|
-
* boolean value indicating whether the element satisfies a certain condition or not.
|
|
353
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
354
|
-
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
355
|
-
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
356
|
-
* @returns The `every` method is returning a boolean value. It returns `true` if every element in
|
|
357
|
-
* the array satisfies the provided predicate function, and `false` otherwise.
|
|
358
|
-
*/
|
|
359
|
-
every(predicate: ElementCallback<E, boolean>, thisArg?: any): boolean {
|
|
360
|
-
let index = 0;
|
|
361
|
-
for (const item of this) {
|
|
362
|
-
if (!predicate.call(thisArg, item, index++, this)) {
|
|
363
|
-
return false;
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
return true;
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
/**
|
|
370
|
-
* Time Complexity: O(n)
|
|
371
|
-
* Space Complexity: O(1)
|
|
372
|
-
*/
|
|
373
|
-
|
|
374
|
-
/**
|
|
375
|
-
* Time Complexity: O(n)
|
|
376
|
-
* Space Complexity: O(1)
|
|
377
|
-
*/
|
|
378
|
-
/**
|
|
379
|
-
* Time Complexity: O(n)
|
|
380
|
-
* Space Complexity: O(1)
|
|
381
|
-
*
|
|
382
|
-
* The "some" function checks if at least one element in a collection satisfies a given predicate.
|
|
383
|
-
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
384
|
-
* `value`, `index`, and `array`. It should return a boolean value indicating whether the current
|
|
385
|
-
* element satisfies the condition.
|
|
386
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
387
|
-
* to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
|
|
388
|
-
* it will be passed as the `this` value to the `predicate` function. If `thisArg
|
|
389
|
-
* @returns a boolean value. It returns true if the predicate function returns true for any element
|
|
390
|
-
* in the collection, and false otherwise.
|
|
391
|
-
*/
|
|
392
|
-
some(predicate: ElementCallback<E, boolean>, thisArg?: any): boolean {
|
|
393
|
-
let index = 0;
|
|
394
|
-
for (const item of this) {
|
|
395
|
-
if (predicate.call(thisArg, item, index++, this)) {
|
|
396
|
-
return true;
|
|
397
|
-
}
|
|
398
|
-
}
|
|
399
|
-
return false;
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
/**
|
|
403
|
-
* Time Complexity: O(n)
|
|
404
|
-
* Space Complexity: O(1)
|
|
405
|
-
*/
|
|
406
|
-
|
|
407
|
-
/**
|
|
408
|
-
* Time Complexity: O(n)
|
|
409
|
-
* Space Complexity: O(1)
|
|
410
|
-
*
|
|
411
|
-
* The `forEach` function iterates over each element in an array-like object and calls a callback
|
|
412
|
-
* function for each element.
|
|
413
|
-
* @param callbackfn - The callbackfn parameter is a function that will be called for each element in
|
|
414
|
-
* the array. It takes three arguments: the current element being processed, the index of the current
|
|
415
|
-
* element, and the array that forEach was called upon.
|
|
416
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
417
|
-
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
|
|
418
|
-
* be passed as the `this` value to the `callbackfn` function. If `thisArg
|
|
419
|
-
*/
|
|
420
|
-
forEach(callbackfn: ElementCallback<E, void>, thisArg?: any): void {
|
|
421
|
-
let index = 0;
|
|
422
|
-
for (const item of this) {
|
|
423
|
-
callbackfn.call(thisArg, item, index++, this);
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
/**
|
|
428
|
-
* Time Complexity: O(n)
|
|
429
|
-
* Space Complexity: O(1)
|
|
430
|
-
*/
|
|
431
|
-
|
|
432
|
-
/**
|
|
433
|
-
* Time Complexity: O(n)
|
|
434
|
-
* Space Complexity: O(1)
|
|
435
|
-
*
|
|
436
|
-
* The `find` function iterates over the elements of an array-like object and returns the first
|
|
437
|
-
* element that satisfies the provided callback function.
|
|
438
|
-
* @param callbackfn - The callbackfn parameter is a function that will be called for each element in
|
|
439
|
-
* the array. It takes three arguments: the current element being processed, the index of the current
|
|
440
|
-
* element, and the array itself. The function should return a boolean value indicating whether the
|
|
441
|
-
* current element matches the desired condition.
|
|
442
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
443
|
-
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
|
|
444
|
-
* be passed as the `this` value to the `callbackfn` function. If `thisArg
|
|
445
|
-
* @returns The `find` method returns the first element in the array that satisfies the provided
|
|
446
|
-
* callback function. If no element satisfies the callback function, `undefined` is returned.
|
|
447
|
-
*/
|
|
448
|
-
find(callbackfn: ElementCallback<E, boolean>, thisArg?: any): E | undefined {
|
|
449
|
-
let index = 0;
|
|
450
|
-
for (const item of this) {
|
|
451
|
-
if (callbackfn.call(thisArg, item, index++, this)) return item;
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
return;
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
/**
|
|
458
|
-
* Time Complexity: O(n)
|
|
459
|
-
* Space Complexity: O(1)
|
|
460
|
-
*
|
|
461
|
-
* The function checks if a given element exists in a collection.
|
|
462
|
-
* @param {E} element - The parameter "element" is of type E, which means it can be any type. It
|
|
463
|
-
* represents the element that we want to check for existence in the collection.
|
|
464
|
-
* @returns a boolean value. It returns true if the element is found in the collection, and false
|
|
465
|
-
* otherwise.
|
|
466
|
-
*/
|
|
467
|
-
has(element: E): boolean {
|
|
468
|
-
for (const ele of this) {
|
|
469
|
-
if (ele === element) return true;
|
|
470
|
-
}
|
|
471
|
-
return false;
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
/**
|
|
475
|
-
* Time Complexity: O(n)
|
|
476
|
-
* Space Complexity: O(1)
|
|
477
|
-
*/
|
|
478
|
-
/**
|
|
479
|
-
* Time Complexity: O(n)
|
|
480
|
-
* Space Complexity: O(1)
|
|
481
310
|
*
|
|
482
|
-
* The
|
|
483
|
-
* function to reduce them into a single value.
|
|
484
|
-
* @param callbackfn - The callbackfn parameter is a function that will be called for each element in
|
|
485
|
-
* the array. It takes four arguments:
|
|
486
|
-
* @param {U} initialValue - The initialValue parameter is the initial value of the accumulator. It
|
|
487
|
-
* is the value that the accumulator starts with before the reduction operation begins.
|
|
488
|
-
* @returns The `reduce` method is returning the final value of the accumulator after iterating over
|
|
489
|
-
* all the elements in the array and applying the callback function to each element.
|
|
490
|
-
*/
|
|
491
|
-
reduce<U>(callbackfn: ReduceElementCallback<E, U>, initialValue: U): U {
|
|
492
|
-
let accumulator = initialValue;
|
|
493
|
-
let index = 0;
|
|
494
|
-
for (const item of this) {
|
|
495
|
-
accumulator = callbackfn(accumulator, item as E, index++, this);
|
|
496
|
-
}
|
|
497
|
-
return accumulator;
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
/**
|
|
501
|
-
* Time Complexity: O(n)
|
|
502
|
-
* Space Complexity: O(n)
|
|
311
|
+
* The print function logs the elements of an array to the console.
|
|
503
312
|
*/
|
|
504
313
|
print(): void {
|
|
505
314
|
console.log([...this]);
|
|
@@ -509,11 +318,11 @@ export abstract class IterableElementBase<E = any, C = any> {
|
|
|
509
318
|
|
|
510
319
|
abstract clear(): void;
|
|
511
320
|
|
|
512
|
-
abstract clone():
|
|
321
|
+
abstract clone(): any;
|
|
513
322
|
|
|
514
323
|
abstract map(...args: any[]): any;
|
|
515
324
|
|
|
516
325
|
abstract filter(...args: any[]): any;
|
|
517
326
|
|
|
518
|
-
protected abstract _getIterator(...args: any[]): IterableIterator<
|
|
327
|
+
protected abstract _getIterator(...args: any[]): IterableIterator<[K, V]>;
|
|
519
328
|
}
|
|
@@ -12,16 +12,15 @@ import type {
|
|
|
12
12
|
BinaryTreeDeleteResult,
|
|
13
13
|
BSTNKeyOrNode,
|
|
14
14
|
BTNCallback,
|
|
15
|
-
|
|
16
|
-
IterationType
|
|
17
|
-
KeyOrNodeOrEntry
|
|
15
|
+
BTNKeyOrNodeOrEntry,
|
|
16
|
+
IterationType
|
|
18
17
|
} from '../../types';
|
|
19
18
|
import { BTNEntry } from '../../types';
|
|
20
19
|
import { IBinaryTree } from '../../interfaces';
|
|
21
20
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
22
21
|
|
|
23
22
|
export class AVLTreeMultiMapNode<
|
|
24
|
-
K
|
|
23
|
+
K = any,
|
|
25
24
|
V = any,
|
|
26
25
|
NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNodeNested<K, V>
|
|
27
26
|
> extends AVLTreeNode<K, V, NODE> {
|
|
@@ -64,20 +63,21 @@ export class AVLTreeMultiMapNode<
|
|
|
64
63
|
* The only distinction between a AVLTreeMultiMap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
65
64
|
*/
|
|
66
65
|
export class AVLTreeMultiMap<
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
66
|
+
K = any,
|
|
67
|
+
V = any,
|
|
68
|
+
R = BTNEntry<K, V>,
|
|
69
|
+
NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNodeNested<K, V>>,
|
|
70
|
+
TREE extends AVLTreeMultiMap<K, V, R, NODE, TREE> = AVLTreeMultiMap<
|
|
71
|
+
K,
|
|
72
|
+
V,
|
|
73
|
+
R,
|
|
74
|
+
NODE,
|
|
75
|
+
AVLTreeMultiMapNested<K, V, R, NODE>
|
|
76
|
+
>
|
|
77
77
|
>
|
|
78
|
-
>
|
|
79
78
|
extends AVLTree<K, V, R, NODE, TREE>
|
|
80
|
-
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
79
|
+
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
80
|
+
{
|
|
81
81
|
/**
|
|
82
82
|
* The constructor initializes a new AVLTreeMultiMap object with optional initial elements.
|
|
83
83
|
* @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
|
|
@@ -87,7 +87,7 @@ export class AVLTreeMultiMap<
|
|
|
87
87
|
* `compareValues` functions to define custom comparison logic for keys and values, respectively.
|
|
88
88
|
*/
|
|
89
89
|
constructor(
|
|
90
|
-
keysOrNodesOrEntriesOrRawElements: Iterable<R |
|
|
90
|
+
keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
|
|
91
91
|
options?: AVLTreeMultiMapOptions<K, V, R>
|
|
92
92
|
) {
|
|
93
93
|
super([], options);
|
|
@@ -156,13 +156,13 @@ export class AVLTreeMultiMap<
|
|
|
156
156
|
|
|
157
157
|
/**
|
|
158
158
|
* The function checks if the input is an instance of AVLTreeMultiMapNode.
|
|
159
|
-
* @param {R |
|
|
160
|
-
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `
|
|
159
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
160
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
161
161
|
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
162
162
|
* an instance of the `AVLTreeMultiMapNode` class.
|
|
163
163
|
*/
|
|
164
164
|
override isNode(
|
|
165
|
-
keyOrNodeOrEntryOrRawElement: R |
|
|
165
|
+
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>
|
|
166
166
|
): keyOrNodeOrEntryOrRawElement is NODE {
|
|
167
167
|
return keyOrNodeOrEntryOrRawElement instanceof AVLTreeMultiMapNode;
|
|
168
168
|
}
|
|
@@ -170,8 +170,8 @@ export class AVLTreeMultiMap<
|
|
|
170
170
|
/**
|
|
171
171
|
* The function `keyValueOrEntryOrRawElementToNode` converts a key, value, entry, or raw element into
|
|
172
172
|
* a node object.
|
|
173
|
-
* @param {R |
|
|
174
|
-
* `keyOrNodeOrEntryOrRawElement` parameter can be of type `R` or `
|
|
173
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
|
|
174
|
+
* `keyOrNodeOrEntryOrRawElement` parameter can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
175
175
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
176
176
|
* `override` function. It represents the value associated with the key in the data structure. If no
|
|
177
177
|
* value is provided, it will default to `undefined`.
|
|
@@ -180,7 +180,7 @@ export class AVLTreeMultiMap<
|
|
|
180
180
|
* @returns either a NODE object or undefined.
|
|
181
181
|
*/
|
|
182
182
|
override keyValueOrEntryOrRawElementToNode(
|
|
183
|
-
keyOrNodeOrEntryOrRawElement: R |
|
|
183
|
+
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
184
184
|
value?: V,
|
|
185
185
|
count = 1
|
|
186
186
|
): NODE | undefined {
|
|
@@ -214,9 +214,9 @@ export class AVLTreeMultiMap<
|
|
|
214
214
|
*
|
|
215
215
|
* The function overrides the add method of a TypeScript class to add a new node to a data structure
|
|
216
216
|
* and update the count.
|
|
217
|
-
* @param {R |
|
|
217
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
|
|
218
218
|
* `keyOrNodeOrEntryOrRawElement` parameter can accept a value of type `R`, which can be any type. It
|
|
219
|
-
* can also accept a value of type `
|
|
219
|
+
* can also accept a value of type `BTNKeyOrNodeOrEntry<K, V, NODE>`, which represents a key, node,
|
|
220
220
|
* entry, or raw element
|
|
221
221
|
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
222
222
|
* data structure. It is an optional parameter, so it can be omitted if not needed.
|
|
@@ -225,7 +225,7 @@ export class AVLTreeMultiMap<
|
|
|
225
225
|
* be added once. However, you can specify a different value for `count` if you want to add
|
|
226
226
|
* @returns a boolean value.
|
|
227
227
|
*/
|
|
228
|
-
override add(keyOrNodeOrEntryOrRawElement: R |
|
|
228
|
+
override add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V, count = 1): boolean {
|
|
229
229
|
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value, count);
|
|
230
230
|
if (newNode === undefined) return false;
|
|
231
231
|
|
|
@@ -13,14 +13,13 @@ import type {
|
|
|
13
13
|
BinaryTreeDeleteResult,
|
|
14
14
|
BSTNKeyOrNode,
|
|
15
15
|
BTNCallback,
|
|
16
|
-
|
|
17
|
-
KeyOrNodeOrEntry
|
|
16
|
+
BTNKeyOrNodeOrEntry
|
|
18
17
|
} from '../../types';
|
|
19
18
|
import { BTNEntry } from '../../types';
|
|
20
19
|
import { IBinaryTree } from '../../interfaces';
|
|
21
20
|
|
|
22
21
|
export class AVLTreeNode<
|
|
23
|
-
K
|
|
22
|
+
K = any,
|
|
24
23
|
V = any,
|
|
25
24
|
NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNodeNested<K, V>
|
|
26
25
|
> extends BSTNode<K, V, NODE> {
|
|
@@ -67,14 +66,15 @@ export class AVLTreeNode<
|
|
|
67
66
|
* 7. Path Length: The path length from the root to any leaf is longer compared to an unbalanced BST, but shorter than a linear chain of nodes.
|
|
68
67
|
*/
|
|
69
68
|
export class AVLTree<
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
>
|
|
69
|
+
K = any,
|
|
70
|
+
V = any,
|
|
71
|
+
R = BTNEntry<K, V>,
|
|
72
|
+
NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>,
|
|
73
|
+
TREE extends AVLTree<K, V, R, NODE, TREE> = AVLTree<K, V, R, NODE, AVLTreeNested<K, V, R, NODE>>
|
|
74
|
+
>
|
|
76
75
|
extends BST<K, V, R, NODE, TREE>
|
|
77
|
-
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
76
|
+
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
77
|
+
{
|
|
78
78
|
/**
|
|
79
79
|
* This is a constructor function for an AVLTree class that initializes the tree with keys, nodes,
|
|
80
80
|
* entries, or raw elements.
|
|
@@ -87,7 +87,7 @@ export class AVLTree<
|
|
|
87
87
|
* `nodeBuilder` (
|
|
88
88
|
*/
|
|
89
89
|
constructor(
|
|
90
|
-
keysOrNodesOrEntriesOrRawElements: Iterable<R |
|
|
90
|
+
keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
|
|
91
91
|
options?: AVLTreeOptions<K, V, R>
|
|
92
92
|
) {
|
|
93
93
|
super([], options);
|
|
@@ -124,13 +124,13 @@ export class AVLTree<
|
|
|
124
124
|
|
|
125
125
|
/**
|
|
126
126
|
* The function checks if the input is an instance of AVLTreeNode.
|
|
127
|
-
* @param {R |
|
|
128
|
-
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `
|
|
127
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
128
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
129
129
|
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
130
130
|
* an instance of the `AVLTreeNode` class.
|
|
131
131
|
*/
|
|
132
132
|
override isNode(
|
|
133
|
-
keyOrNodeOrEntryOrRawElement: R |
|
|
133
|
+
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>
|
|
134
134
|
): keyOrNodeOrEntryOrRawElement is NODE {
|
|
135
135
|
return keyOrNodeOrEntryOrRawElement instanceof AVLTreeNode;
|
|
136
136
|
}
|
|
@@ -147,14 +147,14 @@ export class AVLTree<
|
|
|
147
147
|
*
|
|
148
148
|
* The function overrides the add method of a class and inserts a key-value pair into a data
|
|
149
149
|
* structure, then balances the path.
|
|
150
|
-
* @param {R |
|
|
151
|
-
* `keyOrNodeOrEntryOrRawElement` can accept values of type `R`, `
|
|
150
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
151
|
+
* `keyOrNodeOrEntryOrRawElement` can accept values of type `R`, `BTNKeyOrNodeOrEntry<K, V, NODE>`, or
|
|
152
152
|
* `RawElement`.
|
|
153
153
|
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
154
154
|
* the key or node being added to the data structure.
|
|
155
155
|
* @returns The method is returning a boolean value.
|
|
156
156
|
*/
|
|
157
|
-
override add(keyOrNodeOrEntryOrRawElement: R |
|
|
157
|
+
override add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
|
|
158
158
|
if (keyOrNodeOrEntryOrRawElement === null) return false;
|
|
159
159
|
const inserted = super.add(keyOrNodeOrEntryOrRawElement, value);
|
|
160
160
|
if (inserted) this._balancePath(keyOrNodeOrEntryOrRawElement);
|
|
@@ -489,10 +489,10 @@ export class AVLTree<
|
|
|
489
489
|
*
|
|
490
490
|
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
491
491
|
* to restore balance in an AVL tree after inserting a node.
|
|
492
|
-
* @param {R |
|
|
493
|
-
* `
|
|
492
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} node - The `node` parameter can be of type `R` or
|
|
493
|
+
* `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
494
494
|
*/
|
|
495
|
-
protected _balancePath(node: R |
|
|
495
|
+
protected _balancePath(node: R | BTNKeyOrNodeOrEntry<K, V, NODE>): void {
|
|
496
496
|
node = this.ensureNode(node);
|
|
497
497
|
const path = this.getPathToRoot(node, false); // first O(log n) + O(log n)
|
|
498
498
|
for (let i = 0; i < path.length; i++) {
|
|
@@ -504,7 +504,7 @@ export class AVLTree<
|
|
|
504
504
|
// Balance Restoration: If a balance issue is discovered after inserting a node, it requires balance restoration operations. Balance restoration includes four basic cases where rotation operations need to be performed to fix the balance:
|
|
505
505
|
switch (
|
|
506
506
|
this._balanceFactor(A) // second O(1)
|
|
507
|
-
|
|
507
|
+
) {
|
|
508
508
|
case -2:
|
|
509
509
|
if (A && A.left) {
|
|
510
510
|
if (this._balanceFactor(A.left) <= 0) {
|