max-priority-queue-typed 2.4.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/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/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
|
@@ -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';
|