min-heap-typed 1.39.2 → 1.39.4

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 (45) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +11 -11
  2. package/dist/data-structures/binary-tree/avl-tree.js +8 -6
  3. package/dist/data-structures/binary-tree/binary-tree.d.ts +50 -117
  4. package/dist/data-structures/binary-tree/binary-tree.js +43 -51
  5. package/dist/data-structures/binary-tree/bst.d.ts +23 -23
  6. package/dist/data-structures/binary-tree/bst.js +18 -18
  7. package/dist/data-structures/binary-tree/rb-tree.d.ts +3 -3
  8. package/dist/data-structures/binary-tree/tree-multiset.d.ts +15 -15
  9. package/dist/data-structures/binary-tree/tree-multiset.js +9 -9
  10. package/dist/interfaces/binary-tree.d.ts +4 -4
  11. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  12. package/dist/types/data-structures/binary-tree/bst.d.ts +2 -2
  13. package/dist/types/helpers.d.ts +1 -1
  14. package/package.json +2 -2
  15. package/src/data-structures/binary-tree/avl-tree.ts +16 -14
  16. package/src/data-structures/binary-tree/binary-indexed-tree.ts +1 -1
  17. package/src/data-structures/binary-tree/binary-tree.ts +145 -84
  18. package/src/data-structures/binary-tree/bst.ts +37 -38
  19. package/src/data-structures/binary-tree/rb-tree.ts +7 -6
  20. package/src/data-structures/binary-tree/tree-multiset.ts +18 -17
  21. package/src/data-structures/graph/abstract-graph.ts +11 -10
  22. package/src/data-structures/graph/directed-graph.ts +2 -1
  23. package/src/data-structures/graph/undirected-graph.ts +5 -4
  24. package/src/data-structures/hash/hash-map.ts +1 -1
  25. package/src/data-structures/hash/tree-map.ts +1 -2
  26. package/src/data-structures/hash/tree-set.ts +1 -2
  27. package/src/data-structures/heap/heap.ts +2 -2
  28. package/src/data-structures/heap/max-heap.ts +1 -1
  29. package/src/data-structures/heap/min-heap.ts +1 -1
  30. package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
  31. package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
  32. package/src/data-structures/matrix/matrix.ts +1 -1
  33. package/src/data-structures/matrix/vector2d.ts +1 -2
  34. package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
  35. package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
  36. package/src/data-structures/priority-queue/priority-queue.ts +1 -1
  37. package/src/data-structures/queue/deque.ts +4 -5
  38. package/src/data-structures/queue/queue.ts +1 -1
  39. package/src/interfaces/binary-tree.ts +4 -4
  40. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  41. package/src/types/data-structures/binary-tree/bst.ts +2 -2
  42. package/src/types/data-structures/matrix/navigator.ts +1 -1
  43. package/src/types/helpers.ts +1 -1
  44. package/src/types/utils/utils.ts +1 -1
  45. package/src/types/utils/validate-type.ts +2 -2
@@ -5,21 +5,22 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type {BinaryTreeNodeKey, BSTComparator, BSTNodeNested, BSTOptions, OneParamCallback} from '../../types';
8
+ import type {BTNKey, BSTComparator, BSTNodeNested, BSTOptions, BTNCallback} from '../../types';
9
9
  import {CP, IterationType} from '../../types';
10
10
  import {BinaryTree, BinaryTreeNode} from './binary-tree';
11
11
  import {IBinaryTree} from '../../interfaces';
12
12
  import {Queue} from '../queue';
13
13
 
14
14
  export class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extends BinaryTreeNode<V, N> {
15
- constructor(key: BinaryTreeNodeKey, val?: V) {
15
+ constructor(key: BTNKey, val?: V) {
16
16
  super(key, val);
17
17
  }
18
18
  }
19
19
 
20
20
  export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>>
21
21
  extends BinaryTree<V, N>
22
- implements IBinaryTree<V, N> {
22
+ implements IBinaryTree<V, N>
23
+ {
23
24
  /**
24
25
  * The constructor function initializes a binary search tree object with an optional comparator
25
26
  * function.
@@ -38,27 +39,27 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
38
39
 
39
40
  /**
40
41
  * The function creates a new binary search tree node with the given key and value.
41
- * @param {BinaryTreeNodeKey} key - The key parameter is the key value that will be associated with
42
+ * @param {BTNKey} key - The key parameter is the key value that will be associated with
42
43
  * the new node. It is used to determine the position of the node in the binary search tree.
43
44
  * @param [val] - The parameter `val` is an optional value that can be assigned to the node. It
44
45
  * represents the value associated with the node in a binary search tree.
45
46
  * @returns a new instance of the BSTNode class with the specified key and value.
46
47
  */
47
- override createNode(key: BinaryTreeNodeKey, val?: V): N {
48
+ override createNode(key: BTNKey, val?: V): N {
48
49
  return new BSTNode<V, N>(key, val) as N;
49
50
  }
50
51
 
51
52
  /**
52
53
  * The `add` function in a binary search tree class inserts a new node with a given key and value
53
54
  * into the tree.
54
- * @param {BinaryTreeNodeKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
55
- * `BinaryTreeNodeKey` (which can be a number or a string), a `BSTNode` object, or `null`.
55
+ * @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
56
+ * `BTNKey` (which can be a number or a string), a `BSTNode` object, or `null`.
56
57
  * @param [val] - The `val` parameter is the value to be assigned to the new node being added to the
57
58
  * binary search tree.
58
59
  * @returns the inserted node (N) if it was successfully added to the binary search tree. If the node
59
60
  * was not added or if the parameters were invalid, it returns null or undefined.
60
61
  */
61
- override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: V): N | null | undefined {
62
+ override add(keyOrNode: BTNKey | N | null, val?: V): N | null | undefined {
62
63
  // TODO support node as a parameter
63
64
  let inserted: N | null = null;
64
65
  let newNode: N | null = null;
@@ -127,9 +128,9 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
127
128
  /**
128
129
  * The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
129
130
  * maintaining balance.
130
- * @param {[BinaryTreeNodeKey | N, N['val']][]} keysOrNodes - The `arr` parameter in the `addMany` function
131
+ * @param {[BTNKey | N, N['val']][]} keysOrNodes - The `arr` parameter in the `addMany` function
131
132
  * represents an array of keys or nodes that need to be added to the binary search tree. It can be an
132
- * array of `BinaryTreeNodeKey` or `N` (which represents the node type in the binary search tree) or
133
+ * array of `BTNKey` or `N` (which represents the node type in the binary search tree) or
133
134
  * `null
134
135
  * @param {V[]} data - The values of tree nodes
135
136
  * @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
@@ -139,13 +140,13 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
139
140
  */
140
141
 
141
142
  override addMany(
142
- keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[],
143
+ keysOrNodes: (BTNKey | null)[] | (N | null)[],
143
144
  data?: V[],
144
145
  isBalanceAdd = true,
145
146
  iterationType = this.iterationType
146
147
  ): (N | null | undefined)[] {
147
148
  // TODO this addMany function is inefficient, it should be optimized
148
- function hasNoNull(arr: (BinaryTreeNodeKey | null)[] | (N | null)[]): arr is BinaryTreeNodeKey[] | N[] {
149
+ function hasNoNull(arr: (BTNKey | null)[] | (N | null)[]): arr is BTNKey[] | N[] {
149
150
  return arr.indexOf(null) === -1;
150
151
  }
151
152
 
@@ -153,17 +154,15 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
153
154
  return super.addMany(keysOrNodes, data);
154
155
  }
155
156
  const inserted: (N | null | undefined)[] = [];
156
- const combinedArr: [BinaryTreeNodeKey | N, N['val']][] = keysOrNodes.map((value, index) => [value, data?.[index]]);
157
+ const combinedArr: [BTNKey | N, N['val']][] = keysOrNodes.map((value, index) => [value, data?.[index]]);
157
158
  let sorted = [];
158
159
 
159
- function isNodeOrNullTuple(arr: [BinaryTreeNodeKey | N, N['val']][]): arr is [N, N['val']][] {
160
+ function isNodeOrNullTuple(arr: [BTNKey | N, N['val']][]): arr is [N, N['val']][] {
160
161
  for (const [keyOrNode] of arr) if (keyOrNode instanceof BSTNode) return true;
161
162
  return false;
162
163
  }
163
164
 
164
- function isBinaryTreeKeyOrNullTuple(
165
- arr: [BinaryTreeNodeKey | N, N['val']][]
166
- ): arr is [BinaryTreeNodeKey, N['val']][] {
165
+ function isBinaryTreeKeyOrNullTuple(arr: [BTNKey | N, N['val']][]): arr is [BTNKey, N['val']][] {
167
166
  for (const [keyOrNode] of arr) if (typeof keyOrNode === 'number') return true;
168
167
  return false;
169
168
  }
@@ -180,7 +179,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
180
179
  }
181
180
  sortedKeysOrNodes = sorted.map(([keyOrNode]) => keyOrNode);
182
181
  sortedData = sorted.map(([, val]) => val);
183
- const recursive = (arr: (BinaryTreeNodeKey | null | N)[], data?: (V | undefined)[]) => {
182
+ const recursive = (arr: (BTNKey | null | N)[], data?: (V | undefined)[]) => {
184
183
  if (arr.length === 0) return;
185
184
 
186
185
  const mid = Math.floor((arr.length - 1) / 2);
@@ -220,7 +219,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
220
219
  * callback.
221
220
  * @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter is used to specify the
222
221
  * property of the binary tree node that you want to search for. It can be either a specific key
223
- * value (`BinaryTreeNodeKey`) or a custom callback function (`OneParamCallback<N>`) that determines
222
+ * value (`BTNKey`) or a custom callback function (`BTNCallback<N>`) that determines
224
223
  * whether a node matches the desired property.
225
224
  * @param callback - The `callback` parameter is a function that is used to determine whether a node
226
225
  * matches the desired property. It takes a node as input and returns a boolean value indicating
@@ -233,9 +232,9 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
233
232
  * @returns either the first node that matches the given nodeProperty and callback, or null if no
234
233
  * matching node is found.
235
234
  */
236
- override get<C extends OneParamCallback<N>>(
235
+ override get<C extends BTNCallback<N>>(
237
236
  identifier: ReturnType<C> | null,
238
- callback: C = this._defaultCallbackByKey as C,
237
+ callback: C = ((node: N) => node.key) as C,
239
238
  beginRoot = this.root,
240
239
  iterationType = this.iterationType
241
240
  ): N | null {
@@ -257,7 +256,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
257
256
  * the key of the leftmost node if the comparison result is greater than, and the key of the
258
257
  * rightmost node otherwise. If no node is found, it returns 0.
259
258
  */
260
- lastKey(beginRoot: N | null = this.root, iterationType = this.iterationType): BinaryTreeNodeKey {
259
+ lastKey(beginRoot: N | null = this.root, iterationType = this.iterationType): BTNKey {
261
260
  if (this._compare(0, 1) === CP.lt) return this.getRightMost(beginRoot, iterationType)?.key ?? 0;
262
261
  else if (this._compare(0, 1) === CP.gt) return this.getLeftMost(beginRoot, iterationType)?.key ?? 0;
263
262
  else return this.getRightMost(beginRoot, iterationType)?.key ?? 0;
@@ -267,11 +266,11 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
267
266
  * The function `getNodes` retrieves nodes from a binary tree based on a given node property or key,
268
267
  * using either recursive or iterative traversal.
269
268
  * @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter represents the property
270
- * of the binary tree node that you want to search for. It can be either a `BinaryTreeNodeKey` or a
269
+ * of the binary tree node that you want to search for. It can be either a `BTNKey` or a
271
270
  * generic type `N`.
272
271
  * @param callback - The `callback` parameter is a function that takes a node as input and returns a
273
272
  * value. This value is compared with the `nodeProperty` parameter to determine if the node should be
274
- * included in the result. The default value for `callback` is `this._defaultCallbackByKey`, which is
273
+ * included in the result. The default value for `callback` is `((node: N) => node.key)`, which is
275
274
  * a
276
275
  * @param [onlyOne=false] - A boolean value indicating whether to stop the traversal after finding
277
276
  * the first node that matches the nodeProperty. If set to true, the function will return an array
@@ -284,9 +283,9 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
284
283
  * traverse the binary tree. It can have one of the following values:
285
284
  * @returns an array of nodes (N[]).
286
285
  */
287
- override getNodes<C extends OneParamCallback<N>>(
286
+ override getNodes<C extends BTNCallback<N>>(
288
287
  identifier: ReturnType<C> | null,
289
- callback: C = this._defaultCallbackByKey as C,
288
+ callback: C = ((node: N) => node.key) as C,
290
289
  onlyOne = false,
291
290
  beginRoot: N | null = this.root,
292
291
  iterationType = this.iterationType
@@ -304,7 +303,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
304
303
 
305
304
  if (!cur.left && !cur.right) return;
306
305
  // TODO potential bug
307
- if (callback === this._defaultCallbackByKey) {
306
+ if (callback === ((node: N) => node.key)) {
308
307
  if (this._compare(cur.key, identifier as number) === CP.gt) cur.left && _traverse(cur.left);
309
308
  if (this._compare(cur.key, identifier as number) === CP.lt) cur.right && _traverse(cur.right);
310
309
  } else {
@@ -325,7 +324,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
325
324
  if (onlyOne) return ans;
326
325
  }
327
326
  // TODO potential bug
328
- if (callback === this._defaultCallbackByKey) {
327
+ if (callback === ((node: N) => node.key)) {
329
328
  if (this._compare(cur.key, identifier as number) === CP.gt) cur.left && queue.push(cur.left);
330
329
  if (this._compare(cur.key, identifier as number) === CP.lt) cur.right && queue.push(cur.right);
331
330
  } else {
@@ -350,22 +349,22 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
350
349
  * @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
351
350
  * traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It can take one
352
351
  * of the following values:
353
- * @param {BinaryTreeNodeKey | N | null} targetNode - The `targetNode` parameter in the
352
+ * @param {BTNKey | N | null} targetNode - The `targetNode` parameter in the
354
353
  * `lesserOrGreaterTraverse` function is used to specify the node from which the traversal should
355
354
  * start. It can be either a reference to a specific node (`N`), the key of a node
356
- * (`BinaryTreeNodeKey`), or `null` to
355
+ * (`BTNKey`), or `null` to
357
356
  * @param iterationType - The `iterationType` parameter determines whether the traversal should be
358
357
  * done recursively or iteratively. It can have two possible values:
359
- * @returns The function `lesserOrGreaterTraverse` returns an array of `ReturnType<OneParamCallback<N>>`.
358
+ * @returns The function `lesserOrGreaterTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
360
359
  */
361
- lesserOrGreaterTraverse<C extends OneParamCallback<N>>(
362
- callback: C = this._defaultCallbackByKey as C,
360
+ lesserOrGreaterTraverse<C extends BTNCallback<N>>(
361
+ callback: C = ((node: N) => node.key) as C,
363
362
  lesserOrGreater: CP = CP.lt,
364
- targetNode: BinaryTreeNodeKey | N | null = this.root,
363
+ targetNode: BTNKey | N | null = this.root,
365
364
  iterationType = this.iterationType
366
365
  ): ReturnType<C>[] {
367
366
  if (typeof targetNode === 'number') targetNode = this.get(targetNode);
368
- const ans: ReturnType<OneParamCallback<N>>[] = [];
367
+ const ans: ReturnType<BTNCallback<N>>[] = [];
369
368
  if (!targetNode) return ans;
370
369
  const targetKey = targetNode.key;
371
370
  if (!this.root) return ans;
@@ -508,12 +507,12 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
508
507
  /**
509
508
  * The function compares two values using a comparator function and returns whether the first value
510
509
  * is greater than, less than, or equal to the second value.
511
- * @param {BinaryTreeNodeKey} a - The parameter "a" is of type BinaryTreeNodeKey.
512
- * @param {BinaryTreeNodeKey} b - The parameter "b" in the above code represents a BinaryTreeNodeKey.
510
+ * @param {BTNKey} a - The parameter "a" is of type BTNKey.
511
+ * @param {BTNKey} b - The parameter "b" in the above code represents a BTNKey.
513
512
  * @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater
514
513
  * than), CP.lt (less than), or CP.eq (equal).
515
514
  */
516
- protected _compare(a: BinaryTreeNodeKey, b: BinaryTreeNodeKey): CP {
515
+ protected _compare(a: BTNKey, b: BTNKey): CP {
517
516
  const compared = this._comparator(a, b);
518
517
  if (compared > 0) return CP.gt;
519
518
  else if (compared < 0) return CP.lt;
@@ -1,9 +1,9 @@
1
- import {BinaryTreeNodeKey, RBColor, RBTreeNodeNested, RBTreeOptions} from '../../types';
1
+ import {BTNKey, RBColor, RBTreeNodeNested, RBTreeOptions} from '../../types';
2
2
  import {IBinaryTree} from '../../interfaces';
3
3
  import {BST, BSTNode} from './bst';
4
4
 
5
5
  export class RBTreeNode<V = any, N extends RBTreeNode<V, N> = RBTreeNodeNested<V>> extends BSTNode<V, N> {
6
- constructor(key: BinaryTreeNodeKey, val?: V) {
6
+ constructor(key: BTNKey, val?: V) {
7
7
  super(key, val);
8
8
  this._color = RBColor.RED;
9
9
  }
@@ -21,16 +21,17 @@ export class RBTreeNode<V = any, N extends RBTreeNode<V, N> = RBTreeNodeNested<V
21
21
 
22
22
  export class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNested<V>>>
23
23
  extends BST<V, N>
24
- implements IBinaryTree<V, N> {
24
+ implements IBinaryTree<V, N>
25
+ {
25
26
  constructor(options?: RBTreeOptions) {
26
27
  super(options);
27
28
  }
28
29
 
29
- override createNode(key: BinaryTreeNodeKey, val?: V): N {
30
+ override createNode(key: BTNKey, val?: V): N {
30
31
  return new RBTreeNode(key, val) as N;
31
32
  }
32
33
 
33
- // override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: V): N | null | undefined {
34
+ // override add(keyOrNode: BTNKey | N | null, val?: V): N | null | undefined {
34
35
  // const inserted = super.add(keyOrNode, val);
35
36
  // if (inserted) this._fixInsertViolation(inserted);
36
37
  // return inserted;
@@ -204,7 +205,7 @@ export class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNest
204
205
  // node.right = null;
205
206
  // }
206
207
  //
207
- // override delete(keyOrNode: BinaryTreeNodeKey | N): BinaryTreeDeletedResult<N>[] {
208
+ // override delete(keyOrNode: BTNKey | N): BinaryTreeDeletedResult<N>[] {
208
209
  // const node = this.get(keyOrNode);
209
210
  // const result: BinaryTreeDeletedResult<N>[] = [{deleted: undefined, needBalanced: null}];
210
211
  // if (!node) return result; // Node does not exist
@@ -5,8 +5,8 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type {BinaryTreeNodeKey, TreeMultisetNodeNested, TreeMultisetOptions} from '../../types';
9
- import {BinaryTreeDeletedResult, CP, FamilyPosition, IterationType, OneParamCallback} from '../../types';
8
+ import type {BTNKey, TreeMultisetNodeNested, TreeMultisetOptions} from '../../types';
9
+ import {BinaryTreeDeletedResult, CP, FamilyPosition, IterationType, BTNCallback} from '../../types';
10
10
  import {IBinaryTree} from '../../interfaces';
11
11
  import {AVLTree, AVLTreeNode} from './avl-tree';
12
12
 
@@ -18,7 +18,7 @@ export class TreeMultisetNode<
18
18
 
19
19
  /**
20
20
  * The constructor function initializes a BinaryTreeNode object with a key, value, and count.
21
- * @param {BinaryTreeNodeKey} key - The `key` parameter is of type `BinaryTreeNodeKey` and represents the unique identifier
21
+ * @param {BTNKey} key - The `key` parameter is of type `BTNKey` and represents the unique identifier
22
22
  * of the binary tree node.
23
23
  * @param {V} [val] - The `val` parameter is an optional parameter of type `V`. It represents the value of the binary
24
24
  * tree node. If no value is provided, it will be `undefined`.
@@ -26,7 +26,7 @@ export class TreeMultisetNode<
26
26
  * occurs in a binary tree node. It has a default value of 1, which means that if no value is provided for the `count`
27
27
  * parameter when creating a new instance of the `BinaryTreeNode` class.
28
28
  */
29
- constructor(key: BinaryTreeNodeKey, val?: V, count = 1) {
29
+ constructor(key: BTNKey, val?: V, count = 1) {
30
30
  super(key, val);
31
31
  this.count = count;
32
32
  }
@@ -37,7 +37,8 @@ export class TreeMultisetNode<
37
37
  */
38
38
  export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultisetNode<V, TreeMultisetNodeNested<V>>>
39
39
  extends AVLTree<V, N>
40
- implements IBinaryTree<V, N> {
40
+ implements IBinaryTree<V, N>
41
+ {
41
42
  /**
42
43
  * The constructor function for a TreeMultiset class in TypeScript, which extends another class and sets an option to
43
44
  * merge duplicated values.
@@ -56,22 +57,22 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
56
57
 
57
58
  /**
58
59
  * The function creates a new BSTNode with the given key, value, and count.
59
- * @param {BinaryTreeNodeKey} key - The key parameter is the unique identifier for the binary tree node. It is used to
60
+ * @param {BTNKey} key - The key parameter is the unique identifier for the binary tree node. It is used to
60
61
  * distinguish one node from another in the tree.
61
62
  * @param {N} val - The `val` parameter represents the value that will be stored in the binary search tree node.
62
63
  * @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
63
64
  * occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
64
65
  * @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
65
66
  */
66
- override createNode(key: BinaryTreeNodeKey, val?: V, count?: number): N {
67
+ override createNode(key: BTNKey, val?: V, count?: number): N {
67
68
  return new TreeMultisetNode(key, val, count) as N;
68
69
  }
69
70
 
70
71
  /**
71
72
  * The `add` function adds a new node to a binary search tree, updating the count if the key already
72
73
  * exists, and balancing the tree if necessary.
73
- * @param {BinaryTreeNodeKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
74
- * `BinaryTreeNodeKey` (which represents the key of the node to be added), a `N` (which represents a
74
+ * @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
75
+ * `BTNKey` (which represents the key of the node to be added), a `N` (which represents a
75
76
  * node to be added), or `null` (which represents a null node).
76
77
  * @param [val] - The `val` parameter represents the value associated with the key that is being
77
78
  * added to the binary tree.
@@ -80,7 +81,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
80
81
  * count is specified, the default count will be 1.
81
82
  * @returns The function `add` returns a value of type `N | null | undefined`.
82
83
  */
83
- override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: V, count = 1): N | null | undefined {
84
+ override add(keyOrNode: BTNKey | N | null, val?: V, count = 1): N | null | undefined {
84
85
  let inserted: N | null | undefined = undefined,
85
86
  newNode: N | null;
86
87
  if (keyOrNode instanceof TreeMultisetNode) {
@@ -184,14 +185,14 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
184
185
  /**
185
186
  * The `addMany` function adds multiple keys or nodes to a TreeMultiset and returns an array of the
186
187
  * inserted nodes.
187
- * @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of keys or nodes to be
188
- * added to the multiset. Each element can be either a BinaryTreeNodeKey or a TreeMultisetNode.
188
+ * @param {(BTNKey | null)[] | (N | null)[]} keysOrNodes - An array of keys or nodes to be
189
+ * added to the multiset. Each element can be either a BTNKey or a TreeMultisetNode.
189
190
  * @param {V[]} [data] - The `data` parameter is an optional array of values that correspond
190
191
  * to the keys or nodes being added to the multiset. It is used to associate additional data with
191
192
  * each key or node.
192
193
  * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
193
194
  */
194
- override addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: V[]): (N | null | undefined)[] {
195
+ override addMany(keysOrNodes: (BTNKey | null)[] | (N | null)[], data?: V[]): (N | null | undefined)[] {
195
196
  const inserted: (N | null | undefined)[] = [];
196
197
 
197
198
  for (let i = 0; i < keysOrNodes.length; i++) {
@@ -262,21 +263,21 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
262
263
  * The `delete` function in a binary search tree deletes a node from the tree and returns the deleted
263
264
  * node along with the parent node that needs to be balanced.
264
265
  * @param {ReturnType<C>} identifier - The `identifier` parameter is either a
265
- * `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are
266
+ * `BTNKey` or a generic type `N`. It represents the property of the node that we are
266
267
  * searching for. It can be a specific key value or any other property of the node.
267
268
  * @param callback - The `callback` parameter is a function that takes a node as input and returns a
268
269
  * value. This value is compared with the `identifier` parameter to determine if the node should be
269
270
  * included in the result. The `callback` parameter has a default value of
270
- * `this._defaultCallbackByKey`
271
+ * `((node: N) => node.key)`
271
272
  * @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
272
273
  * being deleted. If set to true, the count of the node will not be considered and the node will be
273
274
  * deleted regardless of its count. If set to false (default), the count of the node will be
274
275
  * decremented by 1 and
275
276
  * @returns The method `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
276
277
  */
277
- override delete<C extends OneParamCallback<N>>(
278
+ override delete<C extends BTNCallback<N>>(
278
279
  identifier: ReturnType<C>,
279
- callback: C = this._defaultCallbackByKey as C,
280
+ callback: C = ((node: N) => node.key) as C,
280
281
  ignoreCount = false
281
282
  ): BinaryTreeDeletedResult<N>[] {
282
283
  const bstDeletedResult: BinaryTreeDeletedResult<N>[] = [];
@@ -105,7 +105,8 @@ export abstract class AbstractEdge<V = any> {
105
105
  export abstract class AbstractGraph<
106
106
  V extends AbstractVertex<any> = AbstractVertex<any>,
107
107
  E extends AbstractEdge<any> = AbstractEdge<any>
108
- > implements IGraph<V, E> {
108
+ > implements IGraph<V, E>
109
+ {
109
110
  private _vertices: Map<VertexKey, V> = new Map<VertexKey, V>();
110
111
 
111
112
  get vertices(): Map<VertexKey, V> {
@@ -553,14 +554,14 @@ export abstract class AbstractGraph<
553
554
  }
554
555
 
555
556
  getMinDist &&
556
- distMap.forEach((d, v) => {
557
- if (v !== srcVertex) {
558
- if (d < minDist) {
559
- minDist = d;
560
- if (genPaths) minDest = v;
557
+ distMap.forEach((d, v) => {
558
+ if (v !== srcVertex) {
559
+ if (d < minDist) {
560
+ minDist = d;
561
+ if (genPaths) minDest = v;
562
+ }
561
563
  }
562
- }
563
- });
564
+ });
564
565
 
565
566
  genPaths && getPaths(minDest);
566
567
 
@@ -622,7 +623,7 @@ export abstract class AbstractGraph<
622
623
  if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity);
623
624
  }
624
625
 
625
- const heap = new PriorityQueue<{ key: number; val: V }>({comparator: (a, b) => a.key - b.key});
626
+ const heap = new PriorityQueue<{key: number; val: V}>({comparator: (a, b) => a.key - b.key});
626
627
  heap.add({key: 0, val: srcVertex});
627
628
 
628
629
  distMap.set(srcVertex, 0);
@@ -851,7 +852,7 @@ export abstract class AbstractGraph<
851
852
  * `predecessor` property is a 2D array of vertices (or `null`) representing the predecessor vertices in the shortest
852
853
  * path between vertices in the
853
854
  */
854
- floyd(): { costs: number[][]; predecessor: (V | null)[][] } {
855
+ floyd(): {costs: number[][]; predecessor: (V | null)[][]} {
855
856
  const idAndVertices = [...this._vertices];
856
857
  const n = idAndVertices.length;
857
858
 
@@ -64,7 +64,8 @@ export class DirectedEdge<V = any> extends AbstractEdge<V> {
64
64
 
65
65
  export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E extends DirectedEdge<any> = DirectedEdge>
66
66
  extends AbstractGraph<V, E>
67
- implements IGraph<V, E> {
67
+ implements IGraph<V, E>
68
+ {
68
69
  /**
69
70
  * The constructor function initializes an instance of a class.
70
71
  */
@@ -51,11 +51,12 @@ export class UndirectedEdge<V = number> extends AbstractEdge<V> {
51
51
  }
52
52
 
53
53
  export class UndirectedGraph<
54
- V extends UndirectedVertex<any> = UndirectedVertex,
55
- E extends UndirectedEdge<any> = UndirectedEdge
56
- >
54
+ V extends UndirectedVertex<any> = UndirectedVertex,
55
+ E extends UndirectedEdge<any> = UndirectedEdge
56
+ >
57
57
  extends AbstractGraph<V, E>
58
- implements IGraph<V, E> {
58
+ implements IGraph<V, E>
59
+ {
59
60
  /**
60
61
  * The constructor initializes a new Map object to store edges.
61
62
  */
@@ -157,7 +157,7 @@ export class HashMap<K, V> {
157
157
  }
158
158
  }
159
159
 
160
- * entries(): IterableIterator<[K, V]> {
160
+ *entries(): IterableIterator<[K, V]> {
161
161
  for (const bucket of this.table) {
162
162
  if (bucket) {
163
163
  for (const [key, value] of bucket) {
@@ -1,2 +1 @@
1
- export class TreeMap {
2
- }
1
+ export class TreeMap {}
@@ -1,2 +1 @@
1
- export class TreeSet {
2
- }
1
+ export class TreeSet {}
@@ -11,7 +11,7 @@ export class Heap<E = any> {
11
11
  protected nodes: E[] = [];
12
12
  protected readonly comparator: Comparator<E>;
13
13
 
14
- constructor(options: { comparator: Comparator<E>; nodes?: E[] }) {
14
+ constructor(options: {comparator: Comparator<E>; nodes?: E[]}) {
15
15
  this.comparator = options.comparator;
16
16
  if (options.nodes && options.nodes.length > 0) {
17
17
  this.nodes = options.nodes;
@@ -39,7 +39,7 @@ export class Heap<E = any> {
39
39
  * @returns A new Heap instance.
40
40
  * @param options
41
41
  */
42
- static heapify<E>(options: { nodes: E[]; comparator: Comparator<E> }): Heap<E> {
42
+ static heapify<E>(options: {nodes: E[]; comparator: Comparator<E>}): Heap<E> {
43
43
  return new Heap<E>(options);
44
44
  }
45
45
 
@@ -11,7 +11,7 @@ import type {Comparator} from '../../types';
11
11
 
12
12
  export class MaxHeap<E = any> extends Heap<E> {
13
13
  constructor(
14
- options: { comparator: Comparator<E>; nodes?: E[] } = {
14
+ options: {comparator: Comparator<E>; nodes?: E[]} = {
15
15
  comparator: (a: E, b: E) => {
16
16
  if (!(typeof a === 'number' && typeof b === 'number')) {
17
17
  throw new Error('The a, b params of compare function must be number');
@@ -11,7 +11,7 @@ import type {Comparator} from '../../types';
11
11
 
12
12
  export class MinHeap<E = any> extends Heap<E> {
13
13
  constructor(
14
- options: { comparator: Comparator<E>; nodes?: E[] } = {
14
+ options: {comparator: Comparator<E>; nodes?: E[]} = {
15
15
  comparator: (a: E, b: E) => {
16
16
  if (!(typeof a === 'number' && typeof b === 'number')) {
17
17
  throw new Error('The a, b params of compare function must be number');
@@ -628,7 +628,7 @@ export class DoublyLinkedList<E = any> {
628
628
  /**
629
629
  * The function returns an iterator that iterates over the values of a linked list.
630
630
  */
631
- * [Symbol.iterator]() {
631
+ *[Symbol.iterator]() {
632
632
  let current = this.head;
633
633
 
634
634
  while (current) {
@@ -590,7 +590,7 @@ export class SinglyLinkedList<E = any> {
590
590
  /**
591
591
  * The function returns an iterator that iterates over the values of a linked list.
592
592
  */
593
- * [Symbol.iterator]() {
593
+ *[Symbol.iterator]() {
594
594
  let current = this.head;
595
595
 
596
596
  while (current) {
@@ -14,7 +14,7 @@ export class MatrixNTI2D<V = any> {
14
14
  * given initial value or 0 if not provided.
15
15
  * @param options - An object containing the following properties:
16
16
  */
17
- constructor(options: { row: number; col: number; initialVal?: V }) {
17
+ constructor(options: {row: number; col: number; initialVal?: V}) {
18
18
  const {row, col, initialVal} = options;
19
19
  this._matrix = new Array(row).fill(undefined).map(() => new Array(col).fill(initialVal || 0));
20
20
  }
@@ -10,8 +10,7 @@ export class Vector2D {
10
10
  public x: number = 0,
11
11
  public y: number = 0,
12
12
  public w: number = 1 // needed for matrix multiplication
13
- ) {
14
- }
13
+ ) {}
15
14
 
16
15
  /**
17
16
  * The function checks if the x and y values of a point are both zero.
@@ -10,7 +10,7 @@ import type {Comparator} from '../../types';
10
10
 
11
11
  export class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
12
12
  constructor(
13
- options: { comparator: Comparator<E>; nodes?: E[] } = {
13
+ options: {comparator: Comparator<E>; nodes?: E[]} = {
14
14
  comparator: (a: E, b: E) => {
15
15
  if (!(typeof a === 'number' && typeof b === 'number')) {
16
16
  throw new Error('The a, b params of compare function must be number');
@@ -10,7 +10,7 @@ import type {Comparator} from '../../types';
10
10
 
11
11
  export class MinPriorityQueue<E = any> extends PriorityQueue<E> {
12
12
  constructor(
13
- options: { comparator: Comparator<E>; nodes?: E[] } = {
13
+ options: {comparator: Comparator<E>; nodes?: E[]} = {
14
14
  comparator: (a: E, b: E) => {
15
15
  if (!(typeof a === 'number' && typeof b === 'number')) {
16
16
  throw new Error('The a, b params of compare function must be number');
@@ -10,7 +10,7 @@ import {Heap} from '../heap';
10
10
  import {Comparator} from '../../types';
11
11
 
12
12
  export class PriorityQueue<E = any> extends Heap<E> {
13
- constructor(options: { comparator: Comparator<E>; nodes?: E[] }) {
13
+ constructor(options: {comparator: Comparator<E>; nodes?: E[]}) {
14
14
  super(options);
15
15
  }
16
16
  }
@@ -9,8 +9,7 @@ import {DoublyLinkedList} from '../linked-list';
9
9
 
10
10
  // O(n) time complexity of obtaining the value
11
11
  // O(1) time complexity of adding at the beginning and the end
12
- export class Deque<E = any> extends DoublyLinkedList<E> {
13
- }
12
+ export class Deque<E = any> extends DoublyLinkedList<E> {}
14
13
 
15
14
  // O(1) time complexity of obtaining the value
16
15
  // O(n) time complexity of adding at the beginning and the end
@@ -20,9 +19,9 @@ export class ObjectDeque<E = number> {
20
19
  if (capacity !== undefined) this._capacity = capacity;
21
20
  }
22
21
 
23
- private _nodes: { [key: number]: E } = {};
22
+ private _nodes: {[key: number]: E} = {};
24
23
 
25
- get nodes(): { [p: number]: E } {
24
+ get nodes(): {[p: number]: E} {
26
25
  return this._nodes;
27
26
  }
28
27
 
@@ -157,7 +156,7 @@ export class ObjectDeque<E = number> {
157
156
  return this._size <= 0;
158
157
  }
159
158
 
160
- protected _seNodes(value: { [p: number]: E }) {
159
+ protected _seNodes(value: {[p: number]: E}) {
161
160
  this._nodes = value;
162
161
  }
163
162
 
@@ -183,7 +183,7 @@ export class Queue<E = any> {
183
183
  return new Queue(this.nodes.slice(this.offset));
184
184
  }
185
185
 
186
- * [Symbol.iterator]() {
186
+ *[Symbol.iterator]() {
187
187
  for (const item of this.nodes) {
188
188
  yield item;
189
189
  }