binary-tree-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 +38 -19
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +40 -21
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +38 -19
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +40 -21
- 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/binary-tree-typed.js +40 -21
- package/dist/umd/binary-tree-typed.js.map +1 -1
- package/dist/umd/binary-tree-typed.min.js +3 -3
- package/dist/umd/binary-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/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
|
@@ -1198,6 +1198,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1198
1198
|
__publicField(this, "iterationType", "ITERATIVE");
|
|
1199
1199
|
__publicField(this, "_isMapMode", true);
|
|
1200
1200
|
__publicField(this, "_isDuplicate", false);
|
|
1201
|
+
// Map mode acceleration store:
|
|
1202
|
+
// - isMapMode=false: unused
|
|
1203
|
+
// - isMapMode=true: key -> node reference (O(1) has/getNode + fast get)
|
|
1201
1204
|
__publicField(this, "_store", /* @__PURE__ */ new Map());
|
|
1202
1205
|
__publicField(this, "_root");
|
|
1203
1206
|
__publicField(this, "_size", 0);
|
|
@@ -1293,7 +1296,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1293
1296
|
* @returns The newly created node.
|
|
1294
1297
|
*/
|
|
1295
1298
|
createNode(key, value) {
|
|
1296
|
-
return new BinaryTreeNode(key,
|
|
1299
|
+
return new BinaryTreeNode(key, value);
|
|
1297
1300
|
}
|
|
1298
1301
|
/**
|
|
1299
1302
|
* Creates a new, empty tree of the same type and configuration.
|
|
@@ -1440,11 +1443,11 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1440
1443
|
* @returns True if the addition was successful, false otherwise.
|
|
1441
1444
|
*/
|
|
1442
1445
|
set(keyNodeOrEntry, value) {
|
|
1443
|
-
const [newNode
|
|
1446
|
+
const [newNode] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
1444
1447
|
if (newNode === void 0) return false;
|
|
1445
1448
|
if (!this._root) {
|
|
1446
1449
|
this._setRoot(newNode);
|
|
1447
|
-
if (this._isMapMode
|
|
1450
|
+
if (this._isMapMode && newNode !== null && newNode !== void 0) this._store.set(newNode.key, newNode);
|
|
1448
1451
|
this._size = 1;
|
|
1449
1452
|
return true;
|
|
1450
1453
|
}
|
|
@@ -1456,7 +1459,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1456
1459
|
if (!this._isDuplicate) {
|
|
1457
1460
|
if (newNode !== null && cur.key === newNode.key) {
|
|
1458
1461
|
this._replaceNode(cur, newNode);
|
|
1459
|
-
if (this._isMapMode) this.
|
|
1462
|
+
if (this._isMapMode && newNode !== null) this._store.set(cur.key, newNode);
|
|
1460
1463
|
return true;
|
|
1461
1464
|
}
|
|
1462
1465
|
}
|
|
@@ -1476,7 +1479,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1476
1479
|
} else if (potentialParent.right === void 0) {
|
|
1477
1480
|
potentialParent.right = newNode;
|
|
1478
1481
|
}
|
|
1479
|
-
if (this._isMapMode
|
|
1482
|
+
if (this._isMapMode && newNode !== null && newNode !== void 0) this._store.set(newNode.key, newNode);
|
|
1480
1483
|
this._size++;
|
|
1481
1484
|
return true;
|
|
1482
1485
|
}
|
|
@@ -1543,13 +1546,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1543
1546
|
* Deletes a node from the tree.
|
|
1544
1547
|
* @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).
|
|
1545
1548
|
*
|
|
1546
|
-
* @param
|
|
1549
|
+
* @param keyNodeEntryRawOrPredicate - The node to delete.
|
|
1547
1550
|
* @returns An array containing deletion results (for compatibility with self-balancing trees).
|
|
1548
1551
|
*/
|
|
1549
|
-
delete(
|
|
1552
|
+
delete(keyNodeEntryRawOrPredicate) {
|
|
1550
1553
|
const deletedResult = [];
|
|
1551
1554
|
if (!this._root) return deletedResult;
|
|
1552
|
-
const curr = this.getNode(
|
|
1555
|
+
const curr = this.getNode(keyNodeEntryRawOrPredicate);
|
|
1553
1556
|
if (!curr) return deletedResult;
|
|
1554
1557
|
const parent = curr == null ? void 0 : curr.parent;
|
|
1555
1558
|
let needBalanced;
|
|
@@ -1561,6 +1564,10 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1561
1564
|
if (leftSubTreeRightMost) {
|
|
1562
1565
|
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
1563
1566
|
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
|
|
1567
|
+
if (this._isMapMode) {
|
|
1568
|
+
this._store.set(curr.key, curr);
|
|
1569
|
+
this._store.set(leftSubTreeRightMost.key, leftSubTreeRightMost);
|
|
1570
|
+
}
|
|
1564
1571
|
if (parentOfLeftSubTreeMax) {
|
|
1565
1572
|
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
1566
1573
|
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
@@ -1644,6 +1651,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1644
1651
|
* @returns The first matching node, or undefined if not found.
|
|
1645
1652
|
*/
|
|
1646
1653
|
getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
1654
|
+
if (this._isMapMode && keyNodeEntryOrPredicate !== null && keyNodeEntryOrPredicate !== void 0) {
|
|
1655
|
+
if (!this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
1656
|
+
const key = this._extractKey(keyNodeEntryOrPredicate);
|
|
1657
|
+
if (key === null || key === void 0) return;
|
|
1658
|
+
return this._store.get(key);
|
|
1659
|
+
}
|
|
1660
|
+
}
|
|
1647
1661
|
return this.search(keyNodeEntryOrPredicate, true, (node) => node, startNode, iterationType)[0];
|
|
1648
1662
|
}
|
|
1649
1663
|
/**
|
|
@@ -1656,15 +1670,22 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1656
1670
|
* @returns The associated value, or undefined.
|
|
1657
1671
|
*/
|
|
1658
1672
|
get(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
1659
|
-
var _a;
|
|
1673
|
+
var _a, _b;
|
|
1660
1674
|
if (this._isMapMode) {
|
|
1661
1675
|
const key = this._extractKey(keyNodeEntryOrPredicate);
|
|
1662
1676
|
if (key === null || key === void 0) return;
|
|
1663
|
-
return this._store.get(key);
|
|
1677
|
+
return (_a = this._store.get(key)) == null ? void 0 : _a.value;
|
|
1664
1678
|
}
|
|
1665
|
-
return (
|
|
1679
|
+
return (_b = this.getNode(keyNodeEntryOrPredicate, startNode, iterationType)) == null ? void 0 : _b.value;
|
|
1666
1680
|
}
|
|
1667
1681
|
has(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
1682
|
+
if (this._isMapMode && keyNodeEntryOrPredicate !== void 0 && keyNodeEntryOrPredicate !== null) {
|
|
1683
|
+
if (!this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
1684
|
+
const key = this._extractKey(keyNodeEntryOrPredicate);
|
|
1685
|
+
if (key === null || key === void 0) return false;
|
|
1686
|
+
return this._store.has(key);
|
|
1687
|
+
}
|
|
1688
|
+
}
|
|
1668
1689
|
return this.search(keyNodeEntryOrPredicate, true, (node) => node, startNode, iterationType).length > 0;
|
|
1669
1690
|
}
|
|
1670
1691
|
/**
|
|
@@ -1733,7 +1754,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1733
1754
|
}
|
|
1734
1755
|
return true;
|
|
1735
1756
|
}, "checkBST");
|
|
1736
|
-
const isStandardBST = checkBST(
|
|
1757
|
+
const isStandardBST = checkBST();
|
|
1737
1758
|
const isInverseBST = checkBST(true);
|
|
1738
1759
|
return isStandardBST || isInverseBST;
|
|
1739
1760
|
}
|
|
@@ -2409,8 +2430,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2409
2430
|
}
|
|
2410
2431
|
current = stack.pop();
|
|
2411
2432
|
if (this.isRealNode(current)) {
|
|
2412
|
-
|
|
2413
|
-
else yield [current.key, current.value];
|
|
2433
|
+
yield [current.key, current.value];
|
|
2414
2434
|
current = current.right;
|
|
2415
2435
|
}
|
|
2416
2436
|
}
|
|
@@ -2418,8 +2438,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2418
2438
|
if (node.left && this.isRealNode(node)) {
|
|
2419
2439
|
yield* this[Symbol.iterator](node.left);
|
|
2420
2440
|
}
|
|
2421
|
-
|
|
2422
|
-
else yield [node.key, node.value];
|
|
2441
|
+
yield [node.key, node.value];
|
|
2423
2442
|
if (node.right && this.isRealNode(node)) {
|
|
2424
2443
|
yield* this[Symbol.iterator](node.right);
|
|
2425
2444
|
}
|
|
@@ -2497,8 +2516,7 @@ var _BinaryTree = class _BinaryTree 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 _BinaryTree 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 _BinaryTree 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.
|