bst-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 (30) hide show
  1. package/README.md +5 -4
  2. package/dist/cjs/index.cjs +56 -59
  3. package/dist/cjs/index.cjs.map +1 -1
  4. package/dist/cjs-legacy/index.cjs +56 -59
  5. package/dist/cjs-legacy/index.cjs.map +1 -1
  6. package/dist/esm/index.mjs +56 -59
  7. package/dist/esm/index.mjs.map +1 -1
  8. package/dist/esm-legacy/index.mjs +56 -59
  9. package/dist/esm-legacy/index.mjs.map +1 -1
  10. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +1 -1
  11. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
  12. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +10 -10
  13. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +22 -24
  14. package/dist/types/data-structures/binary-tree/bst.d.ts +11 -11
  15. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +1 -1
  16. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +1 -1
  17. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
  18. package/dist/umd/bst-typed.js +56 -59
  19. package/dist/umd/bst-typed.js.map +1 -1
  20. package/dist/umd/bst-typed.min.js +2 -2
  21. package/dist/umd/bst-typed.min.js.map +1 -1
  22. package/package.json +2 -2
  23. package/src/data-structures/binary-tree/avl-tree-counter.ts +6 -6
  24. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +13 -13
  25. package/src/data-structures/binary-tree/avl-tree.ts +15 -15
  26. package/src/data-structures/binary-tree/binary-tree.ts +52 -55
  27. package/src/data-structures/binary-tree/bst.ts +21 -22
  28. package/src/data-structures/binary-tree/red-black-tree.ts +3 -3
  29. package/src/data-structures/binary-tree/tree-counter.ts +4 -4
  30. package/src/data-structures/binary-tree/tree-multi-map.ts +13 -13
@@ -1186,9 +1186,9 @@ var BinaryTreeNode = _BinaryTreeNode;
1186
1186
  var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1187
1187
  /**
1188
1188
  * Creates an instance of BinaryTree.
1189
- * @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.
1189
+ * @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.
1190
1190
  *
1191
- * @param [keysNodesEntriesOrRaws=[]] - An iterable of items to add.
1191
+ * @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
1192
1192
  * @param [options] - Configuration options for the tree.
1193
1193
  */
1194
1194
  constructor(keysNodesEntriesOrRaws = [], options) {
@@ -1217,7 +1217,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1217
1217
  if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
1218
1218
  else if (toEntryFn) throw TypeError("toEntryFn must be a function type");
1219
1219
  }
1220
- if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
1220
+ if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
1221
1221
  }
1222
1222
  /**
1223
1223
  * Gets whether the tree is in Map mode.
@@ -1424,10 +1424,20 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1424
1424
  * @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).
1425
1425
  *
1426
1426
  * @param keyNodeOrEntry - The key, node, or entry to add.
1427
+ * @returns True if the addition was successful, false otherwise.
1428
+ */
1429
+ add(keyNodeOrEntry) {
1430
+ return this.set(keyNodeOrEntry);
1431
+ }
1432
+ /**
1433
+ * Adds or updates a new node to the tree.
1434
+ * @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).
1435
+ *
1436
+ * @param keyNodeOrEntry - The key, node, or entry to set or update.
1427
1437
  * @param [value] - The value, if providing just a key.
1428
1438
  * @returns True if the addition was successful, false otherwise.
1429
1439
  */
1430
- add(keyNodeOrEntry, value) {
1440
+ set(keyNodeOrEntry, value) {
1431
1441
  const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
1432
1442
  if (newNode === void 0) return false;
1433
1443
  if (!this._root) {
@@ -1471,25 +1481,24 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1471
1481
  return false;
1472
1482
  }
1473
1483
  /**
1474
- * Adds or updates a new node to the tree.
1475
- * @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).
1484
+ * Adds multiple items to the tree.
1485
+ * @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).
1476
1486
  *
1477
- * @param keyNodeOrEntry - The key, node, or entry to add or update.
1478
- * @param [value] - The value, if providing just a key.
1479
- * @returns True if the addition was successful, false otherwise.
1487
+ * @param keysNodesEntriesOrRaws - An iterable of items to set.
1488
+ * @returns An array of booleans indicating the success of each individual `set` operation.
1480
1489
  */
1481
- set(keyNodeOrEntry, value) {
1482
- return this.add(keyNodeOrEntry, value);
1490
+ addMany(keysNodesEntriesOrRaws) {
1491
+ return this.setMany(keysNodesEntriesOrRaws);
1483
1492
  }
1484
1493
  /**
1485
- * Adds multiple items to the tree.
1486
- * @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).
1494
+ * Adds or updates multiple items to the tree.
1495
+ * @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).
1487
1496
  *
1488
- * @param keysNodesEntriesOrRaws - An iterable of items to add.
1497
+ * @param keysNodesEntriesOrRaws - An iterable of items to set or update.
1489
1498
  * @param [values] - An optional parallel iterable of values.
1490
- * @returns An array of booleans indicating the success of each individual `add` operation.
1499
+ * @returns An array of booleans indicating the success of each individual `set` operation.
1491
1500
  */
1492
- addMany(keysNodesEntriesOrRaws, values) {
1501
+ setMany(keysNodesEntriesOrRaws, values) {
1493
1502
  const inserted = [];
1494
1503
  let valuesIterator;
1495
1504
  if (values) {
@@ -1504,40 +1513,29 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1504
1513
  }
1505
1514
  }
1506
1515
  if (this.isRaw(keyNodeEntryOrRaw)) keyNodeEntryOrRaw = this._toEntryFn(keyNodeEntryOrRaw);
1507
- inserted.push(this.add(keyNodeEntryOrRaw, value));
1516
+ inserted.push(this.set(keyNodeEntryOrRaw, value));
1508
1517
  }
1509
1518
  return inserted;
1510
1519
  }
1511
1520
  /**
1512
- * Adds or updates multiple items to the tree.
1513
- * @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).
1514
- *
1515
- * @param keysNodesEntriesOrRaws - An iterable of items to add or update.
1516
- * @param [values] - An optional parallel iterable of values.
1517
- * @returns An array of booleans indicating the success of each individual `add` operation.
1518
- */
1519
- setMany(keysNodesEntriesOrRaws, values) {
1520
- return this.addMany(keysNodesEntriesOrRaws, values);
1521
- }
1522
- /**
1523
- * Merges another tree into this one by adding all its nodes.
1524
- * @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`).
1521
+ * Merges another tree into this one by seting all its nodes.
1522
+ * @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`).
1525
1523
  *
1526
1524
  * @param anotherTree - The tree to merge.
1527
1525
  */
1528
1526
  merge(anotherTree) {
1529
- this.addMany(anotherTree, []);
1527
+ this.setMany(anotherTree, []);
1530
1528
  }
1531
1529
  /**
1532
1530
  * Clears the tree and refills it with new items.
1533
- * @remarks Time O(N) (for `clear`) + O(N * M) (for `addMany`) = O(N * M). Space O(M) (from `addMany`).
1531
+ * @remarks Time O(N) (for `clear`) + O(N * M) (for `setMany`) = O(N * M). Space O(M) (from `setMany`).
1534
1532
  *
1535
- * @param keysNodesEntriesOrRaws - An iterable of items to add.
1533
+ * @param keysNodesEntriesOrRaws - An iterable of items to set.
1536
1534
  * @param [values] - An optional parallel iterable of values.
1537
1535
  */
1538
1536
  refill(keysNodesEntriesOrRaws, values) {
1539
1537
  this.clear();
1540
- this.addMany(keysNodesEntriesOrRaws, values);
1538
+ this.setMany(keysNodesEntriesOrRaws, values);
1541
1539
  }
1542
1540
  /**
1543
1541
  * Deletes a node from the tree.
@@ -2203,7 +2201,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2203
2201
  }
2204
2202
  /**
2205
2203
  * Clones the tree.
2206
- * @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.
2204
+ * @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.
2207
2205
  *
2208
2206
  * @returns A new, cloned instance of the tree.
2209
2207
  */
@@ -2214,7 +2212,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2214
2212
  }
2215
2213
  /**
2216
2214
  * Creates a new tree containing only the entries that satisfy the predicate.
2217
- * @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.
2215
+ * @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.
2218
2216
  *
2219
2217
  * @param predicate - A function to test each [key, value] pair.
2220
2218
  * @param [thisArg] - `this` context for the predicate.
@@ -2223,7 +2221,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2223
2221
  filter(predicate, thisArg) {
2224
2222
  const out = this._createInstance();
2225
2223
  let i = 0;
2226
- for (const [k, v] of this) if (predicate.call(thisArg, v, k, i++, this)) out.add([k, v]);
2224
+ for (const [k, v] of this) if (predicate.call(thisArg, v, k, i++, this)) out.set([k, v]);
2227
2225
  return out;
2228
2226
  }
2229
2227
  /**
@@ -2241,7 +2239,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2241
2239
  map(cb, options, thisArg) {
2242
2240
  const out = this._createLike([], options);
2243
2241
  let i = 0;
2244
- for (const [k, v] of this) out.add(cb.call(thisArg, v, k, i++, this));
2242
+ for (const [k, v] of this) out.set(cb.call(thisArg, v, k, i++, this));
2245
2243
  return out;
2246
2244
  }
2247
2245
  /**
@@ -2487,18 +2485,18 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2487
2485
  return [this.createNode(keyNodeOrEntry, value), value];
2488
2486
  }
2489
2487
  /**
2490
- * (Protected) Helper for cloning. Performs a BFS and adds all nodes to the new tree.
2491
- * @remarks Time O(N * M) (O(N) BFS + O(M) `add` for each node).
2488
+ * (Protected) Helper for cloning. Performs a BFS and sets all nodes to the new tree.
2489
+ * @remarks Time O(N * M) (O(N) BFS + O(M) `set` for each node).
2492
2490
  *
2493
2491
  * @param cloned - The new, empty tree instance to populate.
2494
2492
  */
2495
2493
  _clone(cloned) {
2496
2494
  this.bfs(
2497
2495
  (node) => {
2498
- if (node === null) cloned.add(null);
2496
+ if (node === null) cloned.set(null);
2499
2497
  else {
2500
- if (this._isMapMode) cloned.add([node.key, this._store.get(node.key)]);
2501
- else cloned.add([node.key, node.value]);
2498
+ if (this._isMapMode) cloned.set([node.key, this._store.get(node.key)]);
2499
+ else cloned.set([node.key, node.value]);
2502
2500
  }
2503
2501
  },
2504
2502
  this._root,
@@ -2829,7 +2827,7 @@ var _BST = class _BST extends BinaryTree {
2829
2827
  * Creates an instance of BST.
2830
2828
  * @remarks Time O(N log N) or O(N^2) depending on `isBalanceAdd` in `addMany` and input order. Space O(N).
2831
2829
  *
2832
- * @param [keysNodesEntriesOrRaws=[]] - An iterable of items to add.
2830
+ * @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
2833
2831
  * @param [options] - Configuration options for the BST, including comparator.
2834
2832
  */
2835
2833
  constructor(keysNodesEntriesOrRaws = [], options) {
@@ -2850,7 +2848,7 @@ var _BST = class _BST extends BinaryTree {
2850
2848
  } else {
2851
2849
  this._comparator = this._createDefaultComparator();
2852
2850
  }
2853
- if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
2851
+ if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
2854
2852
  }
2855
2853
  /**
2856
2854
  * Gets the root node of the tree.
@@ -3061,11 +3059,11 @@ var _BST = class _BST extends BinaryTree {
3061
3059
  * Adds a new node to the BST based on key comparison.
3062
3060
  * @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
3063
3061
  *
3064
- * @param keyNodeOrEntry - The key, node, or entry to add.
3062
+ * @param keyNodeOrEntry - The key, node, or entry to set.
3065
3063
  * @param [value] - The value, if providing just a key.
3066
3064
  * @returns True if the addition was successful, false otherwise.
3067
3065
  */
3068
- add(keyNodeOrEntry, value) {
3066
+ set(keyNodeOrEntry, value) {
3069
3067
  const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
3070
3068
  if (newNode === void 0) return false;
3071
3069
  if (this._root === void 0) {
@@ -3102,24 +3100,24 @@ var _BST = class _BST extends BinaryTree {
3102
3100
  }
3103
3101
  /**
3104
3102
  * Adds multiple items to the tree.
3105
- * @remarks If `isBalanceAdd` is true, sorts the input and builds a balanced tree. Time O(N log N) (due to sort and balanced add).
3103
+ * @remarks If `isBalanceAdd` is true, sorts the input and builds a balanced tree. Time O(N log N) (due to sort and balanced set).
3106
3104
  * If false, adds items one by one. Time O(N * H), which is O(N^2) worst-case.
3107
3105
  * Space O(N) for sorting and recursion/iteration stack.
3108
3106
  *
3109
- * @param keysNodesEntriesOrRaws - An iterable of items to add.
3107
+ * @param keysNodesEntriesOrRaws - An iterable of items to set.
3110
3108
  * @param [values] - An optional parallel iterable of values.
3111
3109
  * @param [isBalanceAdd=true] - If true, builds a balanced tree from the items.
3112
- * @param [iterationType=this.iterationType] - The traversal method for balanced add (recursive or iterative).
3113
- * @returns An array of booleans indicating the success of each individual `add` operation.
3110
+ * @param [iterationType=this.iterationType] - The traversal method for balanced set (recursive or iterative).
3111
+ * @returns An array of booleans indicating the success of each individual `set` operation.
3114
3112
  */
3115
- addMany(keysNodesEntriesOrRaws, values, isBalanceAdd = true, iterationType = this.iterationType) {
3113
+ setMany(keysNodesEntriesOrRaws, values, isBalanceAdd = true, iterationType = this.iterationType) {
3116
3114
  const inserted = [];
3117
3115
  const valuesIterator = values == null ? void 0 : values[Symbol.iterator]();
3118
3116
  if (!isBalanceAdd) {
3119
3117
  for (let kve of keysNodesEntriesOrRaws) {
3120
3118
  const val = valuesIterator == null ? void 0 : valuesIterator.next().value;
3121
3119
  if (this.isRaw(kve)) kve = this._toEntryFn(kve);
3122
- inserted.push(this.add(kve, val));
3120
+ inserted.push(this.set(kve, val));
3123
3121
  }
3124
3122
  return inserted;
3125
3123
  }
@@ -3147,9 +3145,9 @@ var _BST = class _BST extends BinaryTree {
3147
3145
  const { key, value, orgIndex } = arr[mid];
3148
3146
  if (this.isRaw(key)) {
3149
3147
  const entry = this._toEntryFn(key);
3150
- inserted[orgIndex] = this.add(entry);
3148
+ inserted[orgIndex] = this.set(entry);
3151
3149
  } else {
3152
- inserted[orgIndex] = this.add(key, value);
3150
+ inserted[orgIndex] = this.set(key, value);
3153
3151
  }
3154
3152
  _dfs(arr.slice(0, mid));
3155
3153
  _dfs(arr.slice(mid + 1));
@@ -3166,9 +3164,9 @@ var _BST = class _BST extends BinaryTree {
3166
3164
  const { key, value, orgIndex } = sorted[m];
3167
3165
  if (this.isRaw(key)) {
3168
3166
  const entry = this._toEntryFn(key);
3169
- inserted[orgIndex] = this.add(entry);
3167
+ inserted[orgIndex] = this.set(entry);
3170
3168
  } else {
3171
- inserted[orgIndex] = this.add(key, value);
3169
+ inserted[orgIndex] = this.set(key, value);
3172
3170
  }
3173
3171
  stack.push([m + 1, r]);
3174
3172
  stack.push([l, m - 1]);
@@ -3443,7 +3441,7 @@ var _BST = class _BST extends BinaryTree {
3443
3441
  const out = this._createLike([], options);
3444
3442
  let index = 0;
3445
3443
  for (const [key, value] of this) {
3446
- out.add(callback.call(thisArg, value, key, index++, this));
3444
+ out.set(callback.call(thisArg, value, key, index++, this));
3447
3445
  }
3448
3446
  return out;
3449
3447
  }
@@ -3499,7 +3497,6 @@ var _BST = class _BST extends BinaryTree {
3499
3497
  */
3500
3498
  _createDefaultComparator() {
3501
3499
  return (a, b) => {
3502
- debugger;
3503
3500
  if (isComparable(a) && isComparable(b)) {
3504
3501
  if (a > b) return 1;
3505
3502
  if (a < b) return -1;