min-heap-typed 1.48.6 → 1.48.8

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.
@@ -132,15 +132,7 @@ export declare class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V,
132
132
  * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
133
133
  * Space Complexity: O(1)
134
134
  */
135
- /**
136
- * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
137
- * Space Complexity: O(1)
138
- *
139
- * The `refill` function clears the current collection and adds new nodes, keys, or entries to it.
140
- * @param nodesOrKeysOrEntries - The parameter `nodesOrKeysOrEntries` is an iterable object that can
141
- * contain either `BTNodeExemplar` objects, keys, or entries.
142
- */
143
- refill(nodesOrKeysOrEntries: Iterable<BTNodeExemplar<K, V, N>>): void;
135
+ refill(nodesOrKeysOrEntries: Iterable<BTNodeExemplar<K, V, N>>, values?: Iterable<V | undefined>): void;
144
136
  /**
145
137
  * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
146
138
  * Space Complexity: O(1)
@@ -200,6 +200,9 @@ class BinaryTree extends base_1.IterableEntryBase {
200
200
  const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
201
201
  if (newNode === undefined)
202
202
  return;
203
+ // TODO There are still some problems with the way duplicate nodes are handled
204
+ if (newNode !== null && this.has(newNode.key))
205
+ return undefined;
203
206
  const _bfs = (root, newNode) => {
204
207
  const queue = new queue_1.Queue([root]);
205
208
  while (queue.size > 0) {
@@ -270,17 +273,9 @@ class BinaryTree extends base_1.IterableEntryBase {
270
273
  * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
271
274
  * Space Complexity: O(1)
272
275
  */
273
- /**
274
- * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
275
- * Space Complexity: O(1)
276
- *
277
- * The `refill` function clears the current collection and adds new nodes, keys, or entries to it.
278
- * @param nodesOrKeysOrEntries - The parameter `nodesOrKeysOrEntries` is an iterable object that can
279
- * contain either `BTNodeExemplar` objects, keys, or entries.
280
- */
281
- refill(nodesOrKeysOrEntries) {
276
+ refill(nodesOrKeysOrEntries, values) {
282
277
  this.clear();
283
- this.addMany(nodesOrKeysOrEntries);
278
+ this.addMany(nodesOrKeysOrEntries, values);
284
279
  }
285
280
  /**
286
281
  * Time Complexity: O(n)
@@ -130,6 +130,26 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
130
130
  * @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
131
131
  */
132
132
  addMany(keysOrNodesOrEntries: Iterable<BTNodeExemplar<K, V, N>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): (N | undefined)[];
133
+ /**
134
+ * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
135
+ * Space Complexity: O(n) - Additional space is required for the sorted array.
136
+ */
137
+ /**
138
+ * Time Complexity: O(log n) - Average case for a balanced tree.
139
+ * Space Complexity: O(1) - Constant space is used.
140
+ *
141
+ * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
142
+ * leftmost node if the comparison result is greater than.
143
+ * @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
144
+ * type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
145
+ * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
146
+ * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
147
+ * be performed. It can have one of the following values:
148
+ * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
149
+ * the key of the leftmost node if the comparison result is greater than, and the key of the
150
+ * rightmost node otherwise. If no node is found, it returns 0.
151
+ */
152
+ lastKey(beginRoot?: BSTNodeKeyOrNode<K, N>): K | undefined;
133
153
  /**
134
154
  * Time Complexity: O(log n) - Average case for a balanced tree.
135
155
  * Space Complexity: O(1) - Constant space is used.
@@ -301,31 +301,43 @@ class BST extends binary_tree_1.BinaryTree {
301
301
  }
302
302
  return inserted;
303
303
  }
304
- // /**
305
- // * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
306
- // * Space Complexity: O(n) - Additional space is required for the sorted array.
307
- // */
308
- //
309
- // /**
310
- // * Time Complexity: O(log n) - Average case for a balanced tree.
311
- // * Space Complexity: O(1) - Constant space is used.
312
- // *
313
- // * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
314
- // * leftmost node if the comparison result is greater than.
315
- // * @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
316
- // * type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
317
- // * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
318
- // * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
319
- // * be performed. It can have one of the following values:
320
- // * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
321
- // * the key of the leftmost node if the comparison result is greater than, and the key of the
322
- // * rightmost node otherwise. If no node is found, it returns 0.
323
- // */
324
- // lastKey(beginRoot: BSTNodeKeyOrNode<K,N> = this.root, iterationType = this.iterationType): K {
325
- // if (this._compare(0, 1) === CP.lt) return this.getRightMost(beginRoot, iterationType)?.key ?? 0;
326
- // else if (this._compare(0, 1) === CP.gt) return this.getLeftMost(beginRoot, iterationType)?.key ?? 0;
327
- // else return this.getRightMost(beginRoot, iterationType)?.key ?? 0;
328
- // }
304
+ /**
305
+ * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
306
+ * Space Complexity: O(n) - Additional space is required for the sorted array.
307
+ */
308
+ /**
309
+ * Time Complexity: O(log n) - Average case for a balanced tree.
310
+ * Space Complexity: O(1) - Constant space is used.
311
+ *
312
+ * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
313
+ * leftmost node if the comparison result is greater than.
314
+ * @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
315
+ * type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
316
+ * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
317
+ * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
318
+ * be performed. It can have one of the following values:
319
+ * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
320
+ * the key of the leftmost node if the comparison result is greater than, and the key of the
321
+ * rightmost node otherwise. If no node is found, it returns 0.
322
+ */
323
+ lastKey(beginRoot = this.root) {
324
+ let current = this.ensureNode(beginRoot);
325
+ if (!current)
326
+ return undefined;
327
+ if (this._variant === types_1.BSTVariant.MIN) {
328
+ // For BSTVariant.MIN, find the rightmost node
329
+ while (current.right !== undefined) {
330
+ current = current.right;
331
+ }
332
+ }
333
+ else {
334
+ // For BSTVariant.MAX, find the leftmost node
335
+ while (current.left !== undefined) {
336
+ current = current.left;
337
+ }
338
+ }
339
+ return current.key;
340
+ }
329
341
  /**
330
342
  * Time Complexity: O(log n) - Average case for a balanced tree.
331
343
  * Space Complexity: O(1) - Constant space is used.
@@ -577,7 +589,6 @@ class BST extends binary_tree_1.BinaryTree {
577
589
  if (l <= r) {
578
590
  const m = l + Math.floor((r - l) / 2);
579
591
  const midNode = sorted[m];
580
- debugger;
581
592
  this.add([midNode.key, midNode.value]);
582
593
  stack.push([m + 1, r]);
583
594
  stack.push([l, m - 1]);
@@ -89,11 +89,11 @@ export declare class DoublyLinkedList<E = any> extends IterableElementBase<E> {
89
89
  * Time Complexity: O(1)
90
90
  * Space Complexity: O(1)
91
91
  *
92
- * The `popLast()` function removes and returns the value of the last node in a doubly linked list.
92
+ * The `pollLast()` function removes and returns the value of the last node in a doubly linked list.
93
93
  * @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
94
94
  * list is empty, it returns undefined.
95
95
  */
96
- popLast(): E | undefined;
96
+ pollLast(): E | undefined;
97
97
  /**
98
98
  * Time Complexity: O(1)
99
99
  * Space Complexity: O(1)
@@ -115,11 +115,11 @@ export declare class DoublyLinkedList<E = any> extends IterableElementBase<E> {
115
115
  * Time Complexity: O(1)
116
116
  * Space Complexity: O(1)
117
117
  *
118
- * The `popFirst()` function removes and returns the value of the first node in a doubly linked list.
118
+ * The `pollFirst()` function removes and returns the value of the first node in a doubly linked list.
119
119
  * @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
120
120
  * list.
121
121
  */
122
- popFirst(): E | undefined;
122
+ pollFirst(): E | undefined;
123
123
  /**
124
124
  * Time Complexity: O(1)
125
125
  * Space Complexity: O(1)
@@ -142,11 +142,11 @@ class DoublyLinkedList extends base_1.IterableElementBase {
142
142
  * Time Complexity: O(1)
143
143
  * Space Complexity: O(1)
144
144
  *
145
- * The `popLast()` function removes and returns the value of the last node in a doubly linked list.
145
+ * The `pollLast()` function removes and returns the value of the last node in a doubly linked list.
146
146
  * @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
147
147
  * list is empty, it returns undefined.
148
148
  */
149
- popLast() {
149
+ pollLast() {
150
150
  return this.pop();
151
151
  }
152
152
  /**
@@ -184,11 +184,11 @@ class DoublyLinkedList extends base_1.IterableElementBase {
184
184
  * Time Complexity: O(1)
185
185
  * Space Complexity: O(1)
186
186
  *
187
- * The `popFirst()` function removes and returns the value of the first node in a doubly linked list.
187
+ * The `pollFirst()` function removes and returns the value of the first node in a doubly linked list.
188
188
  * @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
189
189
  * list.
190
190
  */
191
- popFirst() {
191
+ pollFirst() {
192
192
  return this.shift();
193
193
  }
194
194
  /**
@@ -90,12 +90,12 @@ export declare class SinglyLinkedList<E = any> extends IterableElementBase<E> {
90
90
  * Time Complexity: O(n) - Linear time in the worst case, as it may need to traverse the list to find the last element.
91
91
  * Space Complexity: O(1) - Constant space.
92
92
  *
93
- * The `popLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
93
+ * The `pollLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
94
94
  * pointers accordingly.
95
95
  * @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
96
96
  * the linked list is empty, it returns `undefined`.
97
97
  */
98
- popLast(): E | undefined;
98
+ pollLast(): E | undefined;
99
99
  /**
100
100
  * Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
101
101
  * Space Complexity: O(1) - Constant space.
@@ -116,10 +116,10 @@ export declare class SinglyLinkedList<E = any> extends IterableElementBase<E> {
116
116
  * Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
117
117
  * Space Complexity: O(1) - Constant space.
118
118
  *
119
- * The `popFirst()` function removes and returns the value of the first node in a linked list.
119
+ * The `pollFirst()` function removes and returns the value of the first node in a linked list.
120
120
  * @returns The value of the node that is being removed from the beginning of the linked list.
121
121
  */
122
- popFirst(): E | undefined;
122
+ pollFirst(): E | undefined;
123
123
  /**
124
124
  * Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
125
125
  * Space Complexity: O(1) - Constant space.
@@ -144,12 +144,12 @@ class SinglyLinkedList extends base_1.IterableElementBase {
144
144
  * Time Complexity: O(n) - Linear time in the worst case, as it may need to traverse the list to find the last element.
145
145
  * Space Complexity: O(1) - Constant space.
146
146
  *
147
- * The `popLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
147
+ * The `pollLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
148
148
  * pointers accordingly.
149
149
  * @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
150
150
  * the linked list is empty, it returns `undefined`.
151
151
  */
152
- popLast() {
152
+ pollLast() {
153
153
  return this.pop();
154
154
  }
155
155
  /**
@@ -179,10 +179,10 @@ class SinglyLinkedList extends base_1.IterableElementBase {
179
179
  * Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
180
180
  * Space Complexity: O(1) - Constant space.
181
181
  *
182
- * The `popFirst()` function removes and returns the value of the first node in a linked list.
182
+ * The `pollFirst()` function removes and returns the value of the first node in a linked list.
183
183
  * @returns The value of the node that is being removed from the beginning of the linked list.
184
184
  */
185
- popFirst() {
185
+ pollFirst() {
186
186
  return this.shift();
187
187
  }
188
188
  /**
@@ -67,10 +67,10 @@ export declare class Deque<E> extends IterableElementBase<E> {
67
67
  * Time Complexity: O(1) - Removes the last element.
68
68
  * Space Complexity: O(1) - Operates in-place.
69
69
  *
70
- * The function "popLast" removes and returns the last element of an array.
70
+ * The function "pollLast" removes and returns the last element of an array.
71
71
  * @returns The last element of the array is being returned.
72
72
  */
73
- popLast(): E | undefined;
73
+ pollLast(): E | undefined;
74
74
  /**
75
75
  * Time Complexity: O(1).
76
76
  * Space Complexity: O(n) - Due to potential resizing.
@@ -84,11 +84,11 @@ export declare class Deque<E> extends IterableElementBase<E> {
84
84
  * Time Complexity: O(1) - Removes the first element.
85
85
  * Space Complexity: O(1) - In-place operation.
86
86
  *
87
- * The function "popFirst" removes and returns the first element of an array.
88
- * @returns The method `popFirst()` is returning the first element of the array after removing it
87
+ * The function "pollFirst" removes and returns the first element of an array.
88
+ * @returns The method `pollFirst()` is returning the first element of the array after removing it
89
89
  * from the beginning. If the array is empty, it will return `undefined`.
90
90
  */
91
- popFirst(): E | undefined;
91
+ pollFirst(): E | undefined;
92
92
  /**
93
93
  * The clear() function resets the state of the object by initializing all variables to their default
94
94
  * values.
@@ -488,10 +488,10 @@ export declare class ObjectDeque<E = number> {
488
488
  * Time Complexity: O(1)
489
489
  * Space Complexity: O(1)
490
490
  *
491
- * The function `popFirst()` removes and returns the first element in a data structure.
491
+ * The function `pollFirst()` removes and returns the first element in a data structure.
492
492
  * @returns The element of the first element in the data structure.
493
493
  */
494
- popFirst(): E | undefined;
494
+ pollFirst(): E | undefined;
495
495
  /**
496
496
  * Time Complexity: O(1)
497
497
  * Space Complexity: O(1)
@@ -512,10 +512,10 @@ export declare class ObjectDeque<E = number> {
512
512
  * Time Complexity: O(1)
513
513
  * Space Complexity: O(1)
514
514
  *
515
- * The `popLast()` function removes and returns the last element in a data structure.
515
+ * The `pollLast()` function removes and returns the last element in a data structure.
516
516
  * @returns The element that was removed from the data structure.
517
517
  */
518
- popLast(): E | undefined;
518
+ pollLast(): E | undefined;
519
519
  /**
520
520
  * Time Complexity: O(1)
521
521
  * Space Complexity: O(1)
@@ -111,10 +111,10 @@ class Deque extends base_1.IterableElementBase {
111
111
  * Time Complexity: O(1) - Removes the last element.
112
112
  * Space Complexity: O(1) - Operates in-place.
113
113
  *
114
- * The function "popLast" removes and returns the last element of an array.
114
+ * The function "pollLast" removes and returns the last element of an array.
115
115
  * @returns The last element of the array is being returned.
116
116
  */
117
- popLast() {
117
+ pollLast() {
118
118
  return this.pop();
119
119
  }
120
120
  /**
@@ -132,11 +132,11 @@ class Deque extends base_1.IterableElementBase {
132
132
  * Time Complexity: O(1) - Removes the first element.
133
133
  * Space Complexity: O(1) - In-place operation.
134
134
  *
135
- * The function "popFirst" removes and returns the first element of an array.
136
- * @returns The method `popFirst()` is returning the first element of the array after removing it
135
+ * The function "pollFirst" removes and returns the first element of an array.
136
+ * @returns The method `pollFirst()` is returning the first element of the array after removing it
137
137
  * from the beginning. If the array is empty, it will return `undefined`.
138
138
  */
139
- popFirst() {
139
+ pollFirst() {
140
140
  return this.shift();
141
141
  }
142
142
  /**
@@ -875,10 +875,10 @@ class ObjectDeque {
875
875
  * Time Complexity: O(1)
876
876
  * Space Complexity: O(1)
877
877
  *
878
- * The function `popFirst()` removes and returns the first element in a data structure.
878
+ * The function `pollFirst()` removes and returns the first element in a data structure.
879
879
  * @returns The element of the first element in the data structure.
880
880
  */
881
- popFirst() {
881
+ pollFirst() {
882
882
  if (!this.size)
883
883
  return;
884
884
  const element = this.getFirst();
@@ -910,10 +910,10 @@ class ObjectDeque {
910
910
  * Time Complexity: O(1)
911
911
  * Space Complexity: O(1)
912
912
  *
913
- * The `popLast()` function removes and returns the last element in a data structure.
913
+ * The `pollLast()` function removes and returns the last element in a data structure.
914
914
  * @returns The element that was removed from the data structure.
915
915
  */
916
- popLast() {
916
+ pollLast() {
917
917
  if (!this.size)
918
918
  return;
919
919
  const element = this.getLast();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "min-heap-typed",
3
- "version": "1.48.6",
3
+ "version": "1.48.8",
4
4
  "description": "Min Heap. Javascript & Typescript Data Structure.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -132,6 +132,6 @@
132
132
  "typescript": "^4.9.5"
133
133
  },
134
134
  "dependencies": {
135
- "data-structure-typed": "^1.48.6"
135
+ "data-structure-typed": "^1.48.8"
136
136
  }
137
137
  }
@@ -248,6 +248,9 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
248
248
  const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
249
249
  if (newNode === undefined) return;
250
250
 
251
+ // TODO There are still some problems with the way duplicate nodes are handled
252
+ if (newNode !== null && this.has(newNode.key)) return undefined;
253
+
251
254
  const _bfs = (root: N, newNode: N | null): N | undefined | null => {
252
255
  const queue = new Queue<N>([root]);
253
256
  while (queue.size > 0) {
@@ -325,17 +328,9 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
325
328
  * Space Complexity: O(1)
326
329
  */
327
330
 
328
- /**
329
- * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
330
- * Space Complexity: O(1)
331
- *
332
- * The `refill` function clears the current collection and adds new nodes, keys, or entries to it.
333
- * @param nodesOrKeysOrEntries - The parameter `nodesOrKeysOrEntries` is an iterable object that can
334
- * contain either `BTNodeExemplar` objects, keys, or entries.
335
- */
336
- refill(nodesOrKeysOrEntries: Iterable<BTNodeExemplar<K, V, N>>): void {
331
+ refill(nodesOrKeysOrEntries: Iterable<BTNodeExemplar<K, V, N>>, values?: Iterable<V | undefined>): void {
337
332
  this.clear();
338
- this.addMany(nodesOrKeysOrEntries);
333
+ this.addMany(nodesOrKeysOrEntries, values);
339
334
  }
340
335
 
341
336
  /**
@@ -359,31 +359,44 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
359
359
  }
360
360
 
361
361
 
362
- // /**
363
- // * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
364
- // * Space Complexity: O(n) - Additional space is required for the sorted array.
365
- // */
366
- //
367
- // /**
368
- // * Time Complexity: O(log n) - Average case for a balanced tree.
369
- // * Space Complexity: O(1) - Constant space is used.
370
- // *
371
- // * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
372
- // * leftmost node if the comparison result is greater than.
373
- // * @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
374
- // * type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
375
- // * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
376
- // * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
377
- // * be performed. It can have one of the following values:
378
- // * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
379
- // * the key of the leftmost node if the comparison result is greater than, and the key of the
380
- // * rightmost node otherwise. If no node is found, it returns 0.
381
- // */
382
- // lastKey(beginRoot: BSTNodeKeyOrNode<K,N> = this.root, iterationType = this.iterationType): K {
383
- // if (this._compare(0, 1) === CP.lt) return this.getRightMost(beginRoot, iterationType)?.key ?? 0;
384
- // else if (this._compare(0, 1) === CP.gt) return this.getLeftMost(beginRoot, iterationType)?.key ?? 0;
385
- // else return this.getRightMost(beginRoot, iterationType)?.key ?? 0;
386
- // }
362
+ /**
363
+ * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
364
+ * Space Complexity: O(n) - Additional space is required for the sorted array.
365
+ */
366
+
367
+ /**
368
+ * Time Complexity: O(log n) - Average case for a balanced tree.
369
+ * Space Complexity: O(1) - Constant space is used.
370
+ *
371
+ * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
372
+ * leftmost node if the comparison result is greater than.
373
+ * @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
374
+ * type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
375
+ * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
376
+ * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
377
+ * be performed. It can have one of the following values:
378
+ * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
379
+ * the key of the leftmost node if the comparison result is greater than, and the key of the
380
+ * rightmost node otherwise. If no node is found, it returns 0.
381
+ */
382
+ lastKey(beginRoot: BSTNodeKeyOrNode<K, N> = this.root): K | undefined {
383
+ let current = this.ensureNode(beginRoot);
384
+ if (!current) return undefined;
385
+
386
+ if (this._variant === BSTVariant.MIN) {
387
+ // For BSTVariant.MIN, find the rightmost node
388
+ while (current.right !== undefined) {
389
+ current = current.right;
390
+ }
391
+ } else {
392
+ // For BSTVariant.MAX, find the leftmost node
393
+ while (current.left !== undefined) {
394
+ current = current.left;
395
+ }
396
+ }
397
+ return current.key;
398
+ }
399
+
387
400
 
388
401
  /**
389
402
  * Time Complexity: O(log n) - Average case for a balanced tree.
@@ -636,7 +649,6 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
636
649
  if (l <= r) {
637
650
  const m = l + Math.floor((r - l) / 2);
638
651
  const midNode = sorted[m];
639
- debugger;
640
652
  this.add([midNode.key, midNode.value]);
641
653
  stack.push([m + 1, r]);
642
654
  stack.push([l, m - 1]);
@@ -625,7 +625,7 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
625
625
  protected* _getIterator() {
626
626
  let node = this._head;
627
627
  while (node !== this._sentinel) {
628
- yield <[K, V]>[node.key, node.value];
628
+ yield [node.key, node.value] as [K, V];
629
629
  node = node.next;
630
630
  }
631
631
  }
@@ -162,11 +162,11 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
162
162
  * Time Complexity: O(1)
163
163
  * Space Complexity: O(1)
164
164
  *
165
- * The `popLast()` function removes and returns the value of the last node in a doubly linked list.
165
+ * The `pollLast()` function removes and returns the value of the last node in a doubly linked list.
166
166
  * @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
167
167
  * list is empty, it returns undefined.
168
168
  */
169
- popLast(): E | undefined {
169
+ pollLast(): E | undefined {
170
170
  return this.pop();
171
171
  }
172
172
 
@@ -206,11 +206,11 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
206
206
  * Time Complexity: O(1)
207
207
  * Space Complexity: O(1)
208
208
  *
209
- * The `popFirst()` function removes and returns the value of the first node in a doubly linked list.
209
+ * The `pollFirst()` function removes and returns the value of the first node in a doubly linked list.
210
210
  * @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
211
211
  * list.
212
212
  */
213
- popFirst(): E | undefined {
213
+ pollFirst(): E | undefined {
214
214
  return this.shift();
215
215
  }
216
216
 
@@ -164,12 +164,12 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
164
164
  * Time Complexity: O(n) - Linear time in the worst case, as it may need to traverse the list to find the last element.
165
165
  * Space Complexity: O(1) - Constant space.
166
166
  *
167
- * The `popLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
167
+ * The `pollLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
168
168
  * pointers accordingly.
169
169
  * @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
170
170
  * the linked list is empty, it returns `undefined`.
171
171
  */
172
- popLast(): E | undefined {
172
+ pollLast(): E | undefined {
173
173
  return this.pop();
174
174
  }
175
175
 
@@ -202,10 +202,10 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
202
202
  * Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
203
203
  * Space Complexity: O(1) - Constant space.
204
204
  *
205
- * The `popFirst()` function removes and returns the value of the first node in a linked list.
205
+ * The `pollFirst()` function removes and returns the value of the first node in a linked list.
206
206
  * @returns The value of the node that is being removed from the beginning of the linked list.
207
207
  */
208
- popFirst(): E | undefined {
208
+ pollFirst(): E | undefined {
209
209
  return this.shift();
210
210
  }
211
211
 
@@ -120,10 +120,10 @@ export class Deque<E> extends IterableElementBase<E> {
120
120
  * Time Complexity: O(1) - Removes the last element.
121
121
  * Space Complexity: O(1) - Operates in-place.
122
122
  *
123
- * The function "popLast" removes and returns the last element of an array.
123
+ * The function "pollLast" removes and returns the last element of an array.
124
124
  * @returns The last element of the array is being returned.
125
125
  */
126
- popLast(): E | undefined {
126
+ pollLast(): E | undefined {
127
127
  return this.pop();
128
128
  }
129
129
 
@@ -143,11 +143,11 @@ export class Deque<E> extends IterableElementBase<E> {
143
143
  * Time Complexity: O(1) - Removes the first element.
144
144
  * Space Complexity: O(1) - In-place operation.
145
145
  *
146
- * The function "popFirst" removes and returns the first element of an array.
147
- * @returns The method `popFirst()` is returning the first element of the array after removing it
146
+ * The function "pollFirst" removes and returns the first element of an array.
147
+ * @returns The method `pollFirst()` is returning the first element of the array after removing it
148
148
  * from the beginning. If the array is empty, it will return `undefined`.
149
149
  */
150
- popFirst(): E | undefined {
150
+ pollFirst(): E | undefined {
151
151
  return this.shift();
152
152
  }
153
153
 
@@ -947,10 +947,10 @@ export class ObjectDeque<E = number> {
947
947
  * Time Complexity: O(1)
948
948
  * Space Complexity: O(1)
949
949
  *
950
- * The function `popFirst()` removes and returns the first element in a data structure.
950
+ * The function `pollFirst()` removes and returns the first element in a data structure.
951
951
  * @returns The element of the first element in the data structure.
952
952
  */
953
- popFirst() {
953
+ pollFirst() {
954
954
  if (!this.size) return;
955
955
  const element = this.getFirst();
956
956
  delete this.nodes[this.first];
@@ -984,10 +984,10 @@ export class ObjectDeque<E = number> {
984
984
  * Time Complexity: O(1)
985
985
  * Space Complexity: O(1)
986
986
  *
987
- * The `popLast()` function removes and returns the last element in a data structure.
987
+ * The `pollLast()` function removes and returns the last element in a data structure.
988
988
  * @returns The element that was removed from the data structure.
989
989
  */
990
- popLast() {
990
+ pollLast() {
991
991
  if (!this.size) return;
992
992
  const element = this.getLast();
993
993
  delete this.nodes[this.last];
@@ -1039,4 +1039,4 @@ export class ObjectDeque<E = number> {
1039
1039
  isEmpty() {
1040
1040
  return this.size <= 0;
1041
1041
  }
1042
- }
1042
+ }
@@ -378,4 +378,4 @@ export class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
378
378
  peek(): E | undefined {
379
379
  return this.getFirst();
380
380
  }
381
- }
381
+ }
@@ -7,4 +7,4 @@ export type RedBlackTreeNodeNested<K, V> = RedBlackTreeNode<K, V, RedBlackTreeNo
7
7
 
8
8
  export type RedBlackTreeNested<K, V, N extends RedBlackTreeNode<K, V, N>> = RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
9
9
 
10
- export type RBTreeOptions<K> = BSTOptions<K> & {};
10
+ export type RBTreeOptions<K> = BSTOptions<K> & {};
@@ -1,3 +1,3 @@
1
1
  import { Comparator } from "../../common";
2
2
 
3
- export type HeapOptions<T> = { comparator: Comparator<T> }
3
+ export type HeapOptions<T> = { comparator: Comparator<T> }
@@ -1,3 +1,3 @@
1
1
  import { HeapOptions } from "../heap";
2
2
 
3
- export type PriorityQueueOptions<T> = HeapOptions<T> & {}
3
+ export type PriorityQueueOptions<T> = HeapOptions<T> & {}