min-heap-typed 1.48.4 → 1.48.5

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 (38) hide show
  1. package/dist/data-structures/base/iterable-base.d.ts +6 -6
  2. package/dist/data-structures/base/iterable-base.js +3 -3
  3. package/dist/data-structures/binary-tree/avl-tree.d.ts +5 -3
  4. package/dist/data-structures/binary-tree/avl-tree.js +6 -4
  5. package/dist/data-structures/binary-tree/binary-tree.d.ts +18 -15
  6. package/dist/data-structures/binary-tree/binary-tree.js +16 -13
  7. package/dist/data-structures/binary-tree/bst.d.ts +15 -11
  8. package/dist/data-structures/binary-tree/bst.js +17 -13
  9. package/dist/data-structures/binary-tree/rb-tree.d.ts +19 -13
  10. package/dist/data-structures/binary-tree/rb-tree.js +20 -14
  11. package/dist/data-structures/binary-tree/tree-multimap.d.ts +21 -14
  12. package/dist/data-structures/binary-tree/tree-multimap.js +25 -18
  13. package/dist/data-structures/graph/abstract-graph.d.ts +52 -52
  14. package/dist/data-structures/graph/abstract-graph.js +78 -78
  15. package/dist/data-structures/graph/directed-graph.d.ts +47 -47
  16. package/dist/data-structures/graph/directed-graph.js +56 -56
  17. package/dist/data-structures/graph/map-graph.d.ts +5 -5
  18. package/dist/data-structures/graph/map-graph.js +8 -8
  19. package/dist/data-structures/graph/undirected-graph.d.ts +29 -29
  20. package/dist/data-structures/graph/undirected-graph.js +57 -57
  21. package/dist/data-structures/hash/hash-map.d.ts +8 -8
  22. package/dist/data-structures/hash/hash-map.js +2 -2
  23. package/dist/interfaces/binary-tree.d.ts +1 -1
  24. package/dist/types/data-structures/base/base.d.ts +3 -3
  25. package/package.json +2 -2
  26. package/src/data-structures/base/iterable-base.ts +6 -6
  27. package/src/data-structures/binary-tree/avl-tree.ts +8 -5
  28. package/src/data-structures/binary-tree/binary-tree.ts +23 -19
  29. package/src/data-structures/binary-tree/bst.ts +19 -14
  30. package/src/data-structures/binary-tree/rb-tree.ts +20 -14
  31. package/src/data-structures/binary-tree/tree-multimap.ts +27 -19
  32. package/src/data-structures/graph/abstract-graph.ts +82 -82
  33. package/src/data-structures/graph/directed-graph.ts +56 -56
  34. package/src/data-structures/graph/map-graph.ts +8 -8
  35. package/src/data-structures/graph/undirected-graph.ts +59 -59
  36. package/src/data-structures/hash/hash-map.ts +8 -8
  37. package/src/interfaces/binary-tree.ts +1 -1
  38. package/src/types/data-structures/base/base.ts +3 -3
@@ -95,19 +95,22 @@ export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeN
95
95
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
96
96
  */
97
97
 
98
+
98
99
  /**
99
100
  * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
100
101
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
101
- *
102
+ *
102
103
  * The function overrides the add method of a binary tree node and balances the tree after inserting
103
104
  * a new node.
104
- * @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be either a key, a node, or an
105
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
105
106
  * entry.
106
- * @returns The method is returning either the inserted node or `undefined`.
107
+ * @param {V} [value] - The `value` parameter represents the value associated with the key that is
108
+ * being added to the binary tree.
109
+ * @returns The method is returning either the inserted node or undefined.
107
110
  */
108
- override add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>): N | undefined {
111
+ override add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V): N | undefined {
109
112
  if (keyOrNodeOrEntry === null) return undefined;
110
- const inserted = super.add(keyOrNodeOrEntry);
113
+ const inserted = super.add(keyOrNodeOrEntry, value);
111
114
  if (inserted) this._balancePath(inserted);
112
115
  return inserted;
113
116
  }
@@ -19,15 +19,15 @@ import {
19
19
  BinaryTreePrintOptions,
20
20
  BiTreeDeleteResult,
21
21
  DFSOrderPattern,
22
+ EntryCallback,
22
23
  FamilyPosition,
23
24
  IterationType,
24
- NodeDisplayLayout,
25
- PairCallback
25
+ NodeDisplayLayout
26
26
  } from '../../types';
27
27
  import { IBinaryTree } from '../../interfaces';
28
28
  import { trampoline } from '../../utils';
29
29
  import { Queue } from '../queue';
30
- import { IterablePairBase } from "../base";
30
+ import { IterableEntryBase } from "../base";
31
31
 
32
32
  /**
33
33
  * Represents a node in a binary tree.
@@ -104,7 +104,7 @@ export class BinaryTreeNode<K = any, V = any, N extends BinaryTreeNode<K, V, N>
104
104
  * 9. Complete Trees: All levels are fully filled except possibly the last, filled from left to right.
105
105
  */
106
106
 
107
- export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>, TREE extends BinaryTree<K, V, N, TREE> = BinaryTree<K, V, N, BinaryTreeNested<K, V, N>>> extends IterablePairBase<K, V | undefined>
107
+ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>, TREE extends BinaryTree<K, V, N, TREE> = BinaryTree<K, V, N, BinaryTreeNested<K, V, N>>> extends IterableEntryBase<K, V | undefined>
108
108
 
109
109
  implements IBinaryTree<K, V, N, TREE> {
110
110
  iterationType = IterationType.ITERATIVE
@@ -184,13 +184,14 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
184
184
  }
185
185
 
186
186
  /**
187
- * The function `exemplarToNode` converts an exemplar of a binary tree node into an actual node
188
- * object.
189
- * @param exemplar - BTNodeExemplar<K, V,N> - A generic type representing the exemplar parameter of the
190
- * function. It can be any type.
191
- * @returns a value of type `N` (which represents a node), or `null`, or `undefined`.
187
+ * The function `exemplarToNode` converts an exemplar object into a node object.
188
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
189
+ * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
190
+ * `exemplarToNode` function. It represents the value associated with the exemplar node. If no value
191
+ * is provided, it will be `undefined`.
192
+ * @returns a value of type N (node), or null, or undefined.
192
193
  */
193
- exemplarToNode(exemplar: BTNodeExemplar<K, V, N>): N | null | undefined {
194
+ exemplarToNode(exemplar: BTNodeExemplar<K, V, N>, value?: V): N | null | undefined {
194
195
  if (exemplar === undefined) return;
195
196
 
196
197
  let node: N | null | undefined;
@@ -208,7 +209,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
208
209
  } else if (this.isNode(exemplar)) {
209
210
  node = exemplar;
210
211
  } else if (this.isNotNodeInstance(exemplar)) {
211
- node = this.createNode(exemplar);
212
+ node = this.createNode(exemplar, value);
212
213
  } else {
213
214
  return;
214
215
  }
@@ -230,18 +231,21 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
230
231
  * Space Complexity O(1)
231
232
  */
232
233
 
234
+
233
235
  /**
234
236
  * Time Complexity O(log n) - O(n)
235
237
  * Space Complexity O(1)
236
- *
237
- * The `add` function adds a new node to a binary tree, either by key or by providing a node object.
238
- * @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be one of the following:
239
- * @returns The function `add` returns the inserted node (`N`), `null`, or `undefined`.
238
+ *
239
+ * The `add` function adds a new node to a binary tree, either by creating a new node or replacing an
240
+ * existing node with the same key.
241
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
242
+ * @param {V} [value] - The value to be inserted into the binary tree.
243
+ * @returns The function `add` returns either a node (`N`), `null`, or `undefined`.
240
244
  */
241
- add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>): N | null | undefined {
245
+ add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V): N | null | undefined {
242
246
 
243
247
  let inserted: N | null | undefined;
244
- const newNode = this.exemplarToNode(keyOrNodeOrEntry);
248
+ const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
245
249
  if (newNode === undefined) return;
246
250
 
247
251
  const _bfs = (root: N, newNode: N | null): N | undefined | null => {
@@ -1775,7 +1779,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
1775
1779
  * @returns The `filter` method is returning a new tree object that contains the key-value pairs that
1776
1780
  * pass the given predicate function.
1777
1781
  */
1778
- filter(predicate: PairCallback<K, V | undefined, boolean>, thisArg?: any) {
1782
+ filter(predicate: EntryCallback<K, V | undefined, boolean>, thisArg?: any) {
1779
1783
  const newTree = this.createTree();
1780
1784
  let index = 0;
1781
1785
  for (const [key, value] of this) {
@@ -1806,7 +1810,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
1806
1810
  * will be used as the `this` value when the callback function is called. If you don't pass a value
1807
1811
  * @returns The `map` method is returning a new tree object.
1808
1812
  */
1809
- map(callback: PairCallback<K, V | undefined, V>, thisArg?: any) {
1813
+ map(callback: EntryCallback<K, V | undefined, V>, thisArg?: any) {
1810
1814
  const newTree = this.createTree();
1811
1815
  let index = 0;
1812
1816
  for (const [key, value] of this) {
@@ -154,13 +154,16 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
154
154
  return exemplar instanceof BSTNode;
155
155
  }
156
156
 
157
+
157
158
  /**
158
- * The function `exemplarToNode` takes an exemplar and returns a corresponding node if the exemplar
159
- * is valid, otherwise it returns undefined.
160
- * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
161
- * @returns a variable `node` which is of type `N` or `undefined`.
159
+ * The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
160
+ * otherwise it returns undefined.
161
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, where:
162
+ * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
163
+ * `exemplarToNode` function. It represents the value associated with the exemplar node.
164
+ * @returns a node of type N or undefined.
162
165
  */
163
- override exemplarToNode(exemplar: BTNodeExemplar<K, V, N>): N | undefined {
166
+ override exemplarToNode(exemplar: BTNodeExemplar<K, V, N>, value?: V): N | undefined {
164
167
  let node: N | undefined;
165
168
  if (exemplar === null || exemplar === undefined) {
166
169
  return;
@@ -174,7 +177,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
174
177
  node = this.createNode(key, value);
175
178
  }
176
179
  } else if (this.isNotNodeInstance(exemplar)) {
177
- node = this.createNode(exemplar);
180
+ node = this.createNode(exemplar, value);
178
181
  } else {
179
182
  return;
180
183
  }
@@ -189,15 +192,17 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
189
192
  /**
190
193
  * Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
191
194
  * Space Complexity: O(1) - Constant space is used.
192
- *
193
- * The `add` function adds a new node to a binary search tree, either by key or by providing a node
194
- * object.
195
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
196
- * @returns The method returns either the newly added node (`newNode`) or `undefined` if the input
197
- * (`keyOrNodeOrEntry`) is null, undefined, or does not match any of the expected types.
195
+ *
196
+ * The `add` function adds a new node to a binary tree, updating the value if the key already exists
197
+ * or inserting a new node if the key is unique.
198
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can accept three types of values:
199
+ * @param {V} [value] - The `value` parameter represents the value associated with the key that is
200
+ * being added to the binary tree.
201
+ * @returns The method `add` returns either the newly added node (`newNode`) or `undefined` if the
202
+ * node was not added.
198
203
  */
199
- override add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>): N | undefined {
200
- const newNode = this.exemplarToNode(keyOrNodeOrEntry);
204
+ override add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V): N | undefined {
205
+ const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
201
206
  if (newNode === undefined) return;
202
207
 
203
208
  if (this.root === undefined) {
@@ -116,14 +116,14 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
116
116
  }
117
117
 
118
118
  /**
119
- * The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
120
- * otherwise it returns undefined.
121
- * @param exemplar - BTNodeExemplar<K, V, N> - A generic type representing an exemplar of a binary tree
122
- * node. It can be either a node itself, an entry (key-value pair), a node key, or any other value
123
- * that is not a valid exemplar.
124
- * @returns a variable `node` which is of type `N | undefined`.
119
+ * The function `exemplarToNode` takes an exemplar and converts it into a node object if possible.
120
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, where:
121
+ * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
122
+ * `exemplarToNode` function. It represents the value associated with the exemplar node. If a value
123
+ * is provided, it will be used when creating the new node. If no value is provided, the new node
124
+ * @returns a node of type N or undefined.
125
125
  */
126
- override exemplarToNode(exemplar: BTNodeExemplar<K, V, N>): N | undefined {
126
+ override exemplarToNode(exemplar: BTNodeExemplar<K, V, N>, value?: V): N | undefined {
127
127
  let node: N | undefined;
128
128
 
129
129
  if (exemplar === null || exemplar === undefined) {
@@ -138,7 +138,7 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
138
138
  node = this.createNode(key, value, RBTNColor.RED);
139
139
  }
140
140
  } else if (this.isNotNodeInstance(exemplar)) {
141
- node = this.createNode(exemplar, undefined, RBTNColor.RED);
141
+ node = this.createNode(exemplar, value, RBTNColor.RED);
142
142
  } else {
143
143
  return;
144
144
  }
@@ -151,13 +151,19 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
151
151
  */
152
152
 
153
153
  /**
154
- * The function adds a node to a Red-Black Tree data structure.
155
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
156
- * @returns The method `add` returns either an instance of `N` (the node that was added) or
157
- * `undefined`.
154
+ * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
155
+ * Space Complexity: O(1)
156
+ *
157
+ * The `add` function adds a new node to a binary search tree and performs necessary rotations and
158
+ * color changes to maintain the red-black tree properties.
159
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
160
+ * entry.
161
+ * @param {V} [value] - The `value` parameter represents the value associated with the key that is
162
+ * being added to the binary search tree.
163
+ * @returns The method `add` returns either the newly added node (`N`) or `undefined`.
158
164
  */
159
- override add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>): N | undefined {
160
- const newNode = this.exemplarToNode(keyOrNodeOrEntry);
165
+ override add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V): N | undefined {
166
+ const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
161
167
  if (newNode === undefined) return;
162
168
 
163
169
  newNode.left = this.Sentinel;
@@ -85,15 +85,19 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
85
85
  return exemplar instanceof TreeMultimapNode;
86
86
  }
87
87
 
88
+
88
89
  /**
89
90
  * The function `exemplarToNode` converts an exemplar object into a node object.
90
- * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, where `V` represents
91
- * the value type and `N` represents the node type.
91
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, which means it
92
+ * can be one of the following:
93
+ * @param {V} [value] - The `value` parameter is an optional argument that represents the value
94
+ * associated with the node. It is of type `V`, which can be any data type. If no value is provided,
95
+ * it defaults to `undefined`.
92
96
  * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
93
- * times the node should be created. If not provided, it defaults to 1.
94
- * @returns a value of type `N` (the generic type parameter) or `undefined`.
97
+ * times the value should be added to the node. If not provided, it defaults to 1.
98
+ * @returns a node of type `N` or `undefined`.
95
99
  */
96
- override exemplarToNode(exemplar: BTNodeExemplar<K, V, N>, count = 1): N | undefined {
100
+ override exemplarToNode(exemplar: BTNodeExemplar<K, V, N>, value?: V, count = 1): N | undefined {
97
101
  let node: N | undefined;
98
102
  if (exemplar === undefined || exemplar === null) {
99
103
  return;
@@ -107,7 +111,7 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
107
111
  node = this.createNode(key, value, count);
108
112
  }
109
113
  } else if (this.isNotNodeInstance(exemplar)) {
110
- node = this.createNode(exemplar, undefined, count);
114
+ node = this.createNode(exemplar, value, count);
111
115
  } else {
112
116
  return;
113
117
  }
@@ -122,17 +126,21 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
122
126
  /**
123
127
  * 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.
124
128
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
125
- *
126
- * The `add` function overrides the base class `add` function to add a new node to the tree multimap
127
- * and update the count.
128
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
129
- * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
130
- * times the key or node or entry should be added to the multimap. If not provided, the default value
131
- * is 1.
132
- * @returns either a node (`N`) or `undefined`.
129
+ *
130
+ * The function overrides the add method of a binary tree node and adds a new node to the tree.
131
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
132
+ * entry. It represents the key, node, or entry that you want to add to the binary tree.
133
+ * @param {V} [value] - The `value` parameter represents the value associated with the key in the
134
+ * binary tree node. It is an optional parameter, meaning it can be omitted when calling the `add`
135
+ * method.
136
+ * @param [count=1] - The `count` parameter represents the number of times the key-value pair should
137
+ * be added to the binary tree. By default, it is set to 1, meaning that the key-value pair will be
138
+ * added once. However, you can specify a different value for `count` if you want to add
139
+ * @returns The method is returning either the newly inserted node or `undefined` if the insertion
140
+ * was not successful.
133
141
  */
134
- override add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, count = 1): N | undefined {
135
- const newNode = this.exemplarToNode(keyOrNodeOrEntry, count);
142
+ override add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V, count = 1): N | undefined {
143
+ const newNode = this.exemplarToNode(keyOrNodeOrEntry, value, count);
136
144
  if (newNode === undefined) return;
137
145
 
138
146
  const orgNodeCount = newNode?.count || 0;
@@ -190,7 +198,7 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
190
198
  if (l > r) return;
191
199
  const m = l + Math.floor((r - l) / 2);
192
200
  const midNode = sorted[m];
193
- this.add([midNode.key, midNode.value], midNode.count);
201
+ this.add(midNode.key, midNode.value, midNode.count);
194
202
  buildBalanceBST(l, m - 1);
195
203
  buildBalanceBST(m + 1, r);
196
204
  };
@@ -206,7 +214,7 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
206
214
  if (l <= r) {
207
215
  const m = l + Math.floor((r - l) / 2);
208
216
  const midNode = sorted[m];
209
- this.add([midNode.key, midNode.value], midNode.count);
217
+ this.add(midNode.key, midNode.value, midNode.count);
210
218
  stack.push([m + 1, r]);
211
219
  stack.push([l, m - 1]);
212
220
  }
@@ -327,7 +335,7 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
327
335
  */
328
336
  override clone(): TREE {
329
337
  const cloned = this.createTree();
330
- this.bfs(node => cloned.add([node.key, node.value], node.count));
338
+ this.bfs(node => cloned.add(node.key, node.value, node.count));
331
339
  return cloned;
332
340
  }
333
341