bst-typed 2.4.0 → 2.4.2
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 +101 -28
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +104 -32
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +101 -28
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +104 -32
- 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/bst-typed.js +104 -32
- package/dist/umd/bst-typed.js.map +1 -1
- package/dist/umd/bst-typed.min.js +2 -2
- package/dist/umd/bst-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/data-structures/trie/trie.ts +6 -8
- 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/dist/esm/index.mjs
CHANGED
|
@@ -1229,6 +1229,9 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
1229
1229
|
get isDuplicate() {
|
|
1230
1230
|
return this._isDuplicate;
|
|
1231
1231
|
}
|
|
1232
|
+
// Map mode acceleration store:
|
|
1233
|
+
// - isMapMode=false: unused
|
|
1234
|
+
// - isMapMode=true: key -> node reference (O(1) has/getNode + fast get)
|
|
1232
1235
|
_store = /* @__PURE__ */ new Map();
|
|
1233
1236
|
/**
|
|
1234
1237
|
* Gets the external value store (used in Map mode).
|
|
@@ -1288,7 +1291,7 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
1288
1291
|
* @returns The newly created node.
|
|
1289
1292
|
*/
|
|
1290
1293
|
createNode(key, value) {
|
|
1291
|
-
return new BinaryTreeNode(key,
|
|
1294
|
+
return new BinaryTreeNode(key, value);
|
|
1292
1295
|
}
|
|
1293
1296
|
/**
|
|
1294
1297
|
* Creates a new, empty tree of the same type and configuration.
|
|
@@ -1435,11 +1438,11 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
1435
1438
|
* @returns True if the addition was successful, false otherwise.
|
|
1436
1439
|
*/
|
|
1437
1440
|
set(keyNodeOrEntry, value) {
|
|
1438
|
-
const [newNode
|
|
1441
|
+
const [newNode] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
1439
1442
|
if (newNode === void 0) return false;
|
|
1440
1443
|
if (!this._root) {
|
|
1441
1444
|
this._setRoot(newNode);
|
|
1442
|
-
if (this._isMapMode) this.
|
|
1445
|
+
if (this._isMapMode && newNode !== null && newNode !== void 0) this._store.set(newNode.key, newNode);
|
|
1443
1446
|
this._size = 1;
|
|
1444
1447
|
return true;
|
|
1445
1448
|
}
|
|
@@ -1451,7 +1454,7 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
1451
1454
|
if (!this._isDuplicate) {
|
|
1452
1455
|
if (newNode !== null && cur.key === newNode.key) {
|
|
1453
1456
|
this._replaceNode(cur, newNode);
|
|
1454
|
-
if (this._isMapMode) this.
|
|
1457
|
+
if (this._isMapMode && newNode !== null) this._store.set(cur.key, newNode);
|
|
1455
1458
|
return true;
|
|
1456
1459
|
}
|
|
1457
1460
|
}
|
|
@@ -1471,7 +1474,7 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
1471
1474
|
} else if (potentialParent.right === void 0) {
|
|
1472
1475
|
potentialParent.right = newNode;
|
|
1473
1476
|
}
|
|
1474
|
-
if (this._isMapMode) this.
|
|
1477
|
+
if (this._isMapMode && newNode !== null && newNode !== void 0) this._store.set(newNode.key, newNode);
|
|
1475
1478
|
this._size++;
|
|
1476
1479
|
return true;
|
|
1477
1480
|
}
|
|
@@ -1538,13 +1541,13 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
1538
1541
|
* Deletes a node from the tree.
|
|
1539
1542
|
* @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).
|
|
1540
1543
|
*
|
|
1541
|
-
* @param
|
|
1544
|
+
* @param keyNodeEntryRawOrPredicate - The node to delete.
|
|
1542
1545
|
* @returns An array containing deletion results (for compatibility with self-balancing trees).
|
|
1543
1546
|
*/
|
|
1544
|
-
delete(
|
|
1547
|
+
delete(keyNodeEntryRawOrPredicate) {
|
|
1545
1548
|
const deletedResult = [];
|
|
1546
1549
|
if (!this._root) return deletedResult;
|
|
1547
|
-
const curr = this.getNode(
|
|
1550
|
+
const curr = this.getNode(keyNodeEntryRawOrPredicate);
|
|
1548
1551
|
if (!curr) return deletedResult;
|
|
1549
1552
|
const parent = curr?.parent;
|
|
1550
1553
|
let needBalanced;
|
|
@@ -1556,6 +1559,10 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
1556
1559
|
if (leftSubTreeRightMost) {
|
|
1557
1560
|
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
1558
1561
|
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
|
|
1562
|
+
if (this._isMapMode) {
|
|
1563
|
+
this._store.set(curr.key, curr);
|
|
1564
|
+
this._store.set(leftSubTreeRightMost.key, leftSubTreeRightMost);
|
|
1565
|
+
}
|
|
1559
1566
|
if (parentOfLeftSubTreeMax) {
|
|
1560
1567
|
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
1561
1568
|
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
@@ -1639,6 +1646,13 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
1639
1646
|
* @returns The first matching node, or undefined if not found.
|
|
1640
1647
|
*/
|
|
1641
1648
|
getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
1649
|
+
if (this._isMapMode && keyNodeEntryOrPredicate !== null && keyNodeEntryOrPredicate !== void 0) {
|
|
1650
|
+
if (!this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
1651
|
+
const key = this._extractKey(keyNodeEntryOrPredicate);
|
|
1652
|
+
if (key === null || key === void 0) return;
|
|
1653
|
+
return this._store.get(key);
|
|
1654
|
+
}
|
|
1655
|
+
}
|
|
1642
1656
|
return this.search(keyNodeEntryOrPredicate, true, (node) => node, startNode, iterationType)[0];
|
|
1643
1657
|
}
|
|
1644
1658
|
/**
|
|
@@ -1654,11 +1668,18 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
1654
1668
|
if (this._isMapMode) {
|
|
1655
1669
|
const key = this._extractKey(keyNodeEntryOrPredicate);
|
|
1656
1670
|
if (key === null || key === void 0) return;
|
|
1657
|
-
return this._store.get(key);
|
|
1671
|
+
return this._store.get(key)?.value;
|
|
1658
1672
|
}
|
|
1659
1673
|
return this.getNode(keyNodeEntryOrPredicate, startNode, iterationType)?.value;
|
|
1660
1674
|
}
|
|
1661
1675
|
has(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
1676
|
+
if (this._isMapMode && keyNodeEntryOrPredicate !== void 0 && keyNodeEntryOrPredicate !== null) {
|
|
1677
|
+
if (!this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
1678
|
+
const key = this._extractKey(keyNodeEntryOrPredicate);
|
|
1679
|
+
if (key === null || key === void 0) return false;
|
|
1680
|
+
return this._store.has(key);
|
|
1681
|
+
}
|
|
1682
|
+
}
|
|
1662
1683
|
return this.search(keyNodeEntryOrPredicate, true, (node) => node, startNode, iterationType).length > 0;
|
|
1663
1684
|
}
|
|
1664
1685
|
/**
|
|
@@ -1727,7 +1748,7 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
1727
1748
|
}
|
|
1728
1749
|
return true;
|
|
1729
1750
|
}, "checkBST");
|
|
1730
|
-
const isStandardBST = checkBST(
|
|
1751
|
+
const isStandardBST = checkBST();
|
|
1731
1752
|
const isInverseBST = checkBST(true);
|
|
1732
1753
|
return isStandardBST || isInverseBST;
|
|
1733
1754
|
}
|
|
@@ -2401,8 +2422,7 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
2401
2422
|
}
|
|
2402
2423
|
current = stack.pop();
|
|
2403
2424
|
if (this.isRealNode(current)) {
|
|
2404
|
-
|
|
2405
|
-
else yield [current.key, current.value];
|
|
2425
|
+
yield [current.key, current.value];
|
|
2406
2426
|
current = current.right;
|
|
2407
2427
|
}
|
|
2408
2428
|
}
|
|
@@ -2410,8 +2430,7 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
2410
2430
|
if (node.left && this.isRealNode(node)) {
|
|
2411
2431
|
yield* this[Symbol.iterator](node.left);
|
|
2412
2432
|
}
|
|
2413
|
-
|
|
2414
|
-
else yield [node.key, node.value];
|
|
2433
|
+
yield [node.key, node.value];
|
|
2415
2434
|
if (node.right && this.isRealNode(node)) {
|
|
2416
2435
|
yield* this[Symbol.iterator](node.right);
|
|
2417
2436
|
}
|
|
@@ -2497,8 +2516,7 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
2497
2516
|
(node) => {
|
|
2498
2517
|
if (node === null) cloned.set(null);
|
|
2499
2518
|
else {
|
|
2500
|
-
|
|
2501
|
-
else cloned.set([node.key, node.value]);
|
|
2519
|
+
cloned.set([node.key, node.value]);
|
|
2502
2520
|
}
|
|
2503
2521
|
},
|
|
2504
2522
|
this._root,
|
|
@@ -2506,7 +2524,6 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
2506
2524
|
true
|
|
2507
2525
|
// Include nulls
|
|
2508
2526
|
);
|
|
2509
|
-
if (this._isMapMode) cloned._store = this._store;
|
|
2510
2527
|
}
|
|
2511
2528
|
/**
|
|
2512
2529
|
* (Protected) Recursive helper for `toVisual`.
|
|
@@ -2669,8 +2686,10 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
2669
2686
|
*/
|
|
2670
2687
|
_setValue(key, value) {
|
|
2671
2688
|
if (key === null || key === void 0) return false;
|
|
2672
|
-
|
|
2673
|
-
|
|
2689
|
+
const node = this._store.get(key);
|
|
2690
|
+
if (!node) return false;
|
|
2691
|
+
node.value = value;
|
|
2692
|
+
return true;
|
|
2674
2693
|
}
|
|
2675
2694
|
/**
|
|
2676
2695
|
* (Protected) Clears all nodes from the tree.
|
|
@@ -2881,7 +2900,7 @@ var BST = class extends BinaryTree {
|
|
|
2881
2900
|
* @returns The newly created BSTNode.
|
|
2882
2901
|
*/
|
|
2883
2902
|
createNode(key, value) {
|
|
2884
|
-
return new BSTNode(key,
|
|
2903
|
+
return new BSTNode(key, value);
|
|
2885
2904
|
}
|
|
2886
2905
|
/**
|
|
2887
2906
|
* Ensures the input is a node. If it's a key or entry, it searches for the node.
|
|
@@ -2965,7 +2984,39 @@ var BST = class extends BinaryTree {
|
|
|
2965
2984
|
* @returns The first matching node, or undefined if not found.
|
|
2966
2985
|
*/
|
|
2967
2986
|
getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
2968
|
-
|
|
2987
|
+
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) return void 0;
|
|
2988
|
+
if (this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
2989
|
+
return this.getNodes(keyNodeEntryOrPredicate, true, startNode, iterationType)[0] ?? void 0;
|
|
2990
|
+
}
|
|
2991
|
+
if (keyNodeEntryOrPredicate instanceof Range) {
|
|
2992
|
+
return this.getNodes(
|
|
2993
|
+
keyNodeEntryOrPredicate,
|
|
2994
|
+
true,
|
|
2995
|
+
startNode,
|
|
2996
|
+
iterationType
|
|
2997
|
+
)[0] ?? void 0;
|
|
2998
|
+
}
|
|
2999
|
+
let targetKey;
|
|
3000
|
+
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
3001
|
+
targetKey = keyNodeEntryOrPredicate.key;
|
|
3002
|
+
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
3003
|
+
const k = keyNodeEntryOrPredicate[0];
|
|
3004
|
+
if (k === null || k === void 0) return void 0;
|
|
3005
|
+
targetKey = k;
|
|
3006
|
+
} else {
|
|
3007
|
+
targetKey = keyNodeEntryOrPredicate;
|
|
3008
|
+
}
|
|
3009
|
+
const start = this.ensureNode(startNode);
|
|
3010
|
+
if (!start) return void 0;
|
|
3011
|
+
const NIL = this._NIL;
|
|
3012
|
+
let cur = start;
|
|
3013
|
+
const cmpFn = this._comparator;
|
|
3014
|
+
while (cur && cur !== NIL) {
|
|
3015
|
+
const c = cmpFn(targetKey, cur.key);
|
|
3016
|
+
if (c === 0) return cur;
|
|
3017
|
+
cur = c < 0 ? cur._left : cur._right;
|
|
3018
|
+
}
|
|
3019
|
+
return void 0;
|
|
2969
3020
|
}
|
|
2970
3021
|
/**
|
|
2971
3022
|
* Searches the tree for nodes matching a predicate, key, or range.
|
|
@@ -2986,8 +3037,30 @@ var BST = class extends BinaryTree {
|
|
|
2986
3037
|
if (keyNodeEntryOrPredicate === null) return [];
|
|
2987
3038
|
startNode = this.ensureNode(startNode);
|
|
2988
3039
|
if (!startNode) return [];
|
|
2989
|
-
let predicate;
|
|
2990
3040
|
const isRange = this.isRange(keyNodeEntryOrPredicate);
|
|
3041
|
+
const isPred = !isRange && this._isPredicate(keyNodeEntryOrPredicate);
|
|
3042
|
+
if (!isRange && !isPred) {
|
|
3043
|
+
let targetKey;
|
|
3044
|
+
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
3045
|
+
targetKey = keyNodeEntryOrPredicate.key;
|
|
3046
|
+
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
3047
|
+
const k = keyNodeEntryOrPredicate[0];
|
|
3048
|
+
if (k !== null && k !== void 0) targetKey = k;
|
|
3049
|
+
} else {
|
|
3050
|
+
targetKey = keyNodeEntryOrPredicate;
|
|
3051
|
+
}
|
|
3052
|
+
if (targetKey === void 0) return [];
|
|
3053
|
+
const NIL = this._NIL;
|
|
3054
|
+
const cmpFn = this._comparator;
|
|
3055
|
+
let cur = startNode;
|
|
3056
|
+
while (cur && cur !== NIL) {
|
|
3057
|
+
const c = cmpFn(targetKey, cur.key);
|
|
3058
|
+
if (c === 0) return [callback(cur)];
|
|
3059
|
+
cur = c < 0 ? cur._left : cur._right;
|
|
3060
|
+
}
|
|
3061
|
+
return [];
|
|
3062
|
+
}
|
|
3063
|
+
let predicate;
|
|
2991
3064
|
if (isRange) {
|
|
2992
3065
|
predicate = /* @__PURE__ */ __name((node) => {
|
|
2993
3066
|
if (!node) return false;
|
|
@@ -3066,11 +3139,11 @@ var BST = class extends BinaryTree {
|
|
|
3066
3139
|
* @returns True if the addition was successful, false otherwise.
|
|
3067
3140
|
*/
|
|
3068
3141
|
set(keyNodeOrEntry, value) {
|
|
3069
|
-
const [newNode
|
|
3142
|
+
const [newNode] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
3070
3143
|
if (newNode === void 0) return false;
|
|
3071
3144
|
if (this._root === void 0) {
|
|
3072
3145
|
this._setRoot(newNode);
|
|
3073
|
-
if (this._isMapMode) this.
|
|
3146
|
+
if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
|
|
3074
3147
|
this._size++;
|
|
3075
3148
|
return true;
|
|
3076
3149
|
}
|
|
@@ -3078,12 +3151,12 @@ var BST = class extends BinaryTree {
|
|
|
3078
3151
|
while (current !== void 0) {
|
|
3079
3152
|
if (this._compare(current.key, newNode.key) === 0) {
|
|
3080
3153
|
this._replaceNode(current, newNode);
|
|
3081
|
-
if (this._isMapMode) this.
|
|
3154
|
+
if (this._isMapMode && this.isRealNode(newNode)) this._store.set(current.key, newNode);
|
|
3082
3155
|
return true;
|
|
3083
3156
|
} else if (this._compare(current.key, newNode.key) > 0) {
|
|
3084
3157
|
if (current.left === void 0) {
|
|
3085
3158
|
current.left = newNode;
|
|
3086
|
-
if (this._isMapMode) this.
|
|
3159
|
+
if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
|
|
3087
3160
|
this._size++;
|
|
3088
3161
|
return true;
|
|
3089
3162
|
}
|
|
@@ -3091,7 +3164,7 @@ var BST = class extends BinaryTree {
|
|
|
3091
3164
|
} else {
|
|
3092
3165
|
if (current.right === void 0) {
|
|
3093
3166
|
current.right = newNode;
|
|
3094
|
-
if (this._isMapMode) this.
|
|
3167
|
+
if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
|
|
3095
3168
|
this._size++;
|
|
3096
3169
|
return true;
|
|
3097
3170
|
}
|
|
@@ -3909,7 +3982,7 @@ var BST = class extends BinaryTree {
|
|
|
3909
3982
|
succ.left = node.left;
|
|
3910
3983
|
if (succ.left) succ.left.parent = succ;
|
|
3911
3984
|
}
|
|
3912
|
-
this._size = Math.max(0,
|
|
3985
|
+
this._size = Math.max(0, this._size - 1);
|
|
3913
3986
|
return true;
|
|
3914
3987
|
}
|
|
3915
3988
|
};
|