tree-multimap-typed 1.51.8 → 1.52.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. package/dist/data-structures/base/index.d.ts +2 -1
  2. package/dist/data-structures/base/index.js +2 -1
  3. package/dist/data-structures/base/iterable-element-base.d.ts +171 -0
  4. package/dist/data-structures/base/iterable-element-base.js +225 -0
  5. package/dist/data-structures/base/{iterable-base.d.ts → iterable-entry-base.d.ts} +4 -147
  6. package/dist/data-structures/base/{iterable-base.js → iterable-entry-base.js} +12 -189
  7. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +106 -68
  8. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +119 -87
  9. package/dist/data-structures/binary-tree/avl-tree.d.ts +82 -62
  10. package/dist/data-structures/binary-tree/avl-tree.js +78 -59
  11. package/dist/data-structures/binary-tree/binary-tree.d.ts +333 -241
  12. package/dist/data-structures/binary-tree/binary-tree.js +478 -366
  13. package/dist/data-structures/binary-tree/bst.d.ts +202 -212
  14. package/dist/data-structures/binary-tree/bst.js +208 -250
  15. package/dist/data-structures/binary-tree/rb-tree.d.ts +73 -74
  16. package/dist/data-structures/binary-tree/rb-tree.js +107 -98
  17. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +92 -75
  18. package/dist/data-structures/binary-tree/tree-multi-map.js +105 -93
  19. package/dist/data-structures/graph/abstract-graph.d.ts +10 -15
  20. package/dist/data-structures/graph/abstract-graph.js +10 -15
  21. package/dist/data-structures/graph/directed-graph.js +2 -1
  22. package/dist/data-structures/hash/hash-map.d.ts +33 -40
  23. package/dist/data-structures/hash/hash-map.js +40 -55
  24. package/dist/data-structures/heap/heap.d.ts +43 -114
  25. package/dist/data-structures/heap/heap.js +59 -127
  26. package/dist/data-structures/heap/max-heap.d.ts +50 -4
  27. package/dist/data-structures/heap/max-heap.js +76 -10
  28. package/dist/data-structures/heap/min-heap.d.ts +51 -5
  29. package/dist/data-structures/heap/min-heap.js +68 -11
  30. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +22 -28
  31. package/dist/data-structures/linked-list/doubly-linked-list.js +26 -28
  32. package/dist/data-structures/linked-list/singly-linked-list.d.ts +22 -25
  33. package/dist/data-structures/linked-list/singly-linked-list.js +29 -26
  34. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +50 -4
  35. package/dist/data-structures/priority-queue/max-priority-queue.js +79 -10
  36. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +51 -5
  37. package/dist/data-structures/priority-queue/min-priority-queue.js +71 -11
  38. package/dist/data-structures/priority-queue/priority-queue.d.ts +50 -4
  39. package/dist/data-structures/priority-queue/priority-queue.js +70 -1
  40. package/dist/data-structures/queue/deque.d.ts +28 -20
  41. package/dist/data-structures/queue/deque.js +45 -24
  42. package/dist/data-structures/queue/queue.d.ts +8 -29
  43. package/dist/data-structures/queue/queue.js +15 -32
  44. package/dist/data-structures/stack/stack.d.ts +17 -22
  45. package/dist/data-structures/stack/stack.js +25 -24
  46. package/dist/data-structures/trie/trie.d.ts +19 -14
  47. package/dist/data-structures/trie/trie.js +27 -16
  48. package/dist/index.d.ts +6 -0
  49. package/dist/index.js +6 -0
  50. package/dist/interfaces/binary-tree.d.ts +7 -7
  51. package/dist/types/common.d.ts +1 -23
  52. package/dist/types/data-structures/base/base.d.ts +5 -2
  53. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -4
  54. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -4
  55. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +22 -5
  56. package/dist/types/data-structures/binary-tree/bst.d.ts +7 -5
  57. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -4
  58. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -4
  59. package/dist/types/data-structures/heap/heap.d.ts +3 -2
  60. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
  61. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +2 -1
  62. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
  63. package/dist/types/data-structures/queue/deque.d.ts +4 -2
  64. package/dist/types/data-structures/queue/queue.d.ts +2 -1
  65. package/dist/types/data-structures/stack/stack.d.ts +2 -1
  66. package/dist/types/data-structures/trie/trie.d.ts +3 -2
  67. package/dist/utils/utils.js +3 -5
  68. package/package.json +2 -2
  69. package/src/data-structures/base/index.ts +2 -1
  70. package/src/data-structures/base/iterable-element-base.ts +250 -0
  71. package/src/data-structures/base/{iterable-base.ts → iterable-entry-base.ts} +22 -213
  72. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +146 -97
  73. package/src/data-structures/binary-tree/avl-tree.ts +97 -70
  74. package/src/data-structures/binary-tree/binary-tree.ts +591 -455
  75. package/src/data-structures/binary-tree/bst.ts +266 -293
  76. package/src/data-structures/binary-tree/rb-tree.ts +124 -104
  77. package/src/data-structures/binary-tree/tree-multi-map.ts +128 -103
  78. package/src/data-structures/graph/abstract-graph.ts +10 -10
  79. package/src/data-structures/graph/directed-graph.ts +2 -1
  80. package/src/data-structures/hash/hash-map.ts +46 -53
  81. package/src/data-structures/heap/heap.ts +71 -152
  82. package/src/data-structures/heap/max-heap.ts +88 -13
  83. package/src/data-structures/heap/min-heap.ts +78 -15
  84. package/src/data-structures/linked-list/doubly-linked-list.ts +32 -32
  85. package/src/data-structures/linked-list/singly-linked-list.ts +37 -29
  86. package/src/data-structures/priority-queue/max-priority-queue.ts +94 -13
  87. package/src/data-structures/priority-queue/min-priority-queue.ts +84 -15
  88. package/src/data-structures/priority-queue/priority-queue.ts +81 -4
  89. package/src/data-structures/queue/deque.ts +52 -27
  90. package/src/data-structures/queue/queue.ts +23 -37
  91. package/src/data-structures/stack/stack.ts +31 -26
  92. package/src/data-structures/trie/trie.ts +35 -20
  93. package/src/index.ts +6 -0
  94. package/src/interfaces/binary-tree.ts +10 -10
  95. package/src/types/common.ts +2 -25
  96. package/src/types/data-structures/base/base.ts +14 -6
  97. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +3 -4
  98. package/src/types/data-structures/binary-tree/avl-tree.ts +3 -4
  99. package/src/types/data-structures/binary-tree/binary-tree.ts +26 -6
  100. package/src/types/data-structures/binary-tree/bst.ts +11 -5
  101. package/src/types/data-structures/binary-tree/rb-tree.ts +3 -4
  102. package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -4
  103. package/src/types/data-structures/heap/heap.ts +4 -1
  104. package/src/types/data-structures/linked-list/doubly-linked-list.ts +3 -1
  105. package/src/types/data-structures/linked-list/singly-linked-list.ts +3 -1
  106. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
  107. package/src/types/data-structures/queue/deque.ts +6 -1
  108. package/src/types/data-structures/queue/queue.ts +3 -1
  109. package/src/types/data-structures/stack/stack.ts +3 -1
  110. package/src/types/data-structures/trie/trie.ts +3 -1
  111. package/src/utils/utils.ts +3 -3
@@ -1,6 +1,14 @@
1
- import { ElementCallback, EntryCallback, ReduceElementCallback, ReduceEntryCallback } from '../../types';
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)
@@ -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 `reduce` function iterates over the elements of an array-like object and applies a callback
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(): C;
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<E>;
327
+ protected abstract _getIterator(...args: any[]): IterableIterator<[K, V]>;
519
328
  }
@@ -12,15 +12,15 @@ import type {
12
12
  BinaryTreeDeleteResult,
13
13
  BSTNKeyOrNode,
14
14
  BTNCallback,
15
- Comparable,
16
- IterationType,
17
- KeyOrNodeOrEntry
15
+ BTNKeyOrNodeOrEntry,
16
+ IterationType
18
17
  } from '../../types';
18
+ import { BTNEntry } from '../../types';
19
19
  import { IBinaryTree } from '../../interfaces';
20
20
  import { AVLTree, AVLTreeNode } from './avl-tree';
21
21
 
22
22
  export class AVLTreeMultiMapNode<
23
- K extends Comparable,
23
+ K = any,
24
24
  V = any,
25
25
  NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNodeNested<K, V>
26
26
  > extends AVLTreeNode<K, V, NODE> {
@@ -63,21 +63,38 @@ export class AVLTreeMultiMapNode<
63
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.
64
64
  */
65
65
  export class AVLTreeMultiMap<
66
- K extends Comparable,
66
+ K = any,
67
67
  V = any,
68
+ R = BTNEntry<K, V>,
68
69
  NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNodeNested<K, V>>,
69
- TREE extends AVLTreeMultiMap<K, V, NODE, TREE> = AVLTreeMultiMap<K, V, NODE, AVLTreeMultiMapNested<K, V, NODE>>
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
+ >
70
77
  >
71
- extends AVLTree<K, V, NODE, TREE>
72
- implements IBinaryTree<K, V, NODE, TREE> {
73
- constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: AVLTreeMultiMapOptions<K>) {
78
+ extends AVLTree<K, V, R, NODE, TREE>
79
+ implements IBinaryTree<K, V, R, NODE, TREE> {
80
+ /**
81
+ * The constructor initializes a new AVLTreeMultiMap object with optional initial elements.
82
+ * @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
83
+ * iterable object that can contain either keys, nodes, entries, or raw elements.
84
+ * @param [options] - The `options` parameter is an optional object that can be used to customize the
85
+ * behavior of the AVLTreeMultiMap. It can include properties such as `compareKeys` and
86
+ * `compareValues` functions to define custom comparison logic for keys and values, respectively.
87
+ */
88
+ constructor(
89
+ keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
90
+ options?: AVLTreeMultiMapOptions<K, V, R>
91
+ ) {
74
92
  super([], options);
75
- if (keysOrNodesOrEntries) this.addMany(keysOrNodesOrEntries);
93
+ if (keysOrNodesOrEntriesOrRawElements) this.addMany(keysOrNodesOrEntriesOrRawElements);
76
94
  }
77
95
 
78
96
  protected _count = 0;
79
97
 
80
- // TODO the _count is not accurate after nodes count modified
81
98
  /**
82
99
  * The function calculates the sum of the count property of all nodes in a tree using depth-first
83
100
  * search.
@@ -107,20 +124,29 @@ export class AVLTreeMultiMap<
107
124
  }
108
125
 
109
126
  /**
110
- * The function creates a new BSTNode with the given key, value, and count.
111
- * @param {K} key - The key parameter is the unique identifier for the binary tree node. It is used to
112
- * distinguish one node from another in the tree.
113
- * @param {NODE} value - The `value` parameter represents the value that will be stored in the binary search tree node.
114
- * @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
115
- * occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
116
- * @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
127
+ * The function creates a new AVLTreeMultiMapNode with the specified key, value, and count.
128
+ * @param {K} key - The key parameter represents the key of the node being created. It is of type K,
129
+ * which is a generic type that can be replaced with any specific type when using the function.
130
+ * @param {V} [value] - The `value` parameter is an optional parameter that represents the value
131
+ * associated with the key in the node. It is of type `V`, which can be any data type.
132
+ * @param {number} [count] - The `count` parameter represents the number of occurrences of a
133
+ * key-value pair in the AVLTreeMultiMapNode. It is an optional parameter, so it can be omitted when
134
+ * calling the `createNode` method. If provided, it specifies the initial count for the node.
135
+ * @returns a new instance of the AVLTreeMultiMapNode class, casted as NODE.
117
136
  */
118
137
  override createNode(key: K, value?: V, count?: number): NODE {
119
138
  return new AVLTreeMultiMapNode(key, value, count) as NODE;
120
139
  }
121
140
 
122
- override createTree(options?: AVLTreeMultiMapOptions<K>): TREE {
123
- return new AVLTreeMultiMap<K, V, NODE, TREE>([], {
141
+ /**
142
+ * The function creates a new AVLTreeMultiMap object with the specified options and returns it.
143
+ * @param [options] - The `options` parameter is an optional object that contains additional
144
+ * configuration options for creating the AVLTreeMultiMap. It can have the following properties:
145
+ * @returns a new instance of the AVLTreeMultiMap class, with the specified options, as a TREE
146
+ * object.
147
+ */
148
+ override createTree(options?: AVLTreeMultiMapOptions<K, V, R>): TREE {
149
+ return new AVLTreeMultiMap<K, V, R, NODE, TREE>([], {
124
150
  iterationType: this.iterationType,
125
151
  comparator: this.comparator,
126
152
  ...options
@@ -128,49 +154,52 @@ export class AVLTreeMultiMap<
128
154
  }
129
155
 
130
156
  /**
131
- * The function `keyValueOrEntryToNode` converts an keyOrNodeOrEntry object into a node object.
132
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`, which means it
133
- * can be one of the following:
134
- * @param {V} [value] - The `value` parameter is an optional argument that represents the value
135
- * associated with the node. It is of type `V`, which can be any data type. If no value is provided,
136
- * it defaults to `undefined`.
157
+ * The function checks if the input is an instance of AVLTreeMultiMapNode.
158
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
159
+ * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
160
+ * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
161
+ * an instance of the `AVLTreeMultiMapNode` class.
162
+ */
163
+ override isNode(
164
+ keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>
165
+ ): keyOrNodeOrEntryOrRawElement is NODE {
166
+ return keyOrNodeOrEntryOrRawElement instanceof AVLTreeMultiMapNode;
167
+ }
168
+
169
+ /**
170
+ * The function `keyValueOrEntryOrRawElementToNode` converts a key, value, entry, or raw element into
171
+ * a node object.
172
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
173
+ * `keyOrNodeOrEntryOrRawElement` parameter can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
174
+ * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
175
+ * `override` function. It represents the value associated with the key in the data structure. If no
176
+ * value is provided, it will default to `undefined`.
137
177
  * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
138
- * times the value should be added to the node. If not provided, it defaults to 1.
139
- * @returns a node of type `NODE` or `undefined`.
178
+ * times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
179
+ * @returns either a NODE object or undefined.
140
180
  */
141
- override keyValueOrEntryToNode(
142
- keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>,
181
+ override keyValueOrEntryOrRawElementToNode(
182
+ keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
143
183
  value?: V,
144
184
  count = 1
145
185
  ): NODE | undefined {
146
- let node: NODE | undefined;
147
- if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) {
148
- return;
149
- } else if (this.isNode(keyOrNodeOrEntry)) {
150
- node = keyOrNodeOrEntry;
151
- } else if (this.isEntry(keyOrNodeOrEntry)) {
152
- const [key, value] = keyOrNodeOrEntry;
153
- if (key === undefined || key === null) {
154
- return;
155
- } else {
156
- node = this.createNode(key, value, count);
157
- }
158
- } else if (!this.isNode(keyOrNodeOrEntry)) {
159
- node = this.createNode(keyOrNodeOrEntry, value, count);
160
- } else {
161
- return;
186
+ if (keyOrNodeOrEntryOrRawElement === undefined || keyOrNodeOrEntryOrRawElement === null) return;
187
+ if (this.isNode(keyOrNodeOrEntryOrRawElement)) return keyOrNodeOrEntryOrRawElement;
188
+
189
+ if (this.toEntryFn) {
190
+ const [key, entryValue] = this.toEntryFn(keyOrNodeOrEntryOrRawElement as R);
191
+ if (key) return this.createNode(key, entryValue ?? value, count);
162
192
  }
163
- return node;
164
- }
165
193
 
166
- /**
167
- * The function checks if an keyOrNodeOrEntry is an instance of the AVLTreeMultiMapNode class.
168
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
169
- * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeMultiMapNode
170
- * class.
171
- */
172
- override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE {
173
- return keyOrNodeOrEntry instanceof AVLTreeMultiMapNode;
194
+ if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
195
+ const [key, value] = keyOrNodeOrEntryOrRawElement;
196
+ if (key === undefined || key === null) return;
197
+ else return this.createNode(key, value, count);
198
+ }
199
+
200
+ if (this.isKey(keyOrNodeOrEntryOrRawElement)) return this.createNode(keyOrNodeOrEntryOrRawElement, value, count);
201
+
202
+ return;
174
203
  }
175
204
 
176
205
  /**
@@ -182,20 +211,21 @@ export class AVLTreeMultiMap<
182
211
  * Time Complexity: O(log n)
183
212
  * Space Complexity: O(1)
184
213
  *
185
- * The function overrides the add method of a binary tree node and adds a new node to the tree.
186
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
187
- * entry. It represents the key, node, or entry that you want to add to the binary tree.
214
+ * The function overrides the add method of a TypeScript class to add a new node to a data structure
215
+ * and update the count.
216
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
217
+ * `keyOrNodeOrEntryOrRawElement` parameter can accept a value of type `R`, which can be any type. It
218
+ * can also accept a value of type `BTNKeyOrNodeOrEntry<K, V, NODE>`, which represents a key, node,
219
+ * entry, or raw element
188
220
  * @param {V} [value] - The `value` parameter represents the value associated with the key in the
189
- * binary tree node. It is an optional parameter, meaning it can be omitted when calling the `add`
190
- * method.
221
+ * data structure. It is an optional parameter, so it can be omitted if not needed.
191
222
  * @param [count=1] - The `count` parameter represents the number of times the key-value pair should
192
- * be added to the binary tree. By default, it is set to 1, meaning that the key-value pair will be
193
- * added once. However, you can specify a different value for `count` if you want to add
194
- * @returns The method is returning either the newly inserted node or `undefined` if the insertion
195
- * was not successful.
223
+ * be added to the data structure. By default, it is set to 1, meaning that the key-value pair will
224
+ * be added once. However, you can specify a different value for `count` if you want to add
225
+ * @returns a boolean value.
196
226
  */
197
- override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V, count = 1): boolean {
198
- const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value, count);
227
+ override add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V, count = 1): boolean {
228
+ const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value, count);
199
229
  if (newNode === undefined) return false;
200
230
 
201
231
  const orgNodeCount = newNode?.count || 0;
@@ -215,19 +245,19 @@ export class AVLTreeMultiMap<
215
245
  * Time Complexity: O(log n)
216
246
  * Space Complexity: O(1)
217
247
  *
218
- * The `delete` function in TypeScript is used to remove a node from a binary tree, taking into
219
- * account the count of the node and balancing the tree if necessary.
220
- * @param identifier - The identifier is the value or key that is used to identify the node that
221
- * needs to be deleted from the binary tree. It can be of any type that is returned by the callback
248
+ * The `delete` function in a binary tree data structure deletes a node based on its identifier and
249
+ * returns the deleted node along with the parent node that needs to be balanced.
250
+ * @param identifier - The identifier parameter is the value used to identify the node that needs to
251
+ * be deleted from the binary tree. It can be of any type and is the return type of the callback
222
252
  * function.
223
- * @param {C} callback - The `callback` parameter is a function that is used to determine if a node
224
- * should be deleted. It is optional and defaults to a default callback function. The `callback`
225
- * function takes one parameter, which is the identifier of the node, and returns a value that is
226
- * used to identify the node to
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 takes a single argument, which is the identifier
256
+ * of a node, and returns a value that
227
257
  * @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
228
258
  * being deleted. If set to true, the count of the node will not be considered and the node will be
229
- * deleted regardless of its count. If set to false (default), the count of the node will be
230
- * decremented by 1 and
259
+ * deleted regardless of its count. If set to false (default), the count of the node will be taken
260
+ * into account and the node
231
261
  * @returns an array of `BinaryTreeDeleteResult<NODE>`.
232
262
  */
233
263
  override delete<C extends BTNCallback<NODE>>(
@@ -300,7 +330,8 @@ export class AVLTreeMultiMap<
300
330
  * Time Complexity: O(1)
301
331
  * Space Complexity: O(1)
302
332
  *
303
- * The clear() function clears the contents of a data structure and sets the count to zero.
333
+ * The "clear" function overrides the parent class's "clear" function and also resets the count to
334
+ * zero.
304
335
  */
305
336
  override clear() {
306
337
  super.clear();
@@ -315,13 +346,14 @@ export class AVLTreeMultiMap<
315
346
  /**
316
347
  * Time Complexity: O(n log n)
317
348
  * Space Complexity: O(log n)
318
- *
319
349
  * The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
320
350
  * tree using either a recursive or iterative approach.
321
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
322
- * type of iteration to use when building the balanced binary search tree. It can have two possible
323
- * values:
324
- * @returns a boolean value.
351
+ * @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
352
+ * specifies the type of iteration to use when building the balanced binary search tree. It has a
353
+ * default value of `this.iterationType`, which means it will use the iteration type currently set in
354
+ * the object.
355
+ * @returns The function `perfectlyBalance` returns a boolean value. It returns `true` if the
356
+ * balancing operation is successful, and `false` if there are no nodes to balance.
325
357
  */
326
358
  override perfectlyBalance(iterationType: IterationType = this.iterationType): boolean {
327
359
  const sorted = this.dfs(node => node, 'IN'),
@@ -370,7 +402,7 @@ export class AVLTreeMultiMap<
370
402
  * Time complexity: O(n)
371
403
  * Space complexity: O(n)
372
404
  *
373
- * The `clone` function creates a deep copy of a tree object.
405
+ * The function overrides the clone method to create a deep copy of a tree object.
374
406
  * @returns The `clone()` method is returning a cloned instance of the `TREE` object.
375
407
  */
376
408
  override clone(): TREE {
@@ -380,17 +412,26 @@ export class AVLTreeMultiMap<
380
412
  }
381
413
 
382
414
  /**
383
- * The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
384
- * @param {K | NODE | undefined} srcNode - The `srcNode` parameter represents the source node from
385
- * which the values will be swapped. It can be of type `K`, `NODE`, or `undefined`.
386
- * @param {K | NODE | undefined} destNode - The `destNode` parameter represents the destination
387
- * node where the values from the source node will be swapped to.
388
- * @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
389
- * if either `srcNode` or `destNode` is undefined.
415
+ * Time Complexity: O(1)
416
+ * Space Complexity: O(1)
417
+ */
418
+
419
+ /**
420
+ * Time Complexity: O(1)
421
+ * Space Complexity: O(1)
422
+ *
423
+ * The `_swapProperties` function swaps the properties (key, value, count, height) between two nodes
424
+ * in a binary search tree.
425
+ * @param {R | BSTNKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents the source node
426
+ * that will be swapped with the `destNode`.
427
+ * @param {R | BSTNKeyOrNode<K, NODE>} destNode - The `destNode` parameter represents the destination
428
+ * node where the properties will be swapped with the source node.
429
+ * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
430
+ * If either `srcNode` or `destNode` is undefined, it returns `undefined`.
390
431
  */
391
432
  protected override _swapProperties(
392
- srcNode: BSTNKeyOrNode<K, NODE>,
393
- destNode: BSTNKeyOrNode<K, NODE>
433
+ srcNode: R | BSTNKeyOrNode<K, NODE>,
434
+ destNode: R | BSTNKeyOrNode<K, NODE>
394
435
  ): NODE | undefined {
395
436
  srcNode = this.ensureNode(srcNode);
396
437
  destNode = this.ensureNode(destNode);
@@ -417,12 +458,20 @@ export class AVLTreeMultiMap<
417
458
  }
418
459
 
419
460
  /**
461
+ * Time Complexity: O(1)
462
+ * Space Complexity: O(1)
463
+ */
464
+
465
+ /**
466
+ * Time Complexity: O(1)
467
+ * Space Complexity: O(1)
468
+ *
420
469
  * The function replaces an old node with a new node and updates the count property of the new node.
421
- * @param {NODE} oldNode - The `oldNode` parameter is of type `NODE` and represents the node that
422
- * needs to be replaced in a data structure.
423
- * @param {NODE} newNode - The `newNode` parameter is an object of type `NODE`.
470
+ * @param {NODE} oldNode - The oldNode parameter represents the node that needs to be replaced in the
471
+ * data structure. It is of type NODE.
472
+ * @param {NODE} newNode - The `newNode` parameter is an instance of the `NODE` class.
424
473
  * @returns The method is returning the result of calling the `_replaceNode` method from the
425
- * superclass, after updating the `count` property of the `newNode` object.
474
+ * superclass, which is of type `NODE`.
426
475
  */
427
476
  protected override _replaceNode(oldNode: NODE, newNode: NODE): NODE {
428
477
  newNode.count = oldNode.count + newNode.count;