min-heap-typed 1.49.4 → 1.49.6

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 (69) hide show
  1. package/dist/data-structures/base/iterable-base.d.ts +1 -1
  2. package/dist/data-structures/binary-tree/avl-tree.d.ts +53 -48
  3. package/dist/data-structures/binary-tree/avl-tree.js +55 -49
  4. package/dist/data-structures/binary-tree/binary-tree.d.ts +154 -143
  5. package/dist/data-structures/binary-tree/binary-tree.js +211 -198
  6. package/dist/data-structures/binary-tree/bst.d.ts +83 -71
  7. package/dist/data-structures/binary-tree/bst.js +113 -89
  8. package/dist/data-structures/binary-tree/rb-tree.d.ts +37 -35
  9. package/dist/data-structures/binary-tree/rb-tree.js +62 -59
  10. package/dist/data-structures/binary-tree/tree-multimap.d.ts +46 -55
  11. package/dist/data-structures/binary-tree/tree-multimap.js +59 -94
  12. package/dist/data-structures/graph/abstract-graph.d.ts +1 -1
  13. package/dist/data-structures/graph/abstract-graph.js +3 -2
  14. package/dist/data-structures/hash/hash-map.d.ts +1 -1
  15. package/dist/data-structures/hash/hash-map.js +2 -2
  16. package/dist/data-structures/heap/heap.js +2 -3
  17. package/dist/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  18. package/dist/data-structures/matrix/index.d.ts +0 -2
  19. package/dist/data-structures/matrix/index.js +0 -2
  20. package/dist/data-structures/matrix/matrix.d.ts +128 -10
  21. package/dist/data-structures/matrix/matrix.js +400 -15
  22. package/dist/data-structures/queue/deque.d.ts +2 -2
  23. package/dist/data-structures/queue/deque.js +5 -7
  24. package/dist/data-structures/queue/queue.d.ts +1 -1
  25. package/dist/interfaces/binary-tree.d.ts +3 -3
  26. package/dist/types/common.d.ts +3 -3
  27. package/dist/types/common.js +2 -2
  28. package/dist/types/data-structures/base/base.d.ts +1 -1
  29. package/dist/types/data-structures/heap/heap.d.ts +1 -1
  30. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
  31. package/dist/utils/utils.d.ts +1 -0
  32. package/dist/utils/utils.js +6 -1
  33. package/package.json +2 -2
  34. package/src/data-structures/base/index.ts +1 -1
  35. package/src/data-structures/base/iterable-base.ts +7 -10
  36. package/src/data-structures/binary-tree/avl-tree.ts +73 -61
  37. package/src/data-structures/binary-tree/binary-tree.ts +301 -270
  38. package/src/data-structures/binary-tree/bst.ts +139 -115
  39. package/src/data-structures/binary-tree/rb-tree.ts +81 -73
  40. package/src/data-structures/binary-tree/tree-multimap.ts +72 -103
  41. package/src/data-structures/graph/abstract-graph.ts +13 -11
  42. package/src/data-structures/graph/directed-graph.ts +1 -3
  43. package/src/data-structures/graph/map-graph.ts +6 -1
  44. package/src/data-structures/graph/undirected-graph.ts +3 -6
  45. package/src/data-structures/hash/hash-map.ts +18 -16
  46. package/src/data-structures/heap/heap.ts +7 -10
  47. package/src/data-structures/heap/max-heap.ts +2 -1
  48. package/src/data-structures/heap/min-heap.ts +2 -1
  49. package/src/data-structures/linked-list/singly-linked-list.ts +2 -3
  50. package/src/data-structures/matrix/index.ts +0 -2
  51. package/src/data-structures/matrix/matrix.ts +442 -13
  52. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -10
  53. package/src/data-structures/queue/deque.ts +18 -39
  54. package/src/data-structures/queue/queue.ts +1 -1
  55. package/src/interfaces/binary-tree.ts +9 -4
  56. package/src/types/common.ts +5 -5
  57. package/src/types/data-structures/base/base.ts +14 -3
  58. package/src/types/data-structures/base/index.ts +1 -1
  59. package/src/types/data-structures/graph/abstract-graph.ts +4 -2
  60. package/src/types/data-structures/hash/hash-map.ts +3 -3
  61. package/src/types/data-structures/heap/heap.ts +2 -2
  62. package/src/types/data-structures/priority-queue/priority-queue.ts +2 -2
  63. package/src/utils/utils.ts +7 -1
  64. package/dist/data-structures/matrix/matrix2d.d.ts +0 -107
  65. package/dist/data-structures/matrix/matrix2d.js +0 -199
  66. package/dist/data-structures/matrix/vector2d.d.ts +0 -200
  67. package/dist/data-structures/matrix/vector2d.js +0 -290
  68. package/src/data-structures/matrix/matrix2d.ts +0 -211
  69. package/src/data-structures/matrix/vector2d.ts +0 -315
@@ -10,9 +10,8 @@ import {
10
10
  BinaryTreeDeleteResult,
11
11
  BSTNKeyOrNode,
12
12
  BTNCallback,
13
- BTNExemplar,
14
- BTNKeyOrNode,
15
13
  IterationType,
14
+ KeyOrNodeOrEntry,
16
15
  RBTNColor,
17
16
  RBTreeOptions,
18
17
  RedBlackTreeNested,
@@ -21,10 +20,11 @@ import {
21
20
  import { BST, BSTNode } from './bst';
22
21
  import { IBinaryTree } from '../../interfaces';
23
22
 
24
- export class RedBlackTreeNode<K = any, V = any, N extends RedBlackTreeNode<K, V, N> = RedBlackTreeNodeNested<K, V>> extends BSTNode<
25
- K, V,
26
- N
27
- > {
23
+ export class RedBlackTreeNode<
24
+ K = any,
25
+ V = any,
26
+ N extends RedBlackTreeNode<K, V, N> = RedBlackTreeNodeNested<K, V>
27
+ > extends BSTNode<K, V, N> {
28
28
  color: RBTNColor;
29
29
 
30
30
  constructor(key: K, value?: V, color: RBTNColor = RBTNColor.BLACK) {
@@ -40,27 +40,32 @@ export class RedBlackTreeNode<K = any, V = any, N extends RedBlackTreeNode<K, V,
40
40
  * 4. Red nodes must have black children.
41
41
  * 5. Black balance: Every path from any node to each of its leaf nodes contains the same number of black nodes.
42
42
  */
43
- export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>, TREE extends RedBlackTree<K, V, N, TREE> = RedBlackTree<K, V, N, RedBlackTreeNested<K, V, N>>>
43
+ export class RedBlackTree<
44
+ K = any,
45
+ V = any,
46
+ N extends RedBlackTreeNode<K, V, N> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>,
47
+ TREE extends RedBlackTree<K, V, N, TREE> = RedBlackTree<K, V, N, RedBlackTreeNested<K, V, N>>
48
+ >
44
49
  extends BST<K, V, N, TREE>
45
50
  implements IBinaryTree<K, V, N, TREE> {
46
51
  Sentinel: N = new RedBlackTreeNode<K, V>(NaN as K) as unknown as N;
47
52
 
48
53
  /**
49
54
  * This is the constructor function for a Red-Black Tree data structure in TypeScript, which
50
- * initializes the tree with optional elements and options.
51
- * @param [elements] - The `elements` parameter is an optional iterable of `BTNExemplar<K, V, N>`
52
- * objects. It represents the initial elements that will be added to the RBTree during its
55
+ * initializes the tree with optional nodes and options.
56
+ * @param [nodes] - The `nodes` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>`
57
+ * objects. It represents the initial nodes that will be added to the RBTree during its
53
58
  * construction. If this parameter is provided, the `addMany` method is called to add all the
54
- * elements to the
59
+ * nodes to the
55
60
  * @param [options] - The `options` parameter is an optional object that allows you to customize the
56
61
  * behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide
57
62
  * only a subset of the properties defined in the `RBTreeOptions` interface.
58
63
  */
59
- constructor(elements?: Iterable<BTNExemplar<K, V, N>>, options?: Partial<RBTreeOptions<K>>) {
64
+ constructor(nodes?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: Partial<RBTreeOptions<K>>) {
60
65
  super([], options);
61
66
 
62
67
  this._root = this.Sentinel;
63
- if (elements) super.addMany(elements);
68
+ if (nodes) super.addMany(nodes);
64
69
  }
65
70
 
66
71
  protected _root: N;
@@ -101,67 +106,79 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
101
106
  override createTree(options?: RBTreeOptions<K>): TREE {
102
107
  return new RedBlackTree<K, V, N, TREE>([], {
103
108
  iterationType: this.iterationType,
104
- variant: this.variant, ...options
109
+ variant: this.variant,
110
+ ...options
105
111
  }) as TREE;
106
112
  }
107
113
 
108
114
  /**
109
- * The function checks if an exemplar is an instance of the RedBlackTreeNode class.
110
- * @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`.
111
- * @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode
112
- * class.
113
- */
114
- override isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N {
115
- return exemplar instanceof RedBlackTreeNode;
116
- }
117
-
118
- /**
119
- * The function "isNotNodeInstance" checks if a potential key is a K.
120
- * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
121
- * data type.
122
- * @returns a boolean value indicating whether the potentialKey is of type number or not.
123
- */
124
- override isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K {
125
- return !(potentialKey instanceof RedBlackTreeNode)
126
- }
127
-
128
- /**
129
- * The function `exemplarToNode` takes an exemplar and converts it into a node object if possible.
130
- * @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, where:
115
+ * The function `exemplarToNode` takes an keyOrNodeOrEntry and converts it into a node object if possible.
116
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, where:
131
117
  * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
132
- * `exemplarToNode` function. It represents the value associated with the exemplar node. If a value
118
+ * `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If a value
133
119
  * is provided, it will be used when creating the new node. If no value is provided, the new node
134
120
  * @returns a node of type N or undefined.
135
121
  */
136
- override exemplarToNode(exemplar: BTNExemplar<K, V, N>, value?: V): N | undefined {
122
+ override exemplarToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): N | undefined {
137
123
  let node: N | undefined;
138
124
 
139
- if (exemplar === null || exemplar === undefined) {
125
+ if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
140
126
  return;
141
- } else if (this.isNode(exemplar)) {
142
- node = exemplar;
143
- } else if (this.isEntry(exemplar)) {
144
- const [key, value] = exemplar;
127
+ } else if (this.isNode(keyOrNodeOrEntry)) {
128
+ node = keyOrNodeOrEntry;
129
+ } else if (this.isEntry(keyOrNodeOrEntry)) {
130
+ const [key, value] = keyOrNodeOrEntry;
145
131
  if (key === undefined || key === null) {
146
132
  return;
147
133
  } else {
148
134
  node = this.createNode(key, value, RBTNColor.RED);
149
135
  }
150
- } else if (this.isNotNodeInstance(exemplar)) {
151
- node = this.createNode(exemplar, value, RBTNColor.RED);
136
+ } else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
137
+ node = this.createNode(keyOrNodeOrEntry, value, RBTNColor.RED);
152
138
  } else {
153
139
  return;
154
140
  }
155
141
  return node;
156
142
  }
157
143
 
144
+ /**
145
+ * The function checks if an keyOrNodeOrEntry is an instance of the RedBlackTreeNode class.
146
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
147
+ * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the RedBlackTreeNode
148
+ * class.
149
+ */
150
+ override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N {
151
+ return keyOrNodeOrEntry instanceof RedBlackTreeNode;
152
+ }
153
+
158
154
  /**
159
155
  * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
160
156
  * Space Complexity: O(1)
161
157
  */
162
158
 
159
+ override isRealNode(node: N | undefined): node is N {
160
+ if (node === this.Sentinel || node === undefined) return false;
161
+ return node instanceof RedBlackTreeNode;
162
+ }
163
+
163
164
  /**
164
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
165
+ * The function "isNotNodeInstance" checks if a potential key is a K.
166
+ * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
167
+ * data type.
168
+ * @returns a boolean value indicating whether the potentialKey is of type number or not.
169
+ */
170
+ override isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K {
171
+ return !(potentialKey instanceof RedBlackTreeNode);
172
+ }
173
+
174
+ /**
175
+ * Time Complexity: O(log n)
176
+ * Space Complexity: O(1)
177
+ * on average (where n is the number of nodes in the tree)
178
+ */
179
+
180
+ /**
181
+ * Time Complexity: O(log n)
165
182
  * Space Complexity: O(1)
166
183
  *
167
184
  * The `add` function adds a new node to a binary search tree and performs necessary rotations and
@@ -172,9 +189,9 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
172
189
  * being added to the binary search tree.
173
190
  * @returns The method `add` returns either the newly added node (`N`) or `undefined`.
174
191
  */
175
- override add(keyOrNodeOrEntry: BTNExemplar<K, V, N>, value?: V): N | undefined {
192
+ override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): boolean {
176
193
  const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
177
- if (newNode === undefined) return;
194
+ if (newNode === undefined) return false;
178
195
 
179
196
  newNode.left = this.Sentinel;
180
197
  newNode.right = this.Sentinel;
@@ -191,12 +208,11 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
191
208
  x = x?.right;
192
209
  } else {
193
210
  if (newNode !== x) {
194
- this._replaceNode(x, newNode)
211
+ this._replaceNode(x, newNode);
195
212
  }
196
- return;
213
+ return false;
197
214
  }
198
215
  }
199
-
200
216
  }
201
217
 
202
218
  newNode.parent = y;
@@ -211,25 +227,27 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
211
227
  if (newNode.parent === undefined) {
212
228
  newNode.color = RBTNColor.BLACK;
213
229
  this._size++;
214
- return;
230
+ return false;
215
231
  }
216
232
 
217
233
  if (newNode.parent.parent === undefined) {
218
234
  this._size++;
219
- return;
235
+ return false;
220
236
  }
221
237
 
222
238
  this._fixInsert(newNode);
223
239
  this._size++;
240
+ return true;
224
241
  }
225
242
 
226
243
  /**
227
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
244
+ * Time Complexity: O(log n)
228
245
  * Space Complexity: O(1)
246
+ * on average (where n is the number of nodes in the tree)
229
247
  */
230
248
 
231
249
  /**
232
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
250
+ * Time Complexity: O(log n)
233
251
  * Space Complexity: O(1)
234
252
  *
235
253
  * The `delete` function removes a node from a binary tree based on a given identifier and updates
@@ -304,16 +322,6 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
304
322
  return ans;
305
323
  }
306
324
 
307
- /**
308
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
309
- * Space Complexity: O(1)
310
- */
311
-
312
- override isRealNode(node: N | undefined): node is N {
313
- if (node === this.Sentinel || node === undefined) return false;
314
- return node instanceof RedBlackTreeNode;
315
- }
316
-
317
325
  getNode<C extends BTNCallback<N, K>>(
318
326
  identifier: K,
319
327
  callback?: C,
@@ -336,12 +344,12 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
336
344
  ): N | undefined;
337
345
 
338
346
  /**
339
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
347
+ * Time Complexity: O(log n)
340
348
  * Space Complexity: O(1)
341
349
  */
342
350
 
343
351
  /**
344
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
352
+ * Time Complexity: O(log n)
345
353
  * Space Complexity: O(1)
346
354
  *
347
355
  * The function `getNode` retrieves a single node from a binary tree based on a given identifier and
@@ -401,7 +409,7 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
401
409
  }
402
410
 
403
411
  /**
404
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
412
+ * Time Complexity: O(1)
405
413
  * Space Complexity: O(1)
406
414
  */
407
415
 
@@ -483,12 +491,12 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
483
491
  }
484
492
 
485
493
  /**
486
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
494
+ * Time Complexity: O(log n)
487
495
  * Space Complexity: O(1)
488
496
  */
489
497
 
490
498
  /**
491
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
499
+ * Time Complexity: O(log n)
492
500
  * Space Complexity: O(1)
493
501
  *
494
502
  * The function `_fixDelete` is used to fix the red-black tree after a node deletion.
@@ -579,12 +587,12 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
579
587
  }
580
588
 
581
589
  /**
582
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
590
+ * Time Complexity: O(log n)
583
591
  * Space Complexity: O(1)
584
592
  */
585
593
 
586
594
  /**
587
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
595
+ * Time Complexity: O(log n)
588
596
  * Space Complexity: O(1)
589
597
  *
590
598
  * The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
@@ -649,6 +657,6 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
649
657
  protected _replaceNode(oldNode: N, newNode: N): N {
650
658
  newNode.color = oldNode.color;
651
659
 
652
- return super._replaceNode(oldNode, newNode)
660
+ return super._replaceNode(oldNode, newNode);
653
661
  }
654
662
  }
@@ -9,8 +9,7 @@ import type {
9
9
  BinaryTreeDeleteResult,
10
10
  BSTNKeyOrNode,
11
11
  BTNCallback,
12
- BTNExemplar,
13
- BTNKeyOrNode,
12
+ KeyOrNodeOrEntry,
14
13
  TreeMultimapNested,
15
14
  TreeMultimapNodeNested,
16
15
  TreeMultimapOptions
@@ -45,14 +44,17 @@ export class TreeMultimapNode<
45
44
  /**
46
45
  * The only distinction between a TreeMultimap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
47
46
  */
48
- export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N> = TreeMultimapNode<K, V, TreeMultimapNodeNested<K, V>>,
49
- TREE extends TreeMultimap<K, V, N, TREE> = TreeMultimap<K, V, N, TreeMultimapNested<K, V, N>>>
47
+ export class TreeMultimap<
48
+ K = any,
49
+ V = any,
50
+ N extends TreeMultimapNode<K, V, N> = TreeMultimapNode<K, V, TreeMultimapNodeNested<K, V>>,
51
+ TREE extends TreeMultimap<K, V, N, TREE> = TreeMultimap<K, V, N, TreeMultimapNested<K, V, N>>
52
+ >
50
53
  extends AVLTree<K, V, N, TREE>
51
54
  implements IBinaryTree<K, V, N, TREE> {
52
-
53
- constructor(elements?: Iterable<BTNExemplar<K, V, N>>, options?: Partial<TreeMultimapOptions<K>>) {
55
+ constructor(nodes?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: Partial<TreeMultimapOptions<K>>) {
54
56
  super([], options);
55
- if (elements) this.addMany(elements);
57
+ if (nodes) this.addMany(nodes);
56
58
  }
57
59
 
58
60
  private _count = 0;
@@ -60,7 +62,7 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
60
62
  // TODO the _count is not accurate after nodes count modified
61
63
  get count(): number {
62
64
  let sum = 0;
63
- this.subTreeTraverse(node => sum += node.count);
65
+ this.subTreeTraverse(node => (sum += node.count));
64
66
  return sum;
65
67
  }
66
68
 
@@ -80,33 +82,14 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
80
82
  override createTree(options?: TreeMultimapOptions<K>): TREE {
81
83
  return new TreeMultimap<K, V, N, TREE>([], {
82
84
  iterationType: this.iterationType,
83
- variant: this.variant, ...options
85
+ variant: this.variant,
86
+ ...options
84
87
  }) as TREE;
85
88
  }
86
89
 
87
90
  /**
88
- * The function checks if an exemplar is an instance of the TreeMultimapNode class.
89
- * @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`.
90
- * @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
91
- * class.
92
- */
93
- override isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N {
94
- return exemplar instanceof TreeMultimapNode;
95
- }
96
-
97
- /**
98
- * The function "isNotNodeInstance" checks if a potential key is a K.
99
- * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
100
- * data type.
101
- * @returns a boolean value indicating whether the potentialKey is of type number or not.
102
- */
103
- override isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K {
104
- return !(potentialKey instanceof TreeMultimapNode)
105
- }
106
-
107
- /**
108
- * The function `exemplarToNode` converts an exemplar object into a node object.
109
- * @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, which means it
91
+ * The function `exemplarToNode` converts an keyOrNodeOrEntry object into a node object.
92
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, which means it
110
93
  * can be one of the following:
111
94
  * @param {V} [value] - The `value` parameter is an optional argument that represents the value
112
95
  * associated with the node. It is of type `V`, which can be any data type. If no value is provided,
@@ -115,21 +98,21 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
115
98
  * times the value should be added to the node. If not provided, it defaults to 1.
116
99
  * @returns a node of type `N` or `undefined`.
117
100
  */
118
- override exemplarToNode(exemplar: BTNExemplar<K, V, N>, value?: V, count = 1): N | undefined {
101
+ override exemplarToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V, count = 1): N | undefined {
119
102
  let node: N | undefined;
120
- if (exemplar === undefined || exemplar === null) {
103
+ if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) {
121
104
  return;
122
- } else if (this.isNode(exemplar)) {
123
- node = exemplar;
124
- } else if (this.isEntry(exemplar)) {
125
- const [key, value] = exemplar;
105
+ } else if (this.isNode(keyOrNodeOrEntry)) {
106
+ node = keyOrNodeOrEntry;
107
+ } else if (this.isEntry(keyOrNodeOrEntry)) {
108
+ const [key, value] = keyOrNodeOrEntry;
126
109
  if (key === undefined || key === null) {
127
110
  return;
128
111
  } else {
129
112
  node = this.createNode(key, value, count);
130
113
  }
131
- } else if (this.isNotNodeInstance(exemplar)) {
132
- node = this.createNode(exemplar, value, count);
114
+ } else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
115
+ node = this.createNode(keyOrNodeOrEntry, value, count);
133
116
  } else {
134
117
  return;
135
118
  }
@@ -137,13 +120,34 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
137
120
  }
138
121
 
139
122
  /**
140
- * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
141
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
123
+ * The function checks if an keyOrNodeOrEntry is an instance of the TreeMultimapNode class.
124
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
125
+ * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the TreeMultimapNode
126
+ * class.
127
+ */
128
+ override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N {
129
+ return keyOrNodeOrEntry instanceof TreeMultimapNode;
130
+ }
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
+ /**
143
+ * Time Complexity: O(log n)
144
+ * Space Complexity: O(1)
145
+ * logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
142
146
  */
143
147
 
144
148
  /**
145
- * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
146
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
149
+ * Time Complexity: O(log n)
150
+ * Space Complexity: O(1)
147
151
  *
148
152
  * The function overrides the add method of a binary tree node and adds a new node to the tree.
149
153
  * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
@@ -157,26 +161,27 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
157
161
  * @returns The method is returning either the newly inserted node or `undefined` if the insertion
158
162
  * was not successful.
159
163
  */
160
- override add(keyOrNodeOrEntry: BTNExemplar<K, V, N>, value?: V, count = 1): N | undefined {
164
+ override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V, count = 1): boolean {
161
165
  const newNode = this.exemplarToNode(keyOrNodeOrEntry, value, count);
162
- if (newNode === undefined) return;
166
+ if (newNode === undefined) return false;
163
167
 
164
168
  const orgNodeCount = newNode?.count || 0;
165
169
  const inserted = super.add(newNode);
166
170
  if (inserted) {
167
171
  this._count += orgNodeCount;
168
172
  }
169
- return inserted;
173
+ return true;
170
174
  }
171
175
 
172
176
  /**
173
- * Time Complexity: O(k log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
174
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
177
+ * Time Complexity: O(k log n)
178
+ * Space Complexity: O(1)
179
+ * logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
175
180
  */
176
181
 
177
182
  /**
178
- * Time Complexity: O(k log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
179
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
183
+ * Time Complexity: O(k log n)
184
+ * Space Complexity: O(1)
180
185
  *
181
186
  * The function overrides the addMany method to add multiple keys, nodes, or entries to a data
182
187
  * structure.
@@ -184,18 +189,19 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
184
189
  * either keys, nodes, or entries.
185
190
  * @returns The method is returning an array of type `N | undefined`.
186
191
  */
187
- override addMany(keysOrNodesOrEntries: Iterable<BTNExemplar<K, V, N>>): (N | undefined)[] {
192
+ override addMany(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>>): boolean[] {
188
193
  return super.addMany(keysOrNodesOrEntries);
189
194
  }
190
195
 
191
196
  /**
192
- * Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
193
- * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
197
+ * Time Complexity: O(n log n)
198
+ * Space Complexity: O(n)
199
+ * logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node. linear space, as it creates an array to store the sorted nodes.
194
200
  */
195
201
 
196
202
  /**
197
- * Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node.
198
- * Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
203
+ * Time Complexity: O(n log n)
204
+ * Space Complexity: O(n)
199
205
  *
200
206
  * The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
201
207
  * tree using either a recursive or iterative approach.
@@ -243,13 +249,14 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
243
249
  }
244
250
 
245
251
  /**
246
- * Time Complexity: O(k log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each.
247
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
252
+ * Time Complexity: O(k log n)
253
+ * Space Complexity: O(1)
254
+ * logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each. constant space, as it doesn't use additional data structures that scale with input size.
248
255
  */
249
256
 
250
257
  /**
251
- * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (AVLTree) has logarithmic time complexity.
252
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
258
+ * Time Complexity: O(k log n)
259
+ * Space Complexity: O(1)
253
260
  *
254
261
  * The `delete` function in TypeScript is used to remove a node from a binary tree, taking into
255
262
  * account the count of the node and balancing the tree if necessary.
@@ -327,11 +334,14 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
327
334
  }
328
335
 
329
336
  /**
330
- * Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node.
331
- * Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
337
+ * Time Complexity: O(1)
338
+ * Space Complexity: O(1)
332
339
  */
333
340
 
334
341
  /**
342
+ * Time Complexity: O(1)
343
+ * Space Complexity: O(1)
344
+ *
335
345
  * The clear() function clears the contents of a data structure and sets the count to zero.
336
346
  */
337
347
  override clear() {
@@ -357,47 +367,6 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
357
367
  return cloned;
358
368
  }
359
369
 
360
- /**
361
- * Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
362
- * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
363
- *
364
- * The function adds a new node to a binary tree, either as the left child or the right child of a
365
- * given parent node.
366
- * @param {N | undefined} newNode - The `newNode` parameter represents the node that needs to be
367
- * added to the binary tree. It can be of type `N` (which represents a node in the binary tree) or
368
- * `undefined` if there is no node to add.
369
- * @param {K | N | undefined} parent - The `parent` parameter represents the parent node to
370
- * which the new node will be added as a child. It can be either a node object (`N`) or a key value
371
- * (`K`).
372
- * @returns The method `_addTo` returns either the `parent.left` or `parent.right` node that was
373
- * added, or `undefined` if no node was added.
374
- */
375
- protected override _addTo(newNode: N | undefined, parent: BSTNKeyOrNode<K, N>): N | undefined {
376
- parent = this.ensureNode(parent);
377
- if (parent) {
378
- if (parent.left === undefined) {
379
- parent.left = newNode;
380
- if (newNode !== undefined) {
381
- this._size = this.size + 1;
382
- this._count += newNode.count;
383
- }
384
-
385
- return parent.left;
386
- } else if (parent.right === undefined) {
387
- parent.right = newNode;
388
- if (newNode !== undefined) {
389
- this._size = this.size + 1;
390
- this._count += newNode.count;
391
- }
392
- return parent.right;
393
- } else {
394
- return;
395
- }
396
- } else {
397
- return;
398
- }
399
- }
400
-
401
370
  /**
402
371
  * The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
403
372
  * @param {K | N | undefined} srcNode - The `srcNode` parameter represents the source node from
@@ -433,7 +402,7 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
433
402
  }
434
403
 
435
404
  protected _replaceNode(oldNode: N, newNode: N): N {
436
- newNode.count = oldNode.count + newNode.count
405
+ newNode.count = oldNode.count + newNode.count;
437
406
  return super._replaceNode(oldNode, newNode);
438
407
  }
439
408
  }