queue-typed 2.3.0 → 2.4.1
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 +12 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +12 -0
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +12 -0
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +12 -0
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/linear-base.d.ts +6 -6
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +6 -6
- package/dist/types/data-structures/binary-tree/bst.d.ts +2 -1
- package/dist/types/data-structures/binary-tree/index.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +150 -20
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +188 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +238 -147
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +270 -0
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +181 -0
- package/dist/types/interfaces/binary-tree.d.ts +2 -2
- package/dist/types/types/data-structures/binary-tree/index.d.ts +3 -3
- package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +33 -0
- package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +16 -0
- package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +33 -0
- package/dist/umd/queue-typed.js +12 -0
- package/dist/umd/queue-typed.js.map +1 -1
- package/dist/umd/queue-typed.min.js +1 -1
- package/dist/umd/queue-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/base/linear-base.ts +2 -12
- package/src/data-structures/binary-tree/avl-tree.ts +1 -1
- package/src/data-structures/binary-tree/binary-tree.ts +45 -21
- package/src/data-structures/binary-tree/bst.ts +85 -10
- package/src/data-structures/binary-tree/index.ts +3 -3
- package/src/data-structures/binary-tree/red-black-tree.ts +568 -76
- package/src/data-structures/binary-tree/tree-map.ts +439 -0
- package/src/data-structures/binary-tree/tree-multi-map.ts +488 -325
- package/src/data-structures/binary-tree/tree-multi-set.ts +502 -0
- package/src/data-structures/binary-tree/tree-set.ts +407 -0
- package/src/data-structures/queue/deque.ts +10 -0
- package/src/interfaces/binary-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/index.ts +3 -3
- package/src/types/data-structures/binary-tree/tree-map.ts +45 -0
- package/src/types/data-structures/binary-tree/tree-multi-set.ts +19 -0
- package/src/types/data-structures/binary-tree/tree-set.ts +39 -0
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +0 -236
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -197
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +0 -243
- package/dist/types/types/data-structures/binary-tree/avl-tree-counter.d.ts +0 -2
- package/dist/types/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -2
- package/dist/types/types/data-structures/binary-tree/tree-counter.d.ts +0 -2
- package/src/data-structures/binary-tree/avl-tree-counter.ts +0 -539
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +0 -438
- package/src/data-structures/binary-tree/tree-counter.ts +0 -575
- package/src/types/data-structures/binary-tree/avl-tree-counter.ts +0 -3
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +0 -3
- package/src/types/data-structures/binary-tree/tree-counter.ts +0 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "queue-typed",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.1",
|
|
4
4
|
"description": "Queue data structure",
|
|
5
5
|
"browser": "dist/umd/queue-typed.min.js",
|
|
6
6
|
"umd:main": "dist/umd/queue-typed.min.js",
|
|
@@ -153,6 +153,6 @@
|
|
|
153
153
|
"typescript": "^4.9.5"
|
|
154
154
|
},
|
|
155
155
|
"dependencies": {
|
|
156
|
-
"data-structure-typed": "^2.
|
|
156
|
+
"data-structure-typed": "^2.4.1"
|
|
157
157
|
}
|
|
158
158
|
}
|
|
@@ -75,7 +75,7 @@ export abstract class LinearBase<
|
|
|
75
75
|
* @param options - `{ maxLen?, ... }` bounds/behavior options.
|
|
76
76
|
* @remarks Time O(1), Space O(1)
|
|
77
77
|
*/
|
|
78
|
-
|
|
78
|
+
constructor(options?: LinearBaseOptions<E, R>) {
|
|
79
79
|
super(options);
|
|
80
80
|
if (options) {
|
|
81
81
|
const { maxLen } = options;
|
|
@@ -156,14 +156,6 @@ export abstract class LinearBase<
|
|
|
156
156
|
return -1;
|
|
157
157
|
}
|
|
158
158
|
|
|
159
|
-
/**
|
|
160
|
-
* Concatenate multiple containers of the same species.
|
|
161
|
-
* @param items - Other lists to append.
|
|
162
|
-
* @returns New container with combined elements (`this` type).
|
|
163
|
-
* @remarks Time O(sum(length)), Space O(sum(length))
|
|
164
|
-
*/
|
|
165
|
-
concat(...items: this[]): this;
|
|
166
|
-
|
|
167
159
|
/**
|
|
168
160
|
* Concatenate elements and/or containers.
|
|
169
161
|
* @param items - Elements or other containers.
|
|
@@ -412,7 +404,7 @@ export abstract class LinearLinkedBase<
|
|
|
412
404
|
R = any,
|
|
413
405
|
NODE extends LinkedListNode<E> = LinkedListNode<E>
|
|
414
406
|
> extends LinearBase<E, R, NODE> {
|
|
415
|
-
|
|
407
|
+
constructor(options?: LinearBaseOptions<E, R>) {
|
|
416
408
|
super(options);
|
|
417
409
|
if (options) {
|
|
418
410
|
const { maxLen } = options;
|
|
@@ -478,8 +470,6 @@ export abstract class LinearLinkedBase<
|
|
|
478
470
|
* @returns New list with combined elements (`this` type).
|
|
479
471
|
* @remarks Time O(sum(length)), Space O(sum(length))
|
|
480
472
|
*/
|
|
481
|
-
override concat(...items: LinearBase<E, R>[]): this;
|
|
482
|
-
|
|
483
473
|
override concat(...items: (E | LinearBase<E, R>)[]): this {
|
|
484
474
|
const newList = this.clone();
|
|
485
475
|
|
|
@@ -383,7 +383,7 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
383
383
|
* @returns The newly created AVLTreeNode.
|
|
384
384
|
*/
|
|
385
385
|
override createNode(key: K, value?: V): AVLTreeNode<K, V> {
|
|
386
|
-
return new AVLTreeNode<K, V>(key,
|
|
386
|
+
return new AVLTreeNode<K, V>(key, value) as AVLTreeNode<K, V>;
|
|
387
387
|
}
|
|
388
388
|
|
|
389
389
|
/**
|
|
@@ -10,7 +10,7 @@ import type {
|
|
|
10
10
|
BinaryTreeDeleteResult,
|
|
11
11
|
BinaryTreeOptions,
|
|
12
12
|
BinaryTreePrintOptions,
|
|
13
|
-
BTNEntry,
|
|
13
|
+
BTNEntry, BTNRep,
|
|
14
14
|
DFSOrderPattern,
|
|
15
15
|
DFSStackItem,
|
|
16
16
|
EntryCallback,
|
|
@@ -401,7 +401,10 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
401
401
|
return this._isDuplicate;
|
|
402
402
|
}
|
|
403
403
|
|
|
404
|
-
|
|
404
|
+
// Map mode acceleration store:
|
|
405
|
+
// - isMapMode=false: unused
|
|
406
|
+
// - isMapMode=true: key -> node reference (O(1) has/getNode + fast get)
|
|
407
|
+
protected _store = new Map<K, BinaryTreeNode<K, V>>();
|
|
405
408
|
|
|
406
409
|
/**
|
|
407
410
|
* Gets the external value store (used in Map mode).
|
|
@@ -470,7 +473,7 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
470
473
|
* @returns The newly created node.
|
|
471
474
|
*/
|
|
472
475
|
createNode(key: K, value?: V): BinaryTreeNode<K, V> {
|
|
473
|
-
return new BinaryTreeNode<K, V>(key,
|
|
476
|
+
return new BinaryTreeNode<K, V>(key, value);
|
|
474
477
|
}
|
|
475
478
|
|
|
476
479
|
/**
|
|
@@ -661,12 +664,12 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
661
664
|
value?: V
|
|
662
665
|
): boolean {
|
|
663
666
|
|
|
664
|
-
const [newNode
|
|
667
|
+
const [newNode] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
665
668
|
if (newNode === undefined) return false;
|
|
666
669
|
|
|
667
670
|
if (!this._root) {
|
|
668
671
|
this._setRoot(newNode);
|
|
669
|
-
if (this._isMapMode) this.
|
|
672
|
+
if (this._isMapMode && newNode !== null && newNode !== undefined) this._store.set(newNode.key, newNode);
|
|
670
673
|
this._size = 1;
|
|
671
674
|
return true;
|
|
672
675
|
}
|
|
@@ -681,7 +684,7 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
681
684
|
if (!this._isDuplicate) {
|
|
682
685
|
if (newNode !== null && cur.key === newNode.key) {
|
|
683
686
|
this._replaceNode(cur, newNode);
|
|
684
|
-
if (this._isMapMode) this.
|
|
687
|
+
if (this._isMapMode && newNode !== null) this._store.set(cur.key, newNode);
|
|
685
688
|
return true; // Replaced existing node
|
|
686
689
|
}
|
|
687
690
|
}
|
|
@@ -704,7 +707,7 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
704
707
|
} else if (potentialParent.right === undefined) {
|
|
705
708
|
potentialParent.right = newNode;
|
|
706
709
|
}
|
|
707
|
-
if (this._isMapMode) this.
|
|
710
|
+
if (this._isMapMode && newNode !== null && newNode !== undefined) this._store.set(newNode.key, newNode);
|
|
708
711
|
this._size++;
|
|
709
712
|
return true;
|
|
710
713
|
}
|
|
@@ -795,16 +798,16 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
795
798
|
* Deletes a node from the tree.
|
|
796
799
|
* @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation finds the node, and if it has two children, swaps it with the rightmost node of its left subtree (in-order predecessor) before deleting. Time O(N) in the worst case. O(N) to find the node (`getNode`) and O(H) (which is O(N) worst-case) to find the rightmost node. Space O(1) (if `getNode` is iterative, which it is).
|
|
797
800
|
*
|
|
798
|
-
* @param
|
|
801
|
+
* @param keyNodeEntryRawOrPredicate - The node to delete.
|
|
799
802
|
* @returns An array containing deletion results (for compatibility with self-balancing trees).
|
|
800
803
|
*/
|
|
801
804
|
delete(
|
|
802
|
-
|
|
805
|
+
keyNodeEntryRawOrPredicate: BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V> | null>
|
|
803
806
|
): BinaryTreeDeleteResult<BinaryTreeNode<K, V>>[] {
|
|
804
807
|
const deletedResult: BinaryTreeDeleteResult<BinaryTreeNode<K, V>>[] = [];
|
|
805
808
|
if (!this._root) return deletedResult;
|
|
806
809
|
|
|
807
|
-
const curr = this.getNode(
|
|
810
|
+
const curr = this.getNode(keyNodeEntryRawOrPredicate);
|
|
808
811
|
if (!curr) return deletedResult;
|
|
809
812
|
|
|
810
813
|
const parent: BinaryTreeNode<K, V> | undefined = curr?.parent;
|
|
@@ -822,6 +825,13 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
822
825
|
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
823
826
|
// Swap properties
|
|
824
827
|
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
|
|
828
|
+
|
|
829
|
+
// Map mode store tracks key -> node reference; after swapping keys we must re-index both nodes.
|
|
830
|
+
if (this._isMapMode) {
|
|
831
|
+
this._store.set(curr.key, curr);
|
|
832
|
+
this._store.set(leftSubTreeRightMost.key, leftSubTreeRightMost);
|
|
833
|
+
}
|
|
834
|
+
|
|
825
835
|
// `orgCurrent` is now the node to be physically deleted (which was the rightmost)
|
|
826
836
|
if (parentOfLeftSubTreeMax) {
|
|
827
837
|
// Unlink the rightmost node
|
|
@@ -1002,6 +1012,13 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
1002
1012
|
startNode: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined = this._root,
|
|
1003
1013
|
iterationType: IterationType = this.iterationType
|
|
1004
1014
|
): BinaryTreeNode<K, V> | null | undefined {
|
|
1015
|
+
if (this._isMapMode && keyNodeEntryOrPredicate !== null && keyNodeEntryOrPredicate !== undefined) {
|
|
1016
|
+
if (!this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
1017
|
+
const key = this._extractKey(keyNodeEntryOrPredicate as any);
|
|
1018
|
+
if (key === null || key === undefined) return;
|
|
1019
|
+
return this._store.get(key);
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1005
1022
|
return this.search(keyNodeEntryOrPredicate, true, node => node, startNode, iterationType)[0];
|
|
1006
1023
|
}
|
|
1007
1024
|
|
|
@@ -1022,7 +1039,7 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
1022
1039
|
if (this._isMapMode) {
|
|
1023
1040
|
const key = this._extractKey(keyNodeEntryOrPredicate);
|
|
1024
1041
|
if (key === null || key === undefined) return;
|
|
1025
|
-
return this._store.get(key);
|
|
1042
|
+
return this._store.get(key)?.value;
|
|
1026
1043
|
}
|
|
1027
1044
|
return this.getNode(keyNodeEntryOrPredicate, startNode, iterationType)?.value;
|
|
1028
1045
|
}
|
|
@@ -1059,6 +1076,13 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
1059
1076
|
startNode: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined = this._root,
|
|
1060
1077
|
iterationType: IterationType = this.iterationType
|
|
1061
1078
|
): boolean {
|
|
1079
|
+
if (this._isMapMode && keyNodeEntryOrPredicate !== undefined && keyNodeEntryOrPredicate !== null) {
|
|
1080
|
+
if (!this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
1081
|
+
const key = this._extractKey(keyNodeEntryOrPredicate as any);
|
|
1082
|
+
if (key === null || key === undefined) return false;
|
|
1083
|
+
return this._store.has(key);
|
|
1084
|
+
}
|
|
1085
|
+
}
|
|
1062
1086
|
return this.search(keyNodeEntryOrPredicate, true, node => node, startNode, iterationType).length > 0;
|
|
1063
1087
|
}
|
|
1064
1088
|
|
|
@@ -1139,7 +1163,7 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
1139
1163
|
}
|
|
1140
1164
|
return true;
|
|
1141
1165
|
};
|
|
1142
|
-
const isStandardBST = checkBST(
|
|
1166
|
+
const isStandardBST = checkBST();
|
|
1143
1167
|
const isInverseBST = checkBST(true);
|
|
1144
1168
|
return isStandardBST || isInverseBST;
|
|
1145
1169
|
}
|
|
@@ -2086,8 +2110,7 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
2086
2110
|
current = stack.pop();
|
|
2087
2111
|
|
|
2088
2112
|
if (this.isRealNode(current)) {
|
|
2089
|
-
|
|
2090
|
-
else yield [current.key, current.value];
|
|
2113
|
+
yield [current.key, current.value];
|
|
2091
2114
|
// Move to the right subtree
|
|
2092
2115
|
current = current.right;
|
|
2093
2116
|
}
|
|
@@ -2098,8 +2121,7 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
2098
2121
|
yield* this[Symbol.iterator](node.left);
|
|
2099
2122
|
}
|
|
2100
2123
|
|
|
2101
|
-
|
|
2102
|
-
else yield [node.key, node.value];
|
|
2124
|
+
yield [node.key, node.value];
|
|
2103
2125
|
|
|
2104
2126
|
if (node.right && this.isRealNode(node)) {
|
|
2105
2127
|
yield* this[Symbol.iterator](node.right);
|
|
@@ -2212,15 +2234,14 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
2212
2234
|
node => {
|
|
2213
2235
|
if (node === null) cloned.set(null);
|
|
2214
2236
|
else {
|
|
2215
|
-
|
|
2216
|
-
else cloned.set([node.key, node.value]);
|
|
2237
|
+
cloned.set([node.key, node.value]);
|
|
2217
2238
|
}
|
|
2218
2239
|
},
|
|
2219
2240
|
this._root,
|
|
2220
2241
|
this.iterationType,
|
|
2221
2242
|
true // Include nulls
|
|
2222
2243
|
);
|
|
2223
|
-
|
|
2244
|
+
// Map mode: store is rebuilt via set() calls above.
|
|
2224
2245
|
}
|
|
2225
2246
|
|
|
2226
2247
|
/**
|
|
@@ -2468,8 +2489,11 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
2468
2489
|
*/
|
|
2469
2490
|
protected _setValue(key: K | null | undefined, value: V | undefined) {
|
|
2470
2491
|
if (key === null || key === undefined) return false;
|
|
2471
|
-
|
|
2472
|
-
|
|
2492
|
+
// Map mode stores node references; update the node value in-place.
|
|
2493
|
+
const node = this._store.get(key);
|
|
2494
|
+
if (!node) return false;
|
|
2495
|
+
node.value = value as V;
|
|
2496
|
+
return true;
|
|
2473
2497
|
}
|
|
2474
2498
|
|
|
2475
2499
|
/**
|
|
@@ -204,7 +204,8 @@ export class BSTNode<K = any, V = any> {
|
|
|
204
204
|
* // Create a simple BST with numeric keys
|
|
205
205
|
* const bst = new BST<number>([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
206
206
|
*
|
|
207
|
-
*
|
|
207
|
+
* // Keep the example output in source comments but avoid noisy test logs.
|
|
208
|
+
* await withMutedConsole(() => bst.print());
|
|
208
209
|
* // _______8__________
|
|
209
210
|
* // / \
|
|
210
211
|
* // ___4___ ____12_____
|
|
@@ -411,7 +412,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
411
412
|
* @returns The newly created BSTNode.
|
|
412
413
|
*/
|
|
413
414
|
override createNode(key: K, value?: V): BSTNode<K, V> {
|
|
414
|
-
return new BSTNode<K, V>(key,
|
|
415
|
+
return new BSTNode<K, V>(key, value);
|
|
415
416
|
}
|
|
416
417
|
|
|
417
418
|
/**
|
|
@@ -556,9 +557,55 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
556
557
|
startNode: BSTNOptKeyOrNode<K, BSTNode<K, V>> = this._root,
|
|
557
558
|
iterationType: IterationType = this.iterationType
|
|
558
559
|
): OptNode<BSTNode<K, V>> {
|
|
559
|
-
|
|
560
|
+
// Fast-path: key lookup should not allocate arrays or build predicate closures.
|
|
561
|
+
// (This is a hot path for get/has in Node Mode.)
|
|
562
|
+
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === undefined) return undefined;
|
|
563
|
+
|
|
564
|
+
// If a predicate is provided, defer to the full search logic.
|
|
565
|
+
if (this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
566
|
+
return this.getNodes(keyNodeEntryOrPredicate, true, startNode, iterationType)[0] ?? undefined;
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
// NOTE: Range<K> is not part of this overload, but callers may still pass it at runtime.
|
|
570
|
+
// Let search handle it.
|
|
571
|
+
if (keyNodeEntryOrPredicate instanceof Range) {
|
|
572
|
+
return (
|
|
573
|
+
this.getNodes(
|
|
574
|
+
keyNodeEntryOrPredicate,
|
|
575
|
+
true,
|
|
576
|
+
startNode as K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
|
|
577
|
+
iterationType
|
|
578
|
+
)[0] ?? undefined
|
|
579
|
+
);
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
let targetKey: K | undefined;
|
|
583
|
+
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
584
|
+
targetKey = keyNodeEntryOrPredicate.key;
|
|
585
|
+
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
586
|
+
const k = keyNodeEntryOrPredicate[0];
|
|
587
|
+
if (k === null || k === undefined) return undefined;
|
|
588
|
+
targetKey = k;
|
|
589
|
+
} else {
|
|
590
|
+
targetKey = keyNodeEntryOrPredicate;
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
const start = this.ensureNode(startNode);
|
|
594
|
+
if (!start) return undefined;
|
|
595
|
+
|
|
596
|
+
const NIL = this._NIL as unknown as BSTNode<K, V> | null | undefined;
|
|
597
|
+
let cur: BSTNode<K, V> | null | undefined = start;
|
|
598
|
+
const cmpFn = this._comparator;
|
|
599
|
+
while (cur && cur !== NIL) {
|
|
600
|
+
const c = cmpFn(targetKey, cur.key);
|
|
601
|
+
if (c === 0) return cur;
|
|
602
|
+
cur = c < 0 ? cur._left : cur._right;
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
return undefined;
|
|
560
606
|
}
|
|
561
607
|
|
|
608
|
+
|
|
562
609
|
override search(
|
|
563
610
|
keyNodeEntryOrPredicate:
|
|
564
611
|
| K
|
|
@@ -619,9 +666,37 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
619
666
|
startNode = this.ensureNode(startNode);
|
|
620
667
|
if (!startNode) return [];
|
|
621
668
|
|
|
622
|
-
|
|
669
|
+
// Fast-path: key lookup (unique keys) using a tight BST walk (no allocations).
|
|
670
|
+
// This is the hot path for get/has/search by key.
|
|
623
671
|
const isRange = this.isRange(keyNodeEntryOrPredicate);
|
|
672
|
+
const isPred = !isRange && this._isPredicate(keyNodeEntryOrPredicate);
|
|
673
|
+
|
|
674
|
+
if (!isRange && !isPred) {
|
|
675
|
+
let targetKey: K | undefined;
|
|
676
|
+
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
677
|
+
targetKey = keyNodeEntryOrPredicate.key;
|
|
678
|
+
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
679
|
+
const k = keyNodeEntryOrPredicate[0];
|
|
680
|
+
if (k !== null && k !== undefined) targetKey = k;
|
|
681
|
+
} else {
|
|
682
|
+
targetKey = keyNodeEntryOrPredicate;
|
|
683
|
+
}
|
|
684
|
+
if (targetKey === undefined) return [];
|
|
685
|
+
|
|
686
|
+
const NIL = this._NIL as unknown as BSTNode<K, V> | null | undefined;
|
|
687
|
+
const cmpFn = this._comparator;
|
|
688
|
+
let cur: BSTNode<K, V> | null | undefined = startNode;
|
|
689
|
+
|
|
690
|
+
// Loop intentionally avoids getters and extra type checks.
|
|
691
|
+
while (cur && cur !== NIL) {
|
|
692
|
+
const c = cmpFn(targetKey, cur.key);
|
|
693
|
+
if (c === 0) return [callback(cur)];
|
|
694
|
+
cur = c < 0 ? cur._left : cur._right;
|
|
695
|
+
}
|
|
696
|
+
return [];
|
|
697
|
+
}
|
|
624
698
|
|
|
699
|
+
let predicate: NodePredicate<BSTNode<K, V>>;
|
|
625
700
|
if (isRange) {
|
|
626
701
|
predicate = node => {
|
|
627
702
|
if (!node) return false;
|
|
@@ -724,12 +799,12 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
724
799
|
keyNodeOrEntry: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
|
|
725
800
|
value?: V
|
|
726
801
|
): boolean {
|
|
727
|
-
const [newNode
|
|
802
|
+
const [newNode] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
728
803
|
if (newNode === undefined) return false;
|
|
729
804
|
|
|
730
805
|
if (this._root === undefined) {
|
|
731
806
|
this._setRoot(newNode);
|
|
732
|
-
if (this._isMapMode) this.
|
|
807
|
+
if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
|
|
733
808
|
this._size++;
|
|
734
809
|
return true;
|
|
735
810
|
}
|
|
@@ -739,13 +814,13 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
739
814
|
if (this._compare(current.key, newNode.key) === 0) {
|
|
740
815
|
// Key exists, replace node
|
|
741
816
|
this._replaceNode(current, newNode);
|
|
742
|
-
if (this._isMapMode) this.
|
|
817
|
+
if (this._isMapMode && this.isRealNode(newNode)) this._store.set(current.key, newNode);
|
|
743
818
|
return true;
|
|
744
819
|
} else if (this._compare(current.key, newNode.key) > 0) {
|
|
745
820
|
// Go left
|
|
746
821
|
if (current.left === undefined) {
|
|
747
822
|
current.left = newNode;
|
|
748
|
-
if (this._isMapMode) this.
|
|
823
|
+
if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
|
|
749
824
|
this._size++;
|
|
750
825
|
return true;
|
|
751
826
|
}
|
|
@@ -754,7 +829,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
754
829
|
// Go right
|
|
755
830
|
if (current.right === undefined) {
|
|
756
831
|
current.right = newNode;
|
|
757
|
-
if (this._isMapMode) this.
|
|
832
|
+
if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
|
|
758
833
|
this._size++;
|
|
759
834
|
return true;
|
|
760
835
|
}
|
|
@@ -2018,7 +2093,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
2018
2093
|
if (succ.left) (succ.left as BSTNode<K, V>).parent = succ;
|
|
2019
2094
|
}
|
|
2020
2095
|
|
|
2021
|
-
this._size = Math.max(0,
|
|
2096
|
+
this._size = Math.max(0, this._size - 1);
|
|
2022
2097
|
return true;
|
|
2023
2098
|
}
|
|
2024
2099
|
}
|
|
@@ -4,7 +4,7 @@ export * from './binary-indexed-tree';
|
|
|
4
4
|
export * from './segment-tree';
|
|
5
5
|
export * from './avl-tree';
|
|
6
6
|
export * from './red-black-tree';
|
|
7
|
-
export * from './avl-tree-multi-map';
|
|
8
7
|
export * from './tree-multi-map';
|
|
9
|
-
export * from './tree-
|
|
10
|
-
export * from './
|
|
8
|
+
export * from './tree-set';
|
|
9
|
+
export * from './tree-map';
|
|
10
|
+
export * from './tree-multi-set';
|