min-heap-typed 1.51.5 → 1.51.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/avl-tree-multi-map.js +1 -0
- package/dist/data-structures/binary-tree/avl-tree.js +0 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +2 -1
- package/dist/data-structures/binary-tree/binary-tree.js +24 -46
- package/dist/data-structures/binary-tree/bst.d.ts +19 -19
- package/dist/data-structures/binary-tree/bst.js +59 -89
- package/dist/data-structures/binary-tree/rb-tree.d.ts +10 -1
- package/dist/data-structures/binary-tree/rb-tree.js +19 -0
- package/dist/data-structures/binary-tree/tree-multi-map.js +1 -0
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +1 -0
- package/src/data-structures/binary-tree/avl-tree.ts +0 -1
- package/src/data-structures/binary-tree/binary-tree.ts +27 -38
- package/src/data-structures/binary-tree/bst.ts +59 -81
- package/src/data-structures/binary-tree/rb-tree.ts +19 -2
- package/src/data-structures/binary-tree/tree-multi-map.ts +1 -0
|
@@ -199,6 +199,7 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
199
199
|
const deletedResult = [];
|
|
200
200
|
if (!this.root)
|
|
201
201
|
return deletedResult;
|
|
202
|
+
callback = this._ensureCallback(identifier, callback);
|
|
202
203
|
const curr = (_a = this.getNode(identifier, callback)) !== null && _a !== void 0 ? _a : undefined;
|
|
203
204
|
if (!curr)
|
|
204
205
|
return deletedResult;
|
|
@@ -138,8 +138,6 @@ class AVLTree extends bst_1.BST {
|
|
|
138
138
|
* @returns The method is returning an array of `BinaryTreeDeleteResult<NODE>`.
|
|
139
139
|
*/
|
|
140
140
|
delete(identifier, callback = this._DEFAULT_CALLBACK) {
|
|
141
|
-
if (identifier instanceof AVLTreeNode)
|
|
142
|
-
callback = (node => node);
|
|
143
141
|
const deletedResults = super.delete(identifier, callback);
|
|
144
142
|
for (const { needBalanced } of deletedResults) {
|
|
145
143
|
if (needBalanced) {
|
|
@@ -254,7 +254,7 @@ export declare class BinaryTree<K = any, V = any, NODE extends BinaryTreeNode<K,
|
|
|
254
254
|
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
255
255
|
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
256
256
|
*/
|
|
257
|
-
getNodeByKey(key: K, iterationType?: IterationType): NODE | undefined;
|
|
257
|
+
getNodeByKey(key: K, iterationType?: IterationType): NODE | null | undefined;
|
|
258
258
|
get<C extends BTNCallback<NODE, K>>(identifier: K, callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): V | undefined;
|
|
259
259
|
get<C extends BTNCallback<NODE, NODE>>(identifier: NODE | null | undefined, callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): V | undefined;
|
|
260
260
|
get<C extends BTNCallback<NODE>>(identifier: ReturnType<C>, callback: C, beginRoot?: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): V | undefined;
|
|
@@ -606,4 +606,5 @@ export declare class BinaryTree<K = any, V = any, NODE extends BinaryTreeNode<K,
|
|
|
606
606
|
* type `NODE` or `null`.
|
|
607
607
|
*/
|
|
608
608
|
protected _setRoot(v: NODE | null | undefined): void;
|
|
609
|
+
protected _ensureCallback<C extends BTNCallback<NODE>>(identifier: ReturnType<C> | null | undefined, callback?: C): C;
|
|
609
610
|
}
|
|
@@ -225,23 +225,24 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
225
225
|
* itself if it is not a valid node key.
|
|
226
226
|
*/
|
|
227
227
|
ensureNode(keyOrNodeOrEntry, iterationType = 'ITERATIVE') {
|
|
228
|
+
if (keyOrNodeOrEntry === this.NIL)
|
|
229
|
+
return;
|
|
228
230
|
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
229
231
|
return keyOrNodeOrEntry;
|
|
230
232
|
}
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
if (keyOrNodeOrEntry[0] === undefined)
|
|
235
|
-
return;
|
|
236
|
-
return this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
|
|
237
|
-
}
|
|
238
|
-
else {
|
|
239
|
-
if (keyOrNodeOrEntry === null)
|
|
233
|
+
if (this.isEntry(keyOrNodeOrEntry)) {
|
|
234
|
+
const key = keyOrNodeOrEntry[0];
|
|
235
|
+
if (key === null)
|
|
240
236
|
return null;
|
|
241
|
-
if (
|
|
237
|
+
if (key === undefined)
|
|
242
238
|
return;
|
|
243
|
-
return this.getNodeByKey(
|
|
239
|
+
return this.getNodeByKey(key, iterationType);
|
|
244
240
|
}
|
|
241
|
+
if (keyOrNodeOrEntry === null)
|
|
242
|
+
return null;
|
|
243
|
+
if (keyOrNodeOrEntry === undefined)
|
|
244
|
+
return;
|
|
245
|
+
return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
245
246
|
}
|
|
246
247
|
/**
|
|
247
248
|
* The function checks if a given node is a real node or null.
|
|
@@ -426,8 +427,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
426
427
|
const deletedResult = [];
|
|
427
428
|
if (!this.root)
|
|
428
429
|
return deletedResult;
|
|
429
|
-
|
|
430
|
-
callback = (node => node);
|
|
430
|
+
callback = this._ensureCallback(identifier, callback);
|
|
431
431
|
const curr = this.getNode(identifier, callback);
|
|
432
432
|
if (!curr)
|
|
433
433
|
return deletedResult;
|
|
@@ -499,11 +499,10 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
499
499
|
* @returns an array of nodes of type `NODE`.
|
|
500
500
|
*/
|
|
501
501
|
getNodes(identifier, callback = this._DEFAULT_CALLBACK, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
502
|
-
if ((!callback || callback === this._DEFAULT_CALLBACK) && identifier instanceof BinaryTreeNode)
|
|
503
|
-
callback = (node => node);
|
|
504
502
|
beginRoot = this.ensureNode(beginRoot);
|
|
505
503
|
if (!beginRoot)
|
|
506
504
|
return [];
|
|
505
|
+
callback = this._ensureCallback(identifier, callback);
|
|
507
506
|
const ans = [];
|
|
508
507
|
if (iterationType === 'RECURSIVE') {
|
|
509
508
|
const dfs = (cur) => {
|
|
@@ -584,33 +583,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
584
583
|
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
585
584
|
*/
|
|
586
585
|
getNodeByKey(key, iterationType = 'ITERATIVE') {
|
|
587
|
-
|
|
588
|
-
return undefined;
|
|
589
|
-
if (iterationType === 'RECURSIVE') {
|
|
590
|
-
const dfs = (cur) => {
|
|
591
|
-
if (cur.key === key)
|
|
592
|
-
return cur;
|
|
593
|
-
if (!cur.left && !cur.right)
|
|
594
|
-
return;
|
|
595
|
-
if (cur.left)
|
|
596
|
-
return dfs(cur.left);
|
|
597
|
-
if (cur.right)
|
|
598
|
-
return dfs(cur.right);
|
|
599
|
-
};
|
|
600
|
-
return dfs(this.root);
|
|
601
|
-
}
|
|
602
|
-
else {
|
|
603
|
-
const stack = [this.root];
|
|
604
|
-
while (stack.length > 0) {
|
|
605
|
-
const cur = stack.pop();
|
|
606
|
-
if (cur) {
|
|
607
|
-
if (cur.key === key)
|
|
608
|
-
return cur;
|
|
609
|
-
cur.left && stack.push(cur.left);
|
|
610
|
-
cur.right && stack.push(cur.right);
|
|
611
|
-
}
|
|
612
|
-
}
|
|
613
|
-
}
|
|
586
|
+
return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
|
|
614
587
|
}
|
|
615
588
|
/**
|
|
616
589
|
* Time Complexity: O(n)
|
|
@@ -639,8 +612,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
639
612
|
* found, `undefined` is returned.
|
|
640
613
|
*/
|
|
641
614
|
get(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
642
|
-
var _a
|
|
643
|
-
return (
|
|
615
|
+
var _a;
|
|
616
|
+
return (_a = this.getNode(identifier, callback, beginRoot, iterationType)) === null || _a === void 0 ? void 0 : _a.value;
|
|
644
617
|
}
|
|
645
618
|
/**
|
|
646
619
|
* Time Complexity: O(n)
|
|
@@ -668,8 +641,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
668
641
|
* @returns a boolean value.
|
|
669
642
|
*/
|
|
670
643
|
has(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
671
|
-
|
|
672
|
-
callback = (node => node);
|
|
644
|
+
callback = this._ensureCallback(identifier, callback);
|
|
673
645
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
|
|
674
646
|
}
|
|
675
647
|
/**
|
|
@@ -1766,5 +1738,11 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1766
1738
|
}
|
|
1767
1739
|
this._root = v;
|
|
1768
1740
|
}
|
|
1741
|
+
_ensureCallback(identifier, callback = this._DEFAULT_CALLBACK) {
|
|
1742
|
+
if ((!callback || callback === this._DEFAULT_CALLBACK) && this.isNode(identifier)) {
|
|
1743
|
+
callback = (node => node);
|
|
1744
|
+
}
|
|
1745
|
+
return callback;
|
|
1746
|
+
}
|
|
1769
1747
|
}
|
|
1770
1748
|
exports.BinaryTree = BinaryTree;
|
|
@@ -161,25 +161,6 @@ export declare class BST<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BS
|
|
|
161
161
|
* @returns The function `addMany` returns an array of nodes (`NODE`) or `undefined` values.
|
|
162
162
|
*/
|
|
163
163
|
addMany(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): boolean[];
|
|
164
|
-
/**
|
|
165
|
-
* Time Complexity: O(log n)
|
|
166
|
-
* Space Complexity: O(1)
|
|
167
|
-
*/
|
|
168
|
-
/**
|
|
169
|
-
* Time Complexity: O(log n)
|
|
170
|
-
* Space Complexity: O(1)
|
|
171
|
-
*
|
|
172
|
-
* The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
|
|
173
|
-
* either recursive or iterative methods.
|
|
174
|
-
* @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
|
|
175
|
-
* It is used to identify the node that we want to retrieve.
|
|
176
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
177
|
-
* type of iteration to use when searching for a node in the binary tree. It can have two possible
|
|
178
|
-
* values:
|
|
179
|
-
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
180
|
-
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
181
|
-
*/
|
|
182
|
-
getNodeByKey(key: K, iterationType?: IterationType): NODE | undefined;
|
|
183
164
|
/**
|
|
184
165
|
* Time Complexity: O(log n)
|
|
185
166
|
* Space Complexity: O(k + log n)
|
|
@@ -235,6 +216,25 @@ export declare class BST<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BS
|
|
|
235
216
|
* @returns The method is returning a value of type `NODE | null | undefined`.
|
|
236
217
|
*/
|
|
237
218
|
getNode<C extends BTNCallback<NODE>>(identifier: ReturnType<C> | undefined, callback?: C, beginRoot?: BSTNKeyOrNode<K, NODE>, iterationType?: IterationType): NODE | undefined;
|
|
219
|
+
/**
|
|
220
|
+
* Time Complexity: O(log n)
|
|
221
|
+
* Space Complexity: O(1)
|
|
222
|
+
*/
|
|
223
|
+
/**
|
|
224
|
+
* Time Complexity: O(log n)
|
|
225
|
+
* Space Complexity: O(1)
|
|
226
|
+
*
|
|
227
|
+
* The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
|
|
228
|
+
* either recursive or iterative methods.
|
|
229
|
+
* @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
|
|
230
|
+
* It is used to identify the node that we want to retrieve.
|
|
231
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
232
|
+
* type of iteration to use when searching for a node in the binary tree. It can have two possible
|
|
233
|
+
* values:
|
|
234
|
+
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
235
|
+
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
236
|
+
*/
|
|
237
|
+
getNodeByKey(key: K, iterationType?: IterationType): NODE | undefined;
|
|
238
238
|
/**
|
|
239
239
|
* Time complexity: O(n)
|
|
240
240
|
* Space complexity: O(n)
|
|
@@ -165,19 +165,21 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
165
165
|
* @returns either a node object (NODE) or undefined.
|
|
166
166
|
*/
|
|
167
167
|
ensureNode(keyOrNodeOrEntry, iterationType = 'ITERATIVE') {
|
|
168
|
+
if (keyOrNodeOrEntry === this.NIL)
|
|
169
|
+
return;
|
|
168
170
|
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
169
171
|
return keyOrNodeOrEntry;
|
|
170
172
|
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
return this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
|
|
175
|
-
}
|
|
176
|
-
else {
|
|
177
|
-
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined)
|
|
173
|
+
if (this.isEntry(keyOrNodeOrEntry)) {
|
|
174
|
+
const key = keyOrNodeOrEntry[0];
|
|
175
|
+
if (key === null || key === undefined)
|
|
178
176
|
return;
|
|
179
|
-
return this.getNodeByKey(
|
|
177
|
+
return this.getNodeByKey(key, iterationType);
|
|
180
178
|
}
|
|
179
|
+
const key = keyOrNodeOrEntry;
|
|
180
|
+
if (key === null || key === undefined)
|
|
181
|
+
return;
|
|
182
|
+
return this.getNodeByKey(key, iterationType);
|
|
181
183
|
}
|
|
182
184
|
/**
|
|
183
185
|
* The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
|
|
@@ -342,56 +344,6 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
342
344
|
}
|
|
343
345
|
return inserted;
|
|
344
346
|
}
|
|
345
|
-
/**
|
|
346
|
-
* Time Complexity: O(log n)
|
|
347
|
-
* Space Complexity: O(1)
|
|
348
|
-
*/
|
|
349
|
-
/**
|
|
350
|
-
* Time Complexity: O(log n)
|
|
351
|
-
* Space Complexity: O(1)
|
|
352
|
-
*
|
|
353
|
-
* The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
|
|
354
|
-
* either recursive or iterative methods.
|
|
355
|
-
* @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
|
|
356
|
-
* It is used to identify the node that we want to retrieve.
|
|
357
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
358
|
-
* type of iteration to use when searching for a node in the binary tree. It can have two possible
|
|
359
|
-
* values:
|
|
360
|
-
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
361
|
-
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
362
|
-
*/
|
|
363
|
-
getNodeByKey(key, iterationType = 'ITERATIVE') {
|
|
364
|
-
// return this.getNodes(key, this._DEFAULT_CALLBACK, true, this.root, iterationType)[0];
|
|
365
|
-
if (!this.isRealNode(this.root))
|
|
366
|
-
return;
|
|
367
|
-
if (iterationType === 'RECURSIVE') {
|
|
368
|
-
const dfs = (cur) => {
|
|
369
|
-
if (cur.key === key)
|
|
370
|
-
return cur;
|
|
371
|
-
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
|
|
372
|
-
return;
|
|
373
|
-
if (this.isRealNode(cur.left) && this._compare(cur.key, key) === 'GT')
|
|
374
|
-
return dfs(cur.left);
|
|
375
|
-
if (this.isRealNode(cur.right) && this._compare(cur.key, key) === 'LT')
|
|
376
|
-
return dfs(cur.right);
|
|
377
|
-
};
|
|
378
|
-
return dfs(this.root);
|
|
379
|
-
}
|
|
380
|
-
else {
|
|
381
|
-
const stack = [this.root];
|
|
382
|
-
while (stack.length > 0) {
|
|
383
|
-
const cur = stack.pop();
|
|
384
|
-
if (this.isRealNode(cur)) {
|
|
385
|
-
if (this._compare(cur.key, key) === 'EQ')
|
|
386
|
-
return cur;
|
|
387
|
-
if (this.isRealNode(cur.left) && this._compare(cur.key, key) === 'GT')
|
|
388
|
-
stack.push(cur.left);
|
|
389
|
-
if (this.isRealNode(cur.right) && this._compare(cur.key, key) === 'LT')
|
|
390
|
-
stack.push(cur.right);
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
347
|
/**
|
|
396
348
|
* Time Complexity: O(log n)
|
|
397
349
|
* Space Complexity: O(k + log n)
|
|
@@ -424,6 +376,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
424
376
|
beginRoot = this.ensureNode(beginRoot);
|
|
425
377
|
if (!beginRoot)
|
|
426
378
|
return [];
|
|
379
|
+
callback = this._ensureCallback(identifier, callback);
|
|
427
380
|
const ans = [];
|
|
428
381
|
if (iterationType === 'RECURSIVE') {
|
|
429
382
|
const dfs = (cur) => {
|
|
@@ -453,30 +406,28 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
453
406
|
const stack = [beginRoot];
|
|
454
407
|
while (stack.length > 0) {
|
|
455
408
|
const cur = stack.pop();
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
if (
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
this.isRealNode(cur.left) && stack.push(cur.left);
|
|
479
|
-
}
|
|
409
|
+
const callbackResult = callback(cur);
|
|
410
|
+
if (callbackResult === identifier) {
|
|
411
|
+
ans.push(cur);
|
|
412
|
+
if (onlyOne)
|
|
413
|
+
return ans;
|
|
414
|
+
}
|
|
415
|
+
// TODO potential bug
|
|
416
|
+
if (callback === this._DEFAULT_CALLBACK) {
|
|
417
|
+
if (this.isRealNode(cur.right) && this._compare(cur.key, identifier) === 'LT')
|
|
418
|
+
stack.push(cur.right);
|
|
419
|
+
if (this.isRealNode(cur.left) && this._compare(cur.key, identifier) === 'GT')
|
|
420
|
+
stack.push(cur.left);
|
|
421
|
+
// if (this.isRealNode(cur.right) && this._lt(cur.key, identifier as K)) stack.push(cur.right);
|
|
422
|
+
// if (this.isRealNode(cur.left) && this._gt(cur.key, identifier as K)) stack.push(cur.left);
|
|
423
|
+
// // @ts-ignore
|
|
424
|
+
// if (this.isRealNode(cur.right) && cur.key > identifier) stack.push(cur.right);
|
|
425
|
+
// // @ts-ignore
|
|
426
|
+
// if (this.isRealNode(cur.left) && cur.key < identifier) stack.push(cur.left);
|
|
427
|
+
}
|
|
428
|
+
else {
|
|
429
|
+
this.isRealNode(cur.right) && stack.push(cur.right);
|
|
430
|
+
this.isRealNode(cur.left) && stack.push(cur.left);
|
|
480
431
|
}
|
|
481
432
|
}
|
|
482
433
|
}
|
|
@@ -511,6 +462,27 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
511
462
|
var _a;
|
|
512
463
|
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
|
|
513
464
|
}
|
|
465
|
+
/**
|
|
466
|
+
* Time Complexity: O(log n)
|
|
467
|
+
* Space Complexity: O(1)
|
|
468
|
+
*/
|
|
469
|
+
/**
|
|
470
|
+
* Time Complexity: O(log n)
|
|
471
|
+
* Space Complexity: O(1)
|
|
472
|
+
*
|
|
473
|
+
* The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
|
|
474
|
+
* either recursive or iterative methods.
|
|
475
|
+
* @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
|
|
476
|
+
* It is used to identify the node that we want to retrieve.
|
|
477
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
478
|
+
* type of iteration to use when searching for a node in the binary tree. It can have two possible
|
|
479
|
+
* values:
|
|
480
|
+
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
481
|
+
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
482
|
+
*/
|
|
483
|
+
getNodeByKey(key, iterationType = 'ITERATIVE') {
|
|
484
|
+
return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
|
|
485
|
+
}
|
|
514
486
|
/**
|
|
515
487
|
* Time complexity: O(n)
|
|
516
488
|
* Space complexity: O(n)
|
|
@@ -826,7 +798,11 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
826
798
|
const extractedA = this.extractor(a);
|
|
827
799
|
const extractedB = this.extractor(b);
|
|
828
800
|
const compared = this.variant === 'STANDARD' ? extractedA - extractedB : extractedB - extractedA;
|
|
829
|
-
|
|
801
|
+
if (compared > 0)
|
|
802
|
+
return 'GT';
|
|
803
|
+
if (compared < 0)
|
|
804
|
+
return 'LT';
|
|
805
|
+
return 'EQ';
|
|
830
806
|
}
|
|
831
807
|
/**
|
|
832
808
|
* The function `_lt` compares two values `a` and `b` using an extractor function and returns true if
|
|
@@ -840,10 +816,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
840
816
|
_lt(a, b) {
|
|
841
817
|
const extractedA = this.extractor(a);
|
|
842
818
|
const extractedB = this.extractor(b);
|
|
843
|
-
// return this.variant === BSTVariant.STANDARD ? extractedA < extractedB : extractedA > extractedB;
|
|
844
819
|
return this.variant === 'STANDARD' ? extractedA < extractedB : extractedA > extractedB;
|
|
845
|
-
// return extractedA < extractedB;
|
|
846
|
-
// return a < b;
|
|
847
820
|
}
|
|
848
821
|
/**
|
|
849
822
|
* The function compares two values using a custom extractor function and returns true if the first
|
|
@@ -856,10 +829,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
856
829
|
_gt(a, b) {
|
|
857
830
|
const extractedA = this.extractor(a);
|
|
858
831
|
const extractedB = this.extractor(b);
|
|
859
|
-
// return this.variant === BSTVariant.STANDARD ? extractedA > extractedB : extractedA < extractedB;
|
|
860
832
|
return this.variant === 'STANDARD' ? extractedA > extractedB : extractedA < extractedB;
|
|
861
|
-
// return extractedA > extractedB;
|
|
862
|
-
// return a > b;
|
|
863
833
|
}
|
|
864
834
|
}
|
|
865
835
|
exports.BST = BST;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { BinaryTreeDeleteResult, BTNCallback, KeyOrNodeOrEntry, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
|
|
2
|
-
import { CRUD, RBTNColor } from '../../types';
|
|
2
|
+
import { CP, CRUD, RBTNColor } from '../../types';
|
|
3
3
|
import { BST, BSTNode } from './bst';
|
|
4
4
|
import { IBinaryTree } from '../../interfaces';
|
|
5
5
|
export declare class RedBlackTreeNode<K = any, V = any, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNodeNested<K, V>> extends BSTNode<K, V, NODE> {
|
|
@@ -255,4 +255,13 @@ export declare class RedBlackTree<K = any, V = any, NODE extends RedBlackTreeNod
|
|
|
255
255
|
* @returns void, which means it does not return any value.
|
|
256
256
|
*/
|
|
257
257
|
protected _rightRotate(y: NODE | undefined): void;
|
|
258
|
+
/**
|
|
259
|
+
* The function compares two values using a comparator function and returns whether the first value
|
|
260
|
+
* is greater than, less than, or equal to the second value.
|
|
261
|
+
* @param {K} a - The parameter "a" is of type K.
|
|
262
|
+
* @param {K} b - The parameter "b" in the above code represents a K.
|
|
263
|
+
* @returns a value of type CP (ComparisonResult). The possible return values are 'GT' (greater
|
|
264
|
+
* than), 'LT' (less than), or 'EQ' (equal).
|
|
265
|
+
*/
|
|
266
|
+
protected _compare(a: K, b: K): CP;
|
|
258
267
|
}
|
|
@@ -214,6 +214,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
214
214
|
if (identifier === null)
|
|
215
215
|
return [];
|
|
216
216
|
const results = [];
|
|
217
|
+
callback = this._ensureCallback(identifier, callback);
|
|
217
218
|
const nodeToDelete = this.isRealNode(identifier) ? identifier : this.getNode(identifier, callback);
|
|
218
219
|
if (!nodeToDelete) {
|
|
219
220
|
return results;
|
|
@@ -598,5 +599,23 @@ class RedBlackTree extends bst_1.BST {
|
|
|
598
599
|
x.right = y;
|
|
599
600
|
y.parent = x;
|
|
600
601
|
}
|
|
602
|
+
/**
|
|
603
|
+
* The function compares two values using a comparator function and returns whether the first value
|
|
604
|
+
* is greater than, less than, or equal to the second value.
|
|
605
|
+
* @param {K} a - The parameter "a" is of type K.
|
|
606
|
+
* @param {K} b - The parameter "b" in the above code represents a K.
|
|
607
|
+
* @returns a value of type CP (ComparisonResult). The possible return values are 'GT' (greater
|
|
608
|
+
* than), 'LT' (less than), or 'EQ' (equal).
|
|
609
|
+
*/
|
|
610
|
+
_compare(a, b) {
|
|
611
|
+
const extractedA = this.extractor(a);
|
|
612
|
+
const extractedB = this.extractor(b);
|
|
613
|
+
const compared = extractedA - extractedB;
|
|
614
|
+
if (compared > 0)
|
|
615
|
+
return 'GT';
|
|
616
|
+
if (compared < 0)
|
|
617
|
+
return 'LT';
|
|
618
|
+
return 'EQ';
|
|
619
|
+
}
|
|
601
620
|
}
|
|
602
621
|
exports.RedBlackTree = RedBlackTree;
|
|
@@ -210,6 +210,7 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
|
210
210
|
if (identifier === null)
|
|
211
211
|
return [];
|
|
212
212
|
const results = [];
|
|
213
|
+
callback = this._ensureCallback(identifier, callback);
|
|
213
214
|
const nodeToDelete = this.isRealNode(identifier) ? identifier : this.getNode(identifier, callback);
|
|
214
215
|
if (!nodeToDelete) {
|
|
215
216
|
return results;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "min-heap-typed",
|
|
3
|
-
"version": "1.51.
|
|
3
|
+
"version": "1.51.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.51.
|
|
135
|
+
"data-structure-typed": "^1.51.7"
|
|
136
136
|
}
|
|
137
137
|
}
|
|
@@ -245,6 +245,7 @@ export class AVLTreeMultiMap<
|
|
|
245
245
|
): BinaryTreeDeleteResult<NODE>[] {
|
|
246
246
|
const deletedResult: BinaryTreeDeleteResult<NODE>[] = [];
|
|
247
247
|
if (!this.root) return deletedResult;
|
|
248
|
+
callback = this._ensureCallback(identifier, callback);
|
|
248
249
|
|
|
249
250
|
const curr: NODE | undefined = this.getNode(identifier, callback) ?? undefined;
|
|
250
251
|
if (!curr) return deletedResult;
|
|
@@ -172,7 +172,6 @@ export class AVLTree<
|
|
|
172
172
|
identifier: ReturnType<C>,
|
|
173
173
|
callback: C = this._DEFAULT_CALLBACK as C
|
|
174
174
|
): BinaryTreeDeleteResult<NODE>[] {
|
|
175
|
-
if ((identifier as any) instanceof AVLTreeNode) callback = (node => node) as C;
|
|
176
175
|
const deletedResults = super.delete(identifier, callback);
|
|
177
176
|
for (const { needBalanced } of deletedResults) {
|
|
178
177
|
if (needBalanced) {
|
|
@@ -278,17 +278,19 @@ export class BinaryTree<
|
|
|
278
278
|
keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>,
|
|
279
279
|
iterationType: IterationType = 'ITERATIVE'
|
|
280
280
|
): NODE | null | undefined {
|
|
281
|
+
if (keyOrNodeOrEntry === this.NIL) return;
|
|
281
282
|
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
282
283
|
return keyOrNodeOrEntry;
|
|
283
|
-
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
284
|
-
if (keyOrNodeOrEntry[0] === null) return null;
|
|
285
|
-
if (keyOrNodeOrEntry[0] === undefined) return;
|
|
286
|
-
return this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
|
|
287
|
-
} else {
|
|
288
|
-
if (keyOrNodeOrEntry === null) return null;
|
|
289
|
-
if (keyOrNodeOrEntry === undefined) return;
|
|
290
|
-
return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
291
284
|
}
|
|
285
|
+
if (this.isEntry(keyOrNodeOrEntry)) {
|
|
286
|
+
const key = keyOrNodeOrEntry[0];
|
|
287
|
+
if (key === null) return null;
|
|
288
|
+
if (key === undefined) return;
|
|
289
|
+
return this.getNodeByKey(key, iterationType);
|
|
290
|
+
}
|
|
291
|
+
if (keyOrNodeOrEntry === null) return null;
|
|
292
|
+
if (keyOrNodeOrEntry === undefined) return;
|
|
293
|
+
return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
292
294
|
}
|
|
293
295
|
|
|
294
296
|
/**
|
|
@@ -506,8 +508,7 @@ export class BinaryTree<
|
|
|
506
508
|
): BinaryTreeDeleteResult<NODE>[] {
|
|
507
509
|
const deletedResult: BinaryTreeDeleteResult<NODE>[] = [];
|
|
508
510
|
if (!this.root) return deletedResult;
|
|
509
|
-
|
|
510
|
-
callback = (node => node) as C;
|
|
511
|
+
callback = this._ensureCallback(identifier, callback);
|
|
511
512
|
|
|
512
513
|
const curr = this.getNode(identifier, callback);
|
|
513
514
|
if (!curr) return deletedResult;
|
|
@@ -610,10 +611,9 @@ export class BinaryTree<
|
|
|
610
611
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
611
612
|
iterationType: IterationType = this.iterationType
|
|
612
613
|
): NODE[] {
|
|
613
|
-
if ((!callback || callback === this._DEFAULT_CALLBACK) && (identifier as any) instanceof BinaryTreeNode)
|
|
614
|
-
callback = (node => node) as C;
|
|
615
614
|
beginRoot = this.ensureNode(beginRoot);
|
|
616
615
|
if (!beginRoot) return [];
|
|
616
|
+
callback = this._ensureCallback(identifier, callback);
|
|
617
617
|
|
|
618
618
|
const ans: NODE[] = [];
|
|
619
619
|
|
|
@@ -722,29 +722,8 @@ export class BinaryTree<
|
|
|
722
722
|
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
723
723
|
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
724
724
|
*/
|
|
725
|
-
getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): NODE | undefined {
|
|
726
|
-
|
|
727
|
-
if (iterationType === 'RECURSIVE') {
|
|
728
|
-
const dfs = (cur: NODE): NODE | undefined => {
|
|
729
|
-
if (cur.key === key) return cur;
|
|
730
|
-
|
|
731
|
-
if (!cur.left && !cur.right) return;
|
|
732
|
-
if (cur.left) return dfs(cur.left);
|
|
733
|
-
if (cur.right) return dfs(cur.right);
|
|
734
|
-
};
|
|
735
|
-
|
|
736
|
-
return dfs(this.root);
|
|
737
|
-
} else {
|
|
738
|
-
const stack = [this.root];
|
|
739
|
-
while (stack.length > 0) {
|
|
740
|
-
const cur = stack.pop();
|
|
741
|
-
if (cur) {
|
|
742
|
-
if (cur.key === key) return cur;
|
|
743
|
-
cur.left && stack.push(cur.left);
|
|
744
|
-
cur.right && stack.push(cur.right);
|
|
745
|
-
}
|
|
746
|
-
}
|
|
747
|
-
}
|
|
725
|
+
getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): NODE | null | undefined {
|
|
726
|
+
return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
|
|
748
727
|
}
|
|
749
728
|
|
|
750
729
|
override get<C extends BTNCallback<NODE, K>>(
|
|
@@ -801,7 +780,7 @@ export class BinaryTree<
|
|
|
801
780
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
802
781
|
iterationType: IterationType = this.iterationType
|
|
803
782
|
): V | undefined {
|
|
804
|
-
return this.getNode(identifier, callback, beginRoot, iterationType)?.value
|
|
783
|
+
return this.getNode(identifier, callback, beginRoot, iterationType)?.value;
|
|
805
784
|
}
|
|
806
785
|
|
|
807
786
|
override has<C extends BTNCallback<NODE, K>>(
|
|
@@ -857,8 +836,7 @@ export class BinaryTree<
|
|
|
857
836
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
858
837
|
iterationType: IterationType = this.iterationType
|
|
859
838
|
): boolean {
|
|
860
|
-
|
|
861
|
-
callback = (node => node) as C;
|
|
839
|
+
callback = this._ensureCallback(identifier, callback);
|
|
862
840
|
|
|
863
841
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
|
|
864
842
|
}
|
|
@@ -2070,4 +2048,15 @@ export class BinaryTree<
|
|
|
2070
2048
|
}
|
|
2071
2049
|
this._root = v;
|
|
2072
2050
|
}
|
|
2051
|
+
|
|
2052
|
+
protected _ensureCallback<C extends BTNCallback<NODE>>(
|
|
2053
|
+
identifier: ReturnType<C> | null | undefined,
|
|
2054
|
+
callback: C = this._DEFAULT_CALLBACK as C
|
|
2055
|
+
): C {
|
|
2056
|
+
if ((!callback || callback === this._DEFAULT_CALLBACK) && this.isNode(identifier)) {
|
|
2057
|
+
callback = (node => node) as C;
|
|
2058
|
+
}
|
|
2059
|
+
|
|
2060
|
+
return callback;
|
|
2061
|
+
}
|
|
2073
2062
|
}
|
|
@@ -214,15 +214,20 @@ export class BST<
|
|
|
214
214
|
keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>,
|
|
215
215
|
iterationType: IterationType = 'ITERATIVE'
|
|
216
216
|
): NODE | undefined {
|
|
217
|
+
if (keyOrNodeOrEntry === this.NIL) return;
|
|
217
218
|
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
218
219
|
return keyOrNodeOrEntry;
|
|
219
|
-
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
220
|
-
if (keyOrNodeOrEntry[0] === null || keyOrNodeOrEntry[0] === undefined) return;
|
|
221
|
-
return this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
|
|
222
|
-
} else {
|
|
223
|
-
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) return;
|
|
224
|
-
return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
225
220
|
}
|
|
221
|
+
|
|
222
|
+
if (this.isEntry(keyOrNodeOrEntry)) {
|
|
223
|
+
const key = keyOrNodeOrEntry[0];
|
|
224
|
+
if (key === null || key === undefined) return;
|
|
225
|
+
return this.getNodeByKey(key, iterationType);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const key = keyOrNodeOrEntry;
|
|
229
|
+
if (key === null || key === undefined) return;
|
|
230
|
+
return this.getNodeByKey(key, iterationType);
|
|
226
231
|
}
|
|
227
232
|
|
|
228
233
|
/**
|
|
@@ -406,51 +411,6 @@ export class BST<
|
|
|
406
411
|
return inserted;
|
|
407
412
|
}
|
|
408
413
|
|
|
409
|
-
/**
|
|
410
|
-
* Time Complexity: O(log n)
|
|
411
|
-
* Space Complexity: O(1)
|
|
412
|
-
*/
|
|
413
|
-
|
|
414
|
-
/**
|
|
415
|
-
* Time Complexity: O(log n)
|
|
416
|
-
* Space Complexity: O(1)
|
|
417
|
-
*
|
|
418
|
-
* The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
|
|
419
|
-
* either recursive or iterative methods.
|
|
420
|
-
* @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
|
|
421
|
-
* It is used to identify the node that we want to retrieve.
|
|
422
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
423
|
-
* type of iteration to use when searching for a node in the binary tree. It can have two possible
|
|
424
|
-
* values:
|
|
425
|
-
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
426
|
-
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
427
|
-
*/
|
|
428
|
-
override getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): NODE | undefined {
|
|
429
|
-
// return this.getNodes(key, this._DEFAULT_CALLBACK, true, this.root, iterationType)[0];
|
|
430
|
-
if (!this.isRealNode(this.root)) return;
|
|
431
|
-
if (iterationType === 'RECURSIVE') {
|
|
432
|
-
const dfs = (cur: NODE): NODE | undefined => {
|
|
433
|
-
if (cur.key === key) return cur;
|
|
434
|
-
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return;
|
|
435
|
-
|
|
436
|
-
if (this.isRealNode(cur.left) && this._compare(cur.key, key) === 'GT') return dfs(cur.left);
|
|
437
|
-
if (this.isRealNode(cur.right) && this._compare(cur.key, key) === 'LT') return dfs(cur.right);
|
|
438
|
-
};
|
|
439
|
-
|
|
440
|
-
return dfs(this.root);
|
|
441
|
-
} else {
|
|
442
|
-
const stack = [this.root];
|
|
443
|
-
while (stack.length > 0) {
|
|
444
|
-
const cur = stack.pop();
|
|
445
|
-
if (this.isRealNode(cur)) {
|
|
446
|
-
if (this._compare(cur.key, key) === 'EQ') return cur;
|
|
447
|
-
if (this.isRealNode(cur.left) && this._compare(cur.key, key) === 'GT') stack.push(cur.left);
|
|
448
|
-
if (this.isRealNode(cur.right) && this._compare(cur.key, key) === 'LT') stack.push(cur.right);
|
|
449
|
-
}
|
|
450
|
-
}
|
|
451
|
-
}
|
|
452
|
-
}
|
|
453
|
-
|
|
454
414
|
/**
|
|
455
415
|
* Time Complexity: O(log n)
|
|
456
416
|
* Space Complexity: O(k + log n)
|
|
@@ -488,6 +448,7 @@ export class BST<
|
|
|
488
448
|
): NODE[] {
|
|
489
449
|
beginRoot = this.ensureNode(beginRoot);
|
|
490
450
|
if (!beginRoot) return [];
|
|
451
|
+
callback = this._ensureCallback(identifier, callback);
|
|
491
452
|
const ans: NODE[] = [];
|
|
492
453
|
|
|
493
454
|
if (iterationType === 'RECURSIVE') {
|
|
@@ -513,29 +474,27 @@ export class BST<
|
|
|
513
474
|
} else {
|
|
514
475
|
const stack = [beginRoot];
|
|
515
476
|
while (stack.length > 0) {
|
|
516
|
-
const cur = stack.pop()
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
if (
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
this.isRealNode(cur.left) && stack.push(cur.left);
|
|
538
|
-
}
|
|
477
|
+
const cur = stack.pop()!;
|
|
478
|
+
const callbackResult = callback(cur);
|
|
479
|
+
if (callbackResult === identifier) {
|
|
480
|
+
ans.push(cur);
|
|
481
|
+
if (onlyOne) return ans;
|
|
482
|
+
}
|
|
483
|
+
// TODO potential bug
|
|
484
|
+
if (callback === this._DEFAULT_CALLBACK) {
|
|
485
|
+
if (this.isRealNode(cur.right) && this._compare(cur.key, identifier as K) === 'LT') stack.push(cur.right);
|
|
486
|
+
if (this.isRealNode(cur.left) && this._compare(cur.key, identifier as K) === 'GT') stack.push(cur.left);
|
|
487
|
+
|
|
488
|
+
// if (this.isRealNode(cur.right) && this._lt(cur.key, identifier as K)) stack.push(cur.right);
|
|
489
|
+
// if (this.isRealNode(cur.left) && this._gt(cur.key, identifier as K)) stack.push(cur.left);
|
|
490
|
+
|
|
491
|
+
// // @ts-ignore
|
|
492
|
+
// if (this.isRealNode(cur.right) && cur.key > identifier) stack.push(cur.right);
|
|
493
|
+
// // @ts-ignore
|
|
494
|
+
// if (this.isRealNode(cur.left) && cur.key < identifier) stack.push(cur.left);
|
|
495
|
+
} else {
|
|
496
|
+
this.isRealNode(cur.right) && stack.push(cur.right);
|
|
497
|
+
this.isRealNode(cur.left) && stack.push(cur.left);
|
|
539
498
|
}
|
|
540
499
|
}
|
|
541
500
|
}
|
|
@@ -578,6 +537,29 @@ export class BST<
|
|
|
578
537
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
|
|
579
538
|
}
|
|
580
539
|
|
|
540
|
+
/**
|
|
541
|
+
* Time Complexity: O(log n)
|
|
542
|
+
* Space Complexity: O(1)
|
|
543
|
+
*/
|
|
544
|
+
|
|
545
|
+
/**
|
|
546
|
+
* Time Complexity: O(log n)
|
|
547
|
+
* Space Complexity: O(1)
|
|
548
|
+
*
|
|
549
|
+
* The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
|
|
550
|
+
* either recursive or iterative methods.
|
|
551
|
+
* @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
|
|
552
|
+
* It is used to identify the node that we want to retrieve.
|
|
553
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
554
|
+
* type of iteration to use when searching for a node in the binary tree. It can have two possible
|
|
555
|
+
* values:
|
|
556
|
+
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
557
|
+
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
558
|
+
*/
|
|
559
|
+
override getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): NODE | undefined {
|
|
560
|
+
return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
|
|
561
|
+
}
|
|
562
|
+
|
|
581
563
|
/**
|
|
582
564
|
* Time complexity: O(n)
|
|
583
565
|
* Space complexity: O(n)
|
|
@@ -920,7 +902,9 @@ export class BST<
|
|
|
920
902
|
const extractedB = this.extractor(b);
|
|
921
903
|
const compared = this.variant === 'STANDARD' ? extractedA - extractedB : extractedB - extractedA;
|
|
922
904
|
|
|
923
|
-
|
|
905
|
+
if (compared > 0) return 'GT';
|
|
906
|
+
if (compared < 0) return 'LT';
|
|
907
|
+
return 'EQ';
|
|
924
908
|
}
|
|
925
909
|
|
|
926
910
|
/**
|
|
@@ -935,10 +919,7 @@ export class BST<
|
|
|
935
919
|
protected _lt(a: K, b: K): boolean {
|
|
936
920
|
const extractedA = this.extractor(a);
|
|
937
921
|
const extractedB = this.extractor(b);
|
|
938
|
-
// return this.variant === BSTVariant.STANDARD ? extractedA < extractedB : extractedA > extractedB;
|
|
939
922
|
return this.variant === 'STANDARD' ? extractedA < extractedB : extractedA > extractedB;
|
|
940
|
-
// return extractedA < extractedB;
|
|
941
|
-
// return a < b;
|
|
942
923
|
}
|
|
943
924
|
|
|
944
925
|
/**
|
|
@@ -952,9 +933,6 @@ export class BST<
|
|
|
952
933
|
protected _gt(a: K, b: K): boolean {
|
|
953
934
|
const extractedA = this.extractor(a);
|
|
954
935
|
const extractedB = this.extractor(b);
|
|
955
|
-
// return this.variant === BSTVariant.STANDARD ? extractedA > extractedB : extractedA < extractedB;
|
|
956
936
|
return this.variant === 'STANDARD' ? extractedA > extractedB : extractedA < extractedB;
|
|
957
|
-
// return extractedA > extractedB;
|
|
958
|
-
// return a > b;
|
|
959
937
|
}
|
|
960
938
|
}
|
|
@@ -6,7 +6,7 @@ import type {
|
|
|
6
6
|
RedBlackTreeNested,
|
|
7
7
|
RedBlackTreeNodeNested
|
|
8
8
|
} from '../../types';
|
|
9
|
-
import { CRUD, RBTNColor } from '../../types';
|
|
9
|
+
import { CP, CRUD, RBTNColor } from '../../types';
|
|
10
10
|
import { BST, BSTNode } from './bst';
|
|
11
11
|
import { IBinaryTree } from '../../interfaces';
|
|
12
12
|
|
|
@@ -252,7 +252,7 @@ export class RedBlackTree<
|
|
|
252
252
|
): BinaryTreeDeleteResult<NODE>[] {
|
|
253
253
|
if (identifier === null) return [];
|
|
254
254
|
const results: BinaryTreeDeleteResult<NODE>[] = [];
|
|
255
|
-
|
|
255
|
+
callback = this._ensureCallback(identifier, callback);
|
|
256
256
|
const nodeToDelete = this.isRealNode(identifier) ? identifier : this.getNode(identifier, callback);
|
|
257
257
|
|
|
258
258
|
if (!nodeToDelete) {
|
|
@@ -656,4 +656,21 @@ export class RedBlackTree<
|
|
|
656
656
|
x.right = y;
|
|
657
657
|
y.parent = x;
|
|
658
658
|
}
|
|
659
|
+
|
|
660
|
+
/**
|
|
661
|
+
* The function compares two values using a comparator function and returns whether the first value
|
|
662
|
+
* is greater than, less than, or equal to the second value.
|
|
663
|
+
* @param {K} a - The parameter "a" is of type K.
|
|
664
|
+
* @param {K} b - The parameter "b" in the above code represents a K.
|
|
665
|
+
* @returns a value of type CP (ComparisonResult). The possible return values are 'GT' (greater
|
|
666
|
+
* than), 'LT' (less than), or 'EQ' (equal).
|
|
667
|
+
*/
|
|
668
|
+
protected override _compare(a: K, b: K): CP {
|
|
669
|
+
const extractedA = this.extractor(a);
|
|
670
|
+
const extractedB = this.extractor(b);
|
|
671
|
+
const compared = extractedA - extractedB;
|
|
672
|
+
if (compared > 0) return 'GT';
|
|
673
|
+
if (compared < 0) return 'LT';
|
|
674
|
+
return 'EQ';
|
|
675
|
+
}
|
|
659
676
|
}
|
|
@@ -257,6 +257,7 @@ export class TreeMultiMap<
|
|
|
257
257
|
): BinaryTreeDeleteResult<NODE>[] {
|
|
258
258
|
if (identifier === null) return [];
|
|
259
259
|
const results: BinaryTreeDeleteResult<NODE>[] = [];
|
|
260
|
+
callback = this._ensureCallback(identifier, callback);
|
|
260
261
|
|
|
261
262
|
const nodeToDelete = this.isRealNode(identifier) ? identifier : this.getNode(identifier, callback);
|
|
262
263
|
|