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
@@ -1193,6 +1193,9 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1193
1193
  __publicField(this, "iterationType", "ITERATIVE");
1194
1194
  __publicField(this, "_isMapMode", true);
1195
1195
  __publicField(this, "_isDuplicate", false);
1196
+ // Map mode acceleration store:
1197
+ // - isMapMode=false: unused
1198
+ // - isMapMode=true: key -> node reference (O(1) has/getNode + fast get)
1196
1199
  __publicField(this, "_store", /* @__PURE__ */ new Map());
1197
1200
  __publicField(this, "_root");
1198
1201
  __publicField(this, "_size", 0);
@@ -1288,7 +1291,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1288
1291
  * @returns The newly created node.
1289
1292
  */
1290
1293
  createNode(key, value) {
1291
- return new BinaryTreeNode(key, this._isMapMode ? void 0 : value);
1294
+ return new BinaryTreeNode(key, value);
1292
1295
  }
1293
1296
  /**
1294
1297
  * Creates a new, empty tree of the same type and configuration.
@@ -1435,11 +1438,11 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1435
1438
  * @returns True if the addition was successful, false otherwise.
1436
1439
  */
1437
1440
  set(keyNodeOrEntry, value) {
1438
- const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
1441
+ const [newNode] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
1439
1442
  if (newNode === void 0) return false;
1440
1443
  if (!this._root) {
1441
1444
  this._setRoot(newNode);
1442
- if (this._isMapMode) this._setValue(newNode == null ? void 0 : newNode.key, newValue);
1445
+ if (this._isMapMode && newNode !== null && newNode !== void 0) this._store.set(newNode.key, newNode);
1443
1446
  this._size = 1;
1444
1447
  return true;
1445
1448
  }
@@ -1451,7 +1454,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1451
1454
  if (!this._isDuplicate) {
1452
1455
  if (newNode !== null && cur.key === newNode.key) {
1453
1456
  this._replaceNode(cur, newNode);
1454
- if (this._isMapMode) this._setValue(cur.key, newValue);
1457
+ if (this._isMapMode && newNode !== null) this._store.set(cur.key, newNode);
1455
1458
  return true;
1456
1459
  }
1457
1460
  }
@@ -1471,7 +1474,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1471
1474
  } else if (potentialParent.right === void 0) {
1472
1475
  potentialParent.right = newNode;
1473
1476
  }
1474
- if (this._isMapMode) this._setValue(newNode == null ? void 0 : newNode.key, newValue);
1477
+ if (this._isMapMode && newNode !== null && newNode !== void 0) this._store.set(newNode.key, newNode);
1475
1478
  this._size++;
1476
1479
  return true;
1477
1480
  }
@@ -1538,13 +1541,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1538
1541
  * Deletes a node from the tree.
1539
1542
  * @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).
1540
1543
  *
1541
- * @param keyNodeOrEntry - The node to delete.
1544
+ * @param keyNodeEntryRawOrPredicate - The node to delete.
1542
1545
  * @returns An array containing deletion results (for compatibility with self-balancing trees).
1543
1546
  */
1544
- delete(keyNodeOrEntry) {
1547
+ delete(keyNodeEntryRawOrPredicate) {
1545
1548
  const deletedResult = [];
1546
1549
  if (!this._root) return deletedResult;
1547
- const curr = this.getNode(keyNodeOrEntry);
1550
+ const curr = this.getNode(keyNodeEntryRawOrPredicate);
1548
1551
  if (!curr) return deletedResult;
1549
1552
  const parent = curr == null ? void 0 : curr.parent;
1550
1553
  let needBalanced;
@@ -1556,6 +1559,10 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1556
1559
  if (leftSubTreeRightMost) {
1557
1560
  const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
1558
1561
  orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
1562
+ if (this._isMapMode) {
1563
+ this._store.set(curr.key, curr);
1564
+ this._store.set(leftSubTreeRightMost.key, leftSubTreeRightMost);
1565
+ }
1559
1566
  if (parentOfLeftSubTreeMax) {
1560
1567
  if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
1561
1568
  parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
@@ -1639,6 +1646,13 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1639
1646
  * @returns The first matching node, or undefined if not found.
1640
1647
  */
1641
1648
  getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
1649
+ if (this._isMapMode && keyNodeEntryOrPredicate !== null && keyNodeEntryOrPredicate !== void 0) {
1650
+ if (!this._isPredicate(keyNodeEntryOrPredicate)) {
1651
+ const key = this._extractKey(keyNodeEntryOrPredicate);
1652
+ if (key === null || key === void 0) return;
1653
+ return this._store.get(key);
1654
+ }
1655
+ }
1642
1656
  return this.search(keyNodeEntryOrPredicate, true, (node) => node, startNode, iterationType)[0];
1643
1657
  }
1644
1658
  /**
@@ -1651,15 +1665,22 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1651
1665
  * @returns The associated value, or undefined.
1652
1666
  */
1653
1667
  get(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
1654
- var _a;
1668
+ var _a, _b;
1655
1669
  if (this._isMapMode) {
1656
1670
  const key = this._extractKey(keyNodeEntryOrPredicate);
1657
1671
  if (key === null || key === void 0) return;
1658
- return this._store.get(key);
1672
+ return (_a = this._store.get(key)) == null ? void 0 : _a.value;
1659
1673
  }
1660
- return (_a = this.getNode(keyNodeEntryOrPredicate, startNode, iterationType)) == null ? void 0 : _a.value;
1674
+ return (_b = this.getNode(keyNodeEntryOrPredicate, startNode, iterationType)) == null ? void 0 : _b.value;
1661
1675
  }
1662
1676
  has(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
1677
+ if (this._isMapMode && keyNodeEntryOrPredicate !== void 0 && keyNodeEntryOrPredicate !== null) {
1678
+ if (!this._isPredicate(keyNodeEntryOrPredicate)) {
1679
+ const key = this._extractKey(keyNodeEntryOrPredicate);
1680
+ if (key === null || key === void 0) return false;
1681
+ return this._store.has(key);
1682
+ }
1683
+ }
1663
1684
  return this.search(keyNodeEntryOrPredicate, true, (node) => node, startNode, iterationType).length > 0;
1664
1685
  }
1665
1686
  /**
@@ -1728,7 +1749,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1728
1749
  }
1729
1750
  return true;
1730
1751
  }, "checkBST");
1731
- const isStandardBST = checkBST(false);
1752
+ const isStandardBST = checkBST();
1732
1753
  const isInverseBST = checkBST(true);
1733
1754
  return isStandardBST || isInverseBST;
1734
1755
  }
@@ -2404,8 +2425,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2404
2425
  }
2405
2426
  current = stack.pop();
2406
2427
  if (this.isRealNode(current)) {
2407
- if (this._isMapMode) yield [current.key, this._store.get(current.key)];
2408
- else yield [current.key, current.value];
2428
+ yield [current.key, current.value];
2409
2429
  current = current.right;
2410
2430
  }
2411
2431
  }
@@ -2413,8 +2433,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2413
2433
  if (node.left && this.isRealNode(node)) {
2414
2434
  yield* this[Symbol.iterator](node.left);
2415
2435
  }
2416
- if (this._isMapMode) yield [node.key, this._store.get(node.key)];
2417
- else yield [node.key, node.value];
2436
+ yield [node.key, node.value];
2418
2437
  if (node.right && this.isRealNode(node)) {
2419
2438
  yield* this[Symbol.iterator](node.right);
2420
2439
  }
@@ -2492,8 +2511,7 @@ var _BinaryTree = class _BinaryTree 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 _BinaryTree 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 _BinaryTree 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.
@@ -2874,7 +2893,7 @@ var _BST = class _BST extends BinaryTree {
2874
2893
  * @returns The newly created BSTNode.
2875
2894
  */
2876
2895
  createNode(key, value) {
2877
- return new BSTNode(key, this._isMapMode ? void 0 : value);
2896
+ return new BSTNode(key, value);
2878
2897
  }
2879
2898
  /**
2880
2899
  * Ensures the input is a node. If it's a key or entry, it searches for the node.
@@ -2959,8 +2978,40 @@ var _BST = class _BST extends BinaryTree {
2959
2978
  * @returns The first matching node, or undefined if not found.
2960
2979
  */
2961
2980
  getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
2962
- var _a;
2963
- return (_a = this.getNodes(keyNodeEntryOrPredicate, true, startNode, iterationType)[0]) != null ? _a : void 0;
2981
+ var _a, _b;
2982
+ if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) return void 0;
2983
+ if (this._isPredicate(keyNodeEntryOrPredicate)) {
2984
+ return (_a = this.getNodes(keyNodeEntryOrPredicate, true, startNode, iterationType)[0]) != null ? _a : void 0;
2985
+ }
2986
+ if (keyNodeEntryOrPredicate instanceof Range) {
2987
+ return (_b = this.getNodes(
2988
+ keyNodeEntryOrPredicate,
2989
+ true,
2990
+ startNode,
2991
+ iterationType
2992
+ )[0]) != null ? _b : 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 _BST 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 _BST 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 == null ? void 0 : 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 _BST 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 == null ? void 0 : 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 _BST extends BinaryTree {
3086
3159
  } else {
3087
3160
  if (current.right === void 0) {
3088
3161
  current.right = newNode;
3089
- if (this._isMapMode) this._setValue(newNode == null ? void 0 : 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
  }
@@ -3869,7 +3942,6 @@ var _BST = class _BST extends BinaryTree {
3869
3942
  * @returns True if the node was found and deleted, false otherwise.
3870
3943
  */
3871
3944
  _deleteByKey(key) {
3872
- var _a;
3873
3945
  let node = this._root;
3874
3946
  while (node) {
3875
3947
  const cmp = this._compare(node.key, key);
@@ -3908,7 +3980,7 @@ var _BST = class _BST extends BinaryTree {
3908
3980
  succ.left = node.left;
3909
3981
  if (succ.left) succ.left.parent = succ;
3910
3982
  }
3911
- this._size = Math.max(0, ((_a = this._size) != null ? _a : 0) - 1);
3983
+ this._size = Math.max(0, this._size - 1);
3912
3984
  return true;
3913
3985
  }
3914
3986
  };
@@ -4073,7 +4145,7 @@ var _AVLTree = class _AVLTree extends BST {
4073
4145
  * @returns The newly created AVLTreeNode.
4074
4146
  */
4075
4147
  createNode(key, value) {
4076
- return new AVLTreeNode(key, this._isMapMode ? void 0 : value);
4148
+ return new AVLTreeNode(key, value);
4077
4149
  }
4078
4150
  /**
4079
4151
  * Checks if the given item is an `AVLTreeNode` instance.