min-heap-typed 1.48.5 → 1.48.7
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.
- package/dist/data-structures/binary-tree/binary-tree.d.ts +7 -16
- package/dist/data-structures/binary-tree/binary-tree.js +24 -19
- package/dist/data-structures/binary-tree/bst.d.ts +35 -11
- package/dist/data-structures/binary-tree/bst.js +58 -39
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +4 -4
- package/dist/data-structures/linked-list/doubly-linked-list.js +4 -4
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +4 -4
- package/dist/data-structures/linked-list/singly-linked-list.js +4 -4
- package/dist/data-structures/queue/deque.d.ts +9 -9
- package/dist/data-structures/queue/deque.js +9 -9
- package/dist/interfaces/binary-tree.d.ts +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +1 -1
- package/src/data-structures/binary-tree/binary-tree.ts +31 -20
- package/src/data-structures/binary-tree/bst.ts +73 -46
- package/src/data-structures/binary-tree/rb-tree.ts +1 -1
- package/src/data-structures/binary-tree/tree-multimap.ts +1 -1
- package/src/data-structures/hash/hash-map.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +4 -4
- package/src/data-structures/linked-list/singly-linked-list.ts +4 -4
- package/src/data-structures/queue/deque.ts +10 -10
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/interfaces/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
- package/src/types/data-structures/heap/heap.ts +1 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
|
@@ -121,27 +121,18 @@ export declare class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V,
|
|
|
121
121
|
* Time Complexity: O(k log n) - O(k * n)
|
|
122
122
|
* Space Complexity: O(1)
|
|
123
123
|
*
|
|
124
|
-
* The
|
|
125
|
-
*
|
|
126
|
-
* @param nodes -
|
|
127
|
-
*
|
|
128
|
-
* @returns The function `addMany` returns an array of
|
|
129
|
-
* `N`, `null`, or `undefined`.
|
|
124
|
+
* The `addMany` function takes in a collection of nodes and an optional collection of values, and
|
|
125
|
+
* adds each node with its corresponding value to the data structure.
|
|
126
|
+
* @param nodes - An iterable collection of BTNodeExemplar objects.
|
|
127
|
+
* @param [values] - An optional iterable of values that will be assigned to each node being added.
|
|
128
|
+
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
130
129
|
*/
|
|
131
|
-
addMany(nodes: Iterable<BTNodeExemplar<K, V, N
|
|
130
|
+
addMany(nodes: Iterable<BTNodeExemplar<K, V, N>>, values?: Iterable<V | undefined>): (N | null | undefined)[];
|
|
132
131
|
/**
|
|
133
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.
|
|
134
133
|
* Space Complexity: O(1)
|
|
135
134
|
*/
|
|
136
|
-
|
|
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.
|
|
138
|
-
* Space Complexity: O(1)
|
|
139
|
-
*
|
|
140
|
-
* The `refill` function clears the current collection and adds new nodes, keys, or entries to it.
|
|
141
|
-
* @param nodesOrKeysOrEntries - The parameter `nodesOrKeysOrEntries` is an iterable object that can
|
|
142
|
-
* contain either `BTNodeExemplar` objects, keys, or entries.
|
|
143
|
-
*/
|
|
144
|
-
refill(nodesOrKeysOrEntries: Iterable<BTNodeExemplar<K, V, N>>): void;
|
|
135
|
+
refill(nodesOrKeysOrEntries: Iterable<BTNodeExemplar<K, V, N>>, values?: Iterable<V | undefined>): void;
|
|
145
136
|
/**
|
|
146
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.
|
|
147
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) {
|
|
@@ -241,18 +244,28 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
241
244
|
* Time Complexity: O(k log n) - O(k * n)
|
|
242
245
|
* Space Complexity: O(1)
|
|
243
246
|
*
|
|
244
|
-
* The
|
|
245
|
-
*
|
|
246
|
-
* @param nodes -
|
|
247
|
-
*
|
|
248
|
-
* @returns The function `addMany` returns an array of
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
addMany(nodes) {
|
|
247
|
+
* The `addMany` function takes in a collection of nodes and an optional collection of values, and
|
|
248
|
+
* adds each node with its corresponding value to the data structure.
|
|
249
|
+
* @param nodes - An iterable collection of BTNodeExemplar objects.
|
|
250
|
+
* @param [values] - An optional iterable of values that will be assigned to each node being added.
|
|
251
|
+
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
252
|
+
*/
|
|
253
|
+
addMany(nodes, values) {
|
|
252
254
|
// TODO not sure addMany not be run multi times
|
|
253
255
|
const inserted = [];
|
|
256
|
+
let valuesIterator;
|
|
257
|
+
if (values) {
|
|
258
|
+
valuesIterator = values[Symbol.iterator]();
|
|
259
|
+
}
|
|
254
260
|
for (const kne of nodes) {
|
|
255
|
-
|
|
261
|
+
let value = undefined;
|
|
262
|
+
if (valuesIterator) {
|
|
263
|
+
const valueResult = valuesIterator.next();
|
|
264
|
+
if (!valueResult.done) {
|
|
265
|
+
value = valueResult.value;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
inserted.push(this.add(kne, value));
|
|
256
269
|
}
|
|
257
270
|
return inserted;
|
|
258
271
|
}
|
|
@@ -260,17 +273,9 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
260
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.
|
|
261
274
|
* Space Complexity: O(1)
|
|
262
275
|
*/
|
|
263
|
-
|
|
264
|
-
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
265
|
-
* Space Complexity: O(1)
|
|
266
|
-
*
|
|
267
|
-
* The `refill` function clears the current collection and adds new nodes, keys, or entries to it.
|
|
268
|
-
* @param nodesOrKeysOrEntries - The parameter `nodesOrKeysOrEntries` is an iterable object that can
|
|
269
|
-
* contain either `BTNodeExemplar` objects, keys, or entries.
|
|
270
|
-
*/
|
|
271
|
-
refill(nodesOrKeysOrEntries) {
|
|
276
|
+
refill(nodesOrKeysOrEntries, values) {
|
|
272
277
|
this.clear();
|
|
273
|
-
this.addMany(nodesOrKeysOrEntries);
|
|
278
|
+
this.addMany(nodesOrKeysOrEntries, values);
|
|
274
279
|
}
|
|
275
280
|
/**
|
|
276
281
|
* Time Complexity: O(n)
|
|
@@ -113,19 +113,43 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
113
113
|
* Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
|
|
114
114
|
* Space Complexity: O(k) - Additional space is required for the sorted array.
|
|
115
115
|
*
|
|
116
|
-
* The `addMany` function in TypeScript adds multiple nodes to a binary tree,
|
|
117
|
-
*
|
|
118
|
-
* @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries to be added to
|
|
119
|
-
* binary tree.
|
|
120
|
-
* @param [
|
|
121
|
-
*
|
|
116
|
+
* The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
|
|
117
|
+
* balancing the tree after each addition.
|
|
118
|
+
* @param keysOrNodesOrEntries - An iterable containing the keys, nodes, or entries to be added to
|
|
119
|
+
* the binary tree.
|
|
120
|
+
* @param [values] - An optional iterable of values to be associated with the keys or nodes being
|
|
121
|
+
* added. If provided, the values will be assigned to the corresponding keys or nodes in the same
|
|
122
|
+
* order. If not provided, undefined will be assigned as the value for each key or node.
|
|
123
|
+
* @param [isBalanceAdd=true] - A boolean flag indicating whether the add operation should be
|
|
124
|
+
* balanced or not. If set to true, the add operation will be balanced using a binary search tree
|
|
125
|
+
* algorithm. If set to false, the add operation will not be balanced and the elements will be added
|
|
126
|
+
* in the order they appear in the input.
|
|
122
127
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
123
|
-
* type of iteration to use when adding multiple keys or nodes
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
* @returns The `addMany` function returns an array of `N` or `undefined` values.
|
|
128
|
+
* type of iteration to use when adding multiple keys or nodes. It has a default value of
|
|
129
|
+
* `this.iterationType`, which suggests that it is a property of the current object.
|
|
130
|
+
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
127
131
|
*/
|
|
128
|
-
addMany(keysOrNodesOrEntries: Iterable<BTNodeExemplar<K, V, N>>, isBalanceAdd?: boolean, iterationType?: IterationType): (N | undefined)[];
|
|
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;
|
|
129
153
|
/**
|
|
130
154
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
131
155
|
* Space Complexity: O(1) - Constant space is used.
|
|
@@ -211,23 +211,32 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
211
211
|
* Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
|
|
212
212
|
* Space Complexity: O(k) - Additional space is required for the sorted array.
|
|
213
213
|
*
|
|
214
|
-
* The `addMany` function in TypeScript adds multiple nodes to a binary tree,
|
|
215
|
-
*
|
|
216
|
-
* @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries to be added to
|
|
217
|
-
* binary tree.
|
|
218
|
-
* @param [
|
|
219
|
-
*
|
|
214
|
+
* The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
|
|
215
|
+
* balancing the tree after each addition.
|
|
216
|
+
* @param keysOrNodesOrEntries - An iterable containing the keys, nodes, or entries to be added to
|
|
217
|
+
* the binary tree.
|
|
218
|
+
* @param [values] - An optional iterable of values to be associated with the keys or nodes being
|
|
219
|
+
* added. If provided, the values will be assigned to the corresponding keys or nodes in the same
|
|
220
|
+
* order. If not provided, undefined will be assigned as the value for each key or node.
|
|
221
|
+
* @param [isBalanceAdd=true] - A boolean flag indicating whether the add operation should be
|
|
222
|
+
* balanced or not. If set to true, the add operation will be balanced using a binary search tree
|
|
223
|
+
* algorithm. If set to false, the add operation will not be balanced and the elements will be added
|
|
224
|
+
* in the order they appear in the input.
|
|
220
225
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
221
|
-
* type of iteration to use when adding multiple keys or nodes
|
|
222
|
-
*
|
|
223
|
-
*
|
|
224
|
-
* @returns The `addMany` function returns an array of `N` or `undefined` values.
|
|
226
|
+
* type of iteration to use when adding multiple keys or nodes. It has a default value of
|
|
227
|
+
* `this.iterationType`, which suggests that it is a property of the current object.
|
|
228
|
+
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
225
229
|
*/
|
|
226
|
-
addMany(keysOrNodesOrEntries, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
230
|
+
addMany(keysOrNodesOrEntries, values, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
227
231
|
const inserted = [];
|
|
232
|
+
let valuesIterator;
|
|
233
|
+
if (values) {
|
|
234
|
+
valuesIterator = values[Symbol.iterator]();
|
|
235
|
+
}
|
|
228
236
|
if (!isBalanceAdd) {
|
|
229
237
|
for (const kve of keysOrNodesOrEntries) {
|
|
230
|
-
const
|
|
238
|
+
const value = valuesIterator === null || valuesIterator === void 0 ? void 0 : valuesIterator.next().value;
|
|
239
|
+
const nn = this.add(kve, value);
|
|
231
240
|
inserted.push(nn);
|
|
232
241
|
}
|
|
233
242
|
return inserted;
|
|
@@ -241,7 +250,6 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
241
250
|
for (const kve of keysOrNodesOrEntries) {
|
|
242
251
|
isRealBTNExemplar(kve) && realBTNExemplars.push(kve);
|
|
243
252
|
}
|
|
244
|
-
// TODO this addMany function is inefficient, it should be optimized
|
|
245
253
|
let sorted = [];
|
|
246
254
|
sorted = realBTNExemplars.sort((a, b) => {
|
|
247
255
|
let aR, bR;
|
|
@@ -293,31 +301,43 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
293
301
|
}
|
|
294
302
|
return inserted;
|
|
295
303
|
}
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
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
|
+
}
|
|
321
341
|
/**
|
|
322
342
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
323
343
|
* Space Complexity: O(1) - Constant space is used.
|
|
@@ -569,7 +589,6 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
569
589
|
if (l <= r) {
|
|
570
590
|
const m = l + Math.floor((r - l) / 2);
|
|
571
591
|
const midNode = sorted[m];
|
|
572
|
-
debugger;
|
|
573
592
|
this.add([midNode.key, midNode.value]);
|
|
574
593
|
stack.push([m + 1, r]);
|
|
575
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 `
|
|
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
|
-
|
|
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 `
|
|
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
|
-
|
|
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 `
|
|
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
|
-
|
|
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 `
|
|
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
|
-
|
|
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 `
|
|
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
|
-
|
|
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 `
|
|
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
|
-
|
|
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 `
|
|
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
|
-
|
|
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 `
|
|
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
|
-
|
|
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 "
|
|
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
|
-
|
|
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 "
|
|
88
|
-
* @returns The method `
|
|
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
|
-
|
|
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 `
|
|
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
|
-
|
|
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 `
|
|
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
|
-
|
|
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 "
|
|
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
|
-
|
|
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 "
|
|
136
|
-
* @returns The method `
|
|
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
|
-
|
|
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 `
|
|
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
|
-
|
|
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 `
|
|
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
|
-
|
|
916
|
+
pollLast() {
|
|
917
917
|
if (!this.size)
|
|
918
918
|
return;
|
|
919
919
|
const element = this.getLast();
|
|
@@ -4,6 +4,6 @@ export interface IBinaryTree<K = number, V = any, N extends BinaryTreeNode<K, V,
|
|
|
4
4
|
createNode(key: K, value?: N['value']): N;
|
|
5
5
|
createTree(options?: Partial<BinaryTreeOptions<K>>): TREE;
|
|
6
6
|
add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V, count?: number): N | null | undefined;
|
|
7
|
-
addMany(nodes: Iterable<BTNodeExemplar<K, V, N
|
|
7
|
+
addMany(nodes: Iterable<BTNodeExemplar<K, V, N>>, values?: Iterable<V | undefined>): (N | null | undefined)[];
|
|
8
8
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BiTreeDeleteResult<N>[];
|
|
9
9
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "min-heap-typed",
|
|
3
|
-
"version": "1.48.
|
|
3
|
+
"version": "1.48.7",
|
|
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.
|
|
135
|
+
"data-structure-typed": "^1.48.7"
|
|
136
136
|
}
|
|
137
137
|
}
|
|
@@ -99,7 +99,7 @@ export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeN
|
|
|
99
99
|
/**
|
|
100
100
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
|
|
101
101
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
102
|
-
*
|
|
102
|
+
*
|
|
103
103
|
* The function overrides the add method of a binary tree node and balances the tree after inserting
|
|
104
104
|
* a new node.
|
|
105
105
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
|
|
@@ -235,7 +235,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
235
235
|
/**
|
|
236
236
|
* Time Complexity O(log n) - O(n)
|
|
237
237
|
* Space Complexity O(1)
|
|
238
|
-
*
|
|
238
|
+
*
|
|
239
239
|
* The `add` function adds a new node to a binary tree, either by creating a new node or replacing an
|
|
240
240
|
* existing node with the same key.
|
|
241
241
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
|
|
@@ -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) {
|
|
@@ -288,38 +291,46 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
288
291
|
* Time Complexity: O(k log n) - O(k * n)
|
|
289
292
|
* Space Complexity: O(1)
|
|
290
293
|
*
|
|
291
|
-
* The
|
|
292
|
-
*
|
|
293
|
-
* @param nodes -
|
|
294
|
-
*
|
|
295
|
-
* @returns The function `addMany` returns an array of
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
addMany(nodes: Iterable<BTNodeExemplar<K, V, N>>): (N | null | undefined)[] {
|
|
294
|
+
* The `addMany` function takes in a collection of nodes and an optional collection of values, and
|
|
295
|
+
* adds each node with its corresponding value to the data structure.
|
|
296
|
+
* @param nodes - An iterable collection of BTNodeExemplar objects.
|
|
297
|
+
* @param [values] - An optional iterable of values that will be assigned to each node being added.
|
|
298
|
+
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
299
|
+
*/
|
|
300
|
+
addMany(nodes: Iterable<BTNodeExemplar<K, V, N>>, values?: Iterable<V | undefined>): (N | null | undefined)[] {
|
|
299
301
|
// TODO not sure addMany not be run multi times
|
|
300
302
|
const inserted: (N | null | undefined)[] = [];
|
|
303
|
+
|
|
304
|
+
let valuesIterator: Iterator<V | undefined> | undefined;
|
|
305
|
+
if (values) {
|
|
306
|
+
valuesIterator = values[Symbol.iterator]();
|
|
307
|
+
}
|
|
308
|
+
|
|
301
309
|
for (const kne of nodes) {
|
|
302
|
-
|
|
310
|
+
let value: V | undefined | null = undefined;
|
|
311
|
+
|
|
312
|
+
if (valuesIterator) {
|
|
313
|
+
const valueResult = valuesIterator.next();
|
|
314
|
+
if (!valueResult.done) {
|
|
315
|
+
value = valueResult.value;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
inserted.push(this.add(kne, value));
|
|
303
320
|
}
|
|
321
|
+
|
|
304
322
|
return inserted;
|
|
305
323
|
}
|
|
306
324
|
|
|
307
|
-
/**
|
|
308
|
-
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
309
|
-
* Space Complexity: O(1)
|
|
310
|
-
*/
|
|
311
325
|
|
|
312
326
|
/**
|
|
313
327
|
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
314
328
|
* Space Complexity: O(1)
|
|
315
|
-
*
|
|
316
|
-
* The `refill` function clears the current collection and adds new nodes, keys, or entries to it.
|
|
317
|
-
* @param nodesOrKeysOrEntries - The parameter `nodesOrKeysOrEntries` is an iterable object that can
|
|
318
|
-
* contain either `BTNodeExemplar` objects, keys, or entries.
|
|
319
329
|
*/
|
|
320
|
-
|
|
330
|
+
|
|
331
|
+
refill(nodesOrKeysOrEntries: Iterable<BTNodeExemplar<K, V, N>>, values?: Iterable<V | undefined>): void {
|
|
321
332
|
this.clear();
|
|
322
|
-
this.addMany(nodesOrKeysOrEntries);
|
|
333
|
+
this.addMany(nodesOrKeysOrEntries, values);
|
|
323
334
|
}
|
|
324
335
|
|
|
325
336
|
/**
|
|
@@ -192,7 +192,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
192
192
|
/**
|
|
193
193
|
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
194
194
|
* Space Complexity: O(1) - Constant space is used.
|
|
195
|
-
*
|
|
195
|
+
*
|
|
196
196
|
* The `add` function adds a new node to a binary tree, updating the value if the key already exists
|
|
197
197
|
* or inserting a new node if the key is unique.
|
|
198
198
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can accept three types of values:
|
|
@@ -256,31 +256,45 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
256
256
|
* Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
|
|
257
257
|
* Space Complexity: O(k) - Additional space is required for the sorted array.
|
|
258
258
|
*
|
|
259
|
-
* The `addMany` function in TypeScript adds multiple nodes to a binary tree,
|
|
260
|
-
*
|
|
261
|
-
* @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries to be added to
|
|
262
|
-
* binary tree.
|
|
263
|
-
* @param [
|
|
264
|
-
*
|
|
259
|
+
* The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
|
|
260
|
+
* balancing the tree after each addition.
|
|
261
|
+
* @param keysOrNodesOrEntries - An iterable containing the keys, nodes, or entries to be added to
|
|
262
|
+
* the binary tree.
|
|
263
|
+
* @param [values] - An optional iterable of values to be associated with the keys or nodes being
|
|
264
|
+
* added. If provided, the values will be assigned to the corresponding keys or nodes in the same
|
|
265
|
+
* order. If not provided, undefined will be assigned as the value for each key or node.
|
|
266
|
+
* @param [isBalanceAdd=true] - A boolean flag indicating whether the add operation should be
|
|
267
|
+
* balanced or not. If set to true, the add operation will be balanced using a binary search tree
|
|
268
|
+
* algorithm. If set to false, the add operation will not be balanced and the elements will be added
|
|
269
|
+
* in the order they appear in the input.
|
|
265
270
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
266
|
-
* type of iteration to use when adding multiple keys or nodes
|
|
267
|
-
*
|
|
268
|
-
*
|
|
269
|
-
* @returns The `addMany` function returns an array of `N` or `undefined` values.
|
|
271
|
+
* type of iteration to use when adding multiple keys or nodes. It has a default value of
|
|
272
|
+
* `this.iterationType`, which suggests that it is a property of the current object.
|
|
273
|
+
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
270
274
|
*/
|
|
271
275
|
override addMany(
|
|
272
276
|
keysOrNodesOrEntries: Iterable<BTNodeExemplar<K, V, N>>,
|
|
277
|
+
values?: Iterable<V | undefined>,
|
|
273
278
|
isBalanceAdd = true,
|
|
274
279
|
iterationType = this.iterationType
|
|
275
280
|
): (N | undefined)[] {
|
|
276
|
-
const inserted: (N | undefined)[] = []
|
|
281
|
+
const inserted: (N | undefined)[] = [];
|
|
282
|
+
|
|
283
|
+
let valuesIterator: Iterator<V | undefined> | undefined;
|
|
284
|
+
|
|
285
|
+
if (values) {
|
|
286
|
+
valuesIterator = values[Symbol.iterator]();
|
|
287
|
+
}
|
|
288
|
+
|
|
277
289
|
if (!isBalanceAdd) {
|
|
278
290
|
for (const kve of keysOrNodesOrEntries) {
|
|
279
|
-
const
|
|
291
|
+
const value = valuesIterator?.next().value;
|
|
292
|
+
const nn = this.add(kve, value);
|
|
280
293
|
inserted.push(nn);
|
|
281
294
|
}
|
|
282
295
|
return inserted;
|
|
283
296
|
}
|
|
297
|
+
|
|
284
298
|
const realBTNExemplars: BTNodePureExemplar<K, V, N>[] = [];
|
|
285
299
|
|
|
286
300
|
const isRealBTNExemplar = (kve: BTNodeExemplar<K, V, N>): kve is BTNodePureExemplar<K, V, N> => {
|
|
@@ -292,22 +306,20 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
292
306
|
isRealBTNExemplar(kve) && realBTNExemplars.push(kve);
|
|
293
307
|
}
|
|
294
308
|
|
|
295
|
-
// TODO this addMany function is inefficient, it should be optimized
|
|
296
309
|
let sorted: BTNodePureExemplar<K, V, N>[] = [];
|
|
297
310
|
|
|
298
311
|
sorted = realBTNExemplars.sort((a, b) => {
|
|
299
312
|
let aR: number, bR: number;
|
|
300
|
-
if (this.isEntry(a)) aR = this.extractor(a[0])
|
|
301
|
-
else if (this.isRealNode(a)) aR = this.extractor(a.key)
|
|
313
|
+
if (this.isEntry(a)) aR = this.extractor(a[0]);
|
|
314
|
+
else if (this.isRealNode(a)) aR = this.extractor(a.key);
|
|
302
315
|
else aR = this.extractor(a);
|
|
303
316
|
|
|
304
|
-
if (this.isEntry(b)) bR = this.extractor(b[0])
|
|
305
|
-
else if (this.isRealNode(b)) bR = this.extractor(b.key)
|
|
317
|
+
if (this.isEntry(b)) bR = this.extractor(b[0]);
|
|
318
|
+
else if (this.isRealNode(b)) bR = this.extractor(b.key);
|
|
306
319
|
else bR = this.extractor(b);
|
|
307
320
|
|
|
308
321
|
return aR - bR;
|
|
309
|
-
})
|
|
310
|
-
|
|
322
|
+
});
|
|
311
323
|
|
|
312
324
|
const _dfs = (arr: BTNodePureExemplar<K, V, N>[]) => {
|
|
313
325
|
if (arr.length === 0) return;
|
|
@@ -318,6 +330,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
318
330
|
_dfs(arr.slice(0, mid));
|
|
319
331
|
_dfs(arr.slice(mid + 1));
|
|
320
332
|
};
|
|
333
|
+
|
|
321
334
|
const _iterate = () => {
|
|
322
335
|
const n = sorted.length;
|
|
323
336
|
const stack: [[number, number]] = [[0, n - 1]];
|
|
@@ -335,6 +348,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
335
348
|
}
|
|
336
349
|
}
|
|
337
350
|
};
|
|
351
|
+
|
|
338
352
|
if (iterationType === IterationType.RECURSIVE) {
|
|
339
353
|
_dfs(sorted);
|
|
340
354
|
} else {
|
|
@@ -344,31 +358,45 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
344
358
|
return inserted;
|
|
345
359
|
}
|
|
346
360
|
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
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): 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
|
+
|
|
372
400
|
|
|
373
401
|
/**
|
|
374
402
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
@@ -621,7 +649,6 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
621
649
|
if (l <= r) {
|
|
622
650
|
const m = l + Math.floor((r - l) / 2);
|
|
623
651
|
const midNode = sorted[m];
|
|
624
|
-
debugger;
|
|
625
652
|
this.add([midNode.key, midNode.value]);
|
|
626
653
|
stack.push([m + 1, r]);
|
|
627
654
|
stack.push([l, m - 1]);
|
|
@@ -153,7 +153,7 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
|
|
|
153
153
|
/**
|
|
154
154
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
155
155
|
* Space Complexity: O(1)
|
|
156
|
-
*
|
|
156
|
+
*
|
|
157
157
|
* The `add` function adds a new node to a binary search tree and performs necessary rotations and
|
|
158
158
|
* color changes to maintain the red-black tree properties.
|
|
159
159
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
|
|
@@ -126,7 +126,7 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|
|
126
126
|
/**
|
|
127
127
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
128
128
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
129
|
-
*
|
|
129
|
+
*
|
|
130
130
|
* The function overrides the add method of a binary tree node and adds a new node to the tree.
|
|
131
131
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
|
|
132
132
|
* entry. It represents the key, node, or entry that you want to add to the binary tree.
|
|
@@ -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
|
|
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 `
|
|
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
|
-
|
|
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 `
|
|
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
|
-
|
|
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 `
|
|
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
|
-
|
|
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 `
|
|
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
|
-
|
|
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 "
|
|
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
|
-
|
|
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 "
|
|
147
|
-
* @returns The method `
|
|
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
|
-
|
|
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 `
|
|
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
|
-
|
|
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 `
|
|
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
|
-
|
|
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
|
+
}
|
|
@@ -15,7 +15,7 @@ export interface IBinaryTree<K = number, V = any, N extends BinaryTreeNode<K, V,
|
|
|
15
15
|
|
|
16
16
|
add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V, count?: number): N | null | undefined;
|
|
17
17
|
|
|
18
|
-
addMany(nodes: Iterable<BTNodeExemplar<K, V, N
|
|
18
|
+
addMany(nodes: Iterable<BTNodeExemplar<K, V, N>>, values?: Iterable<V | undefined>): (N | null | undefined)[];
|
|
19
19
|
|
|
20
20
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BiTreeDeleteResult<N>[];
|
|
21
21
|
}
|
|
@@ -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> & {};
|