avl-tree-typed 2.2.7 → 2.3.0

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 (29) hide show
  1. package/dist/cjs/index.cjs +66 -69
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +66 -69
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +66 -69
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +66 -69
  8. package/dist/esm-legacy/index.mjs.map +1 -1
  9. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +1 -1
  10. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
  11. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +10 -10
  12. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +22 -24
  13. package/dist/types/data-structures/binary-tree/bst.d.ts +11 -11
  14. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +1 -1
  15. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +1 -1
  16. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
  17. package/dist/umd/avl-tree-typed.js +66 -69
  18. package/dist/umd/avl-tree-typed.js.map +1 -1
  19. package/dist/umd/avl-tree-typed.min.js +3 -3
  20. package/dist/umd/avl-tree-typed.min.js.map +1 -1
  21. package/package.json +2 -2
  22. package/src/data-structures/binary-tree/avl-tree-counter.ts +6 -6
  23. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +13 -13
  24. package/src/data-structures/binary-tree/avl-tree.ts +15 -15
  25. package/src/data-structures/binary-tree/binary-tree.ts +52 -55
  26. package/src/data-structures/binary-tree/bst.ts +21 -22
  27. package/src/data-structures/binary-tree/red-black-tree.ts +3 -3
  28. package/src/data-structures/binary-tree/tree-counter.ts +4 -4
  29. package/src/data-structures/binary-tree/tree-multi-map.ts +13 -13
@@ -1189,9 +1189,9 @@ var BinaryTree = class extends IterableEntryBase {
1189
1189
  iterationType = "ITERATIVE";
1190
1190
  /**
1191
1191
  * Creates an instance of BinaryTree.
1192
- * @remarks Time O(N * M), where N is the number of items in `keysNodesEntriesOrRaws` and M is the tree size at insertion time (due to O(M) `add` operation). Space O(N) for storing the nodes.
1192
+ * @remarks Time O(N * M), where N is the number of items in `keysNodesEntriesOrRaws` and M is the tree size at insertion time (due to O(M) `set` operation). Space O(N) for storing the nodes.
1193
1193
  *
1194
- * @param [keysNodesEntriesOrRaws=[]] - An iterable of items to add.
1194
+ * @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
1195
1195
  * @param [options] - Configuration options for the tree.
1196
1196
  */
1197
1197
  constructor(keysNodesEntriesOrRaws = [], options) {
@@ -1204,7 +1204,7 @@ var BinaryTree = class extends IterableEntryBase {
1204
1204
  if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
1205
1205
  else if (toEntryFn) throw TypeError("toEntryFn must be a function type");
1206
1206
  }
1207
- if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
1207
+ if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
1208
1208
  }
1209
1209
  _isMapMode = true;
1210
1210
  /**
@@ -1418,10 +1418,20 @@ var BinaryTree = class extends IterableEntryBase {
1418
1418
  * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation adds the node at the first available position in a level-order (BFS) traversal. This is NOT a Binary Search Tree insertion. Time O(N), where N is the number of nodes. It must traverse level-by-level to find an empty slot. Space O(N) in the worst case for the BFS queue (e.g., a full last level).
1419
1419
  *
1420
1420
  * @param keyNodeOrEntry - The key, node, or entry to add.
1421
+ * @returns True if the addition was successful, false otherwise.
1422
+ */
1423
+ add(keyNodeOrEntry) {
1424
+ return this.set(keyNodeOrEntry);
1425
+ }
1426
+ /**
1427
+ * Adds or updates a new node to the tree.
1428
+ * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation sets the node at the first available position in a level-order (BFS) traversal. This is NOT a Binary Search Tree insertion. Time O(N), where N is the number of nodes. It must traverse level-by-level to find an empty slot. Space O(N) in the worst case for the BFS queue (e.g., a full last level).
1429
+ *
1430
+ * @param keyNodeOrEntry - The key, node, or entry to set or update.
1421
1431
  * @param [value] - The value, if providing just a key.
1422
1432
  * @returns True if the addition was successful, false otherwise.
1423
1433
  */
1424
- add(keyNodeOrEntry, value) {
1434
+ set(keyNodeOrEntry, value) {
1425
1435
  const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
1426
1436
  if (newNode === void 0) return false;
1427
1437
  if (!this._root) {
@@ -1465,25 +1475,24 @@ var BinaryTree = class extends IterableEntryBase {
1465
1475
  return false;
1466
1476
  }
1467
1477
  /**
1468
- * Adds or updates a new node to the tree.
1469
- * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation adds the node at the first available position in a level-order (BFS) traversal. This is NOT a Binary Search Tree insertion. Time O(N), where N is the number of nodes. It must traverse level-by-level to find an empty slot. Space O(N) in the worst case for the BFS queue (e.g., a full last level).
1478
+ * Adds multiple items to the tree.
1479
+ * @remarks Time O(N * M), where N is the number of items to set and M is the size of the tree at insertion (due to O(M) `set` operation). Space O(M) (from `set`) + O(N) (for the `inserted` array).
1470
1480
  *
1471
- * @param keyNodeOrEntry - The key, node, or entry to add or update.
1472
- * @param [value] - The value, if providing just a key.
1473
- * @returns True if the addition was successful, false otherwise.
1481
+ * @param keysNodesEntriesOrRaws - An iterable of items to set.
1482
+ * @returns An array of booleans indicating the success of each individual `set` operation.
1474
1483
  */
1475
- set(keyNodeOrEntry, value) {
1476
- return this.add(keyNodeOrEntry, value);
1484
+ addMany(keysNodesEntriesOrRaws) {
1485
+ return this.setMany(keysNodesEntriesOrRaws);
1477
1486
  }
1478
1487
  /**
1479
- * Adds multiple items to the tree.
1480
- * @remarks Time O(N * M), where N is the number of items to add and M is the size of the tree at insertion (due to O(M) `add` operation). Space O(M) (from `add`) + O(N) (for the `inserted` array).
1488
+ * Adds or updates multiple items to the tree.
1489
+ * @remarks Time O(N * M), where N is the number of items to set and M is the size of the tree at insertion (due to O(M) `set` operation). Space O(M) (from `set`) + O(N) (for the `inserted` array).
1481
1490
  *
1482
- * @param keysNodesEntriesOrRaws - An iterable of items to add.
1491
+ * @param keysNodesEntriesOrRaws - An iterable of items to set or update.
1483
1492
  * @param [values] - An optional parallel iterable of values.
1484
- * @returns An array of booleans indicating the success of each individual `add` operation.
1493
+ * @returns An array of booleans indicating the success of each individual `set` operation.
1485
1494
  */
1486
- addMany(keysNodesEntriesOrRaws, values) {
1495
+ setMany(keysNodesEntriesOrRaws, values) {
1487
1496
  const inserted = [];
1488
1497
  let valuesIterator;
1489
1498
  if (values) {
@@ -1498,40 +1507,29 @@ var BinaryTree = class extends IterableEntryBase {
1498
1507
  }
1499
1508
  }
1500
1509
  if (this.isRaw(keyNodeEntryOrRaw)) keyNodeEntryOrRaw = this._toEntryFn(keyNodeEntryOrRaw);
1501
- inserted.push(this.add(keyNodeEntryOrRaw, value));
1510
+ inserted.push(this.set(keyNodeEntryOrRaw, value));
1502
1511
  }
1503
1512
  return inserted;
1504
1513
  }
1505
1514
  /**
1506
- * Adds or updates multiple items to the tree.
1507
- * @remarks Time O(N * M), where N is the number of items to add and M is the size of the tree at insertion (due to O(M) `add` operation). Space O(M) (from `add`) + O(N) (for the `inserted` array).
1508
- *
1509
- * @param keysNodesEntriesOrRaws - An iterable of items to add or update.
1510
- * @param [values] - An optional parallel iterable of values.
1511
- * @returns An array of booleans indicating the success of each individual `add` operation.
1512
- */
1513
- setMany(keysNodesEntriesOrRaws, values) {
1514
- return this.addMany(keysNodesEntriesOrRaws, values);
1515
- }
1516
- /**
1517
- * Merges another tree into this one by adding all its nodes.
1518
- * @remarks Time O(N * M), same as `addMany`, where N is the size of `anotherTree` and M is the size of this tree. Space O(M) (from `add`).
1515
+ * Merges another tree into this one by seting all its nodes.
1516
+ * @remarks Time O(N * M), same as `setMany`, where N is the size of `anotherTree` and M is the size of this tree. Space O(M) (from `set`).
1519
1517
  *
1520
1518
  * @param anotherTree - The tree to merge.
1521
1519
  */
1522
1520
  merge(anotherTree) {
1523
- this.addMany(anotherTree, []);
1521
+ this.setMany(anotherTree, []);
1524
1522
  }
1525
1523
  /**
1526
1524
  * Clears the tree and refills it with new items.
1527
- * @remarks Time O(N) (for `clear`) + O(N * M) (for `addMany`) = O(N * M). Space O(M) (from `addMany`).
1525
+ * @remarks Time O(N) (for `clear`) + O(N * M) (for `setMany`) = O(N * M). Space O(M) (from `setMany`).
1528
1526
  *
1529
- * @param keysNodesEntriesOrRaws - An iterable of items to add.
1527
+ * @param keysNodesEntriesOrRaws - An iterable of items to set.
1530
1528
  * @param [values] - An optional parallel iterable of values.
1531
1529
  */
1532
1530
  refill(keysNodesEntriesOrRaws, values) {
1533
1531
  this.clear();
1534
- this.addMany(keysNodesEntriesOrRaws, values);
1532
+ this.setMany(keysNodesEntriesOrRaws, values);
1535
1533
  }
1536
1534
  /**
1537
1535
  * Deletes a node from the tree.
@@ -2196,7 +2194,7 @@ var BinaryTree = class extends IterableEntryBase {
2196
2194
  }
2197
2195
  /**
2198
2196
  * Clones the tree.
2199
- * @remarks Time O(N * M), where N is the number of nodes and M is the tree size during insertion (due to `bfs` + `add`, and `add` is O(M)). Space O(N) for the new tree and the BFS queue.
2197
+ * @remarks Time O(N * M), where N is the number of nodes and M is the tree size during insertion (due to `bfs` + `set`, and `set` is O(M)). Space O(N) for the new tree and the BFS queue.
2200
2198
  *
2201
2199
  * @returns A new, cloned instance of the tree.
2202
2200
  */
@@ -2207,7 +2205,7 @@ var BinaryTree = class extends IterableEntryBase {
2207
2205
  }
2208
2206
  /**
2209
2207
  * Creates a new tree containing only the entries that satisfy the predicate.
2210
- * @remarks Time O(N * M), where N is nodes in this tree, and M is size of the new tree during insertion (O(N) iteration + O(M) `add` for each item). Space O(N) for the new tree.
2208
+ * @remarks Time O(N * M), where N is nodes in this tree, and M is size of the new tree during insertion (O(N) iteration + O(M) `set` for each item). Space O(N) for the new tree.
2211
2209
  *
2212
2210
  * @param predicate - A function to test each [key, value] pair.
2213
2211
  * @param [thisArg] - `this` context for the predicate.
@@ -2216,7 +2214,7 @@ var BinaryTree = class extends IterableEntryBase {
2216
2214
  filter(predicate, thisArg) {
2217
2215
  const out = this._createInstance();
2218
2216
  let i = 0;
2219
- for (const [k, v] of this) if (predicate.call(thisArg, v, k, i++, this)) out.add([k, v]);
2217
+ for (const [k, v] of this) if (predicate.call(thisArg, v, k, i++, this)) out.set([k, v]);
2220
2218
  return out;
2221
2219
  }
2222
2220
  /**
@@ -2234,7 +2232,7 @@ var BinaryTree = class extends IterableEntryBase {
2234
2232
  map(cb, options, thisArg) {
2235
2233
  const out = this._createLike([], options);
2236
2234
  let i = 0;
2237
- for (const [k, v] of this) out.add(cb.call(thisArg, v, k, i++, this));
2235
+ for (const [k, v] of this) out.set(cb.call(thisArg, v, k, i++, this));
2238
2236
  return out;
2239
2237
  }
2240
2238
  /**
@@ -2486,18 +2484,18 @@ var BinaryTree = class extends IterableEntryBase {
2486
2484
  return [this.createNode(keyNodeOrEntry, value), value];
2487
2485
  }
2488
2486
  /**
2489
- * (Protected) Helper for cloning. Performs a BFS and adds all nodes to the new tree.
2490
- * @remarks Time O(N * M) (O(N) BFS + O(M) `add` for each node).
2487
+ * (Protected) Helper for cloning. Performs a BFS and sets all nodes to the new tree.
2488
+ * @remarks Time O(N * M) (O(N) BFS + O(M) `set` for each node).
2491
2489
  *
2492
2490
  * @param cloned - The new, empty tree instance to populate.
2493
2491
  */
2494
2492
  _clone(cloned) {
2495
2493
  this.bfs(
2496
2494
  (node) => {
2497
- if (node === null) cloned.add(null);
2495
+ if (node === null) cloned.set(null);
2498
2496
  else {
2499
- if (this._isMapMode) cloned.add([node.key, this._store.get(node.key)]);
2500
- else cloned.add([node.key, node.value]);
2497
+ if (this._isMapMode) cloned.set([node.key, this._store.get(node.key)]);
2498
+ else cloned.set([node.key, node.value]);
2501
2499
  }
2502
2500
  },
2503
2501
  this._root,
@@ -2830,7 +2828,7 @@ var BST = class extends BinaryTree {
2830
2828
  * Creates an instance of BST.
2831
2829
  * @remarks Time O(N log N) or O(N^2) depending on `isBalanceAdd` in `addMany` and input order. Space O(N).
2832
2830
  *
2833
- * @param [keysNodesEntriesOrRaws=[]] - An iterable of items to add.
2831
+ * @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
2834
2832
  * @param [options] - Configuration options for the BST, including comparator.
2835
2833
  */
2836
2834
  constructor(keysNodesEntriesOrRaws = [], options) {
@@ -2844,7 +2842,7 @@ var BST = class extends BinaryTree {
2844
2842
  } else {
2845
2843
  this._comparator = this._createDefaultComparator();
2846
2844
  }
2847
- if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
2845
+ if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
2848
2846
  }
2849
2847
  _root = void 0;
2850
2848
  /**
@@ -3060,11 +3058,11 @@ var BST = class extends BinaryTree {
3060
3058
  * Adds a new node to the BST based on key comparison.
3061
3059
  * @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
3062
3060
  *
3063
- * @param keyNodeOrEntry - The key, node, or entry to add.
3061
+ * @param keyNodeOrEntry - The key, node, or entry to set.
3064
3062
  * @param [value] - The value, if providing just a key.
3065
3063
  * @returns True if the addition was successful, false otherwise.
3066
3064
  */
3067
- add(keyNodeOrEntry, value) {
3065
+ set(keyNodeOrEntry, value) {
3068
3066
  const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
3069
3067
  if (newNode === void 0) return false;
3070
3068
  if (this._root === void 0) {
@@ -3101,24 +3099,24 @@ var BST = class extends BinaryTree {
3101
3099
  }
3102
3100
  /**
3103
3101
  * Adds multiple items to the tree.
3104
- * @remarks If `isBalanceAdd` is true, sorts the input and builds a balanced tree. Time O(N log N) (due to sort and balanced add).
3102
+ * @remarks If `isBalanceAdd` is true, sorts the input and builds a balanced tree. Time O(N log N) (due to sort and balanced set).
3105
3103
  * If false, adds items one by one. Time O(N * H), which is O(N^2) worst-case.
3106
3104
  * Space O(N) for sorting and recursion/iteration stack.
3107
3105
  *
3108
- * @param keysNodesEntriesOrRaws - An iterable of items to add.
3106
+ * @param keysNodesEntriesOrRaws - An iterable of items to set.
3109
3107
  * @param [values] - An optional parallel iterable of values.
3110
3108
  * @param [isBalanceAdd=true] - If true, builds a balanced tree from the items.
3111
- * @param [iterationType=this.iterationType] - The traversal method for balanced add (recursive or iterative).
3112
- * @returns An array of booleans indicating the success of each individual `add` operation.
3109
+ * @param [iterationType=this.iterationType] - The traversal method for balanced set (recursive or iterative).
3110
+ * @returns An array of booleans indicating the success of each individual `set` operation.
3113
3111
  */
3114
- addMany(keysNodesEntriesOrRaws, values, isBalanceAdd = true, iterationType = this.iterationType) {
3112
+ setMany(keysNodesEntriesOrRaws, values, isBalanceAdd = true, iterationType = this.iterationType) {
3115
3113
  const inserted = [];
3116
3114
  const valuesIterator = values?.[Symbol.iterator]();
3117
3115
  if (!isBalanceAdd) {
3118
3116
  for (let kve of keysNodesEntriesOrRaws) {
3119
3117
  const val = valuesIterator?.next().value;
3120
3118
  if (this.isRaw(kve)) kve = this._toEntryFn(kve);
3121
- inserted.push(this.add(kve, val));
3119
+ inserted.push(this.set(kve, val));
3122
3120
  }
3123
3121
  return inserted;
3124
3122
  }
@@ -3146,9 +3144,9 @@ var BST = class extends BinaryTree {
3146
3144
  const { key, value, orgIndex } = arr[mid];
3147
3145
  if (this.isRaw(key)) {
3148
3146
  const entry = this._toEntryFn(key);
3149
- inserted[orgIndex] = this.add(entry);
3147
+ inserted[orgIndex] = this.set(entry);
3150
3148
  } else {
3151
- inserted[orgIndex] = this.add(key, value);
3149
+ inserted[orgIndex] = this.set(key, value);
3152
3150
  }
3153
3151
  _dfs(arr.slice(0, mid));
3154
3152
  _dfs(arr.slice(mid + 1));
@@ -3165,9 +3163,9 @@ var BST = class extends BinaryTree {
3165
3163
  const { key, value, orgIndex } = sorted[m];
3166
3164
  if (this.isRaw(key)) {
3167
3165
  const entry = this._toEntryFn(key);
3168
- inserted[orgIndex] = this.add(entry);
3166
+ inserted[orgIndex] = this.set(entry);
3169
3167
  } else {
3170
- inserted[orgIndex] = this.add(key, value);
3168
+ inserted[orgIndex] = this.set(key, value);
3171
3169
  }
3172
3170
  stack.push([m + 1, r]);
3173
3171
  stack.push([l, m - 1]);
@@ -3442,7 +3440,7 @@ var BST = class extends BinaryTree {
3442
3440
  const out = this._createLike([], options);
3443
3441
  let index = 0;
3444
3442
  for (const [key, value] of this) {
3445
- out.add(callback.call(thisArg, value, key, index++, this));
3443
+ out.set(callback.call(thisArg, value, key, index++, this));
3446
3444
  }
3447
3445
  return out;
3448
3446
  }
@@ -3498,7 +3496,6 @@ var BST = class extends BinaryTree {
3498
3496
  */
3499
3497
  _createDefaultComparator() {
3500
3498
  return (a, b) => {
3501
- debugger;
3502
3499
  if (isComparable(a) && isComparable(b)) {
3503
3500
  if (a > b) return 1;
3504
3501
  if (a < b) return -1;
@@ -4058,14 +4055,14 @@ var AVLTree = class extends BST {
4058
4055
  }
4059
4056
  /**
4060
4057
  * Creates an instance of AVLTree.
4061
- * @remarks Time O(N log N) (from `addMany` with balanced add). Space O(N).
4058
+ * @remarks Time O(N log N) (from `setMany` with balanced set). Space O(N).
4062
4059
  *
4063
- * @param [keysNodesEntriesOrRaws=[]] - An iterable of items to add.
4060
+ * @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
4064
4061
  * @param [options] - Configuration options for the AVL tree.
4065
4062
  */
4066
4063
  constructor(keysNodesEntriesOrRaws = [], options) {
4067
4064
  super([], options);
4068
- if (keysNodesEntriesOrRaws) super.addMany(keysNodesEntriesOrRaws);
4065
+ if (keysNodesEntriesOrRaws) super.setMany(keysNodesEntriesOrRaws);
4069
4066
  }
4070
4067
  /**
4071
4068
  * (Protected) Creates a new AVL tree node.
@@ -4089,16 +4086,16 @@ var AVLTree = class extends BST {
4089
4086
  return keyNodeOrEntry instanceof AVLTreeNode;
4090
4087
  }
4091
4088
  /**
4092
- * Adds a new node to the AVL tree and balances the tree path.
4093
- * @remarks Time O(log N) (O(H) for BST add + O(H) for `_balancePath`). Space O(H) for path/recursion.
4089
+ * Sets a new node to the AVL tree and balances the tree path.
4090
+ * @remarks Time O(log N) (O(H) for BST set + O(H) for `_balancePath`). Space O(H) for path/recursion.
4094
4091
  *
4095
- * @param keyNodeOrEntry - The key, node, or entry to add.
4092
+ * @param keyNodeOrEntry - The key, node, or entry to set.
4096
4093
  * @param [value] - The value, if providing just a key.
4097
4094
  * @returns True if the addition was successful, false otherwise.
4098
4095
  */
4099
- add(keyNodeOrEntry, value) {
4096
+ set(keyNodeOrEntry, value) {
4100
4097
  if (keyNodeOrEntry === null) return false;
4101
- const inserted = super.add(keyNodeOrEntry, value);
4098
+ const inserted = super.set(keyNodeOrEntry, value);
4102
4099
  if (inserted) this._balancePath(keyNodeOrEntry);
4103
4100
  return inserted;
4104
4101
  }
@@ -4150,7 +4147,7 @@ var AVLTree = class extends BST {
4150
4147
  }
4151
4148
  /**
4152
4149
  * Creates a new AVLTree by mapping each [key, value] pair.
4153
- * @remarks Time O(N log N) (O(N) iteration + O(log M) `add` for each item into the new tree). Space O(N) for the new tree.
4150
+ * @remarks Time O(N log N) (O(N) iteration + O(log M) `set` for each item into the new tree). Space O(N) for the new tree.
4154
4151
  *
4155
4152
  * @template MK - New key type.
4156
4153
  * @template MV - New value type.
@@ -4164,7 +4161,7 @@ var AVLTree = class extends BST {
4164
4161
  const out = this._createLike([], options);
4165
4162
  let index = 0;
4166
4163
  for (const [key, value] of this) {
4167
- out.add(callback.call(thisArg, value, key, index++, this));
4164
+ out.set(callback.call(thisArg, value, key, index++, this));
4168
4165
  }
4169
4166
  return out;
4170
4167
  }