avl-tree-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/dist/cjs/index.cjs +67 -69
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +67 -69
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +67 -69
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +67 -69
- 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/avl-tree-typed.js +67 -69
- package/dist/umd/avl-tree-typed.js.map +1 -1
- package/dist/umd/avl-tree-typed.min.js +3 -3
- package/dist/umd/avl-tree-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
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "avl-tree-typed",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.8",
|
|
4
4
|
"description": "Standard AVL tree",
|
|
5
5
|
"browser": "dist/umd/avl-tree-typed.min.js",
|
|
6
6
|
"umd:main": "dist/umd/avl-tree-typed.min.js",
|
|
@@ -201,6 +201,6 @@
|
|
|
201
201
|
"typescript": "^5.9.3"
|
|
202
202
|
},
|
|
203
203
|
"dependencies": {
|
|
204
|
-
"data-structure-typed": "^2.2.
|
|
204
|
+
"data-structure-typed": "^2.2.8"
|
|
205
205
|
}
|
|
206
206
|
}
|
|
@@ -200,7 +200,7 @@ export class AVLTreeCounter<K = any, V = any, R = any> extends AVLTree<K, V, R>
|
|
|
200
200
|
options?: AVLTreeCounterOptions<K, V, R>
|
|
201
201
|
) {
|
|
202
202
|
super([], options);
|
|
203
|
-
if (keysNodesEntriesOrRaws) this.
|
|
203
|
+
if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
|
|
204
204
|
}
|
|
205
205
|
|
|
206
206
|
protected _count = 0;
|
|
@@ -243,7 +243,7 @@ export class AVLTreeCounter<K = any, V = any, R = any> extends AVLTree<K, V, R>
|
|
|
243
243
|
* @param [count] - How much to increase the node's count (default 1).
|
|
244
244
|
* @returns True if inserted/updated; false if ignored.
|
|
245
245
|
*/
|
|
246
|
-
override
|
|
246
|
+
override set(
|
|
247
247
|
keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
|
|
248
248
|
value?: V,
|
|
249
249
|
count = 1
|
|
@@ -252,7 +252,7 @@ export class AVLTreeCounter<K = any, V = any, R = any> extends AVLTree<K, V, R>
|
|
|
252
252
|
if (newNode === undefined) return false;
|
|
253
253
|
|
|
254
254
|
const orgNodeCount = newNode?.count || 0;
|
|
255
|
-
const inserted = super.
|
|
255
|
+
const inserted = super.set(newNode, newValue);
|
|
256
256
|
if (inserted) {
|
|
257
257
|
this._count += orgNodeCount;
|
|
258
258
|
}
|
|
@@ -380,9 +380,9 @@ export class AVLTreeCounter<K = any, V = any, R = any> extends AVLTree<K, V, R>
|
|
|
380
380
|
const out = this._createInstance();
|
|
381
381
|
|
|
382
382
|
if (this._isMapMode) {
|
|
383
|
-
this.bfs(node => out.
|
|
383
|
+
this.bfs(node => out.set(node.key, undefined, node.count));
|
|
384
384
|
} else {
|
|
385
|
-
this.bfs(node => out.
|
|
385
|
+
this.bfs(node => out.set(node.key, node.value, node.count));
|
|
386
386
|
}
|
|
387
387
|
|
|
388
388
|
if (this._isMapMode) out._store = this._store;
|
|
@@ -410,7 +410,7 @@ export class AVLTreeCounter<K = any, V = any, R = any> extends AVLTree<K, V, R>
|
|
|
410
410
|
|
|
411
411
|
let index = 0;
|
|
412
412
|
for (const [key, value] of this) {
|
|
413
|
-
out.
|
|
413
|
+
out.set(callback.call(thisArg, value, key, index++, this));
|
|
414
414
|
}
|
|
415
415
|
return out;
|
|
416
416
|
}
|
|
@@ -199,7 +199,7 @@ export class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<K, V[],
|
|
|
199
199
|
) {
|
|
200
200
|
super([], { ...options, isMapMode: true });
|
|
201
201
|
if (keysNodesEntriesOrRaws) {
|
|
202
|
-
this.
|
|
202
|
+
this.setMany(keysNodesEntriesOrRaws);
|
|
203
203
|
}
|
|
204
204
|
}
|
|
205
205
|
|
|
@@ -220,29 +220,29 @@ export class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<K, V[],
|
|
|
220
220
|
return keyNodeOrEntry instanceof AVLTreeMultiMapNode;
|
|
221
221
|
}
|
|
222
222
|
|
|
223
|
-
override
|
|
223
|
+
override set(
|
|
224
224
|
keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined
|
|
225
225
|
): boolean;
|
|
226
226
|
|
|
227
|
-
override
|
|
227
|
+
override set(key: K, value: V): boolean;
|
|
228
228
|
|
|
229
229
|
/**
|
|
230
230
|
* Insert a value or a list of values into the multimap. If the key exists, values are appended.
|
|
231
231
|
* @remarks Time O(log N + M), Space O(1)
|
|
232
232
|
* @param keyNodeOrEntry - Key, node, or [key, values] entry.
|
|
233
|
-
* @param [value] - Single value to
|
|
233
|
+
* @param [value] - Single value to set when a bare key is provided.
|
|
234
234
|
* @returns True if inserted or appended; false if ignored.
|
|
235
235
|
*/
|
|
236
|
-
override
|
|
236
|
+
override set(
|
|
237
237
|
keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined,
|
|
238
238
|
value?: V
|
|
239
239
|
): boolean {
|
|
240
|
-
if (this.isRealNode(keyNodeOrEntry)) return super.
|
|
240
|
+
if (this.isRealNode(keyNodeOrEntry)) return super.set(keyNodeOrEntry);
|
|
241
241
|
|
|
242
242
|
const _commonAdd = (key?: BTNOptKeyOrNull<K>, values?: V[]) => {
|
|
243
243
|
if (key === undefined || key === null) return false;
|
|
244
244
|
|
|
245
|
-
const
|
|
245
|
+
const _setToValues = () => {
|
|
246
246
|
const existingValues = this.get(key);
|
|
247
247
|
if (existingValues !== undefined && values !== undefined) {
|
|
248
248
|
for (const value of values) existingValues.push(value);
|
|
@@ -251,12 +251,12 @@ export class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<K, V[],
|
|
|
251
251
|
return false;
|
|
252
252
|
};
|
|
253
253
|
|
|
254
|
-
const
|
|
254
|
+
const _setByNode = () => {
|
|
255
255
|
const existingNode = this.getNode(key);
|
|
256
256
|
if (this.isRealNode(existingNode)) {
|
|
257
257
|
const existingValues = this.get(existingNode);
|
|
258
258
|
if (existingValues === undefined) {
|
|
259
|
-
super.
|
|
259
|
+
super.set(key, values);
|
|
260
260
|
return true;
|
|
261
261
|
}
|
|
262
262
|
if (values !== undefined) {
|
|
@@ -266,14 +266,14 @@ export class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<K, V[],
|
|
|
266
266
|
return false;
|
|
267
267
|
}
|
|
268
268
|
} else {
|
|
269
|
-
return super.
|
|
269
|
+
return super.set(key, values);
|
|
270
270
|
}
|
|
271
271
|
};
|
|
272
272
|
|
|
273
273
|
if (this._isMapMode) {
|
|
274
|
-
return
|
|
274
|
+
return _setByNode() || _setToValues();
|
|
275
275
|
}
|
|
276
|
-
return
|
|
276
|
+
return _setToValues() || _setByNode();
|
|
277
277
|
};
|
|
278
278
|
|
|
279
279
|
if (this.isEntry(keyNodeOrEntry)) {
|
|
@@ -392,7 +392,7 @@ export class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<K, V[],
|
|
|
392
392
|
): AVLTree<MK, MV, MR> {
|
|
393
393
|
const out = this._createLike<MK, MV, MR>([], options);
|
|
394
394
|
let i = 0;
|
|
395
|
-
for (const [k, v] of this) out.
|
|
395
|
+
for (const [k, v] of this) out.set(callback.call(thisArg, v, k, i++, this));
|
|
396
396
|
return out;
|
|
397
397
|
}
|
|
398
398
|
|
|
@@ -182,7 +182,7 @@ export class AVLTreeNode<K = any, V = any> {
|
|
|
182
182
|
|
|
183
183
|
/**
|
|
184
184
|
* Represents a self-balancing AVL (Adelson-Velsky and Landis) Tree.
|
|
185
|
-
* This tree extends BST and performs rotations on
|
|
185
|
+
* This tree extends BST and performs rotations on set/delete to maintain balance.
|
|
186
186
|
*
|
|
187
187
|
* @template K - The type of the key.
|
|
188
188
|
* @template V - The type of the value.
|
|
@@ -215,7 +215,7 @@ export class AVLTreeNode<K = any, V = any> {
|
|
|
215
215
|
* console.log(tree.size); // 5;
|
|
216
216
|
*
|
|
217
217
|
* // Add a new element
|
|
218
|
-
* tree.
|
|
218
|
+
* tree.set(3);
|
|
219
219
|
* console.log(tree.size); // 6;
|
|
220
220
|
* console.log([...tree.keys()]); // [1, 2, 3, 5, 8, 9];
|
|
221
221
|
* @example
|
|
@@ -278,7 +278,7 @@ export class AVLTreeNode<K = any, V = any> {
|
|
|
278
278
|
* console.log(universityTree.isAVLBalanced()); // true;
|
|
279
279
|
*
|
|
280
280
|
* // Add more universities
|
|
281
|
-
* universityTree.
|
|
281
|
+
* universityTree.set(6, { name: 'Oxford', rank: 6, students: 2000 });
|
|
282
282
|
* console.log(universityTree.isAVLBalanced()); // true;
|
|
283
283
|
*
|
|
284
284
|
* // Delete and verify balance is maintained
|
|
@@ -358,9 +358,9 @@ export class AVLTreeNode<K = any, V = any> {
|
|
|
358
358
|
export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements IBinaryTree<K, V, R> {
|
|
359
359
|
/**
|
|
360
360
|
* Creates an instance of AVLTree.
|
|
361
|
-
* @remarks Time O(N log N) (from `
|
|
361
|
+
* @remarks Time O(N log N) (from `setMany` with balanced set). Space O(N).
|
|
362
362
|
*
|
|
363
|
-
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to
|
|
363
|
+
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
|
|
364
364
|
* @param [options] - Configuration options for the AVL tree.
|
|
365
365
|
*/
|
|
366
366
|
constructor(
|
|
@@ -370,8 +370,8 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
370
370
|
options?: AVLTreeOptions<K, V, R>
|
|
371
371
|
) {
|
|
372
372
|
super([], options);
|
|
373
|
-
// Note: super.
|
|
374
|
-
if (keysNodesEntriesOrRaws) super.
|
|
373
|
+
// Note: super.setMany is called, which in BST defaults to balanced set.
|
|
374
|
+
if (keysNodesEntriesOrRaws) super.setMany(keysNodesEntriesOrRaws);
|
|
375
375
|
}
|
|
376
376
|
|
|
377
377
|
/**
|
|
@@ -400,19 +400,19 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
400
400
|
}
|
|
401
401
|
|
|
402
402
|
/**
|
|
403
|
-
*
|
|
404
|
-
* @remarks Time O(log N) (O(H) for BST
|
|
403
|
+
* Sets a new node to the AVL tree and balances the tree path.
|
|
404
|
+
* @remarks Time O(log N) (O(H) for BST set + O(H) for `_balancePath`). Space O(H) for path/recursion.
|
|
405
405
|
*
|
|
406
|
-
* @param keyNodeOrEntry - The key, node, or entry to
|
|
406
|
+
* @param keyNodeOrEntry - The key, node, or entry to set.
|
|
407
407
|
* @param [value] - The value, if providing just a key.
|
|
408
408
|
* @returns True if the addition was successful, false otherwise.
|
|
409
409
|
*/
|
|
410
|
-
override
|
|
410
|
+
override set(
|
|
411
411
|
keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
|
|
412
412
|
value?: V
|
|
413
413
|
): boolean {
|
|
414
414
|
if (keyNodeOrEntry === null) return false;
|
|
415
|
-
const inserted = super.
|
|
415
|
+
const inserted = super.set(keyNodeOrEntry, value);
|
|
416
416
|
// If insertion was successful, balance the path from the new node up to the root.
|
|
417
417
|
if (inserted) this._balancePath(keyNodeOrEntry);
|
|
418
418
|
return inserted;
|
|
@@ -477,7 +477,7 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
477
477
|
|
|
478
478
|
/**
|
|
479
479
|
* Creates a new AVLTree by mapping each [key, value] pair.
|
|
480
|
-
* @remarks Time O(N log N) (O(N) iteration + O(log M) `
|
|
480
|
+
* @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.
|
|
481
481
|
*
|
|
482
482
|
* @template MK - New key type.
|
|
483
483
|
* @template MV - New value type.
|
|
@@ -497,8 +497,8 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
497
497
|
let index = 0;
|
|
498
498
|
// Iterates in-order
|
|
499
499
|
for (const [key, value] of this) {
|
|
500
|
-
// `
|
|
501
|
-
out.
|
|
500
|
+
// `set` on the new tree will be O(log N) and will self-balance.
|
|
501
|
+
out.set(callback.call(thisArg, value, key, index++, this));
|
|
502
502
|
}
|
|
503
503
|
return out;
|
|
504
504
|
}
|
|
@@ -71,7 +71,7 @@ export class BinaryTreeNode<K = any, V = any> {
|
|
|
71
71
|
*/
|
|
72
72
|
set left(v: BinaryTreeNode<K, V> | null | undefined) {
|
|
73
73
|
if (v) {
|
|
74
|
-
v.parent = this
|
|
74
|
+
v.parent = this;
|
|
75
75
|
}
|
|
76
76
|
this._left = v;
|
|
77
77
|
}
|
|
@@ -193,7 +193,7 @@ export class BinaryTreeNode<K = any, V = any> {
|
|
|
193
193
|
*
|
|
194
194
|
* @remarks
|
|
195
195
|
* This class implements a basic Binary Tree, not a Binary Search Tree.
|
|
196
|
-
* The `
|
|
196
|
+
* The `set` operation inserts nodes level-by-level (BFS) into the first available slot.
|
|
197
197
|
*
|
|
198
198
|
* @template K - The type of the key.
|
|
199
199
|
* @template V - The type of the value.
|
|
@@ -225,7 +225,7 @@ export class BinaryTreeNode<K = any, V = any> {
|
|
|
225
225
|
* console.log(tree.size); // 9;
|
|
226
226
|
*
|
|
227
227
|
* // Add new element
|
|
228
|
-
* tree.
|
|
228
|
+
* tree.set(10, 'ten');
|
|
229
229
|
* console.log(tree.size); // 10;
|
|
230
230
|
* @example
|
|
231
231
|
* // BinaryTree get and has operations
|
|
@@ -353,9 +353,9 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
353
353
|
|
|
354
354
|
/**
|
|
355
355
|
* Creates an instance of BinaryTree.
|
|
356
|
-
* @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) `
|
|
356
|
+
* @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.
|
|
357
357
|
*
|
|
358
|
-
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to
|
|
358
|
+
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
|
|
359
359
|
* @param [options] - Configuration options for the tree.
|
|
360
360
|
*/
|
|
361
361
|
constructor(
|
|
@@ -374,7 +374,7 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
374
374
|
else if (toEntryFn) throw TypeError('toEntryFn must be a function type');
|
|
375
375
|
}
|
|
376
376
|
|
|
377
|
-
if (keysNodesEntriesOrRaws) this.
|
|
377
|
+
if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
|
|
378
378
|
}
|
|
379
379
|
|
|
380
380
|
protected _isMapMode = true;
|
|
@@ -640,13 +640,27 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
640
640
|
* @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).
|
|
641
641
|
*
|
|
642
642
|
* @param keyNodeOrEntry - The key, node, or entry to add.
|
|
643
|
-
* @param [value] - The value, if providing just a key.
|
|
644
643
|
* @returns True if the addition was successful, false otherwise.
|
|
645
644
|
*/
|
|
646
645
|
add(
|
|
646
|
+
keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
|
|
647
|
+
): boolean {
|
|
648
|
+
return this.set(keyNodeOrEntry);
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
/**
|
|
652
|
+
* Adds or updates a new node to the tree.
|
|
653
|
+
* @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).
|
|
654
|
+
*
|
|
655
|
+
* @param keyNodeOrEntry - The key, node, or entry to set or update.
|
|
656
|
+
* @param [value] - The value, if providing just a key.
|
|
657
|
+
* @returns True if the addition was successful, false otherwise.
|
|
658
|
+
*/
|
|
659
|
+
set(
|
|
647
660
|
keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
|
|
648
661
|
value?: V
|
|
649
662
|
): boolean {
|
|
663
|
+
|
|
650
664
|
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
651
665
|
if (newNode === undefined) return false;
|
|
652
666
|
|
|
@@ -699,29 +713,30 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
699
713
|
}
|
|
700
714
|
|
|
701
715
|
/**
|
|
702
|
-
* Adds
|
|
703
|
-
* @remarks Time O(
|
|
716
|
+
* Adds multiple items to the tree.
|
|
717
|
+
* @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).
|
|
704
718
|
*
|
|
705
|
-
* @param
|
|
706
|
-
* @param [
|
|
707
|
-
* @returns
|
|
719
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set.
|
|
720
|
+
* @param [values] - An optional parallel iterable of values.
|
|
721
|
+
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
708
722
|
*/
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
723
|
+
addMany(
|
|
724
|
+
keysNodesEntriesOrRaws: Iterable<
|
|
725
|
+
K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R
|
|
726
|
+
>
|
|
727
|
+
): boolean[] {
|
|
728
|
+
return this.setMany(keysNodesEntriesOrRaws);
|
|
714
729
|
}
|
|
715
730
|
|
|
716
731
|
/**
|
|
717
|
-
* Adds multiple items to the tree.
|
|
718
|
-
* @remarks Time O(N * M), where N is the number of items to
|
|
732
|
+
* Adds or updates multiple items to the tree.
|
|
733
|
+
* @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).
|
|
719
734
|
*
|
|
720
|
-
* @param keysNodesEntriesOrRaws - An iterable of items to
|
|
735
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set or update.
|
|
721
736
|
* @param [values] - An optional parallel iterable of values.
|
|
722
|
-
* @returns An array of booleans indicating the success of each individual `
|
|
737
|
+
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
723
738
|
*/
|
|
724
|
-
|
|
739
|
+
setMany(
|
|
725
740
|
keysNodesEntriesOrRaws: Iterable<
|
|
726
741
|
K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R
|
|
727
742
|
>,
|
|
@@ -744,44 +759,27 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
744
759
|
}
|
|
745
760
|
}
|
|
746
761
|
if (this.isRaw(keyNodeEntryOrRaw)) keyNodeEntryOrRaw = this._toEntryFn!(keyNodeEntryOrRaw);
|
|
747
|
-
inserted.push(this.
|
|
762
|
+
inserted.push(this.set(keyNodeEntryOrRaw, value));
|
|
748
763
|
}
|
|
749
764
|
|
|
750
765
|
return inserted;
|
|
751
766
|
}
|
|
752
767
|
|
|
753
768
|
/**
|
|
754
|
-
*
|
|
755
|
-
* @remarks Time O(N * M), where N is the
|
|
756
|
-
*
|
|
757
|
-
* @param keysNodesEntriesOrRaws - An iterable of items to add or update.
|
|
758
|
-
* @param [values] - An optional parallel iterable of values.
|
|
759
|
-
* @returns An array of booleans indicating the success of each individual `add` operation.
|
|
760
|
-
*/
|
|
761
|
-
setMany(
|
|
762
|
-
keysNodesEntriesOrRaws: Iterable<
|
|
763
|
-
K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R
|
|
764
|
-
>,
|
|
765
|
-
values?: Iterable<V | undefined>
|
|
766
|
-
): boolean[] {
|
|
767
|
-
return this.addMany(keysNodesEntriesOrRaws, values);
|
|
768
|
-
}
|
|
769
|
-
|
|
770
|
-
/**
|
|
771
|
-
* Merges another tree into this one by adding all its nodes.
|
|
772
|
-
* @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`).
|
|
769
|
+
* Merges another tree into this one by seting all its nodes.
|
|
770
|
+
* @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`).
|
|
773
771
|
*
|
|
774
772
|
* @param anotherTree - The tree to merge.
|
|
775
773
|
*/
|
|
776
774
|
merge(anotherTree: BinaryTree<K, V, R>) {
|
|
777
|
-
this.
|
|
775
|
+
this.setMany(anotherTree, []);
|
|
778
776
|
}
|
|
779
777
|
|
|
780
778
|
/**
|
|
781
779
|
* Clears the tree and refills it with new items.
|
|
782
|
-
* @remarks Time O(N) (for `clear`) + O(N * M) (for `
|
|
780
|
+
* @remarks Time O(N) (for `clear`) + O(N * M) (for `setMany`) = O(N * M). Space O(M) (from `setMany`).
|
|
783
781
|
*
|
|
784
|
-
* @param keysNodesEntriesOrRaws - An iterable of items to
|
|
782
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set.
|
|
785
783
|
* @param [values] - An optional parallel iterable of values.
|
|
786
784
|
*/
|
|
787
785
|
refill(
|
|
@@ -791,7 +789,7 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
791
789
|
values?: Iterable<V | undefined>
|
|
792
790
|
): void {
|
|
793
791
|
this.clear();
|
|
794
|
-
this.
|
|
792
|
+
this.setMany(keysNodesEntriesOrRaws, values);
|
|
795
793
|
}
|
|
796
794
|
|
|
797
795
|
/**
|
|
@@ -1830,7 +1828,7 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
1830
1828
|
|
|
1831
1829
|
/**
|
|
1832
1830
|
* Clones the tree.
|
|
1833
|
-
* @remarks Time O(N * M), where N is the number of nodes and M is the tree size during insertion (due to `bfs` + `
|
|
1831
|
+
* @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.
|
|
1834
1832
|
*
|
|
1835
1833
|
* @returns A new, cloned instance of the tree.
|
|
1836
1834
|
*/
|
|
@@ -1842,7 +1840,7 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
1842
1840
|
|
|
1843
1841
|
/**
|
|
1844
1842
|
* Creates a new tree containing only the entries that satisfy the predicate.
|
|
1845
|
-
* @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) `
|
|
1843
|
+
* @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.
|
|
1846
1844
|
*
|
|
1847
1845
|
* @param predicate - A function to test each [key, value] pair.
|
|
1848
1846
|
* @param [thisArg] - `this` context for the predicate.
|
|
@@ -1851,7 +1849,7 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
1851
1849
|
filter(predicate: EntryCallback<K, V | undefined, boolean>, thisArg?: unknown): this {
|
|
1852
1850
|
const out = this._createInstance<K, V, R>();
|
|
1853
1851
|
let i = 0;
|
|
1854
|
-
for (const [k, v] of this) if (predicate.call(thisArg, v, k, i++, this)) out.
|
|
1852
|
+
for (const [k, v] of this) if (predicate.call(thisArg, v, k, i++, this)) out.set([k, v]);
|
|
1855
1853
|
return out;
|
|
1856
1854
|
}
|
|
1857
1855
|
|
|
@@ -1874,7 +1872,7 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
1874
1872
|
): BinaryTree<MK, MV, MR> {
|
|
1875
1873
|
const out = this._createLike<MK, MV, MR>([], options);
|
|
1876
1874
|
let i = 0;
|
|
1877
|
-
for (const [k, v] of this) out.
|
|
1875
|
+
for (const [k, v] of this) out.set(cb.call(thisArg, v, k, i++, this));
|
|
1878
1876
|
return out;
|
|
1879
1877
|
}
|
|
1880
1878
|
|
|
@@ -2204,8 +2202,8 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
2204
2202
|
}
|
|
2205
2203
|
|
|
2206
2204
|
/**
|
|
2207
|
-
* (Protected) Helper for cloning. Performs a BFS and
|
|
2208
|
-
* @remarks Time O(N * M) (O(N) BFS + O(M) `
|
|
2205
|
+
* (Protected) Helper for cloning. Performs a BFS and sets all nodes to the new tree.
|
|
2206
|
+
* @remarks Time O(N * M) (O(N) BFS + O(M) `set` for each node).
|
|
2209
2207
|
*
|
|
2210
2208
|
* @param cloned - The new, empty tree instance to populate.
|
|
2211
2209
|
*/
|
|
@@ -2213,10 +2211,10 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
2213
2211
|
// Use BFS with nulls to preserve the tree structure
|
|
2214
2212
|
this.bfs(
|
|
2215
2213
|
node => {
|
|
2216
|
-
if (node === null) cloned.
|
|
2214
|
+
if (node === null) cloned.set(null);
|
|
2217
2215
|
else {
|
|
2218
|
-
if (this._isMapMode) cloned.
|
|
2219
|
-
else cloned.
|
|
2216
|
+
if (this._isMapMode) cloned.set([node.key, this._store.get(node.key)]);
|
|
2217
|
+
else cloned.set([node.key, node.value]);
|
|
2220
2218
|
}
|
|
2221
2219
|
},
|
|
2222
2220
|
this._root,
|
|
@@ -219,8 +219,8 @@ export class BSTNode<K = any, V = any> {
|
|
|
219
219
|
* console.log(bst.size); // 16;
|
|
220
220
|
*
|
|
221
221
|
* // Add new elements
|
|
222
|
-
* bst.
|
|
223
|
-
* bst.
|
|
222
|
+
* bst.set(17);
|
|
223
|
+
* bst.set(0);
|
|
224
224
|
* console.log(bst.size); // 18;
|
|
225
225
|
*
|
|
226
226
|
* // Verify keys are searchable
|
|
@@ -266,7 +266,7 @@ export class BSTNode<K = any, V = any> {
|
|
|
266
266
|
*
|
|
267
267
|
* // Merge datasets into a single BinarySearchTree
|
|
268
268
|
* const merged = new BST<number, string>(dataset1);
|
|
269
|
-
* merged.
|
|
269
|
+
* merged.setMany(dataset2);
|
|
270
270
|
* merged.merge(dataset3);
|
|
271
271
|
*
|
|
272
272
|
* // Verify merged dataset is in sorted order
|
|
@@ -350,7 +350,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
350
350
|
* Creates an instance of BST.
|
|
351
351
|
* @remarks Time O(N log N) or O(N^2) depending on `isBalanceAdd` in `addMany` and input order. Space O(N).
|
|
352
352
|
*
|
|
353
|
-
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to
|
|
353
|
+
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
|
|
354
354
|
* @param [options] - Configuration options for the BST, including comparator.
|
|
355
355
|
*/
|
|
356
356
|
constructor(
|
|
@@ -370,7 +370,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
370
370
|
this._comparator = this._createDefaultComparator();
|
|
371
371
|
}
|
|
372
372
|
|
|
373
|
-
if (keysNodesEntriesOrRaws) this.
|
|
373
|
+
if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
|
|
374
374
|
}
|
|
375
375
|
|
|
376
376
|
protected override _root?: BSTNode<K, V> = undefined;
|
|
@@ -625,7 +625,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
625
625
|
if (isRange) {
|
|
626
626
|
predicate = node => {
|
|
627
627
|
if (!node) return false;
|
|
628
|
-
return (keyNodeEntryOrPredicate
|
|
628
|
+
return (keyNodeEntryOrPredicate).isInRange(node.key, this._comparator);
|
|
629
629
|
};
|
|
630
630
|
} else {
|
|
631
631
|
predicate = this._ensurePredicate(keyNodeEntryOrPredicate);
|
|
@@ -637,7 +637,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
637
637
|
if (!this.isRealNode(cur.left)) return false;
|
|
638
638
|
if (isRange) {
|
|
639
639
|
// Range search: Only go left if the current key is >= the lower bound
|
|
640
|
-
const range = keyNodeEntryOrPredicate
|
|
640
|
+
const range = keyNodeEntryOrPredicate;
|
|
641
641
|
const leftS = range.low;
|
|
642
642
|
const leftI = range.includeLow;
|
|
643
643
|
return (leftI && this._compare(cur.key, leftS) >= 0) || (!leftI && this._compare(cur.key, leftS) > 0);
|
|
@@ -655,7 +655,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
655
655
|
if (!this.isRealNode(cur.right)) return false;
|
|
656
656
|
if (isRange) {
|
|
657
657
|
// Range search: Only go right if current key <= upper bound
|
|
658
|
-
const range = keyNodeEntryOrPredicate
|
|
658
|
+
const range = keyNodeEntryOrPredicate;
|
|
659
659
|
const rightS = range.high;
|
|
660
660
|
const rightI = range.includeHigh;
|
|
661
661
|
return (rightI && this._compare(cur.key, rightS) <= 0) || (!rightI && this._compare(cur.key, rightS) < 0);
|
|
@@ -716,11 +716,11 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
716
716
|
* Adds a new node to the BST based on key comparison.
|
|
717
717
|
* @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
|
|
718
718
|
*
|
|
719
|
-
* @param keyNodeOrEntry - The key, node, or entry to
|
|
719
|
+
* @param keyNodeOrEntry - The key, node, or entry to set.
|
|
720
720
|
* @param [value] - The value, if providing just a key.
|
|
721
721
|
* @returns True if the addition was successful, false otherwise.
|
|
722
722
|
*/
|
|
723
|
-
override
|
|
723
|
+
override set(
|
|
724
724
|
keyNodeOrEntry: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
|
|
725
725
|
value?: V
|
|
726
726
|
): boolean {
|
|
@@ -766,17 +766,17 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
766
766
|
|
|
767
767
|
/**
|
|
768
768
|
* Adds multiple items to the tree.
|
|
769
|
-
* @remarks If `isBalanceAdd` is true, sorts the input and builds a balanced tree. Time O(N log N) (due to sort and balanced
|
|
769
|
+
* @remarks If `isBalanceAdd` is true, sorts the input and builds a balanced tree. Time O(N log N) (due to sort and balanced set).
|
|
770
770
|
* If false, adds items one by one. Time O(N * H), which is O(N^2) worst-case.
|
|
771
771
|
* Space O(N) for sorting and recursion/iteration stack.
|
|
772
772
|
*
|
|
773
|
-
* @param keysNodesEntriesOrRaws - An iterable of items to
|
|
773
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set.
|
|
774
774
|
* @param [values] - An optional parallel iterable of values.
|
|
775
775
|
* @param [isBalanceAdd=true] - If true, builds a balanced tree from the items.
|
|
776
|
-
* @param [iterationType=this.iterationType] - The traversal method for balanced
|
|
777
|
-
* @returns An array of booleans indicating the success of each individual `
|
|
776
|
+
* @param [iterationType=this.iterationType] - The traversal method for balanced set (recursive or iterative).
|
|
777
|
+
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
778
778
|
*/
|
|
779
|
-
override
|
|
779
|
+
override setMany(
|
|
780
780
|
keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, BSTNode<K, V>>>,
|
|
781
781
|
values?: Iterable<V | undefined>,
|
|
782
782
|
isBalanceAdd = true,
|
|
@@ -790,7 +790,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
790
790
|
for (let kve of keysNodesEntriesOrRaws) {
|
|
791
791
|
const val = valuesIterator?.next().value;
|
|
792
792
|
if (this.isRaw(kve)) kve = this._toEntryFn!(kve);
|
|
793
|
-
inserted.push(this.
|
|
793
|
+
inserted.push(this.set(kve, val));
|
|
794
794
|
}
|
|
795
795
|
return inserted;
|
|
796
796
|
}
|
|
@@ -831,9 +831,9 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
831
831
|
const { key, value, orgIndex } = arr[mid];
|
|
832
832
|
if (this.isRaw(key)) {
|
|
833
833
|
const entry = this._toEntryFn!(key);
|
|
834
|
-
inserted[orgIndex] = this.
|
|
834
|
+
inserted[orgIndex] = this.set(entry);
|
|
835
835
|
} else {
|
|
836
|
-
inserted[orgIndex] = this.
|
|
836
|
+
inserted[orgIndex] = this.set(key, value);
|
|
837
837
|
}
|
|
838
838
|
_dfs(arr.slice(0, mid));
|
|
839
839
|
_dfs(arr.slice(mid + 1));
|
|
@@ -852,9 +852,9 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
852
852
|
const { key, value, orgIndex } = sorted[m];
|
|
853
853
|
if (this.isRaw(key)) {
|
|
854
854
|
const entry = this._toEntryFn!(key);
|
|
855
|
-
inserted[orgIndex] = this.
|
|
855
|
+
inserted[orgIndex] = this.set(entry);
|
|
856
856
|
} else {
|
|
857
|
-
inserted[orgIndex] = this.
|
|
857
|
+
inserted[orgIndex] = this.set(key, value);
|
|
858
858
|
}
|
|
859
859
|
stack.push([m + 1, r]);
|
|
860
860
|
stack.push([l, m - 1]);
|
|
@@ -1369,7 +1369,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
1369
1369
|
let index = 0;
|
|
1370
1370
|
// Iterates in-order
|
|
1371
1371
|
for (const [key, value] of this) {
|
|
1372
|
-
out.
|
|
1372
|
+
out.set(callback.call(thisArg, value, key, index++, this));
|
|
1373
1373
|
}
|
|
1374
1374
|
return out;
|
|
1375
1375
|
}
|
|
@@ -1441,7 +1441,6 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
1441
1441
|
*/
|
|
1442
1442
|
protected _createDefaultComparator(): Comparator<K> {
|
|
1443
1443
|
return (a: K, b: K): number => {
|
|
1444
|
-
debugger;
|
|
1445
1444
|
// If both keys are comparable (primitive types), use direct comparison
|
|
1446
1445
|
if (isComparable(a) && isComparable(b)) {
|
|
1447
1446
|
if (a > b) return 1;
|
|
@@ -292,7 +292,7 @@ export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implem
|
|
|
292
292
|
this._root = this.NIL;
|
|
293
293
|
|
|
294
294
|
if (keysNodesEntriesOrRaws) {
|
|
295
|
-
this.
|
|
295
|
+
this.setMany(keysNodesEntriesOrRaws);
|
|
296
296
|
}
|
|
297
297
|
}
|
|
298
298
|
|
|
@@ -350,7 +350,7 @@ export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implem
|
|
|
350
350
|
* @param [value]- See parameter type for details.
|
|
351
351
|
* @returns True if inserted or updated; false if ignored.
|
|
352
352
|
*/
|
|
353
|
-
override
|
|
353
|
+
override set(
|
|
354
354
|
keyNodeOrEntry: K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
|
|
355
355
|
value?: V
|
|
356
356
|
): boolean {
|
|
@@ -469,7 +469,7 @@ export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implem
|
|
|
469
469
|
|
|
470
470
|
let index = 0;
|
|
471
471
|
for (const [key, value] of this) {
|
|
472
|
-
out.
|
|
472
|
+
out.set(callback.call(thisArg, value, key, index++, this));
|
|
473
473
|
}
|
|
474
474
|
return out;
|
|
475
475
|
}
|