avl-tree-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 +102 -29
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +105 -33
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +102 -29
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +105 -33
- 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/avl-tree-typed.js +105 -33
- package/dist/umd/avl-tree-typed.js.map +1 -1
- package/dist/umd/avl-tree-typed.min.js +2 -2
- package/dist/umd/avl-tree-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
|
@@ -1224,6 +1224,9 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
1224
1224
|
get isDuplicate() {
|
|
1225
1225
|
return this._isDuplicate;
|
|
1226
1226
|
}
|
|
1227
|
+
// Map mode acceleration store:
|
|
1228
|
+
// - isMapMode=false: unused
|
|
1229
|
+
// - isMapMode=true: key -> node reference (O(1) has/getNode + fast get)
|
|
1227
1230
|
_store = /* @__PURE__ */ new Map();
|
|
1228
1231
|
/**
|
|
1229
1232
|
* Gets the external value store (used in Map mode).
|
|
@@ -1283,7 +1286,7 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
1283
1286
|
* @returns The newly created node.
|
|
1284
1287
|
*/
|
|
1285
1288
|
createNode(key, value) {
|
|
1286
|
-
return new BinaryTreeNode(key,
|
|
1289
|
+
return new BinaryTreeNode(key, value);
|
|
1287
1290
|
}
|
|
1288
1291
|
/**
|
|
1289
1292
|
* Creates a new, empty tree of the same type and configuration.
|
|
@@ -1430,11 +1433,11 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
1430
1433
|
* @returns True if the addition was successful, false otherwise.
|
|
1431
1434
|
*/
|
|
1432
1435
|
set(keyNodeOrEntry, value) {
|
|
1433
|
-
const [newNode
|
|
1436
|
+
const [newNode] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
1434
1437
|
if (newNode === void 0) return false;
|
|
1435
1438
|
if (!this._root) {
|
|
1436
1439
|
this._setRoot(newNode);
|
|
1437
|
-
if (this._isMapMode) this.
|
|
1440
|
+
if (this._isMapMode && newNode !== null && newNode !== void 0) this._store.set(newNode.key, newNode);
|
|
1438
1441
|
this._size = 1;
|
|
1439
1442
|
return true;
|
|
1440
1443
|
}
|
|
@@ -1446,7 +1449,7 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
1446
1449
|
if (!this._isDuplicate) {
|
|
1447
1450
|
if (newNode !== null && cur.key === newNode.key) {
|
|
1448
1451
|
this._replaceNode(cur, newNode);
|
|
1449
|
-
if (this._isMapMode) this.
|
|
1452
|
+
if (this._isMapMode && newNode !== null) this._store.set(cur.key, newNode);
|
|
1450
1453
|
return true;
|
|
1451
1454
|
}
|
|
1452
1455
|
}
|
|
@@ -1466,7 +1469,7 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
1466
1469
|
} else if (potentialParent.right === void 0) {
|
|
1467
1470
|
potentialParent.right = newNode;
|
|
1468
1471
|
}
|
|
1469
|
-
if (this._isMapMode) this.
|
|
1472
|
+
if (this._isMapMode && newNode !== null && newNode !== void 0) this._store.set(newNode.key, newNode);
|
|
1470
1473
|
this._size++;
|
|
1471
1474
|
return true;
|
|
1472
1475
|
}
|
|
@@ -1533,13 +1536,13 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
1533
1536
|
* Deletes a node from the tree.
|
|
1534
1537
|
* @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).
|
|
1535
1538
|
*
|
|
1536
|
-
* @param
|
|
1539
|
+
* @param keyNodeEntryRawOrPredicate - The node to delete.
|
|
1537
1540
|
* @returns An array containing deletion results (for compatibility with self-balancing trees).
|
|
1538
1541
|
*/
|
|
1539
|
-
delete(
|
|
1542
|
+
delete(keyNodeEntryRawOrPredicate) {
|
|
1540
1543
|
const deletedResult = [];
|
|
1541
1544
|
if (!this._root) return deletedResult;
|
|
1542
|
-
const curr = this.getNode(
|
|
1545
|
+
const curr = this.getNode(keyNodeEntryRawOrPredicate);
|
|
1543
1546
|
if (!curr) return deletedResult;
|
|
1544
1547
|
const parent = curr?.parent;
|
|
1545
1548
|
let needBalanced;
|
|
@@ -1551,6 +1554,10 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
1551
1554
|
if (leftSubTreeRightMost) {
|
|
1552
1555
|
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
1553
1556
|
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
|
|
1557
|
+
if (this._isMapMode) {
|
|
1558
|
+
this._store.set(curr.key, curr);
|
|
1559
|
+
this._store.set(leftSubTreeRightMost.key, leftSubTreeRightMost);
|
|
1560
|
+
}
|
|
1554
1561
|
if (parentOfLeftSubTreeMax) {
|
|
1555
1562
|
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
1556
1563
|
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
@@ -1634,6 +1641,13 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
1634
1641
|
* @returns The first matching node, or undefined if not found.
|
|
1635
1642
|
*/
|
|
1636
1643
|
getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
1644
|
+
if (this._isMapMode && keyNodeEntryOrPredicate !== null && keyNodeEntryOrPredicate !== void 0) {
|
|
1645
|
+
if (!this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
1646
|
+
const key = this._extractKey(keyNodeEntryOrPredicate);
|
|
1647
|
+
if (key === null || key === void 0) return;
|
|
1648
|
+
return this._store.get(key);
|
|
1649
|
+
}
|
|
1650
|
+
}
|
|
1637
1651
|
return this.search(keyNodeEntryOrPredicate, true, (node) => node, startNode, iterationType)[0];
|
|
1638
1652
|
}
|
|
1639
1653
|
/**
|
|
@@ -1649,11 +1663,18 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
1649
1663
|
if (this._isMapMode) {
|
|
1650
1664
|
const key = this._extractKey(keyNodeEntryOrPredicate);
|
|
1651
1665
|
if (key === null || key === void 0) return;
|
|
1652
|
-
return this._store.get(key);
|
|
1666
|
+
return this._store.get(key)?.value;
|
|
1653
1667
|
}
|
|
1654
1668
|
return this.getNode(keyNodeEntryOrPredicate, startNode, iterationType)?.value;
|
|
1655
1669
|
}
|
|
1656
1670
|
has(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
1671
|
+
if (this._isMapMode && keyNodeEntryOrPredicate !== void 0 && keyNodeEntryOrPredicate !== null) {
|
|
1672
|
+
if (!this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
1673
|
+
const key = this._extractKey(keyNodeEntryOrPredicate);
|
|
1674
|
+
if (key === null || key === void 0) return false;
|
|
1675
|
+
return this._store.has(key);
|
|
1676
|
+
}
|
|
1677
|
+
}
|
|
1657
1678
|
return this.search(keyNodeEntryOrPredicate, true, (node) => node, startNode, iterationType).length > 0;
|
|
1658
1679
|
}
|
|
1659
1680
|
/**
|
|
@@ -1722,7 +1743,7 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
1722
1743
|
}
|
|
1723
1744
|
return true;
|
|
1724
1745
|
}, "checkBST");
|
|
1725
|
-
const isStandardBST = checkBST(
|
|
1746
|
+
const isStandardBST = checkBST();
|
|
1726
1747
|
const isInverseBST = checkBST(true);
|
|
1727
1748
|
return isStandardBST || isInverseBST;
|
|
1728
1749
|
}
|
|
@@ -2396,8 +2417,7 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
2396
2417
|
}
|
|
2397
2418
|
current = stack.pop();
|
|
2398
2419
|
if (this.isRealNode(current)) {
|
|
2399
|
-
|
|
2400
|
-
else yield [current.key, current.value];
|
|
2420
|
+
yield [current.key, current.value];
|
|
2401
2421
|
current = current.right;
|
|
2402
2422
|
}
|
|
2403
2423
|
}
|
|
@@ -2405,8 +2425,7 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
2405
2425
|
if (node.left && this.isRealNode(node)) {
|
|
2406
2426
|
yield* this[Symbol.iterator](node.left);
|
|
2407
2427
|
}
|
|
2408
|
-
|
|
2409
|
-
else yield [node.key, node.value];
|
|
2428
|
+
yield [node.key, node.value];
|
|
2410
2429
|
if (node.right && this.isRealNode(node)) {
|
|
2411
2430
|
yield* this[Symbol.iterator](node.right);
|
|
2412
2431
|
}
|
|
@@ -2492,8 +2511,7 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
2492
2511
|
(node) => {
|
|
2493
2512
|
if (node === null) cloned.set(null);
|
|
2494
2513
|
else {
|
|
2495
|
-
|
|
2496
|
-
else cloned.set([node.key, node.value]);
|
|
2514
|
+
cloned.set([node.key, node.value]);
|
|
2497
2515
|
}
|
|
2498
2516
|
},
|
|
2499
2517
|
this._root,
|
|
@@ -2501,7 +2519,6 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
2501
2519
|
true
|
|
2502
2520
|
// Include nulls
|
|
2503
2521
|
);
|
|
2504
|
-
if (this._isMapMode) cloned._store = this._store;
|
|
2505
2522
|
}
|
|
2506
2523
|
/**
|
|
2507
2524
|
* (Protected) Recursive helper for `toVisual`.
|
|
@@ -2664,8 +2681,10 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
2664
2681
|
*/
|
|
2665
2682
|
_setValue(key, value) {
|
|
2666
2683
|
if (key === null || key === void 0) return false;
|
|
2667
|
-
|
|
2668
|
-
|
|
2684
|
+
const node = this._store.get(key);
|
|
2685
|
+
if (!node) return false;
|
|
2686
|
+
node.value = value;
|
|
2687
|
+
return true;
|
|
2669
2688
|
}
|
|
2670
2689
|
/**
|
|
2671
2690
|
* (Protected) Clears all nodes from the tree.
|
|
@@ -2876,7 +2895,7 @@ var BST = class extends BinaryTree {
|
|
|
2876
2895
|
* @returns The newly created BSTNode.
|
|
2877
2896
|
*/
|
|
2878
2897
|
createNode(key, value) {
|
|
2879
|
-
return new BSTNode(key,
|
|
2898
|
+
return new BSTNode(key, value);
|
|
2880
2899
|
}
|
|
2881
2900
|
/**
|
|
2882
2901
|
* Ensures the input is a node. If it's a key or entry, it searches for the node.
|
|
@@ -2960,7 +2979,39 @@ var BST = class extends BinaryTree {
|
|
|
2960
2979
|
* @returns The first matching node, or undefined if not found.
|
|
2961
2980
|
*/
|
|
2962
2981
|
getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
2963
|
-
|
|
2982
|
+
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) return void 0;
|
|
2983
|
+
if (this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
2984
|
+
return this.getNodes(keyNodeEntryOrPredicate, true, startNode, iterationType)[0] ?? void 0;
|
|
2985
|
+
}
|
|
2986
|
+
if (keyNodeEntryOrPredicate instanceof Range) {
|
|
2987
|
+
return this.getNodes(
|
|
2988
|
+
keyNodeEntryOrPredicate,
|
|
2989
|
+
true,
|
|
2990
|
+
startNode,
|
|
2991
|
+
iterationType
|
|
2992
|
+
)[0] ?? void 0;
|
|
2993
|
+
}
|
|
2994
|
+
let targetKey;
|
|
2995
|
+
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
2996
|
+
targetKey = keyNodeEntryOrPredicate.key;
|
|
2997
|
+
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
2998
|
+
const k = keyNodeEntryOrPredicate[0];
|
|
2999
|
+
if (k === null || k === void 0) return void 0;
|
|
3000
|
+
targetKey = k;
|
|
3001
|
+
} else {
|
|
3002
|
+
targetKey = keyNodeEntryOrPredicate;
|
|
3003
|
+
}
|
|
3004
|
+
const start = this.ensureNode(startNode);
|
|
3005
|
+
if (!start) return void 0;
|
|
3006
|
+
const NIL = this._NIL;
|
|
3007
|
+
let cur = start;
|
|
3008
|
+
const cmpFn = this._comparator;
|
|
3009
|
+
while (cur && cur !== NIL) {
|
|
3010
|
+
const c = cmpFn(targetKey, cur.key);
|
|
3011
|
+
if (c === 0) return cur;
|
|
3012
|
+
cur = c < 0 ? cur._left : cur._right;
|
|
3013
|
+
}
|
|
3014
|
+
return void 0;
|
|
2964
3015
|
}
|
|
2965
3016
|
/**
|
|
2966
3017
|
* Searches the tree for nodes matching a predicate, key, or range.
|
|
@@ -2981,8 +3032,30 @@ var BST = class extends BinaryTree {
|
|
|
2981
3032
|
if (keyNodeEntryOrPredicate === null) return [];
|
|
2982
3033
|
startNode = this.ensureNode(startNode);
|
|
2983
3034
|
if (!startNode) return [];
|
|
2984
|
-
let predicate;
|
|
2985
3035
|
const isRange = this.isRange(keyNodeEntryOrPredicate);
|
|
3036
|
+
const isPred = !isRange && this._isPredicate(keyNodeEntryOrPredicate);
|
|
3037
|
+
if (!isRange && !isPred) {
|
|
3038
|
+
let targetKey;
|
|
3039
|
+
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
3040
|
+
targetKey = keyNodeEntryOrPredicate.key;
|
|
3041
|
+
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
3042
|
+
const k = keyNodeEntryOrPredicate[0];
|
|
3043
|
+
if (k !== null && k !== void 0) targetKey = k;
|
|
3044
|
+
} else {
|
|
3045
|
+
targetKey = keyNodeEntryOrPredicate;
|
|
3046
|
+
}
|
|
3047
|
+
if (targetKey === void 0) return [];
|
|
3048
|
+
const NIL = this._NIL;
|
|
3049
|
+
const cmpFn = this._comparator;
|
|
3050
|
+
let cur = startNode;
|
|
3051
|
+
while (cur && cur !== NIL) {
|
|
3052
|
+
const c = cmpFn(targetKey, cur.key);
|
|
3053
|
+
if (c === 0) return [callback(cur)];
|
|
3054
|
+
cur = c < 0 ? cur._left : cur._right;
|
|
3055
|
+
}
|
|
3056
|
+
return [];
|
|
3057
|
+
}
|
|
3058
|
+
let predicate;
|
|
2986
3059
|
if (isRange) {
|
|
2987
3060
|
predicate = /* @__PURE__ */ __name((node) => {
|
|
2988
3061
|
if (!node) return false;
|
|
@@ -3061,11 +3134,11 @@ var BST = class extends BinaryTree {
|
|
|
3061
3134
|
* @returns True if the addition was successful, false otherwise.
|
|
3062
3135
|
*/
|
|
3063
3136
|
set(keyNodeOrEntry, value) {
|
|
3064
|
-
const [newNode
|
|
3137
|
+
const [newNode] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
3065
3138
|
if (newNode === void 0) return false;
|
|
3066
3139
|
if (this._root === void 0) {
|
|
3067
3140
|
this._setRoot(newNode);
|
|
3068
|
-
if (this._isMapMode) this.
|
|
3141
|
+
if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
|
|
3069
3142
|
this._size++;
|
|
3070
3143
|
return true;
|
|
3071
3144
|
}
|
|
@@ -3073,12 +3146,12 @@ var BST = class extends BinaryTree {
|
|
|
3073
3146
|
while (current !== void 0) {
|
|
3074
3147
|
if (this._compare(current.key, newNode.key) === 0) {
|
|
3075
3148
|
this._replaceNode(current, newNode);
|
|
3076
|
-
if (this._isMapMode) this.
|
|
3149
|
+
if (this._isMapMode && this.isRealNode(newNode)) this._store.set(current.key, newNode);
|
|
3077
3150
|
return true;
|
|
3078
3151
|
} else if (this._compare(current.key, newNode.key) > 0) {
|
|
3079
3152
|
if (current.left === void 0) {
|
|
3080
3153
|
current.left = newNode;
|
|
3081
|
-
if (this._isMapMode) this.
|
|
3154
|
+
if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
|
|
3082
3155
|
this._size++;
|
|
3083
3156
|
return true;
|
|
3084
3157
|
}
|
|
@@ -3086,7 +3159,7 @@ var BST = class extends BinaryTree {
|
|
|
3086
3159
|
} else {
|
|
3087
3160
|
if (current.right === void 0) {
|
|
3088
3161
|
current.right = newNode;
|
|
3089
|
-
if (this._isMapMode) this.
|
|
3162
|
+
if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
|
|
3090
3163
|
this._size++;
|
|
3091
3164
|
return true;
|
|
3092
3165
|
}
|
|
@@ -3904,7 +3977,7 @@ var BST = class extends BinaryTree {
|
|
|
3904
3977
|
succ.left = node.left;
|
|
3905
3978
|
if (succ.left) succ.left.parent = succ;
|
|
3906
3979
|
}
|
|
3907
|
-
this._size = Math.max(0,
|
|
3980
|
+
this._size = Math.max(0, this._size - 1);
|
|
3908
3981
|
return true;
|
|
3909
3982
|
}
|
|
3910
3983
|
};
|
|
@@ -4071,7 +4144,7 @@ var AVLTree = class extends BST {
|
|
|
4071
4144
|
* @returns The newly created AVLTreeNode.
|
|
4072
4145
|
*/
|
|
4073
4146
|
createNode(key, value) {
|
|
4074
|
-
return new AVLTreeNode(key,
|
|
4147
|
+
return new AVLTreeNode(key, value);
|
|
4075
4148
|
}
|
|
4076
4149
|
/**
|
|
4077
4150
|
* Checks if the given item is an `AVLTreeNode` instance.
|