undirected-graph-typed 1.51.9 → 1.52.2

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 (106) hide show
  1. package/dist/data-structures/base/index.d.ts +2 -1
  2. package/dist/data-structures/base/index.js +2 -1
  3. package/dist/data-structures/base/iterable-element-base.d.ts +171 -0
  4. package/dist/data-structures/base/iterable-element-base.js +225 -0
  5. package/dist/data-structures/base/{iterable-base.d.ts → iterable-entry-base.d.ts} +4 -147
  6. package/dist/data-structures/base/{iterable-base.js → iterable-entry-base.js} +12 -189
  7. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +13 -13
  8. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +6 -6
  9. package/dist/data-structures/binary-tree/avl-tree.d.ts +13 -13
  10. package/dist/data-structures/binary-tree/avl-tree.js +6 -6
  11. package/dist/data-structures/binary-tree/binary-tree.d.ts +99 -99
  12. package/dist/data-structures/binary-tree/binary-tree.js +56 -54
  13. package/dist/data-structures/binary-tree/bst.d.ts +37 -45
  14. package/dist/data-structures/binary-tree/bst.js +19 -27
  15. package/dist/data-structures/binary-tree/rb-tree.d.ts +10 -10
  16. package/dist/data-structures/binary-tree/rb-tree.js +6 -6
  17. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +12 -12
  18. package/dist/data-structures/binary-tree/tree-multi-map.js +5 -5
  19. package/dist/data-structures/graph/directed-graph.js +2 -1
  20. package/dist/data-structures/hash/hash-map.d.ts +2 -2
  21. package/dist/data-structures/heap/heap.d.ts +43 -114
  22. package/dist/data-structures/heap/heap.js +59 -127
  23. package/dist/data-structures/heap/max-heap.d.ts +50 -4
  24. package/dist/data-structures/heap/max-heap.js +76 -10
  25. package/dist/data-structures/heap/min-heap.d.ts +51 -5
  26. package/dist/data-structures/heap/min-heap.js +68 -11
  27. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +22 -28
  28. package/dist/data-structures/linked-list/doubly-linked-list.js +26 -28
  29. package/dist/data-structures/linked-list/singly-linked-list.d.ts +22 -25
  30. package/dist/data-structures/linked-list/singly-linked-list.js +29 -26
  31. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +50 -4
  32. package/dist/data-structures/priority-queue/max-priority-queue.js +79 -10
  33. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +51 -5
  34. package/dist/data-structures/priority-queue/min-priority-queue.js +71 -11
  35. package/dist/data-structures/priority-queue/priority-queue.d.ts +50 -4
  36. package/dist/data-structures/priority-queue/priority-queue.js +70 -1
  37. package/dist/data-structures/queue/deque.d.ts +27 -18
  38. package/dist/data-structures/queue/deque.js +43 -21
  39. package/dist/data-structures/queue/queue.d.ts +26 -29
  40. package/dist/data-structures/queue/queue.js +47 -38
  41. package/dist/data-structures/stack/stack.d.ts +17 -22
  42. package/dist/data-structures/stack/stack.js +25 -24
  43. package/dist/data-structures/trie/trie.d.ts +18 -13
  44. package/dist/data-structures/trie/trie.js +26 -15
  45. package/dist/interfaces/binary-tree.d.ts +4 -4
  46. package/dist/types/common.d.ts +1 -22
  47. package/dist/types/data-structures/base/base.d.ts +5 -2
  48. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -3
  49. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -3
  50. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +20 -4
  51. package/dist/types/data-structures/binary-tree/bst.d.ts +5 -3
  52. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +2 -3
  53. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -3
  54. package/dist/types/data-structures/heap/heap.d.ts +3 -2
  55. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
  56. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +2 -1
  57. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
  58. package/dist/types/data-structures/queue/deque.d.ts +4 -2
  59. package/dist/types/data-structures/queue/queue.d.ts +4 -1
  60. package/dist/types/data-structures/stack/stack.d.ts +2 -1
  61. package/dist/types/data-structures/trie/trie.d.ts +3 -2
  62. package/package.json +2 -2
  63. package/src/data-structures/base/index.ts +2 -1
  64. package/src/data-structures/base/iterable-element-base.ts +250 -0
  65. package/src/data-structures/base/{iterable-base.ts → iterable-entry-base.ts} +26 -217
  66. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +26 -26
  67. package/src/data-structures/binary-tree/avl-tree.ts +21 -21
  68. package/src/data-structures/binary-tree/binary-tree.ts +166 -161
  69. package/src/data-structures/binary-tree/bst.ts +61 -68
  70. package/src/data-structures/binary-tree/rb-tree.ts +19 -19
  71. package/src/data-structures/binary-tree/tree-multi-map.ts +19 -19
  72. package/src/data-structures/graph/abstract-graph.ts +15 -14
  73. package/src/data-structures/graph/directed-graph.ts +9 -7
  74. package/src/data-structures/graph/undirected-graph.ts +7 -6
  75. package/src/data-structures/hash/hash-map.ts +8 -8
  76. package/src/data-structures/heap/heap.ts +72 -153
  77. package/src/data-structures/heap/max-heap.ts +88 -13
  78. package/src/data-structures/heap/min-heap.ts +78 -15
  79. package/src/data-structures/linked-list/doubly-linked-list.ts +33 -33
  80. package/src/data-structures/linked-list/singly-linked-list.ts +38 -30
  81. package/src/data-structures/priority-queue/max-priority-queue.ts +94 -13
  82. package/src/data-structures/priority-queue/min-priority-queue.ts +84 -15
  83. package/src/data-structures/priority-queue/priority-queue.ts +81 -4
  84. package/src/data-structures/queue/deque.ts +53 -28
  85. package/src/data-structures/queue/queue.ts +61 -44
  86. package/src/data-structures/stack/stack.ts +32 -27
  87. package/src/data-structures/trie/trie.ts +34 -19
  88. package/src/interfaces/binary-tree.ts +4 -5
  89. package/src/types/common.ts +2 -24
  90. package/src/types/data-structures/base/base.ts +14 -6
  91. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +2 -3
  92. package/src/types/data-structures/binary-tree/avl-tree.ts +2 -3
  93. package/src/types/data-structures/binary-tree/binary-tree.ts +24 -5
  94. package/src/types/data-structures/binary-tree/bst.ts +9 -3
  95. package/src/types/data-structures/binary-tree/rb-tree.ts +2 -3
  96. package/src/types/data-structures/binary-tree/tree-multi-map.ts +2 -3
  97. package/src/types/data-structures/graph/abstract-graph.ts +8 -8
  98. package/src/types/data-structures/heap/heap.ts +4 -1
  99. package/src/types/data-structures/linked-list/doubly-linked-list.ts +3 -1
  100. package/src/types/data-structures/linked-list/singly-linked-list.ts +3 -1
  101. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
  102. package/src/types/data-structures/queue/deque.ts +6 -1
  103. package/src/types/data-structures/queue/queue.ts +5 -1
  104. package/src/types/data-structures/stack/stack.ts +3 -1
  105. package/src/types/data-structures/trie/trie.ts +3 -1
  106. package/src/types/utils/utils.ts +4 -4
@@ -11,24 +11,24 @@ import type {
11
11
  BSTNodeNested,
12
12
  BSTOptions,
13
13
  BTNCallback,
14
+ BTNKeyOrNodeOrEntry,
14
15
  BTNPureKeyOrNodeOrEntry,
15
- Comparable,
16
16
  Comparator,
17
17
  CP,
18
18
  DFSOrderPattern,
19
19
  IterationType,
20
- KeyOrNodeOrEntry
20
+ OptBSTN
21
21
  } from '../../types';
22
22
  import { BTNEntry } from '../../types';
23
23
  import { BinaryTree, BinaryTreeNode } from './binary-tree';
24
24
  import { IBinaryTree } from '../../interfaces';
25
25
  import { Queue } from '../queue';
26
26
 
27
- export class BSTNode<
28
- K extends Comparable,
29
- V = any,
30
- NODE extends BSTNode<K, V, NODE> = BSTNodeNested<K, V>
31
- > extends BinaryTreeNode<K, V, NODE> {
27
+ export class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNodeNested<K, V>> extends BinaryTreeNode<
28
+ K,
29
+ V,
30
+ NODE
31
+ > {
32
32
  override parent?: NODE;
33
33
 
34
34
  constructor(key: K, value?: V) {
@@ -44,16 +44,16 @@ export class BSTNode<
44
44
  * The function returns the value of the `_left` property.
45
45
  * @returns The `_left` property of the current object is being returned.
46
46
  */
47
- override get left(): NODE | undefined {
47
+ override get left(): OptBSTN<NODE> {
48
48
  return this._left;
49
49
  }
50
50
 
51
51
  /**
52
52
  * The function sets the left child of a node and updates the parent reference of the child.
53
- * @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. It can either be an
53
+ * @param {OptBSTN<NODE>} v - The parameter `v` is of type `OptBSTN<NODE>`. It can either be an
54
54
  * instance of the `NODE` class or `undefined`.
55
55
  */
56
- override set left(v: NODE | undefined) {
56
+ override set left(v: OptBSTN<NODE>) {
57
57
  if (v) {
58
58
  v.parent = this as unknown as NODE;
59
59
  }
@@ -67,16 +67,16 @@ export class BSTNode<
67
67
  * @returns The method is returning the value of the `_right` property, which is of type `NODE` or
68
68
  * `undefined`.
69
69
  */
70
- override get right(): NODE | undefined {
70
+ override get right(): OptBSTN<NODE> {
71
71
  return this._right;
72
72
  }
73
73
 
74
74
  /**
75
75
  * The function sets the right child of a node and updates the parent reference of the child.
76
- * @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. It can either be a
76
+ * @param {OptBSTN<NODE>} v - The parameter `v` is of type `OptBSTN<NODE>`. It can either be a
77
77
  * `NODE` object or `undefined`.
78
78
  */
79
- override set right(v: NODE | undefined) {
79
+ override set right(v: OptBSTN<NODE>) {
80
80
  if (v) {
81
81
  v.parent = this as unknown as NODE;
82
82
  }
@@ -94,14 +94,15 @@ export class BSTNode<
94
94
  * 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
95
95
  */
96
96
  export class BST<
97
- K extends Comparable,
98
- V = any,
99
- R = BTNEntry<K, V>,
100
- NODE extends BSTNode<K, V, NODE> = BSTNode<K, V, BSTNodeNested<K, V>>,
101
- TREE extends BST<K, V, R, NODE, TREE> = BST<K, V, R, NODE, BSTNested<K, V, R, NODE>>
102
- >
97
+ K = any,
98
+ V = any,
99
+ R = BTNEntry<K, V>,
100
+ NODE extends BSTNode<K, V, NODE> = BSTNode<K, V, BSTNodeNested<K, V>>,
101
+ TREE extends BST<K, V, R, NODE, TREE> = BST<K, V, R, NODE, BSTNested<K, V, R, NODE>>
102
+ >
103
103
  extends BinaryTree<K, V, R, NODE, TREE>
104
- implements IBinaryTree<K, V, R, NODE, TREE> {
104
+ implements IBinaryTree<K, V, R, NODE, TREE>
105
+ {
105
106
  /**
106
107
  * This is the constructor function for a Binary Search Tree class in TypeScript.
107
108
  * @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
@@ -111,7 +112,7 @@ export class BST<
111
112
  * It can include a comparator function that defines the order of the elements in the tree.
112
113
  */
113
114
  constructor(
114
- keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>> = [],
115
+ keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
115
116
  options?: BSTOptions<K, V, R>
116
117
  ) {
117
118
  super([], options);
@@ -130,7 +131,7 @@ export class BST<
130
131
  * The function returns the root node of a tree structure.
131
132
  * @returns The `_root` property of the object, which is of type `NODE` or `undefined`.
132
133
  */
133
- override get root(): NODE | undefined {
134
+ override get root(): OptBSTN<NODE> {
134
135
  return this._root;
135
136
  }
136
137
 
@@ -163,17 +164,17 @@ export class BST<
163
164
 
164
165
  /**
165
166
  * The function overrides a method and converts a key, value pair or entry or raw element to a node.
166
- * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - A variable that can be of
167
- * type R or KeyOrNodeOrEntry<K, V, NODE>. It represents either a key, a node, an entry, or a raw
167
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - A variable that can be of
168
+ * type R or BTNKeyOrNodeOrEntry<K, V, NODE>. It represents either a key, a node, an entry, or a raw
168
169
  * element.
169
170
  * @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
170
171
  * value associated with a key in a key-value pair.
171
172
  * @returns either a NODE object or undefined.
172
173
  */
173
174
  override keyValueOrEntryOrRawElementToNode(
174
- keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>,
175
+ keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
175
176
  value?: V
176
- ): NODE | undefined {
177
+ ): OptBSTN<NODE> {
177
178
  return super.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value) ?? undefined;
178
179
  }
179
180
 
@@ -183,7 +184,7 @@ export class BST<
183
184
  *
184
185
  * The function ensures the existence of a node in a data structure and returns it, or undefined if
185
186
  * it doesn't exist.
186
- * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
187
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
187
188
  * `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, which represents the key, node,
188
189
  * entry, or raw element that needs to be ensured in the tree.
189
190
  * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
@@ -193,21 +194,21 @@ export class BST<
193
194
  * not be ensured.
194
195
  */
195
196
  override ensureNode(
196
- keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>,
197
+ keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
197
198
  iterationType: IterationType = 'ITERATIVE'
198
- ): NODE | undefined {
199
+ ): OptBSTN<NODE> {
199
200
  return super.ensureNode(keyOrNodeOrEntryOrRawElement, iterationType) ?? undefined;
200
201
  }
201
202
 
202
203
  /**
203
204
  * The function checks if the input is an instance of the BSTNode class.
204
- * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
205
- * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
205
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
206
+ * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
206
207
  * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
207
208
  * an instance of the `BSTNode` class.
208
209
  */
209
210
  override isNode(
210
- keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>
211
+ keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>
211
212
  ): keyOrNodeOrEntryOrRawElement is NODE {
212
213
  return keyOrNodeOrEntryOrRawElement instanceof BSTNode;
213
214
  }
@@ -217,13 +218,13 @@ export class BST<
217
218
  * Space Complexity: O(1)
218
219
  *
219
220
  * The `add` function in TypeScript adds a new node to a binary search tree based on the key value.
220
- * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
221
- * `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
221
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
222
+ * `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
222
223
  * @param {V} [value] - The `value` parameter is an optional value that can be associated with the
223
224
  * key in the binary search tree. If provided, it will be stored in the node along with the key.
224
225
  * @returns a boolean value.
225
226
  */
226
- override add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
227
+ override add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
227
228
  const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
228
229
  if (newNode === undefined) return false;
229
230
 
@@ -285,7 +286,7 @@ export class BST<
285
286
  * successfully inserted into the data structure.
286
287
  */
287
288
  override addMany(
288
- keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>>,
289
+ keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>>,
289
290
  values?: Iterable<V | undefined>,
290
291
  isBalanceAdd = true,
291
292
  iterationType: IterationType = this.iterationType
@@ -309,7 +310,9 @@ export class BST<
309
310
 
310
311
  const realBTNExemplars: (R | BTNPureKeyOrNodeOrEntry<K, V, NODE>)[] = [];
311
312
 
312
- const isRealBTNExemplar = (kve: R | KeyOrNodeOrEntry<K, V, NODE>): kve is BTNPureKeyOrNodeOrEntry<K, V, NODE> => {
313
+ const isRealBTNExemplar = (
314
+ kve: R | BTNKeyOrNodeOrEntry<K, V, NODE>
315
+ ): kve is BTNPureKeyOrNodeOrEntry<K, V, NODE> => {
313
316
  if (kve === undefined || kve === null) return false;
314
317
  return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null));
315
318
  };
@@ -396,7 +399,7 @@ export class BST<
396
399
  * @param [onlyOne=false] - A boolean value indicating whether to return only the first matching node
397
400
  * or all matching nodes. If set to true, only the first matching node will be returned. If set to
398
401
  * false, all matching nodes will be returned. The default value is false.
399
- * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
402
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
400
403
  * point for the search in the binary tree. It can be either a node object, a key-value pair, or an
401
404
  * entry object. If it is not provided, the `root` of the binary tree is used as the starting point.
402
405
  * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
@@ -407,7 +410,7 @@ export class BST<
407
410
  identifier: ReturnType<C> | undefined,
408
411
  callback: C = this._DEFAULT_CALLBACK as C,
409
412
  onlyOne = false,
410
- beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
413
+ beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
411
414
  iterationType: IterationType = this.iterationType
412
415
  ): NODE[] {
413
416
  beginRoot = this.ensureNode(beginRoot);
@@ -497,7 +500,7 @@ export class BST<
497
500
  callback: C = this._DEFAULT_CALLBACK as C,
498
501
  beginRoot: R | BSTNKeyOrNode<K, NODE> = this.root,
499
502
  iterationType: IterationType = this.iterationType
500
- ): NODE | undefined {
503
+ ): OptBSTN<NODE> {
501
504
  return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
502
505
  }
503
506
 
@@ -519,7 +522,7 @@ export class BST<
519
522
  * It has a default value of `'ITERATIVE'`.
520
523
  * @returns The method is returning a NODE object or undefined.
521
524
  */
522
- override getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): NODE | undefined {
525
+ override getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): OptBSTN<NODE> {
523
526
  return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
524
527
  }
525
528
 
@@ -540,7 +543,7 @@ export class BST<
540
543
  * @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
541
544
  * order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
542
545
  * take one of the following values:
543
- * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
546
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
544
547
  * point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
545
548
  * node entry. If not specified, the default value is the root of the tree.
546
549
  * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
@@ -551,7 +554,7 @@ export class BST<
551
554
  override dfs<C extends BTNCallback<NODE>>(
552
555
  callback: C = this._DEFAULT_CALLBACK as C,
553
556
  pattern: DFSOrderPattern = 'IN',
554
- beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
557
+ beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
555
558
  iterationType: IterationType = 'ITERATIVE'
556
559
  ): ReturnType<C>[] {
557
560
  return super.dfs(callback, pattern, beginRoot, iterationType, false);
@@ -571,7 +574,7 @@ export class BST<
571
574
  * @param {C} callback - The `callback` parameter is a function that will be called for each node
572
575
  * visited during the breadth-first search. It should take a single argument, which is the current
573
576
  * node being visited, and it can return a value of any type.
574
- * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
577
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
575
578
  * point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
576
579
  * object. If no value is provided, the default value is the root of the tree.
577
580
  * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
@@ -581,7 +584,7 @@ export class BST<
581
584
  */
582
585
  override bfs<C extends BTNCallback<NODE>>(
583
586
  callback: C = this._DEFAULT_CALLBACK as C,
584
- beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
587
+ beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
585
588
  iterationType: IterationType = this.iterationType
586
589
  ): ReturnType<C>[] {
587
590
  return super.bfs(callback, beginRoot, iterationType, false);
@@ -601,7 +604,7 @@ export class BST<
601
604
  * @param {C} callback - The `callback` parameter is a generic type `C` that extends
602
605
  * `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the
603
606
  * tree during the iteration process.
604
- * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
607
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
605
608
  * point for listing the levels of the binary tree. It can be either a root node of the tree, a
606
609
  * key-value pair representing a node in the tree, or a key representing a node in the tree. If no
607
610
  * value is provided, the root of
@@ -612,7 +615,7 @@ export class BST<
612
615
  */
613
616
  override listLevels<C extends BTNCallback<NODE>>(
614
617
  callback: C = this._DEFAULT_CALLBACK as C,
615
- beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
618
+ beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
616
619
  iterationType: IterationType = this.iterationType
617
620
  ): ReturnType<C>[][] {
618
621
  return super.listLevels(callback, beginRoot, iterationType, false);
@@ -635,7 +638,7 @@ export class BST<
635
638
  * @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
636
639
  * traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
637
640
  * 0, or 1, where:
638
- * @param {R | KeyOrNodeOrEntry<K, V, NODE>} targetNode - The `targetNode` parameter is the node in
641
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} targetNode - The `targetNode` parameter is the node in
639
642
  * the binary tree that you want to start traversing from. It can be specified either by providing
640
643
  * the key of the node, the node itself, or an entry containing the key and value of the node. If no
641
644
  * `targetNode` is provided,
@@ -647,7 +650,7 @@ export class BST<
647
650
  lesserOrGreaterTraverse<C extends BTNCallback<NODE>>(
648
651
  callback: C = this._DEFAULT_CALLBACK as C,
649
652
  lesserOrGreater: CP = -1,
650
- targetNode: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
653
+ targetNode: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
651
654
  iterationType: IterationType = this.iterationType
652
655
  ): ReturnType<C>[] {
653
656
  const targetNodeEnsured = this.ensureNode(targetNode);
@@ -761,7 +764,7 @@ export class BST<
761
764
  let balanced = true;
762
765
 
763
766
  if (iterationType === 'RECURSIVE') {
764
- const _height = (cur: NODE | undefined): number => {
767
+ const _height = (cur: OptBSTN<NODE>): number => {
765
768
  if (!cur) return 0;
766
769
  const leftHeight = _height(cur.left),
767
770
  rightHeight = _height(cur.right);
@@ -771,8 +774,8 @@ export class BST<
771
774
  _height(this.root);
772
775
  } else {
773
776
  const stack: NODE[] = [];
774
- let node: NODE | undefined = this.root,
775
- last: NODE | undefined = undefined;
777
+ let node: OptBSTN<NODE> = this.root,
778
+ last: OptBSTN<NODE> = undefined;
776
779
  const depths: Map<NODE, number> = new Map();
777
780
 
778
781
  while (stack.length > 0 || node) {
@@ -784,8 +787,8 @@ export class BST<
784
787
  if (!node.right || last === node.right) {
785
788
  node = stack.pop();
786
789
  if (node) {
787
- const left = node.left ? depths.get(node.left) ?? -1 : -1;
788
- const right = node.right ? depths.get(node.right) ?? -1 : -1;
790
+ const left = node.left ? (depths.get(node.left) ?? -1) : -1;
791
+ const right = node.right ? (depths.get(node.right) ?? -1) : -1;
789
792
  if (Math.abs(left - right) > 1) return false;
790
793
  depths.set(node, 1 + Math.max(left, right));
791
794
  last = node;
@@ -799,15 +802,10 @@ export class BST<
799
802
  return balanced;
800
803
  }
801
804
 
802
- /**
803
- * Time complexity: O(n)
804
- * Space complexity: O(n)
805
- */
806
-
807
805
  protected _DEFAULT_COMPARATOR = (a: K, b: K): number => {
808
- if (typeof a === 'object' && typeof b === 'object' && this.comparator === this._DEFAULT_COMPARATOR) {
806
+ if (typeof a === 'object' || typeof b === 'object') {
809
807
  throw TypeError(
810
- 'When comparing two object types, it is necessary to customize a [comparator] function of options parameter during the instantiation of the data structure.'
808
+ `When comparing object types, a custom comparator must be defined in the constructor's options parameter.`
811
809
  );
812
810
  }
813
811
  if (a > b) return 1;
@@ -815,11 +813,6 @@ export class BST<
815
813
  return 0;
816
814
  };
817
815
 
818
- /**
819
- * Time complexity: O(n)
820
- * Space complexity: O(n)
821
- */
822
-
823
816
  protected _comparator: Comparator<K> = this._DEFAULT_COMPARATOR;
824
817
 
825
818
  /**
@@ -838,9 +831,9 @@ export class BST<
838
831
  /**
839
832
  * The function sets the root of a tree-like structure and updates the parent property of the new
840
833
  * root.
841
- * @param {NODE | undefined} v - v is a parameter of type NODE or undefined.
834
+ * @param {OptBSTN<NODE>} v - v is a parameter of type NODE or undefined.
842
835
  */
843
- protected override _setRoot(v: NODE | undefined) {
836
+ protected override _setRoot(v: OptBSTN<NODE>) {
844
837
  if (v) {
845
838
  v.parent = undefined;
846
839
  }
@@ -1,9 +1,8 @@
1
1
  import type {
2
2
  BinaryTreeDeleteResult,
3
3
  BTNCallback,
4
- Comparable,
4
+ BTNKeyOrNodeOrEntry,
5
5
  CRUD,
6
- KeyOrNodeOrEntry,
7
6
  RBTNColor,
8
7
  RBTreeOptions,
9
8
  RedBlackTreeNested,
@@ -14,7 +13,7 @@ import { BST, BSTNode } from './bst';
14
13
  import { IBinaryTree } from '../../interfaces';
15
14
 
16
15
  export class RedBlackTreeNode<
17
- K extends Comparable,
16
+ K = any,
18
17
  V = any,
19
18
  NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNodeNested<K, V>
20
19
  > extends BSTNode<K, V, NODE> {
@@ -54,14 +53,15 @@ export class RedBlackTreeNode<
54
53
  }
55
54
 
56
55
  export class RedBlackTree<
57
- K extends Comparable,
58
- V = any,
59
- R = BTNEntry<K, V>,
60
- NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>,
61
- TREE extends RedBlackTree<K, V, R, NODE, TREE> = RedBlackTree<K, V, R, NODE, RedBlackTreeNested<K, V, R, NODE>>
62
- >
56
+ K = any,
57
+ V = any,
58
+ R = BTNEntry<K, V>,
59
+ NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>,
60
+ TREE extends RedBlackTree<K, V, R, NODE, TREE> = RedBlackTree<K, V, R, NODE, RedBlackTreeNested<K, V, R, NODE>>
61
+ >
63
62
  extends BST<K, V, R, NODE, TREE>
64
- implements IBinaryTree<K, V, R, NODE, TREE> {
63
+ implements IBinaryTree<K, V, R, NODE, TREE>
64
+ {
65
65
  /**
66
66
  * This is the constructor function for a Red-Black Tree data structure in TypeScript.
67
67
  * @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
@@ -73,7 +73,7 @@ export class RedBlackTree<
73
73
  * depend on the implementation
74
74
  */
75
75
  constructor(
76
- keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>> = [],
76
+ keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
77
77
  options?: RBTreeOptions<K, V, R>
78
78
  ) {
79
79
  super([], options);
@@ -136,13 +136,13 @@ export class RedBlackTree<
136
136
  * Space Complexity: O(1)
137
137
  *
138
138
  * The function checks if the input is an instance of the RedBlackTreeNode class.
139
- * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
140
- * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
139
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
140
+ * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
141
141
  * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
142
142
  * an instance of the `RedBlackTreeNode` class.
143
143
  */
144
144
  override isNode(
145
- keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>
145
+ keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>
146
146
  ): keyOrNodeOrEntryOrRawElement is NODE {
147
147
  return keyOrNodeOrEntryOrRawElement instanceof RedBlackTreeNode;
148
148
  }
@@ -158,11 +158,11 @@ export class RedBlackTree<
158
158
  // *
159
159
  // * The function `keyValueOrEntryOrRawElementToNode` takes a key, value, or entry and returns a node if it is
160
160
  // * valid, otherwise it returns undefined.
161
- // * @param {KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The key, value, or entry to convert.
161
+ // * @param {BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The key, value, or entry to convert.
162
162
  // * @param {V} [value] - The value associated with the key (if `keyOrNodeOrEntryOrRawElement` is a key).
163
163
  // * @returns {NODE | undefined} - The corresponding Red-Black Tree node, or `undefined` if conversion fails.
164
164
  // */
165
- // override keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V): NODE | undefined {
165
+ // override keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V): NODE | undefined {
166
166
  //
167
167
  // if (keyOrNodeOrEntryOrRawElement === null || keyOrNodeOrEntryOrRawElement === undefined) return;
168
168
  // if (this.isNode(keyOrNodeOrEntryOrRawElement)) return keyOrNodeOrEntryOrRawElement;
@@ -211,8 +211,8 @@ export class RedBlackTree<
211
211
  *
212
212
  * The function adds a new node to a binary search tree and returns true if the node was successfully
213
213
  * added.
214
- * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
215
- * `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
214
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
215
+ * `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
216
216
  * @param {V} [value] - The `value` parameter is an optional value that you want to associate with
217
217
  * the key in the data structure. It represents the value that you want to add or update in the data
218
218
  * structure.
@@ -220,7 +220,7 @@ export class RedBlackTree<
220
220
  * the method returns true. If the node already exists and its value is updated, the method also
221
221
  * returns true. If the node cannot be added or updated, the method returns false.
222
222
  */
223
- override add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
223
+ override add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
224
224
  const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
225
225
  if (!this.isRealNode(newNode)) return false;
226
226
 
@@ -9,9 +9,8 @@ import type {
9
9
  BinaryTreeDeleteResult,
10
10
  BSTNKeyOrNode,
11
11
  BTNCallback,
12
- Comparable,
12
+ BTNKeyOrNodeOrEntry,
13
13
  IterationType,
14
- KeyOrNodeOrEntry,
15
14
  RBTNColor,
16
15
  TreeMultiMapNested,
17
16
  TreeMultiMapNodeNested,
@@ -22,7 +21,7 @@ import { IBinaryTree } from '../../interfaces';
22
21
  import { RedBlackTree, RedBlackTreeNode } from './rb-tree';
23
22
 
24
23
  export class TreeMultiMapNode<
25
- K extends Comparable,
24
+ K = any,
26
25
  V = any,
27
26
  NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNodeNested<K, V>
28
27
  > extends RedBlackTreeNode<K, V, NODE> {
@@ -64,14 +63,15 @@ export class TreeMultiMapNode<
64
63
  }
65
64
 
66
65
  export class TreeMultiMap<
67
- K extends Comparable,
68
- V = any,
69
- R = BTNEntry<K, V>,
70
- NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNode<K, V, TreeMultiMapNodeNested<K, V>>,
71
- TREE extends TreeMultiMap<K, V, R, NODE, TREE> = TreeMultiMap<K, V, R, NODE, TreeMultiMapNested<K, V, R, NODE>>
72
- >
66
+ K = any,
67
+ V = any,
68
+ R = BTNEntry<K, V>,
69
+ NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNode<K, V, TreeMultiMapNodeNested<K, V>>,
70
+ TREE extends TreeMultiMap<K, V, R, NODE, TREE> = TreeMultiMap<K, V, R, NODE, TreeMultiMapNested<K, V, R, NODE>>
71
+ >
73
72
  extends RedBlackTree<K, V, R, NODE, TREE>
74
- implements IBinaryTree<K, V, R, NODE, TREE> {
73
+ implements IBinaryTree<K, V, R, NODE, TREE>
74
+ {
75
75
  /**
76
76
  * The constructor function initializes a TreeMultiMap object with optional initial data.
77
77
  * @param keysOrNodesOrEntriesOrRawElements - The parameter `keysOrNodesOrEntriesOrRawElements` is an
@@ -82,7 +82,7 @@ export class TreeMultiMap<
82
82
  * `compareValues`, which are functions used to compare keys and values respectively.
83
83
  */
84
84
  constructor(
85
- keysOrNodesOrEntriesOrRawElements: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [],
85
+ keysOrNodesOrEntriesOrRawElements: Iterable<BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
86
86
  options?: TreeMultiMapOptions<K, V, R>
87
87
  ) {
88
88
  super([], options);
@@ -154,8 +154,8 @@ export class TreeMultiMap<
154
154
  /**
155
155
  * The function `keyValueOrEntryOrRawElementToNode` takes in a key, value, and count and returns a
156
156
  * node based on the input.
157
- * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
158
- * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
157
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
158
+ * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
159
159
  * @param {V} [value] - The `value` parameter is an optional value that represents the value
160
160
  * associated with the key in the node. It is used when creating a new node or updating the value of
161
161
  * an existing node.
@@ -164,7 +164,7 @@ export class TreeMultiMap<
164
164
  * @returns either a NODE object or undefined.
165
165
  */
166
166
  override keyValueOrEntryOrRawElementToNode(
167
- keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>,
167
+ keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
168
168
  value?: V,
169
169
  count = 1
170
170
  ): NODE | undefined {
@@ -191,13 +191,13 @@ export class TreeMultiMap<
191
191
 
192
192
  /**
193
193
  * The function checks if the input is an instance of the TreeMultiMapNode class.
194
- * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
195
- * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
194
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
195
+ * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
196
196
  * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
197
197
  * an instance of the `TreeMultiMapNode` class.
198
198
  */
199
199
  override isNode(
200
- keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>
200
+ keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>
201
201
  ): keyOrNodeOrEntryOrRawElement is NODE {
202
202
  return keyOrNodeOrEntryOrRawElement instanceof TreeMultiMapNode;
203
203
  }
@@ -213,7 +213,7 @@ export class TreeMultiMap<
213
213
  *
214
214
  * The function overrides the add method of a class and adds a new node to a data structure, updating
215
215
  * the count and returning a boolean indicating success.
216
- * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
216
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
217
217
  * `keyOrNodeOrEntryOrRawElement` parameter can accept one of the following types:
218
218
  * @param {V} [value] - The `value` parameter represents the value associated with the key in the
219
219
  * data structure. It is an optional parameter, so it can be omitted if not needed.
@@ -223,7 +223,7 @@ export class TreeMultiMap<
223
223
  * @returns The method is returning a boolean value. It returns true if the addition of the new node
224
224
  * was successful, and false otherwise.
225
225
  */
226
- override add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V, count = 1): boolean {
226
+ override add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V, count = 1): boolean {
227
227
  const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value, count);
228
228
  const orgCount = newNode?.count || 0;
229
229
  const isSuccessAdded = super.add(newNode);
@@ -61,13 +61,14 @@ export abstract class AbstractEdge<E = any> {
61
61
  }
62
62
 
63
63
  export abstract class AbstractGraph<
64
- V = any,
65
- E = any,
66
- VO extends AbstractVertex<V> = AbstractVertex<V>,
67
- EO extends AbstractEdge<E> = AbstractEdge<E>
68
- >
64
+ V = any,
65
+ E = any,
66
+ VO extends AbstractVertex<V> = AbstractVertex<V>,
67
+ EO extends AbstractEdge<E> = AbstractEdge<E>
68
+ >
69
69
  extends IterableEntryBase<VertexKey, V | undefined>
70
- implements IGraph<V, E, VO, EO> {
70
+ implements IGraph<V, E, VO, EO>
71
+ {
71
72
  constructor() {
72
73
  super();
73
74
  }
@@ -618,14 +619,14 @@ export abstract class AbstractGraph<
618
619
  }
619
620
 
620
621
  getMinDist &&
621
- distMap.forEach((d, v) => {
622
- if (v !== srcVertex) {
623
- if (d < minDist) {
624
- minDist = d;
625
- if (genPaths) minDest = v;
622
+ distMap.forEach((d, v) => {
623
+ if (v !== srcVertex) {
624
+ if (d < minDist) {
625
+ minDist = d;
626
+ if (genPaths) minDest = v;
627
+ }
626
628
  }
627
- }
628
- });
629
+ });
629
630
 
630
631
  genPaths && getPaths(minDest);
631
632
 
@@ -1069,7 +1070,7 @@ export abstract class AbstractGraph<
1069
1070
  return mapped;
1070
1071
  }
1071
1072
 
1072
- protected* _getIterator(): IterableIterator<[VertexKey, V | undefined]> {
1073
+ protected *_getIterator(): IterableIterator<[VertexKey, V | undefined]> {
1073
1074
  for (const vertex of this._vertexMap.values()) {
1074
1075
  yield [vertex.key, vertex.value];
1075
1076
  }