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