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