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.
Files changed (56) hide show
  1. package/dist/cjs/index.cjs +101 -28
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +104 -32
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +101 -28
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +104 -32
  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/bst-typed.js +104 -32
  24. package/dist/umd/bst-typed.js.map +1 -1
  25. package/dist/umd/bst-typed.min.js +2 -2
  26. package/dist/umd/bst-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/data-structures/trie/trie.ts +6 -8
  40. package/src/interfaces/binary-tree.ts +2 -2
  41. package/src/types/data-structures/binary-tree/index.ts +3 -3
  42. package/src/types/data-structures/binary-tree/tree-map.ts +45 -0
  43. package/src/types/data-structures/binary-tree/tree-multi-set.ts +19 -0
  44. package/src/types/data-structures/binary-tree/tree-set.ts +39 -0
  45. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +0 -236
  46. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -197
  47. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +0 -243
  48. package/dist/types/types/data-structures/binary-tree/avl-tree-counter.d.ts +0 -2
  49. package/dist/types/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -2
  50. package/dist/types/types/data-structures/binary-tree/tree-counter.d.ts +0 -2
  51. package/src/data-structures/binary-tree/avl-tree-counter.ts +0 -539
  52. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +0 -438
  53. package/src/data-structures/binary-tree/tree-counter.ts +0 -575
  54. package/src/types/data-structures/binary-tree/avl-tree-counter.ts +0 -3
  55. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +0 -3
  56. 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, this._isMapMode ? void 0 : value);
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, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
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) this._setValue(newNode == null ? void 0 : newNode.key, newValue);
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._setValue(cur.key, newValue);
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) this._setValue(newNode == null ? void 0 : newNode.key, newValue);
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 keyNodeOrEntry - The node to delete.
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(keyNodeOrEntry) {
1552
+ delete(keyNodeEntryRawOrPredicate) {
1550
1553
  const deletedResult = [];
1551
1554
  if (!this._root) return deletedResult;
1552
- const curr = this.getNode(keyNodeOrEntry);
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 (_a = this.getNode(keyNodeEntryOrPredicate, startNode, iterationType)) == null ? void 0 : _a.value;
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(false);
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
- if (this._isMapMode) yield [current.key, this._store.get(current.key)];
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
- if (this._isMapMode) yield [node.key, this._store.get(node.key)];
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
- if (this._isMapMode) cloned.set([node.key, this._store.get(node.key)]);
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
- if (value === void 0) return false;
2673
- return this._store.set(key, value);
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.
@@ -2879,7 +2898,7 @@ var _BST = class _BST extends BinaryTree {
2879
2898
  * @returns The newly created BSTNode.
2880
2899
  */
2881
2900
  createNode(key, value) {
2882
- return new BSTNode(key, this._isMapMode ? void 0 : value);
2901
+ return new BSTNode(key, value);
2883
2902
  }
2884
2903
  /**
2885
2904
  * Ensures the input is a node. If it's a key or entry, it searches for the node.
@@ -2964,8 +2983,40 @@ var _BST = class _BST extends BinaryTree {
2964
2983
  * @returns The first matching node, or undefined if not found.
2965
2984
  */
2966
2985
  getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
2967
- var _a;
2968
- return (_a = this.getNodes(keyNodeEntryOrPredicate, true, startNode, iterationType)[0]) != null ? _a : void 0;
2986
+ var _a, _b;
2987
+ if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) return void 0;
2988
+ if (this._isPredicate(keyNodeEntryOrPredicate)) {
2989
+ return (_a = this.getNodes(keyNodeEntryOrPredicate, true, startNode, iterationType)[0]) != null ? _a : void 0;
2990
+ }
2991
+ if (keyNodeEntryOrPredicate instanceof Range) {
2992
+ return (_b = this.getNodes(
2993
+ keyNodeEntryOrPredicate,
2994
+ true,
2995
+ startNode,
2996
+ iterationType
2997
+ )[0]) != null ? _b : 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 _BST 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 _BST extends BinaryTree {
3066
3139
  * @returns True if the addition was successful, false otherwise.
3067
3140
  */
3068
3141
  set(keyNodeOrEntry, value) {
3069
- const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
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._setValue(newNode == null ? void 0 : newNode.key, newValue);
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 _BST 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._setValue(current.key, newValue);
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._setValue(newNode == null ? void 0 : newNode.key, newValue);
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 _BST extends BinaryTree {
3091
3164
  } else {
3092
3165
  if (current.right === void 0) {
3093
3166
  current.right = newNode;
3094
- if (this._isMapMode) this._setValue(newNode == null ? void 0 : newNode.key, newValue);
3167
+ if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
3095
3168
  this._size++;
3096
3169
  return true;
3097
3170
  }
@@ -3874,7 +3947,6 @@ var _BST = class _BST extends BinaryTree {
3874
3947
  * @returns True if the node was found and deleted, false otherwise.
3875
3948
  */
3876
3949
  _deleteByKey(key) {
3877
- var _a;
3878
3950
  let node = this._root;
3879
3951
  while (node) {
3880
3952
  const cmp = this._compare(node.key, key);
@@ -3913,7 +3985,7 @@ var _BST = class _BST extends BinaryTree {
3913
3985
  succ.left = node.left;
3914
3986
  if (succ.left) succ.left.parent = succ;
3915
3987
  }
3916
- this._size = Math.max(0, ((_a = this._size) != null ? _a : 0) - 1);
3988
+ this._size = Math.max(0, this._size - 1);
3917
3989
  return true;
3918
3990
  }
3919
3991
  };