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
@@ -1196,6 +1196,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1196
1196
  __publicField(this, "iterationType", "ITERATIVE");
1197
1197
  __publicField(this, "_isMapMode", true);
1198
1198
  __publicField(this, "_isDuplicate", false);
1199
+ // Map mode acceleration store:
1200
+ // - isMapMode=false: unused
1201
+ // - isMapMode=true: key -> node reference (O(1) has/getNode + fast get)
1199
1202
  __publicField(this, "_store", /* @__PURE__ */ new Map());
1200
1203
  __publicField(this, "_root");
1201
1204
  __publicField(this, "_size", 0);
@@ -1291,7 +1294,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1291
1294
  * @returns The newly created node.
1292
1295
  */
1293
1296
  createNode(key, value) {
1294
- return new BinaryTreeNode(key, this._isMapMode ? void 0 : value);
1297
+ return new BinaryTreeNode(key, value);
1295
1298
  }
1296
1299
  /**
1297
1300
  * Creates a new, empty tree of the same type and configuration.
@@ -1438,11 +1441,11 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1438
1441
  * @returns True if the addition was successful, false otherwise.
1439
1442
  */
1440
1443
  set(keyNodeOrEntry, value) {
1441
- const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
1444
+ const [newNode] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
1442
1445
  if (newNode === void 0) return false;
1443
1446
  if (!this._root) {
1444
1447
  this._setRoot(newNode);
1445
- if (this._isMapMode) this._setValue(newNode == null ? void 0 : newNode.key, newValue);
1448
+ if (this._isMapMode && newNode !== null && newNode !== void 0) this._store.set(newNode.key, newNode);
1446
1449
  this._size = 1;
1447
1450
  return true;
1448
1451
  }
@@ -1454,7 +1457,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1454
1457
  if (!this._isDuplicate) {
1455
1458
  if (newNode !== null && cur.key === newNode.key) {
1456
1459
  this._replaceNode(cur, newNode);
1457
- if (this._isMapMode) this._setValue(cur.key, newValue);
1460
+ if (this._isMapMode && newNode !== null) this._store.set(cur.key, newNode);
1458
1461
  return true;
1459
1462
  }
1460
1463
  }
@@ -1474,7 +1477,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1474
1477
  } else if (potentialParent.right === void 0) {
1475
1478
  potentialParent.right = newNode;
1476
1479
  }
1477
- if (this._isMapMode) this._setValue(newNode == null ? void 0 : newNode.key, newValue);
1480
+ if (this._isMapMode && newNode !== null && newNode !== void 0) this._store.set(newNode.key, newNode);
1478
1481
  this._size++;
1479
1482
  return true;
1480
1483
  }
@@ -1541,13 +1544,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1541
1544
  * Deletes a node from the tree.
1542
1545
  * @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).
1543
1546
  *
1544
- * @param keyNodeOrEntry - The node to delete.
1547
+ * @param keyNodeEntryRawOrPredicate - The node to delete.
1545
1548
  * @returns An array containing deletion results (for compatibility with self-balancing trees).
1546
1549
  */
1547
- delete(keyNodeOrEntry) {
1550
+ delete(keyNodeEntryRawOrPredicate) {
1548
1551
  const deletedResult = [];
1549
1552
  if (!this._root) return deletedResult;
1550
- const curr = this.getNode(keyNodeOrEntry);
1553
+ const curr = this.getNode(keyNodeEntryRawOrPredicate);
1551
1554
  if (!curr) return deletedResult;
1552
1555
  const parent = curr == null ? void 0 : curr.parent;
1553
1556
  let needBalanced;
@@ -1559,6 +1562,10 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1559
1562
  if (leftSubTreeRightMost) {
1560
1563
  const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
1561
1564
  orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
1565
+ if (this._isMapMode) {
1566
+ this._store.set(curr.key, curr);
1567
+ this._store.set(leftSubTreeRightMost.key, leftSubTreeRightMost);
1568
+ }
1562
1569
  if (parentOfLeftSubTreeMax) {
1563
1570
  if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
1564
1571
  parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
@@ -1642,6 +1649,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1642
1649
  * @returns The first matching node, or undefined if not found.
1643
1650
  */
1644
1651
  getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
1652
+ if (this._isMapMode && keyNodeEntryOrPredicate !== null && keyNodeEntryOrPredicate !== void 0) {
1653
+ if (!this._isPredicate(keyNodeEntryOrPredicate)) {
1654
+ const key = this._extractKey(keyNodeEntryOrPredicate);
1655
+ if (key === null || key === void 0) return;
1656
+ return this._store.get(key);
1657
+ }
1658
+ }
1645
1659
  return this.search(keyNodeEntryOrPredicate, true, (node) => node, startNode, iterationType)[0];
1646
1660
  }
1647
1661
  /**
@@ -1654,15 +1668,22 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1654
1668
  * @returns The associated value, or undefined.
1655
1669
  */
1656
1670
  get(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
1657
- var _a;
1671
+ var _a, _b;
1658
1672
  if (this._isMapMode) {
1659
1673
  const key = this._extractKey(keyNodeEntryOrPredicate);
1660
1674
  if (key === null || key === void 0) return;
1661
- return this._store.get(key);
1675
+ return (_a = this._store.get(key)) == null ? void 0 : _a.value;
1662
1676
  }
1663
- return (_a = this.getNode(keyNodeEntryOrPredicate, startNode, iterationType)) == null ? void 0 : _a.value;
1677
+ return (_b = this.getNode(keyNodeEntryOrPredicate, startNode, iterationType)) == null ? void 0 : _b.value;
1664
1678
  }
1665
1679
  has(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
1680
+ if (this._isMapMode && keyNodeEntryOrPredicate !== void 0 && keyNodeEntryOrPredicate !== null) {
1681
+ if (!this._isPredicate(keyNodeEntryOrPredicate)) {
1682
+ const key = this._extractKey(keyNodeEntryOrPredicate);
1683
+ if (key === null || key === void 0) return false;
1684
+ return this._store.has(key);
1685
+ }
1686
+ }
1666
1687
  return this.search(keyNodeEntryOrPredicate, true, (node) => node, startNode, iterationType).length > 0;
1667
1688
  }
1668
1689
  /**
@@ -1731,7 +1752,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1731
1752
  }
1732
1753
  return true;
1733
1754
  }, "checkBST");
1734
- const isStandardBST = checkBST(false);
1755
+ const isStandardBST = checkBST();
1735
1756
  const isInverseBST = checkBST(true);
1736
1757
  return isStandardBST || isInverseBST;
1737
1758
  }
@@ -2407,8 +2428,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2407
2428
  }
2408
2429
  current = stack.pop();
2409
2430
  if (this.isRealNode(current)) {
2410
- if (this._isMapMode) yield [current.key, this._store.get(current.key)];
2411
- else yield [current.key, current.value];
2431
+ yield [current.key, current.value];
2412
2432
  current = current.right;
2413
2433
  }
2414
2434
  }
@@ -2416,8 +2436,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2416
2436
  if (node.left && this.isRealNode(node)) {
2417
2437
  yield* this[Symbol.iterator](node.left);
2418
2438
  }
2419
- if (this._isMapMode) yield [node.key, this._store.get(node.key)];
2420
- else yield [node.key, node.value];
2439
+ yield [node.key, node.value];
2421
2440
  if (node.right && this.isRealNode(node)) {
2422
2441
  yield* this[Symbol.iterator](node.right);
2423
2442
  }
@@ -2495,8 +2514,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2495
2514
  (node) => {
2496
2515
  if (node === null) cloned.set(null);
2497
2516
  else {
2498
- if (this._isMapMode) cloned.set([node.key, this._store.get(node.key)]);
2499
- else cloned.set([node.key, node.value]);
2517
+ cloned.set([node.key, node.value]);
2500
2518
  }
2501
2519
  },
2502
2520
  this._root,
@@ -2504,7 +2522,6 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2504
2522
  true
2505
2523
  // Include nulls
2506
2524
  );
2507
- if (this._isMapMode) cloned._store = this._store;
2508
2525
  }
2509
2526
  /**
2510
2527
  * (Protected) Recursive helper for `toVisual`.
@@ -2667,8 +2684,10 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2667
2684
  */
2668
2685
  _setValue(key, value) {
2669
2686
  if (key === null || key === void 0) return false;
2670
- if (value === void 0) return false;
2671
- return this._store.set(key, value);
2687
+ const node = this._store.get(key);
2688
+ if (!node) return false;
2689
+ node.value = value;
2690
+ return true;
2672
2691
  }
2673
2692
  /**
2674
2693
  * (Protected) Clears all nodes from the tree.
@@ -2877,7 +2896,7 @@ var _BST = class _BST extends BinaryTree {
2877
2896
  * @returns The newly created BSTNode.
2878
2897
  */
2879
2898
  createNode(key, value) {
2880
- return new BSTNode(key, this._isMapMode ? void 0 : value);
2899
+ return new BSTNode(key, value);
2881
2900
  }
2882
2901
  /**
2883
2902
  * Ensures the input is a node. If it's a key or entry, it searches for the node.
@@ -2962,8 +2981,40 @@ var _BST = class _BST extends BinaryTree {
2962
2981
  * @returns The first matching node, or undefined if not found.
2963
2982
  */
2964
2983
  getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
2965
- var _a;
2966
- return (_a = this.getNodes(keyNodeEntryOrPredicate, true, startNode, iterationType)[0]) != null ? _a : void 0;
2984
+ var _a, _b;
2985
+ if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) return void 0;
2986
+ if (this._isPredicate(keyNodeEntryOrPredicate)) {
2987
+ return (_a = this.getNodes(keyNodeEntryOrPredicate, true, startNode, iterationType)[0]) != null ? _a : void 0;
2988
+ }
2989
+ if (keyNodeEntryOrPredicate instanceof Range) {
2990
+ return (_b = this.getNodes(
2991
+ keyNodeEntryOrPredicate,
2992
+ true,
2993
+ startNode,
2994
+ iterationType
2995
+ )[0]) != null ? _b : void 0;
2996
+ }
2997
+ let targetKey;
2998
+ if (this.isNode(keyNodeEntryOrPredicate)) {
2999
+ targetKey = keyNodeEntryOrPredicate.key;
3000
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
3001
+ const k = keyNodeEntryOrPredicate[0];
3002
+ if (k === null || k === void 0) return void 0;
3003
+ targetKey = k;
3004
+ } else {
3005
+ targetKey = keyNodeEntryOrPredicate;
3006
+ }
3007
+ const start = this.ensureNode(startNode);
3008
+ if (!start) return void 0;
3009
+ const NIL = this._NIL;
3010
+ let cur = start;
3011
+ const cmpFn = this._comparator;
3012
+ while (cur && cur !== NIL) {
3013
+ const c = cmpFn(targetKey, cur.key);
3014
+ if (c === 0) return cur;
3015
+ cur = c < 0 ? cur._left : cur._right;
3016
+ }
3017
+ return void 0;
2967
3018
  }
2968
3019
  /**
2969
3020
  * Searches the tree for nodes matching a predicate, key, or range.
@@ -2984,8 +3035,30 @@ var _BST = class _BST extends BinaryTree {
2984
3035
  if (keyNodeEntryOrPredicate === null) return [];
2985
3036
  startNode = this.ensureNode(startNode);
2986
3037
  if (!startNode) return [];
2987
- let predicate;
2988
3038
  const isRange = this.isRange(keyNodeEntryOrPredicate);
3039
+ const isPred = !isRange && this._isPredicate(keyNodeEntryOrPredicate);
3040
+ if (!isRange && !isPred) {
3041
+ let targetKey;
3042
+ if (this.isNode(keyNodeEntryOrPredicate)) {
3043
+ targetKey = keyNodeEntryOrPredicate.key;
3044
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
3045
+ const k = keyNodeEntryOrPredicate[0];
3046
+ if (k !== null && k !== void 0) targetKey = k;
3047
+ } else {
3048
+ targetKey = keyNodeEntryOrPredicate;
3049
+ }
3050
+ if (targetKey === void 0) return [];
3051
+ const NIL = this._NIL;
3052
+ const cmpFn = this._comparator;
3053
+ let cur = startNode;
3054
+ while (cur && cur !== NIL) {
3055
+ const c = cmpFn(targetKey, cur.key);
3056
+ if (c === 0) return [callback(cur)];
3057
+ cur = c < 0 ? cur._left : cur._right;
3058
+ }
3059
+ return [];
3060
+ }
3061
+ let predicate;
2989
3062
  if (isRange) {
2990
3063
  predicate = /* @__PURE__ */ __name((node) => {
2991
3064
  if (!node) return false;
@@ -3064,11 +3137,11 @@ var _BST = class _BST extends BinaryTree {
3064
3137
  * @returns True if the addition was successful, false otherwise.
3065
3138
  */
3066
3139
  set(keyNodeOrEntry, value) {
3067
- const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
3140
+ const [newNode] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
3068
3141
  if (newNode === void 0) return false;
3069
3142
  if (this._root === void 0) {
3070
3143
  this._setRoot(newNode);
3071
- if (this._isMapMode) this._setValue(newNode == null ? void 0 : newNode.key, newValue);
3144
+ if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
3072
3145
  this._size++;
3073
3146
  return true;
3074
3147
  }
@@ -3076,12 +3149,12 @@ var _BST = class _BST extends BinaryTree {
3076
3149
  while (current !== void 0) {
3077
3150
  if (this._compare(current.key, newNode.key) === 0) {
3078
3151
  this._replaceNode(current, newNode);
3079
- if (this._isMapMode) this._setValue(current.key, newValue);
3152
+ if (this._isMapMode && this.isRealNode(newNode)) this._store.set(current.key, newNode);
3080
3153
  return true;
3081
3154
  } else if (this._compare(current.key, newNode.key) > 0) {
3082
3155
  if (current.left === void 0) {
3083
3156
  current.left = newNode;
3084
- if (this._isMapMode) this._setValue(newNode == null ? void 0 : newNode.key, newValue);
3157
+ if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
3085
3158
  this._size++;
3086
3159
  return true;
3087
3160
  }
@@ -3089,7 +3162,7 @@ var _BST = class _BST extends BinaryTree {
3089
3162
  } else {
3090
3163
  if (current.right === void 0) {
3091
3164
  current.right = newNode;
3092
- if (this._isMapMode) this._setValue(newNode == null ? void 0 : newNode.key, newValue);
3165
+ if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
3093
3166
  this._size++;
3094
3167
  return true;
3095
3168
  }
@@ -3872,7 +3945,6 @@ var _BST = class _BST extends BinaryTree {
3872
3945
  * @returns True if the node was found and deleted, false otherwise.
3873
3946
  */
3874
3947
  _deleteByKey(key) {
3875
- var _a;
3876
3948
  let node = this._root;
3877
3949
  while (node) {
3878
3950
  const cmp = this._compare(node.key, key);
@@ -3911,7 +3983,7 @@ var _BST = class _BST extends BinaryTree {
3911
3983
  succ.left = node.left;
3912
3984
  if (succ.left) succ.left.parent = succ;
3913
3985
  }
3914
- this._size = Math.max(0, ((_a = this._size) != null ? _a : 0) - 1);
3986
+ this._size = Math.max(0, this._size - 1);
3915
3987
  return true;
3916
3988
  }
3917
3989
  };