bst-typed 2.2.7 → 2.2.8

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 +3 -3
  2. package/dist/cjs/index.cjs +57 -59
  3. package/dist/cjs/index.cjs.map +1 -1
  4. package/dist/cjs-legacy/index.cjs +57 -59
  5. package/dist/cjs-legacy/index.cjs.map +1 -1
  6. package/dist/esm/index.mjs +57 -59
  7. package/dist/esm/index.mjs.map +1 -1
  8. package/dist/esm-legacy/index.mjs +57 -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 -23
  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 +57 -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 +53 -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
package/README.md CHANGED
@@ -365,8 +365,8 @@ bfsNodes[2].id; // 16
365
365
  console.log(bst.size); // 16;
366
366
 
367
367
  // Add new elements
368
- bst.add(17);
369
- bst.add(0);
368
+ bst.set(17);
369
+ bst.set(0);
370
370
  console.log(bst.size); // 18;
371
371
 
372
372
  // Verify keys are searchable
@@ -416,7 +416,7 @@ bfsNodes[2].id; // 16
416
416
 
417
417
  // Merge datasets into a single BinarySearchTree
418
418
  const merged = new BST<number, string>(dataset1);
419
- merged.addMany(dataset2);
419
+ merged.setMany(dataset2);
420
420
  merged.merge(dataset3);
421
421
 
422
422
  // Verify merged dataset is in sorted order
@@ -1194,9 +1194,9 @@ var BinaryTree = class extends IterableEntryBase {
1194
1194
  iterationType = "ITERATIVE";
1195
1195
  /**
1196
1196
  * Creates an instance of BinaryTree.
1197
- * @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.
1197
+ * @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.
1198
1198
  *
1199
- * @param [keysNodesEntriesOrRaws=[]] - An iterable of items to add.
1199
+ * @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
1200
1200
  * @param [options] - Configuration options for the tree.
1201
1201
  */
1202
1202
  constructor(keysNodesEntriesOrRaws = [], options) {
@@ -1209,7 +1209,7 @@ var BinaryTree = class extends IterableEntryBase {
1209
1209
  if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
1210
1210
  else if (toEntryFn) throw TypeError("toEntryFn must be a function type");
1211
1211
  }
1212
- if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
1212
+ if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
1213
1213
  }
1214
1214
  _isMapMode = true;
1215
1215
  /**
@@ -1423,10 +1423,20 @@ var BinaryTree = class extends IterableEntryBase {
1423
1423
  * @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).
1424
1424
  *
1425
1425
  * @param keyNodeOrEntry - The key, node, or entry to add.
1426
+ * @returns True if the addition was successful, false otherwise.
1427
+ */
1428
+ add(keyNodeOrEntry) {
1429
+ return this.set(keyNodeOrEntry);
1430
+ }
1431
+ /**
1432
+ * Adds or updates a new node to the tree.
1433
+ * @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).
1434
+ *
1435
+ * @param keyNodeOrEntry - The key, node, or entry to set or update.
1426
1436
  * @param [value] - The value, if providing just a key.
1427
1437
  * @returns True if the addition was successful, false otherwise.
1428
1438
  */
1429
- add(keyNodeOrEntry, value) {
1439
+ set(keyNodeOrEntry, value) {
1430
1440
  const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
1431
1441
  if (newNode === void 0) return false;
1432
1442
  if (!this._root) {
@@ -1470,25 +1480,25 @@ var BinaryTree = class extends IterableEntryBase {
1470
1480
  return false;
1471
1481
  }
1472
1482
  /**
1473
- * Adds or updates a new node to the tree.
1474
- * @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).
1483
+ * Adds multiple items to the tree.
1484
+ * @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).
1475
1485
  *
1476
- * @param keyNodeOrEntry - The key, node, or entry to add or update.
1477
- * @param [value] - The value, if providing just a key.
1478
- * @returns True if the addition was successful, false otherwise.
1486
+ * @param keysNodesEntriesOrRaws - An iterable of items to set.
1487
+ * @param [values] - An optional parallel iterable of values.
1488
+ * @returns An array of booleans indicating the success of each individual `set` operation.
1479
1489
  */
1480
- set(keyNodeOrEntry, value) {
1481
- return this.add(keyNodeOrEntry, value);
1490
+ addMany(keysNodesEntriesOrRaws) {
1491
+ return this.setMany(keysNodesEntriesOrRaws);
1482
1492
  }
1483
1493
  /**
1484
- * Adds multiple items to the tree.
1485
- * @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).
1486
1496
  *
1487
- * @param keysNodesEntriesOrRaws - An iterable of items to add.
1497
+ * @param keysNodesEntriesOrRaws - An iterable of items to set or update.
1488
1498
  * @param [values] - An optional parallel iterable of values.
1489
- * @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.
1490
1500
  */
1491
- addMany(keysNodesEntriesOrRaws, values) {
1501
+ setMany(keysNodesEntriesOrRaws, values) {
1492
1502
  const inserted = [];
1493
1503
  let valuesIterator;
1494
1504
  if (values) {
@@ -1503,40 +1513,29 @@ var BinaryTree = class extends IterableEntryBase {
1503
1513
  }
1504
1514
  }
1505
1515
  if (this.isRaw(keyNodeEntryOrRaw)) keyNodeEntryOrRaw = this._toEntryFn(keyNodeEntryOrRaw);
1506
- inserted.push(this.add(keyNodeEntryOrRaw, value));
1516
+ inserted.push(this.set(keyNodeEntryOrRaw, value));
1507
1517
  }
1508
1518
  return inserted;
1509
1519
  }
1510
1520
  /**
1511
- * Adds or updates multiple items to the tree.
1512
- * @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).
1513
- *
1514
- * @param keysNodesEntriesOrRaws - An iterable of items to add or update.
1515
- * @param [values] - An optional parallel iterable of values.
1516
- * @returns An array of booleans indicating the success of each individual `add` operation.
1517
- */
1518
- setMany(keysNodesEntriesOrRaws, values) {
1519
- return this.addMany(keysNodesEntriesOrRaws, values);
1520
- }
1521
- /**
1522
- * Merges another tree into this one by adding all its nodes.
1523
- * @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`).
1524
1523
  *
1525
1524
  * @param anotherTree - The tree to merge.
1526
1525
  */
1527
1526
  merge(anotherTree) {
1528
- this.addMany(anotherTree, []);
1527
+ this.setMany(anotherTree, []);
1529
1528
  }
1530
1529
  /**
1531
1530
  * Clears the tree and refills it with new items.
1532
- * @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`).
1533
1532
  *
1534
- * @param keysNodesEntriesOrRaws - An iterable of items to add.
1533
+ * @param keysNodesEntriesOrRaws - An iterable of items to set.
1535
1534
  * @param [values] - An optional parallel iterable of values.
1536
1535
  */
1537
1536
  refill(keysNodesEntriesOrRaws, values) {
1538
1537
  this.clear();
1539
- this.addMany(keysNodesEntriesOrRaws, values);
1538
+ this.setMany(keysNodesEntriesOrRaws, values);
1540
1539
  }
1541
1540
  /**
1542
1541
  * Deletes a node from the tree.
@@ -2201,7 +2200,7 @@ var BinaryTree = class extends IterableEntryBase {
2201
2200
  }
2202
2201
  /**
2203
2202
  * Clones the tree.
2204
- * @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.
2203
+ * @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.
2205
2204
  *
2206
2205
  * @returns A new, cloned instance of the tree.
2207
2206
  */
@@ -2212,7 +2211,7 @@ var BinaryTree = class extends IterableEntryBase {
2212
2211
  }
2213
2212
  /**
2214
2213
  * Creates a new tree containing only the entries that satisfy the predicate.
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) `add` for each item). Space O(N) for the new tree.
2214
+ * @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.
2216
2215
  *
2217
2216
  * @param predicate - A function to test each [key, value] pair.
2218
2217
  * @param [thisArg] - `this` context for the predicate.
@@ -2221,7 +2220,7 @@ var BinaryTree = class extends IterableEntryBase {
2221
2220
  filter(predicate, thisArg) {
2222
2221
  const out = this._createInstance();
2223
2222
  let i = 0;
2224
- for (const [k, v] of this) if (predicate.call(thisArg, v, k, i++, this)) out.add([k, v]);
2223
+ for (const [k, v] of this) if (predicate.call(thisArg, v, k, i++, this)) out.set([k, v]);
2225
2224
  return out;
2226
2225
  }
2227
2226
  /**
@@ -2239,7 +2238,7 @@ var BinaryTree = class extends IterableEntryBase {
2239
2238
  map(cb, options, thisArg) {
2240
2239
  const out = this._createLike([], options);
2241
2240
  let i = 0;
2242
- for (const [k, v] of this) out.add(cb.call(thisArg, v, k, i++, this));
2241
+ for (const [k, v] of this) out.set(cb.call(thisArg, v, k, i++, this));
2243
2242
  return out;
2244
2243
  }
2245
2244
  /**
@@ -2491,18 +2490,18 @@ var BinaryTree = class extends IterableEntryBase {
2491
2490
  return [this.createNode(keyNodeOrEntry, value), value];
2492
2491
  }
2493
2492
  /**
2494
- * (Protected) Helper for cloning. Performs a BFS and adds all nodes to the new tree.
2495
- * @remarks Time O(N * M) (O(N) BFS + O(M) `add` for each node).
2493
+ * (Protected) Helper for cloning. Performs a BFS and sets all nodes to the new tree.
2494
+ * @remarks Time O(N * M) (O(N) BFS + O(M) `set` for each node).
2496
2495
  *
2497
2496
  * @param cloned - The new, empty tree instance to populate.
2498
2497
  */
2499
2498
  _clone(cloned) {
2500
2499
  this.bfs(
2501
2500
  (node) => {
2502
- if (node === null) cloned.add(null);
2501
+ if (node === null) cloned.set(null);
2503
2502
  else {
2504
- if (this._isMapMode) cloned.add([node.key, this._store.get(node.key)]);
2505
- else cloned.add([node.key, node.value]);
2503
+ if (this._isMapMode) cloned.set([node.key, this._store.get(node.key)]);
2504
+ else cloned.set([node.key, node.value]);
2506
2505
  }
2507
2506
  },
2508
2507
  this._root,
@@ -2835,7 +2834,7 @@ var BST = class extends BinaryTree {
2835
2834
  * Creates an instance of BST.
2836
2835
  * @remarks Time O(N log N) or O(N^2) depending on `isBalanceAdd` in `addMany` and input order. Space O(N).
2837
2836
  *
2838
- * @param [keysNodesEntriesOrRaws=[]] - An iterable of items to add.
2837
+ * @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
2839
2838
  * @param [options] - Configuration options for the BST, including comparator.
2840
2839
  */
2841
2840
  constructor(keysNodesEntriesOrRaws = [], options) {
@@ -2849,7 +2848,7 @@ var BST = class extends BinaryTree {
2849
2848
  } else {
2850
2849
  this._comparator = this._createDefaultComparator();
2851
2850
  }
2852
- if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
2851
+ if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
2853
2852
  }
2854
2853
  _root = void 0;
2855
2854
  /**
@@ -3065,11 +3064,11 @@ var BST = class extends BinaryTree {
3065
3064
  * Adds a new node to the BST based on key comparison.
3066
3065
  * @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
3067
3066
  *
3068
- * @param keyNodeOrEntry - The key, node, or entry to add.
3067
+ * @param keyNodeOrEntry - The key, node, or entry to set.
3069
3068
  * @param [value] - The value, if providing just a key.
3070
3069
  * @returns True if the addition was successful, false otherwise.
3071
3070
  */
3072
- add(keyNodeOrEntry, value) {
3071
+ set(keyNodeOrEntry, value) {
3073
3072
  const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
3074
3073
  if (newNode === void 0) return false;
3075
3074
  if (this._root === void 0) {
@@ -3106,24 +3105,24 @@ var BST = class extends BinaryTree {
3106
3105
  }
3107
3106
  /**
3108
3107
  * Adds multiple items to the tree.
3109
- * @remarks If `isBalanceAdd` is true, sorts the input and builds a balanced tree. Time O(N log N) (due to sort and balanced add).
3108
+ * @remarks If `isBalanceAdd` is true, sorts the input and builds a balanced tree. Time O(N log N) (due to sort and balanced set).
3110
3109
  * If false, adds items one by one. Time O(N * H), which is O(N^2) worst-case.
3111
3110
  * Space O(N) for sorting and recursion/iteration stack.
3112
3111
  *
3113
- * @param keysNodesEntriesOrRaws - An iterable of items to add.
3112
+ * @param keysNodesEntriesOrRaws - An iterable of items to set.
3114
3113
  * @param [values] - An optional parallel iterable of values.
3115
3114
  * @param [isBalanceAdd=true] - If true, builds a balanced tree from the items.
3116
- * @param [iterationType=this.iterationType] - The traversal method for balanced add (recursive or iterative).
3117
- * @returns An array of booleans indicating the success of each individual `add` operation.
3115
+ * @param [iterationType=this.iterationType] - The traversal method for balanced set (recursive or iterative).
3116
+ * @returns An array of booleans indicating the success of each individual `set` operation.
3118
3117
  */
3119
- addMany(keysNodesEntriesOrRaws, values, isBalanceAdd = true, iterationType = this.iterationType) {
3118
+ setMany(keysNodesEntriesOrRaws, values, isBalanceAdd = true, iterationType = this.iterationType) {
3120
3119
  const inserted = [];
3121
3120
  const valuesIterator = values?.[Symbol.iterator]();
3122
3121
  if (!isBalanceAdd) {
3123
3122
  for (let kve of keysNodesEntriesOrRaws) {
3124
3123
  const val = valuesIterator?.next().value;
3125
3124
  if (this.isRaw(kve)) kve = this._toEntryFn(kve);
3126
- inserted.push(this.add(kve, val));
3125
+ inserted.push(this.set(kve, val));
3127
3126
  }
3128
3127
  return inserted;
3129
3128
  }
@@ -3151,9 +3150,9 @@ var BST = class extends BinaryTree {
3151
3150
  const { key, value, orgIndex } = arr[mid];
3152
3151
  if (this.isRaw(key)) {
3153
3152
  const entry = this._toEntryFn(key);
3154
- inserted[orgIndex] = this.add(entry);
3153
+ inserted[orgIndex] = this.set(entry);
3155
3154
  } else {
3156
- inserted[orgIndex] = this.add(key, value);
3155
+ inserted[orgIndex] = this.set(key, value);
3157
3156
  }
3158
3157
  _dfs(arr.slice(0, mid));
3159
3158
  _dfs(arr.slice(mid + 1));
@@ -3170,9 +3169,9 @@ var BST = class extends BinaryTree {
3170
3169
  const { key, value, orgIndex } = sorted[m];
3171
3170
  if (this.isRaw(key)) {
3172
3171
  const entry = this._toEntryFn(key);
3173
- inserted[orgIndex] = this.add(entry);
3172
+ inserted[orgIndex] = this.set(entry);
3174
3173
  } else {
3175
- inserted[orgIndex] = this.add(key, value);
3174
+ inserted[orgIndex] = this.set(key, value);
3176
3175
  }
3177
3176
  stack.push([m + 1, r]);
3178
3177
  stack.push([l, m - 1]);
@@ -3447,7 +3446,7 @@ var BST = class extends BinaryTree {
3447
3446
  const out = this._createLike([], options);
3448
3447
  let index = 0;
3449
3448
  for (const [key, value] of this) {
3450
- out.add(callback.call(thisArg, value, key, index++, this));
3449
+ out.set(callback.call(thisArg, value, key, index++, this));
3451
3450
  }
3452
3451
  return out;
3453
3452
  }
@@ -3503,7 +3502,6 @@ var BST = class extends BinaryTree {
3503
3502
  */
3504
3503
  _createDefaultComparator() {
3505
3504
  return (a, b) => {
3506
- debugger;
3507
3505
  if (isComparable(a) && isComparable(b)) {
3508
3506
  if (a > b) return 1;
3509
3507
  if (a < b) return -1;