tree-multimap-typed 2.2.3 → 2.2.4
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/cjs/index.cjs +85 -75
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +85 -75
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +85 -75
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +85 -75
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -5
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -3
- package/dist/types/data-structures/binary-tree/bst.d.ts +46 -26
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +4 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +5 -5
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +5 -5
- package/dist/umd/tree-multimap-typed.js +85 -75
- package/dist/umd/tree-multimap-typed.js.map +1 -1
- package/dist/umd/tree-multimap-typed.min.js +3 -3
- package/dist/umd/tree-multimap-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-counter.ts +1 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +7 -8
- package/src/data-structures/binary-tree/avl-tree.ts +4 -5
- package/src/data-structures/binary-tree/bst.ts +111 -82
- package/src/data-structures/binary-tree/red-black-tree.ts +1 -2
- package/src/data-structures/binary-tree/tree-counter.ts +5 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +7 -8
- package/src/types/data-structures/binary-tree/bst.ts +5 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tree-multimap-typed",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.4",
|
|
4
4
|
"description": "Tree Multimap",
|
|
5
5
|
"browser": "dist/umd/tree-multimap-typed.min.js",
|
|
6
6
|
"umd:main": "dist/umd/tree-multimap-typed.min.js",
|
|
@@ -217,6 +217,6 @@
|
|
|
217
217
|
"typescript": "^4.9.5"
|
|
218
218
|
},
|
|
219
219
|
"dependencies": {
|
|
220
|
-
"data-structure-typed": "^2.2.
|
|
220
|
+
"data-structure-typed": "^2.2.4"
|
|
221
221
|
}
|
|
222
222
|
}
|
|
@@ -9,7 +9,6 @@
|
|
|
9
9
|
import type {
|
|
10
10
|
AVLTreeCounterOptions,
|
|
11
11
|
BinaryTreeDeleteResult,
|
|
12
|
-
BinaryTreeOptions,
|
|
13
12
|
BSTNOptKeyOrNode,
|
|
14
13
|
EntryCallback,
|
|
15
14
|
FamilyPosition,
|
|
@@ -404,7 +403,7 @@ export class AVLTreeCounter<K = any, V = any, R = any> extends AVLTree<K, V, R>
|
|
|
404
403
|
*/
|
|
405
404
|
override map<MK = K, MV = V, MR = any>(
|
|
406
405
|
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
407
|
-
options?: Partial<
|
|
406
|
+
options?: Partial<AVLTreeCounterOptions<MK, MV, MR>>,
|
|
408
407
|
thisArg?: unknown
|
|
409
408
|
): AVLTreeCounter<MK, MV, MR> {
|
|
410
409
|
const out = this._createLike<MK, MV, MR>([], options);
|
|
@@ -8,7 +8,6 @@
|
|
|
8
8
|
|
|
9
9
|
import type {
|
|
10
10
|
AVLTreeMultiMapOptions,
|
|
11
|
-
AVLTreeOptions,
|
|
12
11
|
BTNOptKeyOrNull,
|
|
13
12
|
ElemOf,
|
|
14
13
|
EntryCallback,
|
|
@@ -354,7 +353,7 @@ export class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<K, V[],
|
|
|
354
353
|
*/
|
|
355
354
|
override map<MK = K, MVArr extends unknown[] = V[], MR = any>(
|
|
356
355
|
callback: EntryCallback<K, V[] | undefined, [MK, MVArr]>,
|
|
357
|
-
options?: Partial<
|
|
356
|
+
options?: Partial<AVLTreeMultiMapOptions<MK, MVArr, MR>>,
|
|
358
357
|
thisArg?: unknown
|
|
359
358
|
): AVLTreeMultiMap<MK, ElemOf<MVArr>, MR>;
|
|
360
359
|
|
|
@@ -371,7 +370,7 @@ export class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<K, V[],
|
|
|
371
370
|
*/
|
|
372
371
|
override map<MK = K, MV = V[], MR = any>(
|
|
373
372
|
callback: EntryCallback<K, V[] | undefined, [MK, MV]>,
|
|
374
|
-
options?: Partial<
|
|
373
|
+
options?: Partial<AVLTreeMultiMapOptions<MK, MV, MR>>,
|
|
375
374
|
thisArg?: unknown
|
|
376
375
|
): AVLTree<MK, MV, MR>;
|
|
377
376
|
|
|
@@ -388,7 +387,7 @@ export class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<K, V[],
|
|
|
388
387
|
*/
|
|
389
388
|
override map<MK, MV, MR extends object>(
|
|
390
389
|
callback: EntryCallback<K, V[] | undefined, [MK, MV]>,
|
|
391
|
-
options?: Partial<
|
|
390
|
+
options?: Partial<AVLTreeMultiMapOptions<MK, MV, MR>>,
|
|
392
391
|
thisArg?: unknown
|
|
393
392
|
): AVLTree<MK, MV, MR> {
|
|
394
393
|
const out = this._createLike<MK, MV, MR>([], options);
|
|
@@ -406,10 +405,10 @@ export class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<K, V[],
|
|
|
406
405
|
* @param [options] - Optional constructor options for the like-kind instance.
|
|
407
406
|
* @returns An empty like-kind instance.
|
|
408
407
|
*/
|
|
409
|
-
protected override _createInstance<TK = K, TV = V, TR = R>(options?: Partial<
|
|
408
|
+
protected override _createInstance<TK = K, TV = V, TR = R>(options?: Partial<AVLTreeMultiMapOptions<TK, TV, TR>>): this {
|
|
410
409
|
const Ctor = this.constructor as unknown as new (
|
|
411
410
|
iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
412
|
-
opts?:
|
|
411
|
+
opts?: AVLTreeMultiMapOptions<TK, TV, TR>
|
|
413
412
|
) => AVLTree<TK, TV, TR>;
|
|
414
413
|
return new Ctor([], { ...(this._snapshotOptions?.<TK, TV, TR>() ?? {}), ...(options ?? {}) }) as unknown as this;
|
|
415
414
|
}
|
|
@@ -426,11 +425,11 @@ export class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<K, V[],
|
|
|
426
425
|
*/
|
|
427
426
|
protected override _createLike<TK = K, TV = V, TR = R>(
|
|
428
427
|
iter: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR> = [],
|
|
429
|
-
options?: Partial<
|
|
428
|
+
options?: Partial<AVLTreeMultiMapOptions<TK, TV, TR>>
|
|
430
429
|
): AVLTree<TK, TV, TR> {
|
|
431
430
|
const Ctor = this.constructor as unknown as new (
|
|
432
431
|
iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
433
|
-
opts?:
|
|
432
|
+
opts?: AVLTreeMultiMapOptions<TK, TV, TR>
|
|
434
433
|
) => AVLTree<TK, TV, TR>;
|
|
435
434
|
return new Ctor(iter, { ...(this._snapshotOptions?.<TK, TV, TR>() ?? {}), ...(options ?? {}) });
|
|
436
435
|
}
|
|
@@ -17,7 +17,6 @@ import type {
|
|
|
17
17
|
IterationType,
|
|
18
18
|
RBTNColor
|
|
19
19
|
} from '../../types';
|
|
20
|
-
import { BSTOptions } from '../../types';
|
|
21
20
|
import { IBinaryTree } from '../../interfaces';
|
|
22
21
|
|
|
23
22
|
/**
|
|
@@ -512,10 +511,10 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
512
511
|
* @param [options] - Options for the new tree.
|
|
513
512
|
* @returns A new, empty tree.
|
|
514
513
|
*/
|
|
515
|
-
protected override _createInstance<TK = K, TV = V, TR = R>(options?: Partial<
|
|
514
|
+
protected override _createInstance<TK = K, TV = V, TR = R>(options?: Partial<AVLTreeOptions<TK, TV, TR>>): this {
|
|
516
515
|
const Ctor = this.constructor as unknown as new (
|
|
517
516
|
iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
518
|
-
opts?:
|
|
517
|
+
opts?: AVLTreeOptions<TK, TV, TR>
|
|
519
518
|
) => this;
|
|
520
519
|
return new Ctor([], { ...this._snapshotOptions<TK, TV, TR>(), ...(options ?? {}) }) as unknown as this;
|
|
521
520
|
}
|
|
@@ -531,11 +530,11 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
531
530
|
*/
|
|
532
531
|
protected override _createLike<TK = K, TV = V, TR = R>(
|
|
533
532
|
iter: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR> = [],
|
|
534
|
-
options?: Partial<
|
|
533
|
+
options?: Partial<AVLTreeOptions<TK, TV, TR>>
|
|
535
534
|
): AVLTree<TK, TV, TR> {
|
|
536
535
|
const Ctor = this.constructor as unknown as new (
|
|
537
536
|
iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
538
|
-
opts?:
|
|
537
|
+
opts?: AVLTreeOptions<TK, TV, TR>
|
|
539
538
|
) => AVLTree<TK, TV, TR>;
|
|
540
539
|
return new Ctor(iter, { ...this._snapshotOptions<TK, TV, TR>(), ...(options ?? {}) });
|
|
541
540
|
}
|
|
@@ -7,11 +7,10 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import type {
|
|
10
|
-
|
|
10
|
+
BinaryTreeDeleteResult,
|
|
11
11
|
BSTNOptKeyOrNode,
|
|
12
12
|
BSTOptions,
|
|
13
13
|
BTNRep,
|
|
14
|
-
Comparable,
|
|
15
14
|
Comparator,
|
|
16
15
|
CP,
|
|
17
16
|
DFSOrderPattern,
|
|
@@ -356,20 +355,28 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
356
355
|
*/
|
|
357
356
|
constructor(
|
|
358
357
|
keysNodesEntriesOrRaws: Iterable<
|
|
359
|
-
K | BSTNode
|
|
358
|
+
K | BSTNode | [K | null | undefined, V | undefined] | null | undefined | R
|
|
360
359
|
> = [],
|
|
361
360
|
options?: BSTOptions<K, V, R>
|
|
362
361
|
) {
|
|
363
362
|
super([], options);
|
|
364
363
|
|
|
365
364
|
if (options) {
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
if (
|
|
365
|
+
|
|
366
|
+
// Use the 'in' operator to check if the field is present
|
|
367
|
+
if ('comparator' in options && options.comparator !== undefined) {
|
|
368
|
+
this._comparator = options.comparator;
|
|
369
|
+
} else {
|
|
370
|
+
this._comparator = this._createDefaultComparator();
|
|
371
|
+
}
|
|
372
|
+
} else {
|
|
373
|
+
this._comparator = this._createDefaultComparator();
|
|
369
374
|
}
|
|
375
|
+
|
|
370
376
|
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
|
|
371
377
|
}
|
|
372
378
|
|
|
379
|
+
|
|
373
380
|
protected override _root?: BSTNode<K, V> = undefined;
|
|
374
381
|
|
|
375
382
|
/**
|
|
@@ -382,42 +389,40 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
382
389
|
return this._root;
|
|
383
390
|
}
|
|
384
391
|
|
|
385
|
-
protected _isReverse: boolean = false;
|
|
386
|
-
|
|
387
392
|
/**
|
|
388
|
-
*
|
|
389
|
-
* @remarks Time O(1)
|
|
390
|
-
*
|
|
391
|
-
* @returns True if the tree is reversed (e.g., a max-heap logic).
|
|
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.
|
|
392
396
|
*/
|
|
393
|
-
|
|
394
|
-
return
|
|
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
|
+
};
|
|
395
417
|
}
|
|
396
418
|
|
|
397
419
|
/**
|
|
398
|
-
* The
|
|
399
|
-
|
|
420
|
+
* The comparator function used to determine the order of keys in the tree.
|
|
421
|
+
|
|
422
|
+
* @remarks Time O(1) Space O(1)
|
|
400
423
|
*/
|
|
401
|
-
protected _comparator: Comparator<K
|
|
402
|
-
|
|
403
|
-
if (a > b) return 1;
|
|
404
|
-
if (a < b) return -1;
|
|
405
|
-
return 0;
|
|
406
|
-
}
|
|
407
|
-
if (this._specifyComparable) {
|
|
408
|
-
const va = this._specifyComparable(a);
|
|
409
|
-
const vb = this._specifyComparable(b);
|
|
410
|
-
if (va > vb) return 1;
|
|
411
|
-
if (va < vb) return -1;
|
|
412
|
-
return 0;
|
|
413
|
-
}
|
|
414
|
-
if (typeof a === 'object' || typeof b === 'object') {
|
|
415
|
-
throw TypeError(
|
|
416
|
-
`When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
|
|
417
|
-
);
|
|
418
|
-
}
|
|
419
|
-
return 0;
|
|
420
|
-
};
|
|
424
|
+
protected _comparator: Comparator<K>;
|
|
425
|
+
|
|
421
426
|
|
|
422
427
|
/**
|
|
423
428
|
* Gets the comparator function used by the tree.
|
|
@@ -429,18 +434,6 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
429
434
|
return this._comparator;
|
|
430
435
|
}
|
|
431
436
|
|
|
432
|
-
protected _specifyComparable?: (key: K) => Comparable;
|
|
433
|
-
|
|
434
|
-
/**
|
|
435
|
-
* Gets the function used to extract a comparable value from a complex key.
|
|
436
|
-
* @remarks Time O(1)
|
|
437
|
-
*
|
|
438
|
-
* @returns The key-to-comparable conversion function.
|
|
439
|
-
*/
|
|
440
|
-
get specifyComparable(): ((key: K) => Comparable) | undefined {
|
|
441
|
-
return this._specifyComparable;
|
|
442
|
-
}
|
|
443
|
-
|
|
444
437
|
/**
|
|
445
438
|
* (Protected) Creates a new BST node.
|
|
446
439
|
* @remarks Time O(1), Space O(1)
|
|
@@ -489,7 +482,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
489
482
|
* @returns True if the key is valid, false otherwise.
|
|
490
483
|
*/
|
|
491
484
|
override isValidKey(key: any): key is K {
|
|
492
|
-
return isComparable(key
|
|
485
|
+
return isComparable(key);
|
|
493
486
|
}
|
|
494
487
|
|
|
495
488
|
/**
|
|
@@ -625,8 +618,8 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
625
618
|
if (isRange) {
|
|
626
619
|
// Range search: Only go left if the current key is >= the lower bound
|
|
627
620
|
const range = keyNodeEntryOrPredicate as Range<K>;
|
|
628
|
-
const leftS =
|
|
629
|
-
const leftI =
|
|
621
|
+
const leftS = range.low;
|
|
622
|
+
const leftI = range.includeLow;
|
|
630
623
|
return (leftI && this._compare(cur.key, leftS) >= 0) || (!leftI && this._compare(cur.key, leftS) > 0);
|
|
631
624
|
}
|
|
632
625
|
if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
@@ -643,8 +636,8 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
643
636
|
if (isRange) {
|
|
644
637
|
// Range search: Only go right if current key <= upper bound
|
|
645
638
|
const range = keyNodeEntryOrPredicate as Range<K>;
|
|
646
|
-
const rightS =
|
|
647
|
-
const rightI =
|
|
639
|
+
const rightS = range.high;
|
|
640
|
+
const rightI = range.includeHigh;
|
|
648
641
|
return (rightI && this._compare(cur.key, rightS) <= 0) || (!rightI && this._compare(cur.key, rightS) < 0);
|
|
649
642
|
}
|
|
650
643
|
if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
@@ -1037,7 +1030,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
1037
1030
|
*/
|
|
1038
1031
|
override map<MK = K, MV = V, MR = any>(
|
|
1039
1032
|
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
1040
|
-
options?: Partial<
|
|
1033
|
+
options?: Partial<BSTOptions<MK, MV, MR>>,
|
|
1041
1034
|
thisArg?: unknown
|
|
1042
1035
|
): BST<MK, MV, MR> {
|
|
1043
1036
|
const out = this._createLike<MK, MV, MR>([], options);
|
|
@@ -1050,36 +1043,73 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
1050
1043
|
}
|
|
1051
1044
|
|
|
1052
1045
|
/**
|
|
1053
|
-
* Deletes
|
|
1054
|
-
*
|
|
1046
|
+
* Deletes nodes that match a key, node, entry, predicate, or range.
|
|
1047
|
+
*
|
|
1048
|
+
* @remarks
|
|
1049
|
+
* Time Complexity: O(N) for search + O(M log N) for M deletions, where N is tree size.
|
|
1050
|
+
* Space Complexity: O(M) for storing matched nodes and result map.
|
|
1051
|
+
*
|
|
1052
|
+
* @template K - The key type.
|
|
1053
|
+
* @template V - The value type.
|
|
1054
|
+
*
|
|
1055
|
+
* @param keyNodeEntryOrPredicate - The search criteria. Can be one of:
|
|
1056
|
+
* - A key (type K): searches for exact key match using the comparator.
|
|
1057
|
+
* - A BSTNode: searches for the matching node in the tree.
|
|
1058
|
+
* - An entry tuple: searches for the key-value pair.
|
|
1059
|
+
* - A NodePredicate function: tests each node and returns true for matches.
|
|
1060
|
+
* - A Range object: searches for nodes whose keys fall within the specified range (inclusive/exclusive based on range settings).
|
|
1061
|
+
* - null or undefined: treated as no match, returns empty results.
|
|
1062
|
+
*
|
|
1063
|
+
* @param onlyOne - If true, stops the search after finding the first match and only deletes that one node.
|
|
1064
|
+
* If false (default), searches for and deletes all matching nodes.
|
|
1065
|
+
*
|
|
1066
|
+
* @param startNode - The node to start the search from. Can be:
|
|
1067
|
+
* - A key, node, or entry: the method resolves it to a node and searches from that subtree.
|
|
1068
|
+
* - null or undefined: defaults to the root, searching the entire tree.
|
|
1069
|
+
* - Default value: this._root (the tree's root).
|
|
1070
|
+
*
|
|
1071
|
+
* @param iterationType - Controls the internal traversal implementation:
|
|
1072
|
+
* - 'RECURSIVE': uses recursive function calls for traversal.
|
|
1073
|
+
* - 'ITERATIVE': uses explicit stack-based iteration.
|
|
1074
|
+
* - Default: this.iterationType (the tree's default iteration mode).
|
|
1055
1075
|
*
|
|
1056
|
-
* @
|
|
1057
|
-
*
|
|
1076
|
+
* @returns A Map<K, boolean> containing the deletion results:
|
|
1077
|
+
* - Key: the matched node's key.
|
|
1078
|
+
* - Value: true if the deletion succeeded, false if it failed (e.g., key not found during deletion phase).
|
|
1079
|
+
* - If no nodes match the search criteria, the returned map is empty.
|
|
1058
1080
|
*/
|
|
1059
|
-
deleteWhere(
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1081
|
+
deleteWhere(
|
|
1082
|
+
keyNodeEntryOrPredicate:
|
|
1083
|
+
| K
|
|
1084
|
+
| BSTNode<K, V>
|
|
1085
|
+
| [K | null | undefined, V | undefined]
|
|
1086
|
+
| null
|
|
1087
|
+
| undefined
|
|
1088
|
+
| NodePredicate<BSTNode<K, V>>
|
|
1089
|
+
| Range<K>,
|
|
1090
|
+
onlyOne = false,
|
|
1091
|
+
startNode: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined = this._root,
|
|
1092
|
+
iterationType: IterationType = this.iterationType
|
|
1093
|
+
): BinaryTreeDeleteResult<BSTNode<K, V>>[] {
|
|
1063
1094
|
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
if (!node) break;
|
|
1095
|
+
const toDelete = this.search (
|
|
1096
|
+
keyNodeEntryOrPredicate,
|
|
1097
|
+
onlyOne,
|
|
1098
|
+
(node) => node,
|
|
1099
|
+
startNode,
|
|
1100
|
+
iterationType
|
|
1101
|
+
);
|
|
1072
1102
|
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
}
|
|
1078
|
-
cur = node.right as BSTNode<K, V> | null | undefined;
|
|
1103
|
+
let results : BinaryTreeDeleteResult<BSTNode<K, V>>[] = [];
|
|
1104
|
+
for (const node of toDelete) {
|
|
1105
|
+
const deleteInfo = this.delete(node);
|
|
1106
|
+
results = results.concat(deleteInfo);
|
|
1079
1107
|
}
|
|
1080
|
-
|
|
1108
|
+
|
|
1109
|
+
return results;
|
|
1081
1110
|
}
|
|
1082
1111
|
|
|
1112
|
+
|
|
1083
1113
|
/**
|
|
1084
1114
|
* (Protected) Core bound search implementation supporting all parameter types.
|
|
1085
1115
|
* Unified logic for both lowerBound and upperBound.
|
|
@@ -1300,8 +1330,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
1300
1330
|
protected override _snapshotOptions<TK = K, TV = V, TR = R>(): BSTOptions<TK, TV, TR> {
|
|
1301
1331
|
return {
|
|
1302
1332
|
...super._snapshotOptions<TK, TV, TR>(),
|
|
1303
|
-
|
|
1304
|
-
isReverse: this.isReverse as BSTOptions<TK, TV, TR>['isReverse']
|
|
1333
|
+
comparator: this._comparator as unknown as BSTOptions<TK, TV, TR>['comparator'],
|
|
1305
1334
|
};
|
|
1306
1335
|
}
|
|
1307
1336
|
|
|
@@ -1335,14 +1364,14 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
1335
1364
|
|
|
1336
1365
|
/**
|
|
1337
1366
|
* (Protected) Compares two keys using the tree's comparator and reverse setting.
|
|
1338
|
-
* @remarks Time O(1)
|
|
1367
|
+
* @remarks Time O(1) Space O(1)
|
|
1339
1368
|
*
|
|
1340
1369
|
* @param a - The first key.
|
|
1341
1370
|
* @param b - The second key.
|
|
1342
1371
|
* @returns A number (1, -1, or 0) representing the comparison.
|
|
1343
1372
|
*/
|
|
1344
1373
|
protected _compare(a: K, b: K) {
|
|
1345
|
-
return this.
|
|
1374
|
+
return this._comparator(a, b);
|
|
1346
1375
|
}
|
|
1347
1376
|
|
|
1348
1377
|
/**
|
|
@@ -1352,7 +1381,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
1352
1381
|
* @param key - The key of the node to delete.
|
|
1353
1382
|
* @returns True if the node was found and deleted, false otherwise.
|
|
1354
1383
|
*/
|
|
1355
|
-
|
|
1384
|
+
protected _deleteByKey(key: K): boolean {
|
|
1356
1385
|
let node = this._root as BSTNode<K, V> | undefined;
|
|
1357
1386
|
|
|
1358
1387
|
// 1. Find the node
|
|
@@ -8,7 +8,6 @@
|
|
|
8
8
|
|
|
9
9
|
import type {
|
|
10
10
|
BinaryTreeDeleteResult,
|
|
11
|
-
BinaryTreeOptions,
|
|
12
11
|
CRUD,
|
|
13
12
|
EntryCallback,
|
|
14
13
|
FamilyPosition,
|
|
@@ -463,7 +462,7 @@ export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implem
|
|
|
463
462
|
|
|
464
463
|
override map<MK = K, MV = V, MR = any>(
|
|
465
464
|
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
466
|
-
options?: Partial<
|
|
465
|
+
options?: Partial<RedBlackTreeOptions<MK, MV, MR>>,
|
|
467
466
|
thisArg?: unknown
|
|
468
467
|
): RedBlackTree<MK, MV, MR> {
|
|
469
468
|
const out = this._createLike<MK, MV, MR>([], options);
|
|
@@ -8,7 +8,6 @@
|
|
|
8
8
|
|
|
9
9
|
import type {
|
|
10
10
|
BinaryTreeDeleteResult,
|
|
11
|
-
BinaryTreeOptions,
|
|
12
11
|
BSTNOptKeyOrNode,
|
|
13
12
|
EntryCallback,
|
|
14
13
|
FamilyPosition,
|
|
@@ -17,7 +16,6 @@ import type {
|
|
|
17
16
|
RBTNColor,
|
|
18
17
|
TreeCounterOptions
|
|
19
18
|
} from '../../types';
|
|
20
|
-
import { BSTOptions } from '../../types';
|
|
21
19
|
import { BSTNode } from './bst';
|
|
22
20
|
import { IBinaryTree } from '../../interfaces';
|
|
23
21
|
import { RedBlackTree } from './red-black-tree';
|
|
@@ -432,7 +430,7 @@ export class TreeCounter<K = any, V = any, R = any> extends RedBlackTree<K, V, R
|
|
|
432
430
|
*/
|
|
433
431
|
override map<MK = K, MV = V, MR = any>(
|
|
434
432
|
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
435
|
-
options?: Partial<
|
|
433
|
+
options?: Partial<TreeCounterOptions<MK, MV, MR>>,
|
|
436
434
|
thisArg?: unknown
|
|
437
435
|
): TreeCounter<MK, MV, MR> {
|
|
438
436
|
const out = this._createLike<MK, MV, MR>([], options);
|
|
@@ -465,10 +463,10 @@ export class TreeCounter<K = any, V = any, R = any> extends RedBlackTree<K, V, R
|
|
|
465
463
|
* @param [options] - Optional constructor options for the like-kind instance.
|
|
466
464
|
* @returns An empty like-kind instance.
|
|
467
465
|
*/
|
|
468
|
-
protected override _createInstance<TK = K, TV = V, TR = R>(options?: Partial<
|
|
466
|
+
protected override _createInstance<TK = K, TV = V, TR = R>(options?: Partial<TreeCounterOptions<TK, TV, TR>>): this {
|
|
469
467
|
const Ctor = this.constructor as unknown as new (
|
|
470
468
|
iter?: Iterable<TK | BSTNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
471
|
-
opts?:
|
|
469
|
+
opts?: TreeCounterOptions<TK, TV, TR>
|
|
472
470
|
) => this;
|
|
473
471
|
return new Ctor([], { ...this._snapshotOptions<TK, TV, TR>(), ...(options ?? {}) }) as unknown as this;
|
|
474
472
|
}
|
|
@@ -485,11 +483,11 @@ export class TreeCounter<K = any, V = any, R = any> extends RedBlackTree<K, V, R
|
|
|
485
483
|
*/
|
|
486
484
|
protected override _createLike<TK = K, TV = V, TR = R>(
|
|
487
485
|
iter: Iterable<TK | BSTNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR> = [],
|
|
488
|
-
options?: Partial<
|
|
486
|
+
options?: Partial<TreeCounterOptions<TK, TV, TR>>
|
|
489
487
|
): TreeCounter<TK, TV, TR> {
|
|
490
488
|
const Ctor = this.constructor as unknown as new (
|
|
491
489
|
iter?: Iterable<TK | BSTNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
492
|
-
opts?:
|
|
490
|
+
opts?: TreeCounterOptions<TK, TV, TR>
|
|
493
491
|
) => TreeCounter<TK, TV, TR>;
|
|
494
492
|
return new Ctor(iter as unknown as Iterable<TK | any>, {
|
|
495
493
|
...this._snapshotOptions<TK, TV, TR>(),
|
|
@@ -12,7 +12,6 @@ import type {
|
|
|
12
12
|
EntryCallback,
|
|
13
13
|
FamilyPosition,
|
|
14
14
|
RBTNColor,
|
|
15
|
-
RedBlackTreeOptions,
|
|
16
15
|
TreeMultiMapOptions
|
|
17
16
|
} from '../../types';
|
|
18
17
|
import { RedBlackTree, RedBlackTreeNode } from './red-black-tree';
|
|
@@ -476,13 +475,13 @@ export class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTree<K, V[]
|
|
|
476
475
|
|
|
477
476
|
override map<MK = K, MVArr extends unknown[] = V[], MR = any>(
|
|
478
477
|
callback: EntryCallback<K, V[] | undefined, [MK, MVArr]>,
|
|
479
|
-
options?: Partial<
|
|
478
|
+
options?: Partial<TreeMultiMapOptions<MK, MVArr, MR>>,
|
|
480
479
|
thisArg?: unknown
|
|
481
480
|
): TreeMultiMap<MK, ElemOf<MVArr>, MR>;
|
|
482
481
|
|
|
483
482
|
override map<MK = K, MV = V[], MR = any>(
|
|
484
483
|
callback: EntryCallback<K, V[] | undefined, [MK, MV]>,
|
|
485
|
-
options?: Partial<
|
|
484
|
+
options?: Partial<TreeMultiMapOptions<MK, MV, MR>>,
|
|
486
485
|
thisArg?: unknown
|
|
487
486
|
): RedBlackTree<MK, MV, MR>;
|
|
488
487
|
|
|
@@ -499,7 +498,7 @@ export class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTree<K, V[]
|
|
|
499
498
|
*/
|
|
500
499
|
override map<MK, MV, MR extends object>(
|
|
501
500
|
callback: EntryCallback<K, V[] | undefined, [MK, MV]>,
|
|
502
|
-
options?: Partial<
|
|
501
|
+
options?: Partial<TreeMultiMapOptions<MK, MV, MR>>,
|
|
503
502
|
thisArg?: unknown
|
|
504
503
|
): RedBlackTree<MK, MV, MR> {
|
|
505
504
|
const out = this._createLike<MK, MV, MR>([], options);
|
|
@@ -517,10 +516,10 @@ export class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTree<K, V[]
|
|
|
517
516
|
* @param [options] - Optional constructor options for the like-kind instance.
|
|
518
517
|
* @returns An empty like-kind instance.
|
|
519
518
|
*/
|
|
520
|
-
protected override _createInstance<TK = K, TV = V, TR = R>(options?: Partial<
|
|
519
|
+
protected override _createInstance<TK = K, TV = V, TR = R>(options?: Partial<TreeMultiMapOptions<TK, TV, TR>>): this {
|
|
521
520
|
const Ctor = this.constructor as unknown as new (
|
|
522
521
|
iter?: Iterable<TK | RedBlackTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
523
|
-
opts?:
|
|
522
|
+
opts?: TreeMultiMapOptions<TK, TV, TR>
|
|
524
523
|
) => RedBlackTree<TK, TV, TR>;
|
|
525
524
|
return new Ctor([], { ...(this._snapshotOptions?.<TK, TV, TR>() ?? {}), ...(options ?? {}) }) as unknown as this;
|
|
526
525
|
}
|
|
@@ -539,11 +538,11 @@ export class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTree<K, V[]
|
|
|
539
538
|
iter: Iterable<
|
|
540
539
|
TK | RedBlackTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR
|
|
541
540
|
> = [],
|
|
542
|
-
options?: Partial<
|
|
541
|
+
options?: Partial<TreeMultiMapOptions<TK, TV, TR>>
|
|
543
542
|
): RedBlackTree<TK, TV, TR> {
|
|
544
543
|
const Ctor = this.constructor as unknown as new (
|
|
545
544
|
iter?: Iterable<TK | RedBlackTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
546
|
-
opts?:
|
|
545
|
+
opts?: TreeMultiMapOptions<TK, TV, TR>
|
|
547
546
|
) => RedBlackTree<TK, TV, TR>;
|
|
548
547
|
return new Ctor(iter, { ...(this._snapshotOptions?.<TK, TV, TR>() ?? {}), ...(options ?? {}) });
|
|
549
548
|
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import type { BinaryTreeOptions } from './binary-tree';
|
|
2
|
-
import {
|
|
3
|
-
import { OptValue } from '../../common';
|
|
2
|
+
import type { Comparator, OptValue } from '../../common';
|
|
4
3
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
type BSTBaseOptions<K, V, R> = Omit<BinaryTreeOptions<K, V, R>, 'isDuplicate'>;
|
|
5
|
+
|
|
6
|
+
export type BSTOptions<K, V, R> = BSTBaseOptions<K, V, R> & {
|
|
7
|
+
comparator?: Comparator<K>;
|
|
8
8
|
}
|
|
9
9
|
|
|
10
10
|
export type BSTNOptKey<K> = K | undefined;
|