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.
Files changed (55) hide show
  1. package/dist/cjs/index.cjs +38 -19
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +40 -21
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +38 -19
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +40 -21
  8. package/dist/esm-legacy/index.mjs.map +1 -1
  9. package/dist/types/data-structures/base/linear-base.d.ts +6 -6
  10. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +6 -6
  11. package/dist/types/data-structures/binary-tree/bst.d.ts +2 -1
  12. package/dist/types/data-structures/binary-tree/index.d.ts +3 -3
  13. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +150 -20
  14. package/dist/types/data-structures/binary-tree/tree-map.d.ts +188 -0
  15. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +238 -147
  16. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +270 -0
  17. package/dist/types/data-structures/binary-tree/tree-set.d.ts +181 -0
  18. package/dist/types/interfaces/binary-tree.d.ts +2 -2
  19. package/dist/types/types/data-structures/binary-tree/index.d.ts +3 -3
  20. package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +33 -0
  21. package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +16 -0
  22. package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +33 -0
  23. package/dist/umd/binary-tree-typed.js +40 -21
  24. package/dist/umd/binary-tree-typed.js.map +1 -1
  25. package/dist/umd/binary-tree-typed.min.js +3 -3
  26. package/dist/umd/binary-tree-typed.min.js.map +1 -1
  27. package/package.json +2 -2
  28. package/src/data-structures/base/linear-base.ts +2 -12
  29. package/src/data-structures/binary-tree/avl-tree.ts +1 -1
  30. package/src/data-structures/binary-tree/binary-tree.ts +45 -21
  31. package/src/data-structures/binary-tree/bst.ts +85 -10
  32. package/src/data-structures/binary-tree/index.ts +3 -3
  33. package/src/data-structures/binary-tree/red-black-tree.ts +568 -76
  34. package/src/data-structures/binary-tree/tree-map.ts +439 -0
  35. package/src/data-structures/binary-tree/tree-multi-map.ts +488 -325
  36. package/src/data-structures/binary-tree/tree-multi-set.ts +502 -0
  37. package/src/data-structures/binary-tree/tree-set.ts +407 -0
  38. package/src/data-structures/queue/deque.ts +10 -0
  39. package/src/interfaces/binary-tree.ts +2 -2
  40. package/src/types/data-structures/binary-tree/index.ts +3 -3
  41. package/src/types/data-structures/binary-tree/tree-map.ts +45 -0
  42. package/src/types/data-structures/binary-tree/tree-multi-set.ts +19 -0
  43. package/src/types/data-structures/binary-tree/tree-set.ts +39 -0
  44. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +0 -236
  45. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -197
  46. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +0 -243
  47. package/dist/types/types/data-structures/binary-tree/avl-tree-counter.d.ts +0 -2
  48. package/dist/types/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -2
  49. package/dist/types/types/data-structures/binary-tree/tree-counter.d.ts +0 -2
  50. package/src/data-structures/binary-tree/avl-tree-counter.ts +0 -539
  51. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +0 -438
  52. package/src/data-structures/binary-tree/tree-counter.ts +0 -575
  53. package/src/types/data-structures/binary-tree/avl-tree-counter.ts +0 -3
  54. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +0 -3
  55. package/src/types/data-structures/binary-tree/tree-counter.ts +0 -3
@@ -1231,6 +1231,9 @@ var BinaryTree = class extends IterableEntryBase {
1231
1231
  get isDuplicate() {
1232
1232
  return this._isDuplicate;
1233
1233
  }
1234
+ // Map mode acceleration store:
1235
+ // - isMapMode=false: unused
1236
+ // - isMapMode=true: key -> node reference (O(1) has/getNode + fast get)
1234
1237
  _store = /* @__PURE__ */ new Map();
1235
1238
  /**
1236
1239
  * Gets the external value store (used in Map mode).
@@ -1290,7 +1293,7 @@ var BinaryTree = class extends IterableEntryBase {
1290
1293
  * @returns The newly created node.
1291
1294
  */
1292
1295
  createNode(key, value) {
1293
- return new BinaryTreeNode(key, this._isMapMode ? void 0 : value);
1296
+ return new BinaryTreeNode(key, value);
1294
1297
  }
1295
1298
  /**
1296
1299
  * Creates a new, empty tree of the same type and configuration.
@@ -1437,11 +1440,11 @@ var BinaryTree = class extends IterableEntryBase {
1437
1440
  * @returns True if the addition was successful, false otherwise.
1438
1441
  */
1439
1442
  set(keyNodeOrEntry, value) {
1440
- const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
1443
+ const [newNode] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
1441
1444
  if (newNode === void 0) return false;
1442
1445
  if (!this._root) {
1443
1446
  this._setRoot(newNode);
1444
- if (this._isMapMode) this._setValue(newNode?.key, newValue);
1447
+ if (this._isMapMode && newNode !== null && newNode !== void 0) this._store.set(newNode.key, newNode);
1445
1448
  this._size = 1;
1446
1449
  return true;
1447
1450
  }
@@ -1453,7 +1456,7 @@ var BinaryTree = class extends IterableEntryBase {
1453
1456
  if (!this._isDuplicate) {
1454
1457
  if (newNode !== null && cur.key === newNode.key) {
1455
1458
  this._replaceNode(cur, newNode);
1456
- if (this._isMapMode) this._setValue(cur.key, newValue);
1459
+ if (this._isMapMode && newNode !== null) this._store.set(cur.key, newNode);
1457
1460
  return true;
1458
1461
  }
1459
1462
  }
@@ -1473,7 +1476,7 @@ var BinaryTree = class extends IterableEntryBase {
1473
1476
  } else if (potentialParent.right === void 0) {
1474
1477
  potentialParent.right = newNode;
1475
1478
  }
1476
- if (this._isMapMode) this._setValue(newNode?.key, newValue);
1479
+ if (this._isMapMode && newNode !== null && newNode !== void 0) this._store.set(newNode.key, newNode);
1477
1480
  this._size++;
1478
1481
  return true;
1479
1482
  }
@@ -1540,13 +1543,13 @@ var BinaryTree = class extends IterableEntryBase {
1540
1543
  * Deletes a node from the tree.
1541
1544
  * @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).
1542
1545
  *
1543
- * @param keyNodeOrEntry - The node to delete.
1546
+ * @param keyNodeEntryRawOrPredicate - The node to delete.
1544
1547
  * @returns An array containing deletion results (for compatibility with self-balancing trees).
1545
1548
  */
1546
- delete(keyNodeOrEntry) {
1549
+ delete(keyNodeEntryRawOrPredicate) {
1547
1550
  const deletedResult = [];
1548
1551
  if (!this._root) return deletedResult;
1549
- const curr = this.getNode(keyNodeOrEntry);
1552
+ const curr = this.getNode(keyNodeEntryRawOrPredicate);
1550
1553
  if (!curr) return deletedResult;
1551
1554
  const parent = curr?.parent;
1552
1555
  let needBalanced;
@@ -1558,6 +1561,10 @@ var BinaryTree = class extends IterableEntryBase {
1558
1561
  if (leftSubTreeRightMost) {
1559
1562
  const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
1560
1563
  orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
1564
+ if (this._isMapMode) {
1565
+ this._store.set(curr.key, curr);
1566
+ this._store.set(leftSubTreeRightMost.key, leftSubTreeRightMost);
1567
+ }
1561
1568
  if (parentOfLeftSubTreeMax) {
1562
1569
  if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
1563
1570
  parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
@@ -1641,6 +1648,13 @@ var BinaryTree = class extends IterableEntryBase {
1641
1648
  * @returns The first matching node, or undefined if not found.
1642
1649
  */
1643
1650
  getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
1651
+ if (this._isMapMode && keyNodeEntryOrPredicate !== null && keyNodeEntryOrPredicate !== void 0) {
1652
+ if (!this._isPredicate(keyNodeEntryOrPredicate)) {
1653
+ const key = this._extractKey(keyNodeEntryOrPredicate);
1654
+ if (key === null || key === void 0) return;
1655
+ return this._store.get(key);
1656
+ }
1657
+ }
1644
1658
  return this.search(keyNodeEntryOrPredicate, true, (node) => node, startNode, iterationType)[0];
1645
1659
  }
1646
1660
  /**
@@ -1656,11 +1670,18 @@ var BinaryTree = class extends IterableEntryBase {
1656
1670
  if (this._isMapMode) {
1657
1671
  const key = this._extractKey(keyNodeEntryOrPredicate);
1658
1672
  if (key === null || key === void 0) return;
1659
- return this._store.get(key);
1673
+ return this._store.get(key)?.value;
1660
1674
  }
1661
1675
  return this.getNode(keyNodeEntryOrPredicate, startNode, iterationType)?.value;
1662
1676
  }
1663
1677
  has(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
1678
+ if (this._isMapMode && keyNodeEntryOrPredicate !== void 0 && keyNodeEntryOrPredicate !== null) {
1679
+ if (!this._isPredicate(keyNodeEntryOrPredicate)) {
1680
+ const key = this._extractKey(keyNodeEntryOrPredicate);
1681
+ if (key === null || key === void 0) return false;
1682
+ return this._store.has(key);
1683
+ }
1684
+ }
1664
1685
  return this.search(keyNodeEntryOrPredicate, true, (node) => node, startNode, iterationType).length > 0;
1665
1686
  }
1666
1687
  /**
@@ -1729,7 +1750,7 @@ var BinaryTree = class extends IterableEntryBase {
1729
1750
  }
1730
1751
  return true;
1731
1752
  }, "checkBST");
1732
- const isStandardBST = checkBST(false);
1753
+ const isStandardBST = checkBST();
1733
1754
  const isInverseBST = checkBST(true);
1734
1755
  return isStandardBST || isInverseBST;
1735
1756
  }
@@ -2403,8 +2424,7 @@ var BinaryTree = class extends IterableEntryBase {
2403
2424
  }
2404
2425
  current = stack.pop();
2405
2426
  if (this.isRealNode(current)) {
2406
- if (this._isMapMode) yield [current.key, this._store.get(current.key)];
2407
- else yield [current.key, current.value];
2427
+ yield [current.key, current.value];
2408
2428
  current = current.right;
2409
2429
  }
2410
2430
  }
@@ -2412,8 +2432,7 @@ var BinaryTree = class extends IterableEntryBase {
2412
2432
  if (node.left && this.isRealNode(node)) {
2413
2433
  yield* this[Symbol.iterator](node.left);
2414
2434
  }
2415
- if (this._isMapMode) yield [node.key, this._store.get(node.key)];
2416
- else yield [node.key, node.value];
2435
+ yield [node.key, node.value];
2417
2436
  if (node.right && this.isRealNode(node)) {
2418
2437
  yield* this[Symbol.iterator](node.right);
2419
2438
  }
@@ -2499,8 +2518,7 @@ var BinaryTree = class extends IterableEntryBase {
2499
2518
  (node) => {
2500
2519
  if (node === null) cloned.set(null);
2501
2520
  else {
2502
- if (this._isMapMode) cloned.set([node.key, this._store.get(node.key)]);
2503
- else cloned.set([node.key, node.value]);
2521
+ cloned.set([node.key, node.value]);
2504
2522
  }
2505
2523
  },
2506
2524
  this._root,
@@ -2508,7 +2526,6 @@ var BinaryTree = class extends IterableEntryBase {
2508
2526
  true
2509
2527
  // Include nulls
2510
2528
  );
2511
- if (this._isMapMode) cloned._store = this._store;
2512
2529
  }
2513
2530
  /**
2514
2531
  * (Protected) Recursive helper for `toVisual`.
@@ -2671,8 +2688,10 @@ var BinaryTree = class extends IterableEntryBase {
2671
2688
  */
2672
2689
  _setValue(key, value) {
2673
2690
  if (key === null || key === void 0) return false;
2674
- if (value === void 0) return false;
2675
- return this._store.set(key, value);
2691
+ const node = this._store.get(key);
2692
+ if (!node) return false;
2693
+ node.value = value;
2694
+ return true;
2676
2695
  }
2677
2696
  /**
2678
2697
  * (Protected) Clears all nodes from the tree.