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
|
@@ -1188,9 +1188,9 @@ var BinaryTreeNode = _BinaryTreeNode;
|
|
|
1188
1188
|
var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
1189
1189
|
/**
|
|
1190
1190
|
* Creates an instance of BinaryTree.
|
|
1191
|
-
* @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) `
|
|
1191
|
+
* @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.
|
|
1192
1192
|
*
|
|
1193
|
-
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to
|
|
1193
|
+
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
|
|
1194
1194
|
* @param [options] - Configuration options for the tree.
|
|
1195
1195
|
*/
|
|
1196
1196
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
@@ -1219,7 +1219,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1219
1219
|
if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
|
|
1220
1220
|
else if (toEntryFn) throw TypeError("toEntryFn must be a function type");
|
|
1221
1221
|
}
|
|
1222
|
-
if (keysNodesEntriesOrRaws) this.
|
|
1222
|
+
if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
|
|
1223
1223
|
}
|
|
1224
1224
|
/**
|
|
1225
1225
|
* Gets whether the tree is in Map mode.
|
|
@@ -1426,10 +1426,20 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1426
1426
|
* @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).
|
|
1427
1427
|
*
|
|
1428
1428
|
* @param keyNodeOrEntry - The key, node, or entry to add.
|
|
1429
|
+
* @returns True if the addition was successful, false otherwise.
|
|
1430
|
+
*/
|
|
1431
|
+
add(keyNodeOrEntry) {
|
|
1432
|
+
return this.set(keyNodeOrEntry);
|
|
1433
|
+
}
|
|
1434
|
+
/**
|
|
1435
|
+
* Adds or updates a new node to the tree.
|
|
1436
|
+
* @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).
|
|
1437
|
+
*
|
|
1438
|
+
* @param keyNodeOrEntry - The key, node, or entry to set or update.
|
|
1429
1439
|
* @param [value] - The value, if providing just a key.
|
|
1430
1440
|
* @returns True if the addition was successful, false otherwise.
|
|
1431
1441
|
*/
|
|
1432
|
-
|
|
1442
|
+
set(keyNodeOrEntry, value) {
|
|
1433
1443
|
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
1434
1444
|
if (newNode === void 0) return false;
|
|
1435
1445
|
if (!this._root) {
|
|
@@ -1473,25 +1483,25 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1473
1483
|
return false;
|
|
1474
1484
|
}
|
|
1475
1485
|
/**
|
|
1476
|
-
* Adds
|
|
1477
|
-
* @remarks Time O(
|
|
1486
|
+
* Adds multiple items to the tree.
|
|
1487
|
+
* @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).
|
|
1478
1488
|
*
|
|
1479
|
-
* @param
|
|
1480
|
-
* @param [
|
|
1481
|
-
* @returns
|
|
1489
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set.
|
|
1490
|
+
* @param [values] - An optional parallel iterable of values.
|
|
1491
|
+
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
1482
1492
|
*/
|
|
1483
|
-
|
|
1484
|
-
return this.
|
|
1493
|
+
addMany(keysNodesEntriesOrRaws) {
|
|
1494
|
+
return this.setMany(keysNodesEntriesOrRaws);
|
|
1485
1495
|
}
|
|
1486
1496
|
/**
|
|
1487
|
-
* Adds multiple items to the tree.
|
|
1488
|
-
* @remarks Time O(N * M), where N is the number of items to
|
|
1497
|
+
* Adds or updates multiple items to the tree.
|
|
1498
|
+
* @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).
|
|
1489
1499
|
*
|
|
1490
|
-
* @param keysNodesEntriesOrRaws - An iterable of items to
|
|
1500
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set or update.
|
|
1491
1501
|
* @param [values] - An optional parallel iterable of values.
|
|
1492
|
-
* @returns An array of booleans indicating the success of each individual `
|
|
1502
|
+
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
1493
1503
|
*/
|
|
1494
|
-
|
|
1504
|
+
setMany(keysNodesEntriesOrRaws, values) {
|
|
1495
1505
|
const inserted = [];
|
|
1496
1506
|
let valuesIterator;
|
|
1497
1507
|
if (values) {
|
|
@@ -1506,40 +1516,29 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1506
1516
|
}
|
|
1507
1517
|
}
|
|
1508
1518
|
if (this.isRaw(keyNodeEntryOrRaw)) keyNodeEntryOrRaw = this._toEntryFn(keyNodeEntryOrRaw);
|
|
1509
|
-
inserted.push(this.
|
|
1519
|
+
inserted.push(this.set(keyNodeEntryOrRaw, value));
|
|
1510
1520
|
}
|
|
1511
1521
|
return inserted;
|
|
1512
1522
|
}
|
|
1513
1523
|
/**
|
|
1514
|
-
*
|
|
1515
|
-
* @remarks Time O(N * M), where N is the
|
|
1516
|
-
*
|
|
1517
|
-
* @param keysNodesEntriesOrRaws - An iterable of items to add or update.
|
|
1518
|
-
* @param [values] - An optional parallel iterable of values.
|
|
1519
|
-
* @returns An array of booleans indicating the success of each individual `add` operation.
|
|
1520
|
-
*/
|
|
1521
|
-
setMany(keysNodesEntriesOrRaws, values) {
|
|
1522
|
-
return this.addMany(keysNodesEntriesOrRaws, values);
|
|
1523
|
-
}
|
|
1524
|
-
/**
|
|
1525
|
-
* Merges another tree into this one by adding all its nodes.
|
|
1526
|
-
* @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`).
|
|
1524
|
+
* Merges another tree into this one by seting all its nodes.
|
|
1525
|
+
* @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`).
|
|
1527
1526
|
*
|
|
1528
1527
|
* @param anotherTree - The tree to merge.
|
|
1529
1528
|
*/
|
|
1530
1529
|
merge(anotherTree) {
|
|
1531
|
-
this.
|
|
1530
|
+
this.setMany(anotherTree, []);
|
|
1532
1531
|
}
|
|
1533
1532
|
/**
|
|
1534
1533
|
* Clears the tree and refills it with new items.
|
|
1535
|
-
* @remarks Time O(N) (for `clear`) + O(N * M) (for `
|
|
1534
|
+
* @remarks Time O(N) (for `clear`) + O(N * M) (for `setMany`) = O(N * M). Space O(M) (from `setMany`).
|
|
1536
1535
|
*
|
|
1537
|
-
* @param keysNodesEntriesOrRaws - An iterable of items to
|
|
1536
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set.
|
|
1538
1537
|
* @param [values] - An optional parallel iterable of values.
|
|
1539
1538
|
*/
|
|
1540
1539
|
refill(keysNodesEntriesOrRaws, values) {
|
|
1541
1540
|
this.clear();
|
|
1542
|
-
this.
|
|
1541
|
+
this.setMany(keysNodesEntriesOrRaws, values);
|
|
1543
1542
|
}
|
|
1544
1543
|
/**
|
|
1545
1544
|
* Deletes a node from the tree.
|
|
@@ -2205,7 +2204,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2205
2204
|
}
|
|
2206
2205
|
/**
|
|
2207
2206
|
* Clones the tree.
|
|
2208
|
-
* @remarks Time O(N * M), where N is the number of nodes and M is the tree size during insertion (due to `bfs` + `
|
|
2207
|
+
* @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.
|
|
2209
2208
|
*
|
|
2210
2209
|
* @returns A new, cloned instance of the tree.
|
|
2211
2210
|
*/
|
|
@@ -2216,7 +2215,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2216
2215
|
}
|
|
2217
2216
|
/**
|
|
2218
2217
|
* Creates a new tree containing only the entries that satisfy the predicate.
|
|
2219
|
-
* @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) `
|
|
2218
|
+
* @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.
|
|
2220
2219
|
*
|
|
2221
2220
|
* @param predicate - A function to test each [key, value] pair.
|
|
2222
2221
|
* @param [thisArg] - `this` context for the predicate.
|
|
@@ -2225,7 +2224,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2225
2224
|
filter(predicate, thisArg) {
|
|
2226
2225
|
const out = this._createInstance();
|
|
2227
2226
|
let i = 0;
|
|
2228
|
-
for (const [k, v] of this) if (predicate.call(thisArg, v, k, i++, this)) out.
|
|
2227
|
+
for (const [k, v] of this) if (predicate.call(thisArg, v, k, i++, this)) out.set([k, v]);
|
|
2229
2228
|
return out;
|
|
2230
2229
|
}
|
|
2231
2230
|
/**
|
|
@@ -2243,7 +2242,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2243
2242
|
map(cb, options, thisArg) {
|
|
2244
2243
|
const out = this._createLike([], options);
|
|
2245
2244
|
let i = 0;
|
|
2246
|
-
for (const [k, v] of this) out.
|
|
2245
|
+
for (const [k, v] of this) out.set(cb.call(thisArg, v, k, i++, this));
|
|
2247
2246
|
return out;
|
|
2248
2247
|
}
|
|
2249
2248
|
/**
|
|
@@ -2489,18 +2488,18 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2489
2488
|
return [this.createNode(keyNodeOrEntry, value), value];
|
|
2490
2489
|
}
|
|
2491
2490
|
/**
|
|
2492
|
-
* (Protected) Helper for cloning. Performs a BFS and
|
|
2493
|
-
* @remarks Time O(N * M) (O(N) BFS + O(M) `
|
|
2491
|
+
* (Protected) Helper for cloning. Performs a BFS and sets all nodes to the new tree.
|
|
2492
|
+
* @remarks Time O(N * M) (O(N) BFS + O(M) `set` for each node).
|
|
2494
2493
|
*
|
|
2495
2494
|
* @param cloned - The new, empty tree instance to populate.
|
|
2496
2495
|
*/
|
|
2497
2496
|
_clone(cloned) {
|
|
2498
2497
|
this.bfs(
|
|
2499
2498
|
(node) => {
|
|
2500
|
-
if (node === null) cloned.
|
|
2499
|
+
if (node === null) cloned.set(null);
|
|
2501
2500
|
else {
|
|
2502
|
-
if (this._isMapMode) cloned.
|
|
2503
|
-
else cloned.
|
|
2501
|
+
if (this._isMapMode) cloned.set([node.key, this._store.get(node.key)]);
|
|
2502
|
+
else cloned.set([node.key, node.value]);
|
|
2504
2503
|
}
|
|
2505
2504
|
},
|
|
2506
2505
|
this._root,
|
|
@@ -2831,7 +2830,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2831
2830
|
* Creates an instance of BST.
|
|
2832
2831
|
* @remarks Time O(N log N) or O(N^2) depending on `isBalanceAdd` in `addMany` and input order. Space O(N).
|
|
2833
2832
|
*
|
|
2834
|
-
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to
|
|
2833
|
+
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
|
|
2835
2834
|
* @param [options] - Configuration options for the BST, including comparator.
|
|
2836
2835
|
*/
|
|
2837
2836
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
@@ -2852,7 +2851,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2852
2851
|
} else {
|
|
2853
2852
|
this._comparator = this._createDefaultComparator();
|
|
2854
2853
|
}
|
|
2855
|
-
if (keysNodesEntriesOrRaws) this.
|
|
2854
|
+
if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
|
|
2856
2855
|
}
|
|
2857
2856
|
/**
|
|
2858
2857
|
* Gets the root node of the tree.
|
|
@@ -3063,11 +3062,11 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3063
3062
|
* Adds a new node to the BST based on key comparison.
|
|
3064
3063
|
* @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
|
|
3065
3064
|
*
|
|
3066
|
-
* @param keyNodeOrEntry - The key, node, or entry to
|
|
3065
|
+
* @param keyNodeOrEntry - The key, node, or entry to set.
|
|
3067
3066
|
* @param [value] - The value, if providing just a key.
|
|
3068
3067
|
* @returns True if the addition was successful, false otherwise.
|
|
3069
3068
|
*/
|
|
3070
|
-
|
|
3069
|
+
set(keyNodeOrEntry, value) {
|
|
3071
3070
|
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
3072
3071
|
if (newNode === void 0) return false;
|
|
3073
3072
|
if (this._root === void 0) {
|
|
@@ -3104,24 +3103,24 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3104
3103
|
}
|
|
3105
3104
|
/**
|
|
3106
3105
|
* Adds multiple items to the tree.
|
|
3107
|
-
* @remarks If `isBalanceAdd` is true, sorts the input and builds a balanced tree. Time O(N log N) (due to sort and balanced
|
|
3106
|
+
* @remarks If `isBalanceAdd` is true, sorts the input and builds a balanced tree. Time O(N log N) (due to sort and balanced set).
|
|
3108
3107
|
* If false, adds items one by one. Time O(N * H), which is O(N^2) worst-case.
|
|
3109
3108
|
* Space O(N) for sorting and recursion/iteration stack.
|
|
3110
3109
|
*
|
|
3111
|
-
* @param keysNodesEntriesOrRaws - An iterable of items to
|
|
3110
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set.
|
|
3112
3111
|
* @param [values] - An optional parallel iterable of values.
|
|
3113
3112
|
* @param [isBalanceAdd=true] - If true, builds a balanced tree from the items.
|
|
3114
|
-
* @param [iterationType=this.iterationType] - The traversal method for balanced
|
|
3115
|
-
* @returns An array of booleans indicating the success of each individual `
|
|
3113
|
+
* @param [iterationType=this.iterationType] - The traversal method for balanced set (recursive or iterative).
|
|
3114
|
+
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
3116
3115
|
*/
|
|
3117
|
-
|
|
3116
|
+
setMany(keysNodesEntriesOrRaws, values, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
3118
3117
|
const inserted = [];
|
|
3119
3118
|
const valuesIterator = values == null ? void 0 : values[Symbol.iterator]();
|
|
3120
3119
|
if (!isBalanceAdd) {
|
|
3121
3120
|
for (let kve of keysNodesEntriesOrRaws) {
|
|
3122
3121
|
const val = valuesIterator == null ? void 0 : valuesIterator.next().value;
|
|
3123
3122
|
if (this.isRaw(kve)) kve = this._toEntryFn(kve);
|
|
3124
|
-
inserted.push(this.
|
|
3123
|
+
inserted.push(this.set(kve, val));
|
|
3125
3124
|
}
|
|
3126
3125
|
return inserted;
|
|
3127
3126
|
}
|
|
@@ -3149,9 +3148,9 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3149
3148
|
const { key, value, orgIndex } = arr[mid];
|
|
3150
3149
|
if (this.isRaw(key)) {
|
|
3151
3150
|
const entry = this._toEntryFn(key);
|
|
3152
|
-
inserted[orgIndex] = this.
|
|
3151
|
+
inserted[orgIndex] = this.set(entry);
|
|
3153
3152
|
} else {
|
|
3154
|
-
inserted[orgIndex] = this.
|
|
3153
|
+
inserted[orgIndex] = this.set(key, value);
|
|
3155
3154
|
}
|
|
3156
3155
|
_dfs(arr.slice(0, mid));
|
|
3157
3156
|
_dfs(arr.slice(mid + 1));
|
|
@@ -3168,9 +3167,9 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3168
3167
|
const { key, value, orgIndex } = sorted[m];
|
|
3169
3168
|
if (this.isRaw(key)) {
|
|
3170
3169
|
const entry = this._toEntryFn(key);
|
|
3171
|
-
inserted[orgIndex] = this.
|
|
3170
|
+
inserted[orgIndex] = this.set(entry);
|
|
3172
3171
|
} else {
|
|
3173
|
-
inserted[orgIndex] = this.
|
|
3172
|
+
inserted[orgIndex] = this.set(key, value);
|
|
3174
3173
|
}
|
|
3175
3174
|
stack.push([m + 1, r]);
|
|
3176
3175
|
stack.push([l, m - 1]);
|
|
@@ -3445,7 +3444,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3445
3444
|
const out = this._createLike([], options);
|
|
3446
3445
|
let index = 0;
|
|
3447
3446
|
for (const [key, value] of this) {
|
|
3448
|
-
out.
|
|
3447
|
+
out.set(callback.call(thisArg, value, key, index++, this));
|
|
3449
3448
|
}
|
|
3450
3449
|
return out;
|
|
3451
3450
|
}
|
|
@@ -3501,7 +3500,6 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3501
3500
|
*/
|
|
3502
3501
|
_createDefaultComparator() {
|
|
3503
3502
|
return (a, b) => {
|
|
3504
|
-
debugger;
|
|
3505
3503
|
if (isComparable(a) && isComparable(b)) {
|
|
3506
3504
|
if (a > b) return 1;
|
|
3507
3505
|
if (a < b) return -1;
|