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
@@ -1191,6 +1191,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1191
1191
  __publicField(this, "iterationType", "ITERATIVE");
1192
1192
  __publicField(this, "_isMapMode", true);
1193
1193
  __publicField(this, "_isDuplicate", false);
1194
+ // Map mode acceleration store:
1195
+ // - isMapMode=false: unused
1196
+ // - isMapMode=true: key -> node reference (O(1) has/getNode + fast get)
1194
1197
  __publicField(this, "_store", /* @__PURE__ */ new Map());
1195
1198
  __publicField(this, "_root");
1196
1199
  __publicField(this, "_size", 0);
@@ -1286,7 +1289,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1286
1289
  * @returns The newly created node.
1287
1290
  */
1288
1291
  createNode(key, value) {
1289
- return new BinaryTreeNode(key, this._isMapMode ? void 0 : value);
1292
+ return new BinaryTreeNode(key, value);
1290
1293
  }
1291
1294
  /**
1292
1295
  * Creates a new, empty tree of the same type and configuration.
@@ -1433,11 +1436,11 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1433
1436
  * @returns True if the addition was successful, false otherwise.
1434
1437
  */
1435
1438
  set(keyNodeOrEntry, value) {
1436
- const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
1439
+ const [newNode] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
1437
1440
  if (newNode === void 0) return false;
1438
1441
  if (!this._root) {
1439
1442
  this._setRoot(newNode);
1440
- if (this._isMapMode) this._setValue(newNode == null ? void 0 : newNode.key, newValue);
1443
+ if (this._isMapMode && newNode !== null && newNode !== void 0) this._store.set(newNode.key, newNode);
1441
1444
  this._size = 1;
1442
1445
  return true;
1443
1446
  }
@@ -1449,7 +1452,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1449
1452
  if (!this._isDuplicate) {
1450
1453
  if (newNode !== null && cur.key === newNode.key) {
1451
1454
  this._replaceNode(cur, newNode);
1452
- if (this._isMapMode) this._setValue(cur.key, newValue);
1455
+ if (this._isMapMode && newNode !== null) this._store.set(cur.key, newNode);
1453
1456
  return true;
1454
1457
  }
1455
1458
  }
@@ -1469,7 +1472,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1469
1472
  } else if (potentialParent.right === void 0) {
1470
1473
  potentialParent.right = newNode;
1471
1474
  }
1472
- if (this._isMapMode) this._setValue(newNode == null ? void 0 : newNode.key, newValue);
1475
+ if (this._isMapMode && newNode !== null && newNode !== void 0) this._store.set(newNode.key, newNode);
1473
1476
  this._size++;
1474
1477
  return true;
1475
1478
  }
@@ -1536,13 +1539,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1536
1539
  * Deletes a node from the tree.
1537
1540
  * @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).
1538
1541
  *
1539
- * @param keyNodeOrEntry - The node to delete.
1542
+ * @param keyNodeEntryRawOrPredicate - The node to delete.
1540
1543
  * @returns An array containing deletion results (for compatibility with self-balancing trees).
1541
1544
  */
1542
- delete(keyNodeOrEntry) {
1545
+ delete(keyNodeEntryRawOrPredicate) {
1543
1546
  const deletedResult = [];
1544
1547
  if (!this._root) return deletedResult;
1545
- const curr = this.getNode(keyNodeOrEntry);
1548
+ const curr = this.getNode(keyNodeEntryRawOrPredicate);
1546
1549
  if (!curr) return deletedResult;
1547
1550
  const parent = curr == null ? void 0 : curr.parent;
1548
1551
  let needBalanced;
@@ -1554,6 +1557,10 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1554
1557
  if (leftSubTreeRightMost) {
1555
1558
  const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
1556
1559
  orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
1560
+ if (this._isMapMode) {
1561
+ this._store.set(curr.key, curr);
1562
+ this._store.set(leftSubTreeRightMost.key, leftSubTreeRightMost);
1563
+ }
1557
1564
  if (parentOfLeftSubTreeMax) {
1558
1565
  if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
1559
1566
  parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
@@ -1637,6 +1644,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1637
1644
  * @returns The first matching node, or undefined if not found.
1638
1645
  */
1639
1646
  getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
1647
+ if (this._isMapMode && keyNodeEntryOrPredicate !== null && keyNodeEntryOrPredicate !== void 0) {
1648
+ if (!this._isPredicate(keyNodeEntryOrPredicate)) {
1649
+ const key = this._extractKey(keyNodeEntryOrPredicate);
1650
+ if (key === null || key === void 0) return;
1651
+ return this._store.get(key);
1652
+ }
1653
+ }
1640
1654
  return this.search(keyNodeEntryOrPredicate, true, (node) => node, startNode, iterationType)[0];
1641
1655
  }
1642
1656
  /**
@@ -1649,15 +1663,22 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1649
1663
  * @returns The associated value, or undefined.
1650
1664
  */
1651
1665
  get(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
1652
- var _a;
1666
+ var _a, _b;
1653
1667
  if (this._isMapMode) {
1654
1668
  const key = this._extractKey(keyNodeEntryOrPredicate);
1655
1669
  if (key === null || key === void 0) return;
1656
- return this._store.get(key);
1670
+ return (_a = this._store.get(key)) == null ? void 0 : _a.value;
1657
1671
  }
1658
- return (_a = this.getNode(keyNodeEntryOrPredicate, startNode, iterationType)) == null ? void 0 : _a.value;
1672
+ return (_b = this.getNode(keyNodeEntryOrPredicate, startNode, iterationType)) == null ? void 0 : _b.value;
1659
1673
  }
1660
1674
  has(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
1675
+ if (this._isMapMode && keyNodeEntryOrPredicate !== void 0 && keyNodeEntryOrPredicate !== null) {
1676
+ if (!this._isPredicate(keyNodeEntryOrPredicate)) {
1677
+ const key = this._extractKey(keyNodeEntryOrPredicate);
1678
+ if (key === null || key === void 0) return false;
1679
+ return this._store.has(key);
1680
+ }
1681
+ }
1661
1682
  return this.search(keyNodeEntryOrPredicate, true, (node) => node, startNode, iterationType).length > 0;
1662
1683
  }
1663
1684
  /**
@@ -1726,7 +1747,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1726
1747
  }
1727
1748
  return true;
1728
1749
  }, "checkBST");
1729
- const isStandardBST = checkBST(false);
1750
+ const isStandardBST = checkBST();
1730
1751
  const isInverseBST = checkBST(true);
1731
1752
  return isStandardBST || isInverseBST;
1732
1753
  }
@@ -2402,8 +2423,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2402
2423
  }
2403
2424
  current = stack.pop();
2404
2425
  if (this.isRealNode(current)) {
2405
- if (this._isMapMode) yield [current.key, this._store.get(current.key)];
2406
- else yield [current.key, current.value];
2426
+ yield [current.key, current.value];
2407
2427
  current = current.right;
2408
2428
  }
2409
2429
  }
@@ -2411,8 +2431,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2411
2431
  if (node.left && this.isRealNode(node)) {
2412
2432
  yield* this[Symbol.iterator](node.left);
2413
2433
  }
2414
- if (this._isMapMode) yield [node.key, this._store.get(node.key)];
2415
- else yield [node.key, node.value];
2434
+ yield [node.key, node.value];
2416
2435
  if (node.right && this.isRealNode(node)) {
2417
2436
  yield* this[Symbol.iterator](node.right);
2418
2437
  }
@@ -2490,8 +2509,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2490
2509
  (node) => {
2491
2510
  if (node === null) cloned.set(null);
2492
2511
  else {
2493
- if (this._isMapMode) cloned.set([node.key, this._store.get(node.key)]);
2494
- else cloned.set([node.key, node.value]);
2512
+ cloned.set([node.key, node.value]);
2495
2513
  }
2496
2514
  },
2497
2515
  this._root,
@@ -2499,7 +2517,6 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2499
2517
  true
2500
2518
  // Include nulls
2501
2519
  );
2502
- if (this._isMapMode) cloned._store = this._store;
2503
2520
  }
2504
2521
  /**
2505
2522
  * (Protected) Recursive helper for `toVisual`.
@@ -2662,8 +2679,10 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2662
2679
  */
2663
2680
  _setValue(key, value) {
2664
2681
  if (key === null || key === void 0) return false;
2665
- if (value === void 0) return false;
2666
- return this._store.set(key, value);
2682
+ const node = this._store.get(key);
2683
+ if (!node) return false;
2684
+ node.value = value;
2685
+ return true;
2667
2686
  }
2668
2687
  /**
2669
2688
  * (Protected) Clears all nodes from the tree.
@@ -2872,7 +2891,7 @@ var _BST = class _BST extends BinaryTree {
2872
2891
  * @returns The newly created BSTNode.
2873
2892
  */
2874
2893
  createNode(key, value) {
2875
- return new BSTNode(key, this._isMapMode ? void 0 : value);
2894
+ return new BSTNode(key, value);
2876
2895
  }
2877
2896
  /**
2878
2897
  * Ensures the input is a node. If it's a key or entry, it searches for the node.
@@ -2957,8 +2976,40 @@ var _BST = class _BST extends BinaryTree {
2957
2976
  * @returns The first matching node, or undefined if not found.
2958
2977
  */
2959
2978
  getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
2960
- var _a;
2961
- return (_a = this.getNodes(keyNodeEntryOrPredicate, true, startNode, iterationType)[0]) != null ? _a : void 0;
2979
+ var _a, _b;
2980
+ if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) return void 0;
2981
+ if (this._isPredicate(keyNodeEntryOrPredicate)) {
2982
+ return (_a = this.getNodes(keyNodeEntryOrPredicate, true, startNode, iterationType)[0]) != null ? _a : void 0;
2983
+ }
2984
+ if (keyNodeEntryOrPredicate instanceof Range) {
2985
+ return (_b = this.getNodes(
2986
+ keyNodeEntryOrPredicate,
2987
+ true,
2988
+ startNode,
2989
+ iterationType
2990
+ )[0]) != null ? _b : void 0;
2991
+ }
2992
+ let targetKey;
2993
+ if (this.isNode(keyNodeEntryOrPredicate)) {
2994
+ targetKey = keyNodeEntryOrPredicate.key;
2995
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
2996
+ const k = keyNodeEntryOrPredicate[0];
2997
+ if (k === null || k === void 0) return void 0;
2998
+ targetKey = k;
2999
+ } else {
3000
+ targetKey = keyNodeEntryOrPredicate;
3001
+ }
3002
+ const start = this.ensureNode(startNode);
3003
+ if (!start) return void 0;
3004
+ const NIL = this._NIL;
3005
+ let cur = start;
3006
+ const cmpFn = this._comparator;
3007
+ while (cur && cur !== NIL) {
3008
+ const c = cmpFn(targetKey, cur.key);
3009
+ if (c === 0) return cur;
3010
+ cur = c < 0 ? cur._left : cur._right;
3011
+ }
3012
+ return void 0;
2962
3013
  }
2963
3014
  /**
2964
3015
  * Searches the tree for nodes matching a predicate, key, or range.
@@ -2979,8 +3030,30 @@ var _BST = class _BST extends BinaryTree {
2979
3030
  if (keyNodeEntryOrPredicate === null) return [];
2980
3031
  startNode = this.ensureNode(startNode);
2981
3032
  if (!startNode) return [];
2982
- let predicate;
2983
3033
  const isRange = this.isRange(keyNodeEntryOrPredicate);
3034
+ const isPred = !isRange && this._isPredicate(keyNodeEntryOrPredicate);
3035
+ if (!isRange && !isPred) {
3036
+ let targetKey;
3037
+ if (this.isNode(keyNodeEntryOrPredicate)) {
3038
+ targetKey = keyNodeEntryOrPredicate.key;
3039
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
3040
+ const k = keyNodeEntryOrPredicate[0];
3041
+ if (k !== null && k !== void 0) targetKey = k;
3042
+ } else {
3043
+ targetKey = keyNodeEntryOrPredicate;
3044
+ }
3045
+ if (targetKey === void 0) return [];
3046
+ const NIL = this._NIL;
3047
+ const cmpFn = this._comparator;
3048
+ let cur = startNode;
3049
+ while (cur && cur !== NIL) {
3050
+ const c = cmpFn(targetKey, cur.key);
3051
+ if (c === 0) return [callback(cur)];
3052
+ cur = c < 0 ? cur._left : cur._right;
3053
+ }
3054
+ return [];
3055
+ }
3056
+ let predicate;
2984
3057
  if (isRange) {
2985
3058
  predicate = /* @__PURE__ */ __name((node) => {
2986
3059
  if (!node) return false;
@@ -3059,11 +3132,11 @@ var _BST = class _BST extends BinaryTree {
3059
3132
  * @returns True if the addition was successful, false otherwise.
3060
3133
  */
3061
3134
  set(keyNodeOrEntry, value) {
3062
- const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
3135
+ const [newNode] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
3063
3136
  if (newNode === void 0) return false;
3064
3137
  if (this._root === void 0) {
3065
3138
  this._setRoot(newNode);
3066
- if (this._isMapMode) this._setValue(newNode == null ? void 0 : newNode.key, newValue);
3139
+ if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
3067
3140
  this._size++;
3068
3141
  return true;
3069
3142
  }
@@ -3071,12 +3144,12 @@ var _BST = class _BST extends BinaryTree {
3071
3144
  while (current !== void 0) {
3072
3145
  if (this._compare(current.key, newNode.key) === 0) {
3073
3146
  this._replaceNode(current, newNode);
3074
- if (this._isMapMode) this._setValue(current.key, newValue);
3147
+ if (this._isMapMode && this.isRealNode(newNode)) this._store.set(current.key, newNode);
3075
3148
  return true;
3076
3149
  } else if (this._compare(current.key, newNode.key) > 0) {
3077
3150
  if (current.left === void 0) {
3078
3151
  current.left = newNode;
3079
- if (this._isMapMode) this._setValue(newNode == null ? void 0 : newNode.key, newValue);
3152
+ if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
3080
3153
  this._size++;
3081
3154
  return true;
3082
3155
  }
@@ -3084,7 +3157,7 @@ var _BST = class _BST extends BinaryTree {
3084
3157
  } else {
3085
3158
  if (current.right === void 0) {
3086
3159
  current.right = newNode;
3087
- if (this._isMapMode) this._setValue(newNode == null ? void 0 : newNode.key, newValue);
3160
+ if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
3088
3161
  this._size++;
3089
3162
  return true;
3090
3163
  }
@@ -3867,7 +3940,6 @@ var _BST = class _BST extends BinaryTree {
3867
3940
  * @returns True if the node was found and deleted, false otherwise.
3868
3941
  */
3869
3942
  _deleteByKey(key) {
3870
- var _a;
3871
3943
  let node = this._root;
3872
3944
  while (node) {
3873
3945
  const cmp = this._compare(node.key, key);
@@ -3906,7 +3978,7 @@ var _BST = class _BST extends BinaryTree {
3906
3978
  succ.left = node.left;
3907
3979
  if (succ.left) succ.left.parent = succ;
3908
3980
  }
3909
- this._size = Math.max(0, ((_a = this._size) != null ? _a : 0) - 1);
3981
+ this._size = Math.max(0, this._size - 1);
3910
3982
  return true;
3911
3983
  }
3912
3984
  };
@@ -4071,7 +4143,7 @@ var _AVLTree = class _AVLTree extends BST {
4071
4143
  * @returns The newly created AVLTreeNode.
4072
4144
  */
4073
4145
  createNode(key, value) {
4074
- return new AVLTreeNode(key, this._isMapMode ? void 0 : value);
4146
+ return new AVLTreeNode(key, value);
4075
4147
  }
4076
4148
  /**
4077
4149
  * Checks if the given item is an `AVLTreeNode` instance.