min-heap-typed 1.49.8 → 1.50.0

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.
@@ -13,7 +13,7 @@ import type {
13
13
  BTNodePureExemplar,
14
14
  KeyOrNodeOrEntry
15
15
  } from '../../types';
16
- import { BSTVariant, CP, IterationType } from '../../types';
16
+ import { BSTVariant, CP, DFSOrderPattern, IterationType } from '../../types';
17
17
  import { BinaryTree, BinaryTreeNode } from './binary-tree';
18
18
  import { IBinaryTree } from '../../interfaces';
19
19
  import { Queue } from '../queue';
@@ -171,7 +171,7 @@ export class BST<
171
171
  } else {
172
172
  node = this.createNode(key, value);
173
173
  }
174
- } else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
174
+ } else if (!this.isNode(keyOrNodeOrEntry)) {
175
175
  node = this.createNode(keyOrNodeOrEntry, value);
176
176
  } else {
177
177
  return;
@@ -212,16 +212,6 @@ export class BST<
212
212
  return res;
213
213
  }
214
214
 
215
- /**
216
- * The function "isNotNodeInstance" checks if a potential key is a K.
217
- * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
218
- * data type.
219
- * @returns a boolean value indicating whether the potentialKey is of type number or not.
220
- */
221
- override isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K {
222
- return !(potentialKey instanceof BSTNode);
223
- }
224
-
225
215
  /**
226
216
  * The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
227
217
  * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, N>`.
@@ -407,43 +397,6 @@ export class BST<
407
397
  return inserted;
408
398
  }
409
399
 
410
- /**
411
- * Time Complexity: O(n log n)
412
- * Space Complexity: O(n)
413
- * Adding each element individually in a balanced tree. Additional space is required for the sorted array.
414
- */
415
-
416
- /**
417
- * Time Complexity: O(n log n)
418
- * Space Complexity: O(n)
419
- *
420
- * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
421
- * leftmost node if the comparison result is greater than.
422
- * @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
423
- * type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
424
- * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
425
- * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
426
- * the key of the leftmost node if the comparison result is greater than, and the key of the
427
- * rightmost node otherwise. If no node is found, it returns 0.
428
- */
429
- lastKey(beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root): K | undefined {
430
- let current = this.ensureNode(beginRoot);
431
- if (!current) return undefined;
432
-
433
- if (this._variant === BSTVariant.STANDARD) {
434
- // For BSTVariant.MIN, find the rightmost node
435
- while (current.right !== undefined) {
436
- current = current.right;
437
- }
438
- } else {
439
- // For BSTVariant.MAX, find the leftmost node
440
- while (current.left !== undefined) {
441
- current = current.left;
442
- }
443
- }
444
- return current.key;
445
- }
446
-
447
400
  /**
448
401
  * Time Complexity: O(log n)
449
402
  * Space Complexity: O(1)
@@ -573,6 +526,157 @@ export class BST<
573
526
  return ans;
574
527
  }
575
528
 
529
+ // /**
530
+ // * The function overrides the subTreeTraverse method and returns the result of calling the super
531
+ // * method with the provided arguments.
532
+ // * @param {C} callback - The `callback` parameter is a function that will be called for each node in
533
+ // * the subtree traversal. It should accept a single parameter of type `N`, which represents a node in
534
+ // * the tree. The return type of the callback function can be any type.
535
+ // * @param beginRoot - The `beginRoot` parameter is the starting point for traversing the subtree. It
536
+ // * can be either a key, a node, or an entry.
537
+ // * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
538
+ // * be performed during the traversal of the subtree. It can have one of the following values:
539
+ // * @returns The method is returning an array of the return type of the callback function.
540
+ // */
541
+ // override subTreeTraverse<C extends BTNCallback<N>>(
542
+ // callback: C = this._defaultOneParamCallback as C,
543
+ // beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
544
+ // iterationType = this.iterationType
545
+ // ): ReturnType<C>[] {
546
+ // return super.subTreeTraverse(callback, beginRoot, iterationType, false);
547
+ // }
548
+
549
+ /**
550
+ * Time complexity: O(n)
551
+ * Space complexity: O(n)
552
+ */
553
+
554
+ /**
555
+ * Time complexity: O(n)
556
+ * Space complexity: O(n)
557
+ *
558
+ * The function overrides the depth-first search method and returns an array of the return types of
559
+ * the callback function.
560
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node
561
+ * during the depth-first search traversal. It is an optional parameter and if not provided, a
562
+ * default callback function will be used.
563
+ * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter specifies the order in which the
564
+ * nodes are visited during the depth-first search. It can have one of the following values:
565
+ * @param beginRoot - The `beginRoot` parameter is used to specify the starting point for the
566
+ * Depth-First Search (DFS) traversal. It can be either a key, a node, or an entry in the tree. If no
567
+ * value is provided, the DFS traversal will start from the root of the tree.
568
+ * @param {IterationType} iterationType - The `iterationType` parameter specifies the type of
569
+ * iteration to be used during the Depth-First Search (DFS) traversal. It can have one of the
570
+ * following values:
571
+ * @returns The method is returning an array of the return type of the callback function.
572
+ */
573
+ override dfs<C extends BTNCallback<N>>(
574
+ callback: C = this._defaultOneParamCallback as C,
575
+ pattern: DFSOrderPattern = 'in',
576
+ beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
577
+ iterationType: IterationType = IterationType.ITERATIVE
578
+ ): ReturnType<C>[] {
579
+ return super.dfs(callback, pattern, beginRoot, iterationType, false);
580
+ }
581
+
582
+ /**
583
+ * Time complexity: O(n)
584
+ * Space complexity: O(n)
585
+ */
586
+
587
+ /**
588
+ * Time complexity: O(n)
589
+ * Space complexity: O(n)
590
+ *
591
+ * The function overrides the breadth-first search method and returns an array of the return types of
592
+ * the callback function.
593
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node
594
+ * visited during the breadth-first search traversal. It is an optional parameter and if not
595
+ * provided, a default callback function will be used.
596
+ * @param beginRoot - The `beginRoot` parameter is the starting point for the breadth-first search
597
+ * traversal. It can be either a key, a node, or an entry in the tree. If not specified, the root of
598
+ * the tree is used as the starting point.
599
+ * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
600
+ * be performed during the breadth-first search (BFS) traversal. It determines the order in which the
601
+ * nodes are visited.
602
+ * @returns The method is returning an array of the return type of the callback function.
603
+ */
604
+ override bfs<C extends BTNCallback<N>>(
605
+ callback: C = this._defaultOneParamCallback as C,
606
+ beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
607
+ iterationType = this.iterationType
608
+ ): ReturnType<C>[] {
609
+ return super.bfs(callback, beginRoot, iterationType, false);
610
+ }
611
+
612
+ /**
613
+ * Time complexity: O(n)
614
+ * Space complexity: O(n)
615
+ */
616
+
617
+ /**
618
+ * Time complexity: O(n)
619
+ * Space complexity: O(n)
620
+ *
621
+ * The function overrides the listLevels method and returns an array of arrays containing the return
622
+ * type of the callback function for each level of the tree.
623
+ * @param {C} callback - The `callback` parameter is a generic type `C` that extends
624
+ * `BTNCallback<N>`. It represents a callback function that will be called for each node in the tree
625
+ * during the level listing process.
626
+ * @param beginRoot - The `beginRoot` parameter is used to specify the starting point for listing the
627
+ * levels of a binary tree. It can be either a key, a node, or an entry in the binary tree. If not
628
+ * provided, the root of the binary tree is used as the starting point.
629
+ * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
630
+ * be performed on the tree. It determines the order in which the nodes are visited during the
631
+ * iteration.
632
+ * @returns The method is returning a two-dimensional array of the return type of the callback
633
+ * function.
634
+ */
635
+ override listLevels<C extends BTNCallback<N>>(
636
+ callback: C = this._defaultOneParamCallback as C,
637
+ beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
638
+ iterationType = this.iterationType
639
+ ): ReturnType<C>[][] {
640
+ return super.listLevels(callback, beginRoot, iterationType, false);
641
+ }
642
+
643
+ /**
644
+ * Time Complexity: O(n log n)
645
+ * Space Complexity: O(n)
646
+ * Adding each element individually in a balanced tree. Additional space is required for the sorted array.
647
+ */
648
+
649
+ /**
650
+ * Time Complexity: O(n log n)
651
+ * Space Complexity: O(n)
652
+ *
653
+ * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
654
+ * leftmost node if the comparison result is greater than.
655
+ * @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
656
+ * type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
657
+ * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
658
+ * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
659
+ * the key of the leftmost node if the comparison result is greater than, and the key of the
660
+ * rightmost node otherwise. If no node is found, it returns 0.
661
+ */
662
+ lastKey(beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root): K | undefined {
663
+ let current = this.ensureNode(beginRoot);
664
+ if (!current) return undefined;
665
+
666
+ if (this._variant === BSTVariant.STANDARD) {
667
+ // For BSTVariant.MIN, find the rightmost node
668
+ while (current.right !== undefined) {
669
+ current = current.right;
670
+ }
671
+ } else {
672
+ // For BSTVariant.MAX, find the leftmost node
673
+ while (current.left !== undefined) {
674
+ current = current.left;
675
+ }
676
+ }
677
+ return current.key;
678
+ }
679
+
576
680
  /**
577
681
  * Time Complexity: O(log n)
578
682
  * Space Complexity: O(log n)
@@ -132,7 +132,7 @@ export class RedBlackTree<
132
132
  } else {
133
133
  node = this.createNode(key, value, RBTNColor.RED);
134
134
  }
135
- } else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
135
+ } else if (!this.isNode(keyOrNodeOrEntry)) {
136
136
  node = this.createNode(keyOrNodeOrEntry, value, RBTNColor.RED);
137
137
  } else {
138
138
  return;
@@ -160,16 +160,6 @@ export class RedBlackTree<
160
160
  return node instanceof RedBlackTreeNode;
161
161
  }
162
162
 
163
- /**
164
- * The function "isNotNodeInstance" checks if a potential key is a K.
165
- * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
166
- * data type.
167
- * @returns a boolean value indicating whether the potentialKey is of type number or not.
168
- */
169
- override isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K {
170
- return !(potentialKey instanceof RedBlackTreeNode);
171
- }
172
-
173
163
  /**
174
164
  * Time Complexity: O(log n)
175
165
  * Space Complexity: O(1)
@@ -62,7 +62,7 @@ export class TreeMultimap<
62
62
  // TODO the _count is not accurate after nodes count modified
63
63
  get count(): number {
64
64
  let sum = 0;
65
- this.subTreeTraverse(node => (sum += node.count));
65
+ this.dfs(node => (sum += node.count));
66
66
  return sum;
67
67
  }
68
68
 
@@ -111,7 +111,7 @@ export class TreeMultimap<
111
111
  } else {
112
112
  node = this.createNode(key, value, count);
113
113
  }
114
- } else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
114
+ } else if (!this.isNode(keyOrNodeOrEntry)) {
115
115
  node = this.createNode(keyOrNodeOrEntry, value, count);
116
116
  } else {
117
117
  return;
@@ -129,16 +129,6 @@ export class TreeMultimap<
129
129
  return keyOrNodeOrEntry instanceof TreeMultimapNode;
130
130
  }
131
131
 
132
- /**
133
- * The function "isNotNodeInstance" checks if a potential key is a K.
134
- * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
135
- * data type.
136
- * @returns a boolean value indicating whether the potentialKey is of type number or not.
137
- */
138
- override isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K {
139
- return !(potentialKey instanceof TreeMultimapNode);
140
- }
141
-
142
132
  /**
143
133
  * Time Complexity: O(log n)
144
134
  * Space Complexity: O(1)
@@ -21,29 +21,46 @@ import { isWeakKey, rangeCheck } from '../../utils';
21
21
  * 3. Unique Keys: Keys are unique. If you try to insert another entry with the same key, the old entry will be replaced by the new one.
22
22
  * 4. Unordered Collection: HashMap does not guarantee the order of entries, and the order may change over time.
23
23
  */
24
- export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
24
+ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K, V> {
25
25
  protected _store: { [key: string]: HashMapStoreItem<K, V> } = {};
26
26
  protected _objMap: Map<object, V> = new Map();
27
27
 
28
28
  /**
29
- * The constructor function initializes a new instance of a class with optional entries and options.
30
- * @param entries - The `entries` parameter is an iterable containing key-value pairs `[K, V]`. It
31
- * is optional and defaults to an empty array `[]`. This parameter is used to initialize the map with
32
- * key-value pairs.
33
- * @param [options] - The `options` parameter is an optional object that can contain additional
34
- * configuration options for the constructor. In this case, it has one property:
29
+ * The constructor function initializes a HashMap object with an optional initial collection and
30
+ * options.
31
+ * @param rawCollection - The `rawCollection` parameter is an iterable collection of elements of type
32
+ * `T`. It is an optional parameter and its default value is an empty array `[]`.
33
+ * @param [options] - The `options` parameter is an optional object that can contain two properties:
35
34
  */
36
- constructor(entries: Iterable<[K, V]> = [], options?: HashMapOptions<K>) {
35
+ constructor(rawCollection: Iterable<R> = [], options?: HashMapOptions<K, V, R>) {
37
36
  super();
38
37
  if (options) {
39
- const { hashFn } = options;
38
+ const { hashFn, toEntryFn } = options;
40
39
  if (hashFn) {
41
40
  this._hashFn = hashFn;
42
41
  }
42
+ if (toEntryFn) {
43
+ this._toEntryFn = toEntryFn;
44
+ }
43
45
  }
44
- if (entries) {
45
- this.setMany(entries);
46
+ if (rawCollection) {
47
+ this.setMany(rawCollection);
48
+ }
49
+ }
50
+
51
+ protected _toEntryFn: (rawElement: R) => [K, V] = (rawElement: R) => {
52
+ if (this.isEntry(rawElement)) {
53
+ // TODO, For performance optimization, it may be necessary to only inspect the first element traversed.
54
+ return rawElement;
55
+ } else {
56
+ throw new Error(
57
+ "If the provided rawCollection does not adhere to the [key, value] type format, the toEntryFn in the constructor's options parameter needs to specified."
58
+ );
46
59
  }
60
+ };
61
+
62
+ get toEntryFn() {
63
+ return this._toEntryFn;
47
64
  }
48
65
 
49
66
  protected _size = 0;
@@ -52,6 +69,10 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
52
69
  return this._size;
53
70
  }
54
71
 
72
+ isEntry(rawElement: any): rawElement is [K, V] {
73
+ return Array.isArray(rawElement) && rawElement.length === 2;
74
+ }
75
+
55
76
  isEmpty(): boolean {
56
77
  return this.size === 0;
57
78
  }
@@ -88,13 +109,18 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
88
109
  }
89
110
 
90
111
  /**
91
- * The function "setMany" sets multiple key-value pairs in a map.
92
- * @param entries - The `entries` parameter is an iterable containing key-value pairs. Each
93
- * key-value pair is represented as an array with two entries: the key and the value.
112
+ * The function `setMany` takes an iterable collection of objects, maps each object to a key-value
113
+ * pair using a mapping function, and sets each key-value pair in the current object.
114
+ * @param rawCollection - The `rawCollection` parameter is an iterable collection of elements of type
115
+ * `T`.
116
+ * @returns The `setMany` function is returning an array of booleans.
94
117
  */
95
- setMany(entries: Iterable<[K, V]>): boolean[] {
118
+ setMany(rawCollection: Iterable<R>): boolean[] {
96
119
  const results: boolean[] = [];
97
- for (const [key, value] of entries) results.push(this.set(key, value));
120
+ for (const rawEle of rawCollection) {
121
+ const [key, value] = this.toEntryFn(rawEle);
122
+ results.push(this.set(key, value));
123
+ }
98
124
  return results;
99
125
  }
100
126
 
@@ -10,8 +10,9 @@ export type LinkedHashMapOptions<K> = {
10
10
  objHashFn?: (key: K) => object;
11
11
  };
12
12
 
13
- export type HashMapOptions<K> = {
13
+ export type HashMapOptions<K, V, T> = {
14
14
  hashFn?: (key: K) => string;
15
+ toEntryFn?: (rawElement: T) => [K, V];
15
16
  };
16
17
 
17
18
  export type HashMapStoreItem<K, V> = { key: K; value: V };