max-priority-queue-typed 2.2.4 → 2.2.5
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.
|
@@ -288,12 +288,6 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
288
288
|
* @returns The root node.
|
|
289
289
|
*/
|
|
290
290
|
get root(): OptNode<BSTNode<K, V>>;
|
|
291
|
-
/**
|
|
292
|
-
* (Protected) Creates the default comparator function for keys that don't have a custom comparator.
|
|
293
|
-
* @remarks Time O(1) Space O(1)
|
|
294
|
-
* @returns The default comparator function.
|
|
295
|
-
*/
|
|
296
|
-
protected _createDefaultComparator(): Comparator<K>;
|
|
297
291
|
/**
|
|
298
292
|
* The comparator function used to determine the order of keys in the tree.
|
|
299
293
|
|
|
@@ -457,6 +451,54 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
457
451
|
* @returns The first node with key > given key, or undefined if no such node exists.
|
|
458
452
|
*/
|
|
459
453
|
upperBound(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, iterationType?: IterationType): BSTNode<K, V> | undefined;
|
|
454
|
+
/**
|
|
455
|
+
* Returns the first node with a key greater than or equal to the given key.
|
|
456
|
+
* This is equivalent to Java TreeMap.ceilingEntry().
|
|
457
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
458
|
+
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
459
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
460
|
+
*
|
|
461
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
462
|
+
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
463
|
+
* @returns The first node with key >= given key, or undefined if no such node exists.
|
|
464
|
+
*/
|
|
465
|
+
ceilingEntry(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, iterationType?: IterationType): BSTNode<K, V> | undefined;
|
|
466
|
+
/**
|
|
467
|
+
* Returns the first node with a key strictly greater than the given key.
|
|
468
|
+
* This is equivalent to Java TreeMap.higherEntry().
|
|
469
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
470
|
+
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
471
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
472
|
+
*
|
|
473
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
474
|
+
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
475
|
+
* @returns The first node with key > given key, or undefined if no such node exists.
|
|
476
|
+
*/
|
|
477
|
+
higherEntry(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, iterationType?: IterationType): BSTNode<K, V> | undefined;
|
|
478
|
+
/**
|
|
479
|
+
* Returns the first node with a key less than or equal to the given key.
|
|
480
|
+
* This is equivalent to Java TreeMap.floorEntry().
|
|
481
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
482
|
+
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
483
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
484
|
+
*
|
|
485
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
486
|
+
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
487
|
+
* @returns The first node with key <= given key, or undefined if no such node exists.
|
|
488
|
+
*/
|
|
489
|
+
floorEntry(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, iterationType?: IterationType): BSTNode<K, V> | undefined;
|
|
490
|
+
/**
|
|
491
|
+
* Returns the first node with a key strictly less than the given key.
|
|
492
|
+
* This is equivalent to Java TreeMap.lowerEntry().
|
|
493
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
494
|
+
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
495
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
496
|
+
*
|
|
497
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
498
|
+
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
499
|
+
* @returns The first node with key < given key, or undefined if no such node exists.
|
|
500
|
+
*/
|
|
501
|
+
lowerEntry(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, iterationType?: IterationType): BSTNode<K, V> | undefined;
|
|
460
502
|
/**
|
|
461
503
|
* Traverses the tree and returns nodes that are lesser or greater than a target node.
|
|
462
504
|
* @remarks Time O(N), as it performs a full traversal. Space O(log N) or O(N).
|
|
@@ -536,6 +578,58 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
536
578
|
* - If no nodes match the search criteria, the returned map is empty.
|
|
537
579
|
*/
|
|
538
580
|
deleteWhere(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>> | Range<K>, onlyOne?: boolean, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): BinaryTreeDeleteResult<BSTNode<K, V>>[];
|
|
581
|
+
/**
|
|
582
|
+
* (Protected) Creates the default comparator function for keys that don't have a custom comparator.
|
|
583
|
+
* @remarks Time O(1) Space O(1)
|
|
584
|
+
* @returns The default comparator function.
|
|
585
|
+
*/
|
|
586
|
+
protected _createDefaultComparator(): Comparator<K>;
|
|
587
|
+
/**
|
|
588
|
+
* (Protected) Binary search for floor by key with pruning optimization.
|
|
589
|
+
* Performs standard BST binary search, choosing left or right subtree based on comparator result.
|
|
590
|
+
* Finds first node where key <= target.
|
|
591
|
+
* @remarks Time O(h) where h is tree height.
|
|
592
|
+
*
|
|
593
|
+
* @param key - The target key to search for.
|
|
594
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
595
|
+
* @returns The first node with key <= target, or undefined if none exists.
|
|
596
|
+
*/
|
|
597
|
+
protected _floorByKey(key: K, iterationType: IterationType): BSTNode<K, V> | undefined;
|
|
598
|
+
/**
|
|
599
|
+
* (Protected) In-order traversal search for floor by predicate.
|
|
600
|
+
* Falls back to linear in-order traversal when predicate-based search is required.
|
|
601
|
+
* Returns the last node that satisfies the predicate function.
|
|
602
|
+
* @remarks Time Complexity: O(n) since it may visit every node.
|
|
603
|
+
* Space Complexity: O(h) for recursion, O(h) for iterative stack.
|
|
604
|
+
*
|
|
605
|
+
* @param predicate - The predicate function to test nodes.
|
|
606
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
607
|
+
* @returns The last node satisfying predicate (highest key), or undefined if none found.
|
|
608
|
+
*/
|
|
609
|
+
protected _floorByPredicate(predicate: NodePredicate<BSTNode<K, V>>, iterationType: IterationType): BSTNode<K, V> | undefined;
|
|
610
|
+
/**
|
|
611
|
+
* (Protected) Binary search for lower by key with pruning optimization.
|
|
612
|
+
* Performs standard BST binary search, choosing left or right subtree based on comparator result.
|
|
613
|
+
* Finds first node where key < target.
|
|
614
|
+
* @remarks Time O(h) where h is tree height.
|
|
615
|
+
*
|
|
616
|
+
* @param key - The target key to search for.
|
|
617
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
618
|
+
* @returns The first node with key < target, or undefined if none exists.
|
|
619
|
+
*/
|
|
620
|
+
protected _lowerByKey(key: K, iterationType: IterationType): BSTNode<K, V> | undefined;
|
|
621
|
+
/**
|
|
622
|
+
* (Protected) In-order traversal search for lower by predicate.
|
|
623
|
+
* Falls back to linear in-order traversal when predicate-based search is required.
|
|
624
|
+
* Returns the node that satisfies the predicate and appears last in in-order traversal.
|
|
625
|
+
* @remarks Time Complexity: O(n) since it may visit every node.
|
|
626
|
+
* Space Complexity: O(h) for recursion, O(h) for iterative stack.
|
|
627
|
+
*
|
|
628
|
+
* @param predicate - The predicate function to test nodes.
|
|
629
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
630
|
+
* @returns The last node satisfying predicate (highest key < target), or undefined if none found.
|
|
631
|
+
*/
|
|
632
|
+
protected _lowerByPredicate(predicate: NodePredicate<BSTNode<K, V>>, iterationType: IterationType): BSTNode<K, V> | undefined;
|
|
539
633
|
/**
|
|
540
634
|
* (Protected) Core bound search implementation supporting all parameter types.
|
|
541
635
|
* Unified logic for both lowerBound and upperBound.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "max-priority-queue-typed",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.5",
|
|
4
4
|
"description": "Max Priority Queue",
|
|
5
5
|
"browser": "dist/umd/max-priority-queue-typed.min.js",
|
|
6
6
|
"umd:main": "dist/umd/max-priority-queue-typed.min.js",
|
|
@@ -165,6 +165,6 @@
|
|
|
165
165
|
"typescript": "^4.9.5"
|
|
166
166
|
},
|
|
167
167
|
"dependencies": {
|
|
168
|
-
"data-structure-typed": "^2.2.
|
|
168
|
+
"data-structure-typed": "^2.2.5"
|
|
169
169
|
}
|
|
170
170
|
}
|
|
@@ -405,7 +405,9 @@ export class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<K, V[],
|
|
|
405
405
|
* @param [options] - Optional constructor options for the like-kind instance.
|
|
406
406
|
* @returns An empty like-kind instance.
|
|
407
407
|
*/
|
|
408
|
-
protected override _createInstance<TK = K, TV = V, TR = R>(
|
|
408
|
+
protected override _createInstance<TK = K, TV = V, TR = R>(
|
|
409
|
+
options?: Partial<AVLTreeMultiMapOptions<TK, TV, TR>>
|
|
410
|
+
): this {
|
|
409
411
|
const Ctor = this.constructor as unknown as new (
|
|
410
412
|
iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
411
413
|
opts?: AVLTreeMultiMapOptions<TK, TV, TR>
|
|
@@ -354,15 +354,12 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
354
354
|
* @param [options] - Configuration options for the BST, including comparator.
|
|
355
355
|
*/
|
|
356
356
|
constructor(
|
|
357
|
-
keysNodesEntriesOrRaws: Iterable<
|
|
358
|
-
K | BSTNode | [K | null | undefined, V | undefined] | null | undefined | R
|
|
359
|
-
> = [],
|
|
357
|
+
keysNodesEntriesOrRaws: Iterable<K | BSTNode | [K | null | undefined, V | undefined] | null | undefined | R> = [],
|
|
360
358
|
options?: BSTOptions<K, V, R>
|
|
361
359
|
) {
|
|
362
360
|
super([], options);
|
|
363
361
|
|
|
364
362
|
if (options) {
|
|
365
|
-
|
|
366
363
|
// Use the 'in' operator to check if the field is present
|
|
367
364
|
if ('comparator' in options && options.comparator !== undefined) {
|
|
368
365
|
this._comparator = options.comparator;
|
|
@@ -376,7 +373,6 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
376
373
|
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
|
|
377
374
|
}
|
|
378
375
|
|
|
379
|
-
|
|
380
376
|
protected override _root?: BSTNode<K, V> = undefined;
|
|
381
377
|
|
|
382
378
|
/**
|
|
@@ -389,33 +385,6 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
389
385
|
return this._root;
|
|
390
386
|
}
|
|
391
387
|
|
|
392
|
-
/**
|
|
393
|
-
* (Protected) Creates the default comparator function for keys that don't have a custom comparator.
|
|
394
|
-
* @remarks Time O(1) Space O(1)
|
|
395
|
-
* @returns The default comparator function.
|
|
396
|
-
*/
|
|
397
|
-
protected _createDefaultComparator(): Comparator<K> {
|
|
398
|
-
return (a: K, b: K): number => {
|
|
399
|
-
debugger
|
|
400
|
-
// If both keys are comparable (primitive types), use direct comparison
|
|
401
|
-
if (isComparable(a) && isComparable(b)) {
|
|
402
|
-
if (a > b) return 1;
|
|
403
|
-
if (a < b) return -1;
|
|
404
|
-
return 0;
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
// If keys are objects and no comparator is provided, throw an error
|
|
408
|
-
if (typeof a === 'object' || typeof b === 'object') {
|
|
409
|
-
throw TypeError(
|
|
410
|
-
`When comparing object type keys, a custom comparator must be provided in the constructor's options!`
|
|
411
|
-
);
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
// Default: keys are equal (fallback case)
|
|
415
|
-
return 0;
|
|
416
|
-
};
|
|
417
|
-
}
|
|
418
|
-
|
|
419
388
|
/**
|
|
420
389
|
* The comparator function used to determine the order of keys in the tree.
|
|
421
390
|
|
|
@@ -423,7 +392,6 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
423
392
|
*/
|
|
424
393
|
protected _comparator: Comparator<K>;
|
|
425
394
|
|
|
426
|
-
|
|
427
395
|
/**
|
|
428
396
|
* Gets the comparator function used by the tree.
|
|
429
397
|
* @remarks Time O(1)
|
|
@@ -618,8 +586,8 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
618
586
|
if (isRange) {
|
|
619
587
|
// Range search: Only go left if the current key is >= the lower bound
|
|
620
588
|
const range = keyNodeEntryOrPredicate as Range<K>;
|
|
621
|
-
const leftS =
|
|
622
|
-
const leftI =
|
|
589
|
+
const leftS = range.low;
|
|
590
|
+
const leftI = range.includeLow;
|
|
623
591
|
return (leftI && this._compare(cur.key, leftS) >= 0) || (!leftI && this._compare(cur.key, leftS) > 0);
|
|
624
592
|
}
|
|
625
593
|
if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
@@ -636,8 +604,8 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
636
604
|
if (isRange) {
|
|
637
605
|
// Range search: Only go right if current key <= upper bound
|
|
638
606
|
const range = keyNodeEntryOrPredicate as Range<K>;
|
|
639
|
-
const rightS =
|
|
640
|
-
const rightI =
|
|
607
|
+
const rightS = range.high;
|
|
608
|
+
const rightI = range.includeHigh;
|
|
641
609
|
return (rightI && this._compare(cur.key, rightS) <= 0) || (!rightI && this._compare(cur.key, rightS) < 0);
|
|
642
610
|
}
|
|
643
611
|
if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
@@ -884,6 +852,164 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
884
852
|
return this._bound(keyNodeEntryOrPredicate, false, iterationType);
|
|
885
853
|
}
|
|
886
854
|
|
|
855
|
+
/**
|
|
856
|
+
* Returns the first node with a key greater than or equal to the given key.
|
|
857
|
+
* This is equivalent to Java TreeMap.ceilingEntry().
|
|
858
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
859
|
+
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
860
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
861
|
+
*
|
|
862
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
863
|
+
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
864
|
+
* @returns The first node with key >= given key, or undefined if no such node exists.
|
|
865
|
+
*/
|
|
866
|
+
ceilingEntry(
|
|
867
|
+
keyNodeEntryOrPredicate:
|
|
868
|
+
| K
|
|
869
|
+
| BSTNode<K, V>
|
|
870
|
+
| [K | null | undefined, V | undefined]
|
|
871
|
+
| null
|
|
872
|
+
| undefined
|
|
873
|
+
| NodePredicate<BSTNode<K, V>>,
|
|
874
|
+
iterationType: IterationType = this.iterationType
|
|
875
|
+
): BSTNode<K, V> | undefined {
|
|
876
|
+
return this.lowerBound(keyNodeEntryOrPredicate, iterationType);
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
/**
|
|
880
|
+
* Returns the first node with a key strictly greater than the given key.
|
|
881
|
+
* This is equivalent to Java TreeMap.higherEntry().
|
|
882
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
883
|
+
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
884
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
885
|
+
*
|
|
886
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
887
|
+
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
888
|
+
* @returns The first node with key > given key, or undefined if no such node exists.
|
|
889
|
+
*/
|
|
890
|
+
higherEntry(
|
|
891
|
+
keyNodeEntryOrPredicate:
|
|
892
|
+
| K
|
|
893
|
+
| BSTNode<K, V>
|
|
894
|
+
| [K | null | undefined, V | undefined]
|
|
895
|
+
| null
|
|
896
|
+
| undefined
|
|
897
|
+
| NodePredicate<BSTNode<K, V>>,
|
|
898
|
+
iterationType: IterationType = this.iterationType
|
|
899
|
+
): BSTNode<K, V> | undefined {
|
|
900
|
+
return this.upperBound(keyNodeEntryOrPredicate, iterationType);
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
/**
|
|
904
|
+
* Returns the first node with a key less than or equal to the given key.
|
|
905
|
+
* This is equivalent to Java TreeMap.floorEntry().
|
|
906
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
907
|
+
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
908
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
909
|
+
*
|
|
910
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
911
|
+
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
912
|
+
* @returns The first node with key <= given key, or undefined if no such node exists.
|
|
913
|
+
*/
|
|
914
|
+
floorEntry(
|
|
915
|
+
keyNodeEntryOrPredicate:
|
|
916
|
+
| K
|
|
917
|
+
| BSTNode<K, V>
|
|
918
|
+
| [K | null | undefined, V | undefined]
|
|
919
|
+
| null
|
|
920
|
+
| undefined
|
|
921
|
+
| NodePredicate<BSTNode<K, V>>,
|
|
922
|
+
iterationType: IterationType = this.iterationType
|
|
923
|
+
): BSTNode<K, V> | undefined {
|
|
924
|
+
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === undefined) {
|
|
925
|
+
return undefined;
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
// Check if input is a predicate function first
|
|
929
|
+
if (this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
930
|
+
return this._floorByPredicate(keyNodeEntryOrPredicate, iterationType);
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
// Resolve input to a comparable key
|
|
934
|
+
let targetKey: K | undefined;
|
|
935
|
+
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
936
|
+
// Input is a BSTNode - extract its key
|
|
937
|
+
targetKey = keyNodeEntryOrPredicate.key;
|
|
938
|
+
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
939
|
+
// Input is a [key, value] entry - extract the key
|
|
940
|
+
const key = keyNodeEntryOrPredicate[0];
|
|
941
|
+
if (key === null || key === undefined) {
|
|
942
|
+
return undefined;
|
|
943
|
+
}
|
|
944
|
+
targetKey = key;
|
|
945
|
+
} else {
|
|
946
|
+
// Input is a raw key
|
|
947
|
+
targetKey = keyNodeEntryOrPredicate;
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
// Execute key-based search with binary search optimization
|
|
951
|
+
if (targetKey !== undefined) {
|
|
952
|
+
return this._floorByKey(targetKey, iterationType);
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
return undefined;
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
/**
|
|
959
|
+
* Returns the first node with a key strictly less than the given key.
|
|
960
|
+
* This is equivalent to Java TreeMap.lowerEntry().
|
|
961
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
962
|
+
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
963
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
964
|
+
*
|
|
965
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
966
|
+
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
967
|
+
* @returns The first node with key < given key, or undefined if no such node exists.
|
|
968
|
+
*/
|
|
969
|
+
lowerEntry(
|
|
970
|
+
keyNodeEntryOrPredicate:
|
|
971
|
+
| K
|
|
972
|
+
| BSTNode<K, V>
|
|
973
|
+
| [K | null | undefined, V | undefined]
|
|
974
|
+
| null
|
|
975
|
+
| undefined
|
|
976
|
+
| NodePredicate<BSTNode<K, V>>,
|
|
977
|
+
iterationType: IterationType = this.iterationType
|
|
978
|
+
): BSTNode<K, V> | undefined {
|
|
979
|
+
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === undefined) {
|
|
980
|
+
return undefined;
|
|
981
|
+
}
|
|
982
|
+
|
|
983
|
+
// Check if input is a predicate function first
|
|
984
|
+
if (this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
985
|
+
return this._lowerByPredicate(keyNodeEntryOrPredicate, iterationType);
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
// Resolve input to a comparable key
|
|
989
|
+
let targetKey: K | undefined;
|
|
990
|
+
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
991
|
+
// Input is a BSTNode - extract its key
|
|
992
|
+
targetKey = keyNodeEntryOrPredicate.key;
|
|
993
|
+
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
994
|
+
// Input is a [key, value] entry - extract the key
|
|
995
|
+
const key = keyNodeEntryOrPredicate[0];
|
|
996
|
+
if (key === null || key === undefined) {
|
|
997
|
+
return undefined;
|
|
998
|
+
}
|
|
999
|
+
targetKey = key;
|
|
1000
|
+
} else {
|
|
1001
|
+
// Input is a raw key
|
|
1002
|
+
targetKey = keyNodeEntryOrPredicate;
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
// Execute key-based search with binary search optimization
|
|
1006
|
+
if (targetKey !== undefined) {
|
|
1007
|
+
return this._lowerByKey(targetKey, iterationType);
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
return undefined;
|
|
1011
|
+
}
|
|
1012
|
+
|
|
887
1013
|
/**
|
|
888
1014
|
* Traverses the tree and returns nodes that are lesser or greater than a target node.
|
|
889
1015
|
* @remarks Time O(N), as it performs a full traversal. Space O(log N) or O(N).
|
|
@@ -1090,17 +1216,10 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
1090
1216
|
onlyOne = false,
|
|
1091
1217
|
startNode: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined = this._root,
|
|
1092
1218
|
iterationType: IterationType = this.iterationType
|
|
1093
|
-
):
|
|
1094
|
-
|
|
1095
|
-
const toDelete = this.search (
|
|
1096
|
-
keyNodeEntryOrPredicate,
|
|
1097
|
-
onlyOne,
|
|
1098
|
-
(node) => node,
|
|
1099
|
-
startNode,
|
|
1100
|
-
iterationType
|
|
1101
|
-
);
|
|
1219
|
+
): BinaryTreeDeleteResult<BSTNode<K, V>>[] {
|
|
1220
|
+
const toDelete = this.search(keyNodeEntryOrPredicate, onlyOne, node => node, startNode, iterationType);
|
|
1102
1221
|
|
|
1103
|
-
let results
|
|
1222
|
+
let results: BinaryTreeDeleteResult<BSTNode<K, V>>[] = [];
|
|
1104
1223
|
for (const node of toDelete) {
|
|
1105
1224
|
const deleteInfo = this.delete(node);
|
|
1106
1225
|
results = results.concat(deleteInfo);
|
|
@@ -1109,6 +1228,268 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
1109
1228
|
return results;
|
|
1110
1229
|
}
|
|
1111
1230
|
|
|
1231
|
+
/**
|
|
1232
|
+
* (Protected) Creates the default comparator function for keys that don't have a custom comparator.
|
|
1233
|
+
* @remarks Time O(1) Space O(1)
|
|
1234
|
+
* @returns The default comparator function.
|
|
1235
|
+
*/
|
|
1236
|
+
protected _createDefaultComparator(): Comparator<K> {
|
|
1237
|
+
return (a: K, b: K): number => {
|
|
1238
|
+
debugger;
|
|
1239
|
+
// If both keys are comparable (primitive types), use direct comparison
|
|
1240
|
+
if (isComparable(a) && isComparable(b)) {
|
|
1241
|
+
if (a > b) return 1;
|
|
1242
|
+
if (a < b) return -1;
|
|
1243
|
+
return 0;
|
|
1244
|
+
}
|
|
1245
|
+
|
|
1246
|
+
// If keys are objects and no comparator is provided, throw an error
|
|
1247
|
+
if (typeof a === 'object' || typeof b === 'object') {
|
|
1248
|
+
throw TypeError(
|
|
1249
|
+
`When comparing object type keys, a custom comparator must be provided in the constructor's options!`
|
|
1250
|
+
);
|
|
1251
|
+
}
|
|
1252
|
+
|
|
1253
|
+
// Default: keys are equal (fallback case)
|
|
1254
|
+
return 0;
|
|
1255
|
+
};
|
|
1256
|
+
}
|
|
1257
|
+
|
|
1258
|
+
/**
|
|
1259
|
+
* (Protected) Binary search for floor by key with pruning optimization.
|
|
1260
|
+
* Performs standard BST binary search, choosing left or right subtree based on comparator result.
|
|
1261
|
+
* Finds first node where key <= target.
|
|
1262
|
+
* @remarks Time O(h) where h is tree height.
|
|
1263
|
+
*
|
|
1264
|
+
* @param key - The target key to search for.
|
|
1265
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
1266
|
+
* @returns The first node with key <= target, or undefined if none exists.
|
|
1267
|
+
*/
|
|
1268
|
+
protected _floorByKey(key: K, iterationType: IterationType): BSTNode<K, V> | undefined {
|
|
1269
|
+
if (iterationType === 'RECURSIVE') {
|
|
1270
|
+
// Recursive binary search implementation
|
|
1271
|
+
const dfs = (cur: BSTNode<K, V> | null | undefined): BSTNode<K, V> | undefined => {
|
|
1272
|
+
if (!this.isRealNode(cur)) return undefined;
|
|
1273
|
+
|
|
1274
|
+
const cmp = this.comparator(cur.key!, key);
|
|
1275
|
+
|
|
1276
|
+
if (cmp <= 0) {
|
|
1277
|
+
// Current node satisfies the floor condition (cur.key <= target).
|
|
1278
|
+
// Try to find a larger candidate in the right subtree.
|
|
1279
|
+
const rightResult = dfs(cur.right);
|
|
1280
|
+
return rightResult ?? cur;
|
|
1281
|
+
} else {
|
|
1282
|
+
// Current node is too large, move left to find smaller keys.
|
|
1283
|
+
return dfs(cur.left);
|
|
1284
|
+
}
|
|
1285
|
+
};
|
|
1286
|
+
|
|
1287
|
+
return dfs(this.root);
|
|
1288
|
+
} else {
|
|
1289
|
+
// Iterative binary search implementation
|
|
1290
|
+
let current: BSTNode<K, V> | undefined = this.root;
|
|
1291
|
+
let result: BSTNode<K, V> | undefined = undefined;
|
|
1292
|
+
|
|
1293
|
+
while (this.isRealNode(current)) {
|
|
1294
|
+
const cmp = this.comparator(current.key!, key);
|
|
1295
|
+
|
|
1296
|
+
if (cmp <= 0) {
|
|
1297
|
+
// Current node is a candidate. Save it and try right subtree for a larger key.
|
|
1298
|
+
result = current;
|
|
1299
|
+
current = current.right ?? undefined;
|
|
1300
|
+
} else {
|
|
1301
|
+
// Current node is too large, move left.
|
|
1302
|
+
current = current.left ?? undefined;
|
|
1303
|
+
}
|
|
1304
|
+
}
|
|
1305
|
+
|
|
1306
|
+
return result;
|
|
1307
|
+
}
|
|
1308
|
+
}
|
|
1309
|
+
|
|
1310
|
+
/**
|
|
1311
|
+
* (Protected) In-order traversal search for floor by predicate.
|
|
1312
|
+
* Falls back to linear in-order traversal when predicate-based search is required.
|
|
1313
|
+
* Returns the last node that satisfies the predicate function.
|
|
1314
|
+
* @remarks Time Complexity: O(n) since it may visit every node.
|
|
1315
|
+
* Space Complexity: O(h) for recursion, O(h) for iterative stack.
|
|
1316
|
+
*
|
|
1317
|
+
* @param predicate - The predicate function to test nodes.
|
|
1318
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
1319
|
+
* @returns The last node satisfying predicate (highest key), or undefined if none found.
|
|
1320
|
+
*/
|
|
1321
|
+
protected _floorByPredicate(
|
|
1322
|
+
predicate: NodePredicate<BSTNode<K, V>>,
|
|
1323
|
+
iterationType: IterationType
|
|
1324
|
+
): BSTNode<K, V> | undefined {
|
|
1325
|
+
if (iterationType === 'RECURSIVE') {
|
|
1326
|
+
// Recursive in-order traversal
|
|
1327
|
+
let result: BSTNode<K, V> | undefined = undefined;
|
|
1328
|
+
|
|
1329
|
+
const dfs = (cur: BSTNode<K, V> | null | undefined): void => {
|
|
1330
|
+
if (!this.isRealNode(cur)) return;
|
|
1331
|
+
|
|
1332
|
+
// In-order: process left subtree first
|
|
1333
|
+
if (this.isRealNode(cur.left)) dfs(cur.left);
|
|
1334
|
+
|
|
1335
|
+
// Check current node
|
|
1336
|
+
if (predicate(cur)) {
|
|
1337
|
+
result = cur;
|
|
1338
|
+
}
|
|
1339
|
+
|
|
1340
|
+
// Process right subtree
|
|
1341
|
+
if (this.isRealNode(cur.right)) dfs(cur.right);
|
|
1342
|
+
};
|
|
1343
|
+
|
|
1344
|
+
dfs(this.root);
|
|
1345
|
+
return result;
|
|
1346
|
+
} else {
|
|
1347
|
+
// Iterative in-order traversal using explicit stack
|
|
1348
|
+
const stack: (BSTNode<K, V> | null | undefined)[] = [];
|
|
1349
|
+
let current: BSTNode<K, V> | null | undefined = this.root;
|
|
1350
|
+
let result: BSTNode<K, V> | undefined = undefined;
|
|
1351
|
+
|
|
1352
|
+
while (stack.length > 0 || this.isRealNode(current)) {
|
|
1353
|
+
if (this.isRealNode(current)) {
|
|
1354
|
+
// Go to the leftmost node
|
|
1355
|
+
stack.push(current);
|
|
1356
|
+
current = current.left;
|
|
1357
|
+
} else {
|
|
1358
|
+
// Pop from stack and process
|
|
1359
|
+
const node = stack.pop();
|
|
1360
|
+
if (!this.isRealNode(node)) break;
|
|
1361
|
+
|
|
1362
|
+
// Check if current node satisfies predicate
|
|
1363
|
+
if (predicate(node)) {
|
|
1364
|
+
result = node;
|
|
1365
|
+
}
|
|
1366
|
+
|
|
1367
|
+
// Visit right subtree
|
|
1368
|
+
current = node.right;
|
|
1369
|
+
}
|
|
1370
|
+
}
|
|
1371
|
+
|
|
1372
|
+
return result;
|
|
1373
|
+
}
|
|
1374
|
+
}
|
|
1375
|
+
|
|
1376
|
+
/**
|
|
1377
|
+
* (Protected) Binary search for lower by key with pruning optimization.
|
|
1378
|
+
* Performs standard BST binary search, choosing left or right subtree based on comparator result.
|
|
1379
|
+
* Finds first node where key < target.
|
|
1380
|
+
* @remarks Time O(h) where h is tree height.
|
|
1381
|
+
*
|
|
1382
|
+
* @param key - The target key to search for.
|
|
1383
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
1384
|
+
* @returns The first node with key < target, or undefined if none exists.
|
|
1385
|
+
*/
|
|
1386
|
+
protected _lowerByKey(key: K, iterationType: IterationType): BSTNode<K, V> | undefined {
|
|
1387
|
+
if (iterationType === 'RECURSIVE') {
|
|
1388
|
+
// Recursive binary search implementation
|
|
1389
|
+
const dfs = (cur: BSTNode<K, V> | null | undefined): BSTNode<K, V> | undefined => {
|
|
1390
|
+
if (!this.isRealNode(cur)) return undefined;
|
|
1391
|
+
|
|
1392
|
+
const cmp = this.comparator(cur.key!, key);
|
|
1393
|
+
|
|
1394
|
+
if (cmp < 0) {
|
|
1395
|
+
// Current node satisfies the lower condition (cur.key < target).
|
|
1396
|
+
// Try to find a larger candidate in the right subtree.
|
|
1397
|
+
const rightResult = dfs(cur.right);
|
|
1398
|
+
return rightResult ?? cur;
|
|
1399
|
+
} else {
|
|
1400
|
+
// Current node is too large or equal, move left to find smaller keys.
|
|
1401
|
+
return dfs(cur.left);
|
|
1402
|
+
}
|
|
1403
|
+
};
|
|
1404
|
+
|
|
1405
|
+
return dfs(this.root);
|
|
1406
|
+
} else {
|
|
1407
|
+
// Iterative binary search implementation
|
|
1408
|
+
let current: BSTNode<K, V> | undefined = this.root;
|
|
1409
|
+
let result: BSTNode<K, V> | undefined = undefined;
|
|
1410
|
+
|
|
1411
|
+
while (this.isRealNode(current)) {
|
|
1412
|
+
const cmp = this.comparator(current.key!, key);
|
|
1413
|
+
|
|
1414
|
+
if (cmp < 0) {
|
|
1415
|
+
// Current node is a candidate. Save it and try right subtree for a larger key.
|
|
1416
|
+
result = current;
|
|
1417
|
+
current = current.right ?? undefined;
|
|
1418
|
+
} else {
|
|
1419
|
+
// Current node is too large or equal, move left.
|
|
1420
|
+
current = current.left ?? undefined;
|
|
1421
|
+
}
|
|
1422
|
+
}
|
|
1423
|
+
|
|
1424
|
+
return result;
|
|
1425
|
+
}
|
|
1426
|
+
}
|
|
1427
|
+
|
|
1428
|
+
/**
|
|
1429
|
+
* (Protected) In-order traversal search for lower by predicate.
|
|
1430
|
+
* Falls back to linear in-order traversal when predicate-based search is required.
|
|
1431
|
+
* Returns the node that satisfies the predicate and appears last in in-order traversal.
|
|
1432
|
+
* @remarks Time Complexity: O(n) since it may visit every node.
|
|
1433
|
+
* Space Complexity: O(h) for recursion, O(h) for iterative stack.
|
|
1434
|
+
*
|
|
1435
|
+
* @param predicate - The predicate function to test nodes.
|
|
1436
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
1437
|
+
* @returns The last node satisfying predicate (highest key < target), or undefined if none found.
|
|
1438
|
+
*/
|
|
1439
|
+
protected _lowerByPredicate(
|
|
1440
|
+
predicate: NodePredicate<BSTNode<K, V>>,
|
|
1441
|
+
iterationType: IterationType
|
|
1442
|
+
): BSTNode<K, V> | undefined {
|
|
1443
|
+
if (iterationType === 'RECURSIVE') {
|
|
1444
|
+
// Recursive in-order traversal
|
|
1445
|
+
let result: BSTNode<K, V> | undefined = undefined;
|
|
1446
|
+
|
|
1447
|
+
const dfs = (cur: BSTNode<K, V> | null | undefined): void => {
|
|
1448
|
+
if (!this.isRealNode(cur)) return;
|
|
1449
|
+
|
|
1450
|
+
// In-order: process left subtree first
|
|
1451
|
+
if (this.isRealNode(cur.left)) dfs(cur.left);
|
|
1452
|
+
|
|
1453
|
+
// Check current node
|
|
1454
|
+
if (predicate(cur)) {
|
|
1455
|
+
result = cur;
|
|
1456
|
+
}
|
|
1457
|
+
|
|
1458
|
+
// Process right subtree
|
|
1459
|
+
if (this.isRealNode(cur.right)) dfs(cur.right);
|
|
1460
|
+
};
|
|
1461
|
+
|
|
1462
|
+
dfs(this.root);
|
|
1463
|
+
return result;
|
|
1464
|
+
} else {
|
|
1465
|
+
// Iterative in-order traversal using explicit stack
|
|
1466
|
+
const stack: (BSTNode<K, V> | null | undefined)[] = [];
|
|
1467
|
+
let current: BSTNode<K, V> | null | undefined = this.root;
|
|
1468
|
+
let result: BSTNode<K, V> | undefined = undefined;
|
|
1469
|
+
|
|
1470
|
+
while (stack.length > 0 || this.isRealNode(current)) {
|
|
1471
|
+
if (this.isRealNode(current)) {
|
|
1472
|
+
// Go to the leftmost node
|
|
1473
|
+
stack.push(current);
|
|
1474
|
+
current = current.left;
|
|
1475
|
+
} else {
|
|
1476
|
+
// Pop from stack and process
|
|
1477
|
+
const node = stack.pop();
|
|
1478
|
+
if (!this.isRealNode(node)) break;
|
|
1479
|
+
|
|
1480
|
+
// Check if current node satisfies predicate
|
|
1481
|
+
if (predicate(node)) {
|
|
1482
|
+
result = node;
|
|
1483
|
+
}
|
|
1484
|
+
|
|
1485
|
+
// Visit right subtree
|
|
1486
|
+
current = node.right;
|
|
1487
|
+
}
|
|
1488
|
+
}
|
|
1489
|
+
|
|
1490
|
+
return result;
|
|
1491
|
+
}
|
|
1492
|
+
}
|
|
1112
1493
|
|
|
1113
1494
|
/**
|
|
1114
1495
|
* (Protected) Core bound search implementation supporting all parameter types.
|
|
@@ -1330,7 +1711,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
1330
1711
|
protected override _snapshotOptions<TK = K, TV = V, TR = R>(): BSTOptions<TK, TV, TR> {
|
|
1331
1712
|
return {
|
|
1332
1713
|
...super._snapshotOptions<TK, TV, TR>(),
|
|
1333
|
-
comparator: this._comparator as unknown as BSTOptions<TK, TV, TR>['comparator']
|
|
1714
|
+
comparator: this._comparator as unknown as BSTOptions<TK, TV, TR>['comparator']
|
|
1334
1715
|
};
|
|
1335
1716
|
}
|
|
1336
1717
|
|