avl-tree-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 +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/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
@@ -1224,6 +1224,9 @@ var BinaryTree = class extends IterableEntryBase {
1224
1224
  get isDuplicate() {
1225
1225
  return this._isDuplicate;
1226
1226
  }
1227
+ // Map mode acceleration store:
1228
+ // - isMapMode=false: unused
1229
+ // - isMapMode=true: key -> node reference (O(1) has/getNode + fast get)
1227
1230
  _store = /* @__PURE__ */ new Map();
1228
1231
  /**
1229
1232
  * Gets the external value store (used in Map mode).
@@ -1283,7 +1286,7 @@ var BinaryTree = class extends IterableEntryBase {
1283
1286
  * @returns The newly created node.
1284
1287
  */
1285
1288
  createNode(key, value) {
1286
- return new BinaryTreeNode(key, this._isMapMode ? void 0 : value);
1289
+ return new BinaryTreeNode(key, value);
1287
1290
  }
1288
1291
  /**
1289
1292
  * Creates a new, empty tree of the same type and configuration.
@@ -1430,11 +1433,11 @@ var BinaryTree = class extends IterableEntryBase {
1430
1433
  * @returns True if the addition was successful, false otherwise.
1431
1434
  */
1432
1435
  set(keyNodeOrEntry, value) {
1433
- const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
1436
+ const [newNode] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
1434
1437
  if (newNode === void 0) return false;
1435
1438
  if (!this._root) {
1436
1439
  this._setRoot(newNode);
1437
- if (this._isMapMode) this._setValue(newNode?.key, newValue);
1440
+ if (this._isMapMode && newNode !== null && newNode !== void 0) this._store.set(newNode.key, newNode);
1438
1441
  this._size = 1;
1439
1442
  return true;
1440
1443
  }
@@ -1446,7 +1449,7 @@ var BinaryTree = class extends IterableEntryBase {
1446
1449
  if (!this._isDuplicate) {
1447
1450
  if (newNode !== null && cur.key === newNode.key) {
1448
1451
  this._replaceNode(cur, newNode);
1449
- if (this._isMapMode) this._setValue(cur.key, newValue);
1452
+ if (this._isMapMode && newNode !== null) this._store.set(cur.key, newNode);
1450
1453
  return true;
1451
1454
  }
1452
1455
  }
@@ -1466,7 +1469,7 @@ var BinaryTree = class extends IterableEntryBase {
1466
1469
  } else if (potentialParent.right === void 0) {
1467
1470
  potentialParent.right = newNode;
1468
1471
  }
1469
- if (this._isMapMode) this._setValue(newNode?.key, newValue);
1472
+ if (this._isMapMode && newNode !== null && newNode !== void 0) this._store.set(newNode.key, newNode);
1470
1473
  this._size++;
1471
1474
  return true;
1472
1475
  }
@@ -1533,13 +1536,13 @@ var BinaryTree = class extends IterableEntryBase {
1533
1536
  * Deletes a node from the tree.
1534
1537
  * @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).
1535
1538
  *
1536
- * @param keyNodeOrEntry - The node to delete.
1539
+ * @param keyNodeEntryRawOrPredicate - The node to delete.
1537
1540
  * @returns An array containing deletion results (for compatibility with self-balancing trees).
1538
1541
  */
1539
- delete(keyNodeOrEntry) {
1542
+ delete(keyNodeEntryRawOrPredicate) {
1540
1543
  const deletedResult = [];
1541
1544
  if (!this._root) return deletedResult;
1542
- const curr = this.getNode(keyNodeOrEntry);
1545
+ const curr = this.getNode(keyNodeEntryRawOrPredicate);
1543
1546
  if (!curr) return deletedResult;
1544
1547
  const parent = curr?.parent;
1545
1548
  let needBalanced;
@@ -1551,6 +1554,10 @@ var BinaryTree = class extends IterableEntryBase {
1551
1554
  if (leftSubTreeRightMost) {
1552
1555
  const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
1553
1556
  orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
1557
+ if (this._isMapMode) {
1558
+ this._store.set(curr.key, curr);
1559
+ this._store.set(leftSubTreeRightMost.key, leftSubTreeRightMost);
1560
+ }
1554
1561
  if (parentOfLeftSubTreeMax) {
1555
1562
  if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
1556
1563
  parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
@@ -1634,6 +1641,13 @@ var BinaryTree = class extends IterableEntryBase {
1634
1641
  * @returns The first matching node, or undefined if not found.
1635
1642
  */
1636
1643
  getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
1644
+ if (this._isMapMode && keyNodeEntryOrPredicate !== null && keyNodeEntryOrPredicate !== void 0) {
1645
+ if (!this._isPredicate(keyNodeEntryOrPredicate)) {
1646
+ const key = this._extractKey(keyNodeEntryOrPredicate);
1647
+ if (key === null || key === void 0) return;
1648
+ return this._store.get(key);
1649
+ }
1650
+ }
1637
1651
  return this.search(keyNodeEntryOrPredicate, true, (node) => node, startNode, iterationType)[0];
1638
1652
  }
1639
1653
  /**
@@ -1649,11 +1663,18 @@ var BinaryTree = class extends IterableEntryBase {
1649
1663
  if (this._isMapMode) {
1650
1664
  const key = this._extractKey(keyNodeEntryOrPredicate);
1651
1665
  if (key === null || key === void 0) return;
1652
- return this._store.get(key);
1666
+ return this._store.get(key)?.value;
1653
1667
  }
1654
1668
  return this.getNode(keyNodeEntryOrPredicate, startNode, iterationType)?.value;
1655
1669
  }
1656
1670
  has(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
1671
+ if (this._isMapMode && keyNodeEntryOrPredicate !== void 0 && keyNodeEntryOrPredicate !== null) {
1672
+ if (!this._isPredicate(keyNodeEntryOrPredicate)) {
1673
+ const key = this._extractKey(keyNodeEntryOrPredicate);
1674
+ if (key === null || key === void 0) return false;
1675
+ return this._store.has(key);
1676
+ }
1677
+ }
1657
1678
  return this.search(keyNodeEntryOrPredicate, true, (node) => node, startNode, iterationType).length > 0;
1658
1679
  }
1659
1680
  /**
@@ -1722,7 +1743,7 @@ var BinaryTree = class extends IterableEntryBase {
1722
1743
  }
1723
1744
  return true;
1724
1745
  }, "checkBST");
1725
- const isStandardBST = checkBST(false);
1746
+ const isStandardBST = checkBST();
1726
1747
  const isInverseBST = checkBST(true);
1727
1748
  return isStandardBST || isInverseBST;
1728
1749
  }
@@ -2396,8 +2417,7 @@ var BinaryTree = class extends IterableEntryBase {
2396
2417
  }
2397
2418
  current = stack.pop();
2398
2419
  if (this.isRealNode(current)) {
2399
- if (this._isMapMode) yield [current.key, this._store.get(current.key)];
2400
- else yield [current.key, current.value];
2420
+ yield [current.key, current.value];
2401
2421
  current = current.right;
2402
2422
  }
2403
2423
  }
@@ -2405,8 +2425,7 @@ var BinaryTree = class extends IterableEntryBase {
2405
2425
  if (node.left && this.isRealNode(node)) {
2406
2426
  yield* this[Symbol.iterator](node.left);
2407
2427
  }
2408
- if (this._isMapMode) yield [node.key, this._store.get(node.key)];
2409
- else yield [node.key, node.value];
2428
+ yield [node.key, node.value];
2410
2429
  if (node.right && this.isRealNode(node)) {
2411
2430
  yield* this[Symbol.iterator](node.right);
2412
2431
  }
@@ -2492,8 +2511,7 @@ var BinaryTree = class extends IterableEntryBase {
2492
2511
  (node) => {
2493
2512
  if (node === null) cloned.set(null);
2494
2513
  else {
2495
- if (this._isMapMode) cloned.set([node.key, this._store.get(node.key)]);
2496
- else cloned.set([node.key, node.value]);
2514
+ cloned.set([node.key, node.value]);
2497
2515
  }
2498
2516
  },
2499
2517
  this._root,
@@ -2501,7 +2519,6 @@ var BinaryTree = class extends IterableEntryBase {
2501
2519
  true
2502
2520
  // Include nulls
2503
2521
  );
2504
- if (this._isMapMode) cloned._store = this._store;
2505
2522
  }
2506
2523
  /**
2507
2524
  * (Protected) Recursive helper for `toVisual`.
@@ -2664,8 +2681,10 @@ var BinaryTree = class extends IterableEntryBase {
2664
2681
  */
2665
2682
  _setValue(key, value) {
2666
2683
  if (key === null || key === void 0) return false;
2667
- if (value === void 0) return false;
2668
- return this._store.set(key, value);
2684
+ const node = this._store.get(key);
2685
+ if (!node) return false;
2686
+ node.value = value;
2687
+ return true;
2669
2688
  }
2670
2689
  /**
2671
2690
  * (Protected) Clears all nodes from the tree.
@@ -2876,7 +2895,7 @@ var BST = class extends BinaryTree {
2876
2895
  * @returns The newly created BSTNode.
2877
2896
  */
2878
2897
  createNode(key, value) {
2879
- return new BSTNode(key, this._isMapMode ? void 0 : value);
2898
+ return new BSTNode(key, value);
2880
2899
  }
2881
2900
  /**
2882
2901
  * Ensures the input is a node. If it's a key or entry, it searches for the node.
@@ -2960,7 +2979,39 @@ var BST = class extends BinaryTree {
2960
2979
  * @returns The first matching node, or undefined if not found.
2961
2980
  */
2962
2981
  getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
2963
- return this.getNodes(keyNodeEntryOrPredicate, true, startNode, iterationType)[0] ?? void 0;
2982
+ if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) return void 0;
2983
+ if (this._isPredicate(keyNodeEntryOrPredicate)) {
2984
+ return this.getNodes(keyNodeEntryOrPredicate, true, startNode, iterationType)[0] ?? void 0;
2985
+ }
2986
+ if (keyNodeEntryOrPredicate instanceof Range) {
2987
+ return this.getNodes(
2988
+ keyNodeEntryOrPredicate,
2989
+ true,
2990
+ startNode,
2991
+ iterationType
2992
+ )[0] ?? void 0;
2993
+ }
2994
+ let targetKey;
2995
+ if (this.isNode(keyNodeEntryOrPredicate)) {
2996
+ targetKey = keyNodeEntryOrPredicate.key;
2997
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
2998
+ const k = keyNodeEntryOrPredicate[0];
2999
+ if (k === null || k === void 0) return void 0;
3000
+ targetKey = k;
3001
+ } else {
3002
+ targetKey = keyNodeEntryOrPredicate;
3003
+ }
3004
+ const start = this.ensureNode(startNode);
3005
+ if (!start) return void 0;
3006
+ const NIL = this._NIL;
3007
+ let cur = start;
3008
+ const cmpFn = this._comparator;
3009
+ while (cur && cur !== NIL) {
3010
+ const c = cmpFn(targetKey, cur.key);
3011
+ if (c === 0) return cur;
3012
+ cur = c < 0 ? cur._left : cur._right;
3013
+ }
3014
+ return void 0;
2964
3015
  }
2965
3016
  /**
2966
3017
  * Searches the tree for nodes matching a predicate, key, or range.
@@ -2981,8 +3032,30 @@ var BST = class extends BinaryTree {
2981
3032
  if (keyNodeEntryOrPredicate === null) return [];
2982
3033
  startNode = this.ensureNode(startNode);
2983
3034
  if (!startNode) return [];
2984
- let predicate;
2985
3035
  const isRange = this.isRange(keyNodeEntryOrPredicate);
3036
+ const isPred = !isRange && this._isPredicate(keyNodeEntryOrPredicate);
3037
+ if (!isRange && !isPred) {
3038
+ let targetKey;
3039
+ if (this.isNode(keyNodeEntryOrPredicate)) {
3040
+ targetKey = keyNodeEntryOrPredicate.key;
3041
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
3042
+ const k = keyNodeEntryOrPredicate[0];
3043
+ if (k !== null && k !== void 0) targetKey = k;
3044
+ } else {
3045
+ targetKey = keyNodeEntryOrPredicate;
3046
+ }
3047
+ if (targetKey === void 0) return [];
3048
+ const NIL = this._NIL;
3049
+ const cmpFn = this._comparator;
3050
+ let cur = startNode;
3051
+ while (cur && cur !== NIL) {
3052
+ const c = cmpFn(targetKey, cur.key);
3053
+ if (c === 0) return [callback(cur)];
3054
+ cur = c < 0 ? cur._left : cur._right;
3055
+ }
3056
+ return [];
3057
+ }
3058
+ let predicate;
2986
3059
  if (isRange) {
2987
3060
  predicate = /* @__PURE__ */ __name((node) => {
2988
3061
  if (!node) return false;
@@ -3061,11 +3134,11 @@ var BST = class extends BinaryTree {
3061
3134
  * @returns True if the addition was successful, false otherwise.
3062
3135
  */
3063
3136
  set(keyNodeOrEntry, value) {
3064
- const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
3137
+ const [newNode] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
3065
3138
  if (newNode === void 0) return false;
3066
3139
  if (this._root === void 0) {
3067
3140
  this._setRoot(newNode);
3068
- if (this._isMapMode) this._setValue(newNode?.key, newValue);
3141
+ if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
3069
3142
  this._size++;
3070
3143
  return true;
3071
3144
  }
@@ -3073,12 +3146,12 @@ var BST = class extends BinaryTree {
3073
3146
  while (current !== void 0) {
3074
3147
  if (this._compare(current.key, newNode.key) === 0) {
3075
3148
  this._replaceNode(current, newNode);
3076
- if (this._isMapMode) this._setValue(current.key, newValue);
3149
+ if (this._isMapMode && this.isRealNode(newNode)) this._store.set(current.key, newNode);
3077
3150
  return true;
3078
3151
  } else if (this._compare(current.key, newNode.key) > 0) {
3079
3152
  if (current.left === void 0) {
3080
3153
  current.left = newNode;
3081
- if (this._isMapMode) this._setValue(newNode?.key, newValue);
3154
+ if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
3082
3155
  this._size++;
3083
3156
  return true;
3084
3157
  }
@@ -3086,7 +3159,7 @@ var BST = class extends BinaryTree {
3086
3159
  } else {
3087
3160
  if (current.right === void 0) {
3088
3161
  current.right = newNode;
3089
- if (this._isMapMode) this._setValue(newNode?.key, newValue);
3162
+ if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
3090
3163
  this._size++;
3091
3164
  return true;
3092
3165
  }
@@ -3904,7 +3977,7 @@ var BST = class extends BinaryTree {
3904
3977
  succ.left = node.left;
3905
3978
  if (succ.left) succ.left.parent = succ;
3906
3979
  }
3907
- this._size = Math.max(0, (this._size ?? 0) - 1);
3980
+ this._size = Math.max(0, this._size - 1);
3908
3981
  return true;
3909
3982
  }
3910
3983
  };
@@ -4071,7 +4144,7 @@ var AVLTree = class extends BST {
4071
4144
  * @returns The newly created AVLTreeNode.
4072
4145
  */
4073
4146
  createNode(key, value) {
4074
- return new AVLTreeNode(key, this._isMapMode ? void 0 : value);
4147
+ return new AVLTreeNode(key, value);
4075
4148
  }
4076
4149
  /**
4077
4150
  * Checks if the given item is an `AVLTreeNode` instance.