min-heap-typed 1.51.8 → 1.52.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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 +106 -68
  8. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +119 -87
  9. package/dist/data-structures/binary-tree/avl-tree.d.ts +82 -62
  10. package/dist/data-structures/binary-tree/avl-tree.js +78 -59
  11. package/dist/data-structures/binary-tree/binary-tree.d.ts +318 -226
  12. package/dist/data-structures/binary-tree/binary-tree.js +475 -363
  13. package/dist/data-structures/binary-tree/bst.d.ts +192 -202
  14. package/dist/data-structures/binary-tree/bst.js +207 -249
  15. package/dist/data-structures/binary-tree/rb-tree.d.ts +73 -74
  16. package/dist/data-structures/binary-tree/rb-tree.js +107 -98
  17. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +92 -75
  18. package/dist/data-structures/binary-tree/tree-multi-map.js +105 -93
  19. package/dist/data-structures/graph/abstract-graph.d.ts +10 -15
  20. package/dist/data-structures/graph/abstract-graph.js +10 -15
  21. package/dist/data-structures/hash/hash-map.d.ts +33 -40
  22. package/dist/data-structures/hash/hash-map.js +40 -55
  23. package/dist/data-structures/heap/heap.d.ts +43 -114
  24. package/dist/data-structures/heap/heap.js +59 -127
  25. package/dist/data-structures/heap/max-heap.d.ts +50 -4
  26. package/dist/data-structures/heap/max-heap.js +76 -10
  27. package/dist/data-structures/heap/min-heap.d.ts +51 -5
  28. package/dist/data-structures/heap/min-heap.js +68 -11
  29. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +22 -28
  30. package/dist/data-structures/linked-list/doubly-linked-list.js +26 -28
  31. package/dist/data-structures/linked-list/singly-linked-list.d.ts +22 -25
  32. package/dist/data-structures/linked-list/singly-linked-list.js +29 -26
  33. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +50 -4
  34. package/dist/data-structures/priority-queue/max-priority-queue.js +79 -10
  35. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +51 -5
  36. package/dist/data-structures/priority-queue/min-priority-queue.js +71 -11
  37. package/dist/data-structures/priority-queue/priority-queue.d.ts +50 -4
  38. package/dist/data-structures/priority-queue/priority-queue.js +70 -1
  39. package/dist/data-structures/queue/deque.d.ts +21 -20
  40. package/dist/data-structures/queue/deque.js +29 -23
  41. package/dist/data-structures/queue/queue.d.ts +8 -28
  42. package/dist/data-structures/queue/queue.js +15 -31
  43. package/dist/data-structures/stack/stack.d.ts +17 -22
  44. package/dist/data-structures/stack/stack.js +25 -24
  45. package/dist/data-structures/trie/trie.d.ts +19 -14
  46. package/dist/data-structures/trie/trie.js +27 -16
  47. package/dist/interfaces/binary-tree.d.ts +7 -7
  48. package/dist/types/common.d.ts +1 -2
  49. package/dist/types/data-structures/base/base.d.ts +5 -2
  50. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -4
  51. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -4
  52. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +5 -5
  53. package/dist/types/data-structures/binary-tree/bst.d.ts +4 -5
  54. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -4
  55. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -4
  56. package/dist/types/data-structures/heap/heap.d.ts +3 -2
  57. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
  58. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +2 -1
  59. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
  60. package/dist/types/data-structures/queue/deque.d.ts +3 -2
  61. package/dist/types/data-structures/queue/queue.d.ts +2 -1
  62. package/dist/types/data-structures/stack/stack.d.ts +2 -1
  63. package/dist/types/data-structures/trie/trie.d.ts +3 -2
  64. package/dist/utils/utils.js +3 -5
  65. package/package.json +2 -2
  66. package/src/data-structures/base/index.ts +2 -1
  67. package/src/data-structures/base/iterable-element-base.ts +250 -0
  68. package/src/data-structures/base/{iterable-base.ts → iterable-entry-base.ts} +22 -213
  69. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +144 -95
  70. package/src/data-structures/binary-tree/avl-tree.ts +96 -69
  71. package/src/data-structures/binary-tree/binary-tree.ts +535 -403
  72. package/src/data-structures/binary-tree/bst.ts +247 -277
  73. package/src/data-structures/binary-tree/rb-tree.ts +123 -103
  74. package/src/data-structures/binary-tree/tree-multi-map.ts +127 -102
  75. package/src/data-structures/graph/abstract-graph.ts +10 -10
  76. package/src/data-structures/hash/hash-map.ts +46 -53
  77. package/src/data-structures/heap/heap.ts +71 -152
  78. package/src/data-structures/heap/max-heap.ts +88 -13
  79. package/src/data-structures/heap/min-heap.ts +78 -15
  80. package/src/data-structures/linked-list/doubly-linked-list.ts +32 -32
  81. package/src/data-structures/linked-list/singly-linked-list.ts +37 -29
  82. package/src/data-structures/priority-queue/max-priority-queue.ts +94 -13
  83. package/src/data-structures/priority-queue/min-priority-queue.ts +84 -15
  84. package/src/data-structures/priority-queue/priority-queue.ts +81 -4
  85. package/src/data-structures/queue/deque.ts +37 -26
  86. package/src/data-structures/queue/queue.ts +23 -36
  87. package/src/data-structures/stack/stack.ts +31 -26
  88. package/src/data-structures/trie/trie.ts +35 -20
  89. package/src/interfaces/binary-tree.ts +9 -9
  90. package/src/types/common.ts +1 -2
  91. package/src/types/data-structures/base/base.ts +14 -6
  92. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +3 -4
  93. package/src/types/data-structures/binary-tree/avl-tree.ts +3 -4
  94. package/src/types/data-structures/binary-tree/binary-tree.ts +6 -6
  95. package/src/types/data-structures/binary-tree/bst.ts +4 -5
  96. package/src/types/data-structures/binary-tree/rb-tree.ts +3 -4
  97. package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -4
  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 +3 -1
  103. package/src/types/data-structures/queue/queue.ts +3 -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/utils/utils.ts +3 -3
@@ -60,31 +60,34 @@ exports.BSTNode = BSTNode;
60
60
  */
61
61
  class BST extends binary_tree_1.BinaryTree {
62
62
  /**
63
- * This is the constructor function for a Binary Search Tree class in TypeScript, which initializes
64
- * the tree with keys, nodes, or entries and optional options.
65
- * @param keysOrNodesOrEntries - The `keysOrNodesOrEntries` parameter is an iterable object that can
66
- * contain keys, nodes, or entries. It is used to initialize the binary search tree with the provided
67
- * keys, nodes, or entries.
68
- * @param [options] - The `options` parameter is an optional object that can contain additional
69
- * configuration options for the binary search tree. It can have the following properties:
70
- */
71
- constructor(keysOrNodesOrEntries = [], options) {
63
+ * This is the constructor function for a Binary Search Tree class in TypeScript.
64
+ * @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
65
+ * iterable that can contain either keys, nodes, entries, or raw elements. These elements will be
66
+ * added to the binary search tree during the construction of the object.
67
+ * @param [options] - An optional object that contains additional options for the Binary Search Tree.
68
+ * It can include a comparator function that defines the order of the elements in the tree.
69
+ */
70
+ constructor(keysOrNodesOrEntriesOrRawElements = [], options) {
72
71
  super([], options);
73
72
  this._root = undefined;
74
- this._comparator = (a, b) => {
73
+ this._DEFAULT_COMPARATOR = (a, b) => {
74
+ if (typeof a === 'object' || typeof b === 'object') {
75
+ throw TypeError(`When comparing object types, a custom comparator must be defined in the constructor's options parameter.`);
76
+ }
75
77
  if (a > b)
76
78
  return 1;
77
79
  if (a < b)
78
80
  return -1;
79
81
  return 0;
80
82
  };
83
+ this._comparator = this._DEFAULT_COMPARATOR;
81
84
  if (options) {
82
85
  const { comparator } = options;
83
86
  if (comparator)
84
87
  this._comparator = comparator;
85
88
  }
86
- if (keysOrNodesOrEntries)
87
- this.addMany(keysOrNodesOrEntries);
89
+ if (keysOrNodesOrEntriesOrRawElements)
90
+ this.addMany(keysOrNodesOrEntriesOrRawElements);
88
91
  }
89
92
  /**
90
93
  * The function returns the root node of a tree structure.
@@ -93,13 +96,6 @@ class BST extends binary_tree_1.BinaryTree {
93
96
  get root() {
94
97
  return this._root;
95
98
  }
96
- /**
97
- * The function returns the value of the _comparator property.
98
- * @returns The `_comparator` property is being returned.
99
- */
100
- get comparator() {
101
- return this._comparator;
102
- }
103
99
  /**
104
100
  * The function creates a new BSTNode with the given key and value and returns it.
105
101
  * @param {K} key - The key parameter is of type K, which represents the type of the key for the node
@@ -114,104 +110,68 @@ class BST extends binary_tree_1.BinaryTree {
114
110
  /**
115
111
  * The function creates a new binary search tree with the specified options.
116
112
  * @param [options] - The `options` parameter is an optional object that allows you to customize the
117
- * behavior of the `createTree` method. It is of type `Partial<BSTOptions<K>>`, which means it is a
118
- * partial object of type `BSTOptions<K>`.
119
- * @returns a new instance of the BST class, with the provided options merged with the default
120
- * options. The returned value is casted as TREE.
113
+ * behavior of the `createTree` method. It accepts a partial `BSTOptions` object, which has the
114
+ * following properties:
115
+ * @returns a new instance of the BST class with the provided options.
121
116
  */
122
117
  createTree(options) {
123
118
  return new BST([], Object.assign({ iterationType: this.iterationType, comparator: this.comparator }, options));
124
119
  }
125
120
  /**
126
- * The function `keyValueOrEntryToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid,
127
- * otherwise it returns undefined.
128
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`, where:
129
- * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
130
- * `keyValueOrEntryToNode` function. It represents the value associated with the keyOrNodeOrEntry node.
131
- * @returns a node of type NODE or undefined.
132
- */
133
- keyValueOrEntryToNode(keyOrNodeOrEntry, value) {
134
- let node;
135
- if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
136
- return;
137
- }
138
- else if (this.isNode(keyOrNodeOrEntry)) {
139
- node = keyOrNodeOrEntry;
140
- }
141
- else if (this.isEntry(keyOrNodeOrEntry)) {
142
- const [key, value] = keyOrNodeOrEntry;
143
- if (key === undefined || key === null) {
144
- return;
145
- }
146
- else {
147
- node = this.createNode(key, value);
148
- }
149
- }
150
- else if (!this.isNode(keyOrNodeOrEntry)) {
151
- node = this.createNode(keyOrNodeOrEntry, value);
152
- }
153
- else {
154
- return;
155
- }
156
- return node;
157
- }
158
- /**
159
- * Time Complexity: O(log n)
160
- * Space Complexity: O(log n)
121
+ * The function overrides a method and converts a key, value pair or entry or raw element to a node.
122
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - A variable that can be of
123
+ * type R or KeyOrNodeOrEntry<K, V, NODE>. It represents either a key, a node, an entry, or a raw
124
+ * element.
125
+ * @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
126
+ * value associated with a key in a key-value pair.
127
+ * @returns either a NODE object or undefined.
161
128
  */
129
+ keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value) {
130
+ var _a;
131
+ return (_a = super.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value)) !== null && _a !== void 0 ? _a : undefined;
132
+ }
162
133
  /**
163
134
  * Time Complexity: O(log n)
164
135
  * Space Complexity: O(log n)
165
136
  *
166
- * The function `ensureNode` returns the node corresponding to the given key if it is a node key,
167
- * otherwise it returns the key itself.
168
- * @param {K | NODE | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `NODE`, or
169
- * `undefined`.
170
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
171
- * type of iteration to be performed. It has a default value of `'ITERATIVE'`.
172
- * @returns either a node object (NODE) or undefined.
173
- */
174
- ensureNode(keyOrNodeOrEntry, iterationType = 'ITERATIVE') {
175
- if (keyOrNodeOrEntry === this.NIL)
176
- return;
177
- if (this.isRealNode(keyOrNodeOrEntry)) {
178
- return keyOrNodeOrEntry;
179
- }
180
- if (this.isEntry(keyOrNodeOrEntry)) {
181
- const key = keyOrNodeOrEntry[0];
182
- if (key === null || key === undefined)
183
- return;
184
- return this.getNodeByKey(key, iterationType);
185
- }
186
- const key = keyOrNodeOrEntry;
187
- if (key === null || key === undefined)
188
- return;
189
- return this.getNodeByKey(key, iterationType);
137
+ * The function ensures the existence of a node in a data structure and returns it, or undefined if
138
+ * it doesn't exist.
139
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
140
+ * `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, which represents the key, node,
141
+ * entry, or raw element that needs to be ensured in the tree.
142
+ * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
143
+ * parameter that specifies the type of iteration to be used when ensuring a node. It has a default
144
+ * value of `'ITERATIVE'`.
145
+ * @returns The method is returning either the node that was ensured or `undefined` if the node could
146
+ * not be ensured.
147
+ */
148
+ ensureNode(keyOrNodeOrEntryOrRawElement, iterationType = 'ITERATIVE') {
149
+ var _a;
150
+ return (_a = super.ensureNode(keyOrNodeOrEntryOrRawElement, iterationType)) !== null && _a !== void 0 ? _a : undefined;
190
151
  }
191
152
  /**
192
- * The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
193
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, NODE>`.
194
- * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the BSTNode class.
153
+ * The function checks if the input is an instance of the BSTNode class.
154
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
155
+ * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
156
+ * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
157
+ * an instance of the `BSTNode` class.
195
158
  */
196
- isNode(keyOrNodeOrEntry) {
197
- return keyOrNodeOrEntry instanceof BSTNode;
159
+ isNode(keyOrNodeOrEntryOrRawElement) {
160
+ return keyOrNodeOrEntryOrRawElement instanceof BSTNode;
198
161
  }
199
- /**
200
- * Time Complexity: O(log n)
201
- * Space Complexity: O(1)
202
- */
203
162
  /**
204
163
  * Time Complexity: O(log n)
205
164
  * Space Complexity: O(1)
206
165
  *
207
- * The `add` function in TypeScript adds a new node to a binary search tree based on the key value,
208
- * updating the value if the key already exists.
209
- * @param keyOrNodeOrEntry - It is a parameter that can accept three types of values:
210
- * @param {V} [value] - The value to be added to the binary search tree.
211
- * @returns The method returns a boolean value.
212
- */
213
- add(keyOrNodeOrEntry, value) {
214
- const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
166
+ * The `add` function in TypeScript adds a new node to a binary search tree based on the key value.
167
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
168
+ * `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
169
+ * @param {V} [value] - The `value` parameter is an optional value that can be associated with the
170
+ * key in the binary search tree. If provided, it will be stored in the node along with the key.
171
+ * @returns a boolean value.
172
+ */
173
+ add(keyOrNodeOrEntryOrRawElement, value) {
174
+ const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
215
175
  if (newNode === undefined)
216
176
  return false;
217
177
  if (this.root === undefined) {
@@ -222,15 +182,8 @@ class BST extends binary_tree_1.BinaryTree {
222
182
  let current = this.root;
223
183
  while (current !== undefined) {
224
184
  if (this.comparator(current.key, newNode.key) === 0) {
225
- // if (current !== newNode) {
226
- // The key value is the same but the reference is different, update the value of the existing node
227
185
  this._replaceNode(current, newNode);
228
186
  return true;
229
- // } else {
230
- // The key value is the same and the reference is the same, replace the entire node
231
- // this._replaceNode(current, newNode);
232
- // return;
233
- // }
234
187
  }
235
188
  else if (this.comparator(current.key, newNode.key) > 0) {
236
189
  if (current.left === undefined) {
@@ -252,18 +205,17 @@ class BST extends binary_tree_1.BinaryTree {
252
205
  return false;
253
206
  }
254
207
  /**
255
- * Time Complexity: O(k log n)
256
- * Space Complexity: O(k + log n)
208
+ * Time Complexity: O(log n)
209
+ * Space Complexity: O(log n)
257
210
  */
258
211
  /**
259
212
  * Time Complexity: O(k log n)
260
213
  * Space Complexity: O(k + log n)
261
214
  *
262
- * The `addMany` function in TypeScript adds multiple keys or nodes to a data structure, balancing
263
- * the structure if specified, and returns an array indicating whether each key or node was
264
- * successfully inserted.
265
- * @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries to be added to the
266
- * data structure.
215
+ * The `addMany` function in TypeScript adds multiple keys or nodes to a data structure and returns
216
+ * an array indicating whether each key or node was successfully inserted.
217
+ * @param keysOrNodesOrEntriesOrRawElements - An iterable containing keys, nodes, entries, or raw
218
+ * elements to be added to the data structure.
267
219
  * @param [values] - An optional iterable of values to be associated with the keys or nodes being
268
220
  * added. If provided, the values will be assigned to the corresponding keys or nodes in the same
269
221
  * order. If not provided, undefined will be assigned as the value for each key or node.
@@ -272,20 +224,19 @@ class BST extends binary_tree_1.BinaryTree {
272
224
  * algorithm. If set to false, the elements will be added without balancing the tree. The default
273
225
  * value is true.
274
226
  * @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
275
- * specifies the type of iteration to use when adding multiple keys or nodes to the binary tree. It
276
- * has a default value of `this.iterationType`, which means it will use the iteration type specified
277
- * in the binary tree instance.
278
- * @returns The function `addMany` returns an array of booleans indicating whether each key or node
279
- * or entry was successfully inserted into the data structure.
227
+ * specifies the type of iteration to use when adding multiple keys or nodes to the binary search
228
+ * tree. It can have two possible values:
229
+ * @returns The function `addMany` returns an array of booleans indicating whether each element was
230
+ * successfully inserted into the data structure.
280
231
  */
281
- addMany(keysOrNodesOrEntries, values, isBalanceAdd = true, iterationType = this.iterationType) {
232
+ addMany(keysOrNodesOrEntriesOrRawElements, values, isBalanceAdd = true, iterationType = this.iterationType) {
282
233
  const inserted = [];
283
234
  let valuesIterator;
284
235
  if (values) {
285
236
  valuesIterator = values[Symbol.iterator]();
286
237
  }
287
238
  if (!isBalanceAdd) {
288
- for (const kve of keysOrNodesOrEntries) {
239
+ for (const kve of keysOrNodesOrEntriesOrRawElements) {
289
240
  const value = valuesIterator === null || valuesIterator === void 0 ? void 0 : valuesIterator.next().value;
290
241
  const nn = this.add(kve, value);
291
242
  inserted.push(nn);
@@ -298,25 +249,32 @@ class BST extends binary_tree_1.BinaryTree {
298
249
  return false;
299
250
  return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null));
300
251
  };
301
- for (const kve of keysOrNodesOrEntries) {
252
+ for (const kve of keysOrNodesOrEntriesOrRawElements) {
302
253
  isRealBTNExemplar(kve) && realBTNExemplars.push(kve);
303
254
  }
304
255
  let sorted = [];
305
256
  sorted = realBTNExemplars.sort((a, b) => {
306
257
  let keyA, keyB;
307
- if (this.isEntry(a)) {
258
+ if (this.isEntry(a))
308
259
  keyA = a[0];
309
- }
310
260
  else if (this.isRealNode(a))
311
261
  keyA = a.key;
312
- else
262
+ else if (this.toEntryFn) {
263
+ keyA = this.toEntryFn(a)[0];
264
+ }
265
+ else {
313
266
  keyA = a;
267
+ }
314
268
  if (this.isEntry(b))
315
269
  keyB = b[0];
316
270
  else if (this.isRealNode(b))
317
271
  keyB = b.key;
318
- else
272
+ else if (this.toEntryFn) {
273
+ keyB = this.toEntryFn(b)[0];
274
+ }
275
+ else {
319
276
  keyB = b;
277
+ }
320
278
  if (keyA !== undefined && keyA !== null && keyB !== undefined && keyB !== null) {
321
279
  return this.comparator(keyA, keyB);
322
280
  }
@@ -357,32 +315,26 @@ class BST extends binary_tree_1.BinaryTree {
357
315
  return inserted;
358
316
  }
359
317
  /**
360
- * Time Complexity: O(log n)
361
- * Space Complexity: O(k + log n)
362
- * /
363
-
364
- /**
365
318
  * Time Complexity: O(log n)
366
319
  * Space Complexity: O(k + log n)
367
320
  *
368
- * The function `getNodes` returns an array of nodes that match a given identifier, using either a
369
- * recursive or iterative approach.
321
+ * The `getNodes` function in TypeScript retrieves nodes from a binary tree based on a given
322
+ * identifier and callback function.
370
323
  * @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
371
- * want to search for in the nodes of the binary tree. It can be of any type that is returned by the
372
- * callback function `C`.
373
- * @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as its
374
- * argument and returns a value of type `ReturnType<C>`. The `C` type parameter represents a callback
375
- * function type that extends the `BTNCallback<NODE>` type. The `BTNCallback<NODE>` type is
376
- * @param [onlyOne=false] - A boolean flag indicating whether to stop searching after finding the
377
- * first node that matches the identifier. If set to true, the function will return an array
378
- * containing only the first matching node. If set to false (default), the function will continue
379
- * searching for all nodes that match the identifier and return an array containing
380
- * @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter represents the starting node
381
- * for the traversal. It can be either a key value or a node object. If it is undefined, the
382
- * traversal will start from the root of the tree.
383
- * @param iterationType - The `iterationType` parameter determines the type of iteration to be
384
- * performed on the binary tree. It can have two possible values:
385
- * @returns The method returns an array of nodes (`NODE[]`).
324
+ * want to search for in the binary tree. It can be of any type that is returned by the callback
325
+ * function.
326
+ * @param {C} callback - The `callback` parameter is a function that takes a node as input and
327
+ * returns a value. This value is used to identify the nodes that match the given identifier. The
328
+ * `callback` function is optional and defaults to `this._DEFAULT_CALLBACK`.
329
+ * @param [onlyOne=false] - A boolean value indicating whether to return only the first matching node
330
+ * or all matching nodes. If set to true, only the first matching node will be returned. If set to
331
+ * false, all matching nodes will be returned. The default value is false.
332
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
333
+ * point for the search in the binary tree. It can be either a node object, a key-value pair, or an
334
+ * entry object. If it is not provided, the `root` of the binary tree is used as the starting point.
335
+ * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
336
+ * iteration to be performed. It can have two possible values:
337
+ * @returns The method `getNodes` returns an array of `NODE` objects.
386
338
  */
387
339
  getNodes(identifier, callback = this._DEFAULT_CALLBACK, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
388
340
  beginRoot = this.ensureNode(beginRoot);
@@ -453,51 +405,50 @@ class BST extends binary_tree_1.BinaryTree {
453
405
  * Time Complexity: O(log n)
454
406
  * Space Complexity: O(1)
455
407
  *
456
- * The `getNode` function retrieves a node from a Red-Black Tree based on the provided identifier and
457
- * callback function.
458
- * @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value or key
459
- * that you want to search for in the binary search tree. It can be of any type that is compatible
460
- * with the type of nodes in the tree.
461
- * @param {C} callback - The `callback` parameter is a function that will be called for each node in
462
- * the tree. It is used to determine whether a node matches the given identifier. The `callback`
463
- * function should take a node as its parameter and return a value that can be compared to the
464
- * `identifier` parameter.
408
+ * The function `getNode` returns the first node that matches the given identifier and callback
409
+ * function in a binary search tree.
410
+ * @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
411
+ * want to search for in the binary search tree. It can be of any type that is compatible with the
412
+ * type returned by the callback function.
413
+ * @param {C} callback - The `callback` parameter is a function that will be used to determine if a
414
+ * node matches the desired criteria. It should be a function that takes a node as an argument and
415
+ * returns a boolean value indicating whether the node matches the criteria or not. If no callback is
416
+ * provided, the default callback will be
465
417
  * @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
466
- * search tree. It can be either a key or a node. If it is a key, it will be converted to a node
467
- * using the `ensureNode` method. If it is not provided, the `root`
468
- * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
469
- * be performed when searching for nodes in the binary search tree. It is an optional parameter and
470
- * its default value is taken from the `iterationType` property of the class.
471
- * @returns The method is returning a value of type `NODE | null | undefined`.
418
+ * search tree. It can be either a key or a node. If it is a key, the search will start from the node
419
+ * with that key. If it is a node, the search will start from that node.
420
+ * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
421
+ * of iteration to be performed when searching for nodes in the binary search tree. It can have one
422
+ * of the following values:
423
+ * @returns The method is returning a NODE object or undefined.
472
424
  */
473
425
  getNode(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
474
426
  var _a;
475
427
  return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
476
428
  }
477
429
  /**
478
- * Time Complexity: O(log n)
479
- * Space Complexity: O(1)
430
+ * Time Complexity: O(k log n)
431
+ * Space Complexity: O(k + log n)
480
432
  */
481
433
  /**
482
434
  * Time Complexity: O(log n)
483
435
  * Space Complexity: O(1)
484
436
  *
485
- * The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
486
- * either recursive or iterative methods.
487
- * @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
488
- * It is used to identify the node that we want to retrieve.
489
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
490
- * type of iteration to use when searching for a node in the binary tree. It can have two possible
491
- * values:
492
- * @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
493
- * found in the binary tree. If no node is found, it returns `undefined`.
437
+ * The function `getNodeByKey` returns a node with a specific key from a tree data structure.
438
+ * @param {K} key - The key parameter is the value used to search for a specific node in the tree. It
439
+ * is typically a unique identifier or a value that can be used to determine the position of the node
440
+ * in the tree structure.
441
+ * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
442
+ * parameter that specifies the type of iteration to be used when searching for a node in the tree.
443
+ * It has a default value of `'ITERATIVE'`.
444
+ * @returns The method is returning a NODE object or undefined.
494
445
  */
495
446
  getNodeByKey(key, iterationType = 'ITERATIVE') {
496
447
  return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
497
448
  }
498
449
  /**
499
- * Time complexity: O(n)
500
- * Space complexity: O(n)
450
+ * Time Complexity: O(log n)
451
+ * Space Complexity: O(k + log n)
501
452
  */
502
453
  /**
503
454
  * Time complexity: O(n)
@@ -506,15 +457,16 @@ class BST extends binary_tree_1.BinaryTree {
506
457
  * The function overrides the depth-first search method and returns an array of the return types of
507
458
  * the callback function.
508
459
  * @param {C} callback - The `callback` parameter is a function that will be called for each node
509
- * during the depth-first search traversal. It is an optional parameter and if not provided, a
510
- * default callback function will be used.
511
- * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter specifies the order in which the
512
- * nodes are visited during the depth-first search. It can have one of the following values:
513
- * @param beginRoot - The `beginRoot` parameter is used to specify the starting point for the
514
- * Depth-First Search (DFS) traversal. It can be either a key, a node, or an entry in the tree. If no
515
- * value is provided, the DFS traversal will start from the root of the tree.
516
- * @param {IterationType} iterationType - The `iterationType` parameter specifies the type of
517
- * iteration to be used during the Depth-First Search (DFS) traversal. It can have one of the
460
+ * during the depth-first search traversal. It is an optional parameter and defaults to
461
+ * `this._DEFAULT_CALLBACK`. The type `C` represents the type of the callback function.
462
+ * @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
463
+ * order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
464
+ * take one of the following values:
465
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
466
+ * point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
467
+ * node entry. If not specified, the default value is the root of the tree.
468
+ * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
469
+ * type of iteration to be used during the Depth-First Search (DFS) traversal. It can have one of the
518
470
  * following values:
519
471
  * @returns The method is returning an array of the return type of the callback function.
520
472
  */
@@ -522,8 +474,8 @@ class BST extends binary_tree_1.BinaryTree {
522
474
  return super.dfs(callback, pattern, beginRoot, iterationType, false);
523
475
  }
524
476
  /**
525
- * Time complexity: O(n)
526
- * Space complexity: O(n)
477
+ * Time Complexity: O(log n)
478
+ * Space Complexity: O(1)
527
479
  */
528
480
  /**
529
481
  * Time complexity: O(n)
@@ -532,38 +484,38 @@ class BST extends binary_tree_1.BinaryTree {
532
484
  * The function overrides the breadth-first search method and returns an array of the return types of
533
485
  * the callback function.
534
486
  * @param {C} callback - The `callback` parameter is a function that will be called for each node
535
- * visited during the breadth-first search traversal. It is an optional parameter and if not
536
- * provided, a default callback function will be used.
537
- * @param beginRoot - The `beginRoot` parameter is the starting point for the breadth-first search
538
- * traversal. It can be either a key, a node, or an entry in the tree. If not specified, the root of
539
- * the tree is used as the starting point.
540
- * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
541
- * be performed during the breadth-first search (BFS) traversal. It determines the order in which the
542
- * nodes are visited.
543
- * @returns The method is returning an array of the return type of the callback function.
487
+ * visited during the breadth-first search. It should take a single argument, which is the current
488
+ * node being visited, and it can return a value of any type.
489
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
490
+ * point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
491
+ * object. If no value is provided, the default value is the root of the tree.
492
+ * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
493
+ * of iteration to be performed during the breadth-first search (BFS) traversal. It can have one of
494
+ * the following values:
495
+ * @returns an array of the return type of the callback function.
544
496
  */
545
497
  bfs(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
546
498
  return super.bfs(callback, beginRoot, iterationType, false);
547
499
  }
548
500
  /**
549
- * Time complexity: O(n)
550
- * Space complexity: O(n)
501
+ * Time Complexity: O(log n)
502
+ * Space Complexity: O(1)
551
503
  */
552
504
  /**
553
505
  * Time complexity: O(n)
554
506
  * Space complexity: O(n)
555
507
  *
556
- * The function overrides the listLevels method and returns an array of arrays containing the return
557
- * type of the callback function for each level of the tree.
508
+ * The function overrides the listLevels method from the superclass and returns an array of arrays
509
+ * containing the results of the callback function applied to each level of the tree.
558
510
  * @param {C} callback - The `callback` parameter is a generic type `C` that extends
559
- * `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the tree
560
- * during the level listing process.
561
- * @param beginRoot - The `beginRoot` parameter is used to specify the starting point for listing the
562
- * levels of a binary tree. It can be either a key, a node, or an entry in the binary tree. If not
563
- * provided, the root of the binary tree is used as the starting point.
564
- * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
565
- * be performed on the tree. It determines the order in which the nodes are visited during the
566
- * iteration.
511
+ * `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the
512
+ * tree during the iteration process.
513
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
514
+ * point for listing the levels of the binary tree. It can be either a root node of the tree, a
515
+ * key-value pair representing a node in the tree, or a key representing a node in the tree. If no
516
+ * value is provided, the root of
517
+ * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
518
+ * of iteration to be performed on the tree. It can have one of the following values:
567
519
  * @returns The method is returning a two-dimensional array of the return type of the callback
568
520
  * function.
569
521
  */
@@ -571,27 +523,27 @@ class BST extends binary_tree_1.BinaryTree {
571
523
  return super.listLevels(callback, beginRoot, iterationType, false);
572
524
  }
573
525
  /**
574
- * Time Complexity: O(log n)
575
- * Space Complexity: O(log n)
526
+ * Time complexity: O(n)
527
+ * Space complexity: O(n)
576
528
  */
577
529
  /**
578
- * Time Complexity: O(log n)
579
- * Space Complexity: O(log n)
530
+ * Time complexity: O(n)
531
+ * Space complexity: O(n)
580
532
  *
581
- * The `lesserOrGreaterTraverse` function traverses a binary tree and returns an array of nodes that
582
- * are either lesser or greater than a target node, depending on the specified comparison type.
533
+ * The `lesserOrGreaterTraverse` function traverses a binary tree and applies a callback function to
534
+ * each node that meets a certain condition based on a target node and a comparison value.
583
535
  * @param {C} callback - The `callback` parameter is a function that will be called for each node
584
- * that satisfies the condition specified by the `lesserOrGreater` parameter. It takes a single
585
- * parameter of type `NODE` (the node type) and returns a value of any type.
536
+ * that meets the condition specified by the `lesserOrGreater` parameter. It takes a single argument,
537
+ * which is the current node being traversed, and returns a value of any type.
586
538
  * @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
587
- * traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It is of type
588
- * `CP`, which is a custom type representing the comparison operator. The possible values for
589
- * `lesserOrGreater` are
590
- * @param {K | NODE | undefined} targetNode - The `targetNode` parameter represents the node in the
591
- * binary tree that you want to traverse from. It can be specified either by its key, by the node
592
- * object itself, or it can be left undefined to start the traversal from the root of the tree.
593
- * @param iterationType - The `iterationType` parameter determines the type of traversal to be
594
- * performed on the binary tree. It can have two possible values:
539
+ * traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
540
+ * 0, or 1, where:
541
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} targetNode - The `targetNode` parameter is the node in
542
+ * the binary tree that you want to start traversing from. It can be specified either by providing
543
+ * the key of the node, the node itself, or an entry containing the key and value of the node. If no
544
+ * `targetNode` is provided,
545
+ * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
546
+ * traversal to be performed on the binary tree. It can have two possible values:
595
547
  * @returns The function `lesserOrGreaterTraverse` returns an array of values of type
596
548
  * `ReturnType<C>`, which is the return type of the callback function passed as an argument.
597
549
  */
@@ -634,18 +586,19 @@ class BST extends binary_tree_1.BinaryTree {
634
586
  }
635
587
  }
636
588
  /**
637
- * Time Complexity: O(log n)
638
- * Space Complexity: O(log n)
589
+ * Time complexity: O(n)
590
+ * Space complexity: O(n)
639
591
  */
640
592
  /**
641
- * Time Complexity: O(log n)
642
- * Space Complexity: O(log n)
593
+ * Time complexity: O(n)
594
+ * Space complexity: O(n)
643
595
  *
644
- * The `perfectlyBalance` function balances a binary search tree by adding nodes in a way that
645
- * ensures the tree is perfectly balanced.
646
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
647
- * type of iteration to use when building a balanced binary search tree. It can have two possible
648
- * values:
596
+ * The `perfectlyBalance` function takes an optional `iterationType` parameter and returns `true` if
597
+ * the binary search tree is perfectly balanced, otherwise it returns `false`.
598
+ * @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
599
+ * specifies the type of iteration to use when building a balanced binary search tree. It has a
600
+ * default value of `this.iterationType`, which means it will use the iteration type specified in the
601
+ * current instance of the class.
649
602
  * @returns The function `perfectlyBalance` returns a boolean value.
650
603
  */
651
604
  perfectlyBalance(iterationType = this.iterationType) {
@@ -685,25 +638,19 @@ class BST extends binary_tree_1.BinaryTree {
685
638
  }
686
639
  }
687
640
  /**
688
- * Balancing Adjustment:
689
- * Perfectly Balanced Binary Tree: Since the balance of a perfectly balanced binary tree is already fixed, no additional balancing adjustment is needed. Any insertion or deletion operation will disrupt the perfect balance, often requiring a complete reconstruction of the tree.
690
- * AVL Tree: After insertion or deletion operations, an AVL tree performs rotation adjustments based on the balance factor of nodes to restore the tree's balance. These rotations can be left rotations, right rotations, left-right rotations, or right-left rotations, performed as needed.
691
- *
692
- * Use Cases and Efficiency:
693
- * Perfectly Balanced Binary Tree: Perfectly balanced binary trees are typically used in specific scenarios such as complete binary heaps in heap sort or certain types of Huffman trees. However, they are not suitable for dynamic operations requiring frequent insertions and deletions, as these operations often necessitate full tree reconstruction.
694
- * AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
695
- */
696
- /**
697
- * Time Complexity: O(n)
698
- * Space Complexity: O(log n)
641
+ * Time complexity: O(n)
642
+ * Space complexity: O(n)
699
643
  */
700
644
  /**
701
645
  * Time Complexity: O(n)
702
646
  * Space Complexity: O(log n)
703
647
  *
704
- * The function checks if a binary tree is AVL balanced using either recursive or iterative approach.
705
- * @param iterationType - The `iterationType` parameter is used to determine the method of iteration
706
- * to check if the AVL tree is balanced. It can have two possible values:
648
+ * The function `isAVLBalanced` checks if a binary tree is AVL balanced using either a recursive or
649
+ * iterative approach.
650
+ * @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
651
+ * specifies the type of iteration to use when checking if the AVL tree is balanced. It has a default
652
+ * value of `this.iterationType`, which means it will use the iteration type specified in the current
653
+ * instance of the AVL tree.
707
654
  * @returns a boolean value.
708
655
  */
709
656
  isAVLBalanced(iterationType = this.iterationType) {
@@ -753,9 +700,20 @@ class BST extends binary_tree_1.BinaryTree {
753
700
  return balanced;
754
701
  }
755
702
  /**
756
- * The function sets the root property of an object and updates the parent property of the new root.
757
- * @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. This means that it
758
- * can either be an object of type `NODE` or it can be `undefined`.
703
+ * Time Complexity: O(n)
704
+ * Space Complexity: O(log n)
705
+ */
706
+ /**
707
+ * The function returns the value of the _comparator property.
708
+ * @returns The `_comparator` property is being returned.
709
+ */
710
+ get comparator() {
711
+ return this._comparator;
712
+ }
713
+ /**
714
+ * The function sets the root of a tree-like structure and updates the parent property of the new
715
+ * root.
716
+ * @param {NODE | undefined} v - v is a parameter of type NODE or undefined.
759
717
  */
760
718
  _setRoot(v) {
761
719
  if (v) {