binary-tree-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.
- package/README.md +1 -1
- package/dist/cjs/index.cjs +41 -43
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +41 -43
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +41 -43
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +41 -43
- 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 -24
- 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/binary-tree-typed.js +41 -43
- package/dist/umd/binary-tree-typed.js.map +1 -1
- package/dist/umd/binary-tree-typed.min.js +2 -2
- package/dist/umd/binary-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 +52 -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
|
@@ -147,7 +147,7 @@ export declare class AVLTreeCounter<K = any, V = any, R = any> extends AVLTree<K
|
|
|
147
147
|
* @param [count] - How much to increase the node's count (default 1).
|
|
148
148
|
* @returns True if inserted/updated; false if ignored.
|
|
149
149
|
*/
|
|
150
|
-
|
|
150
|
+
set(keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V, count?: number): boolean;
|
|
151
151
|
/**
|
|
152
152
|
* Delete a node (or decrement its count) and rebalance if needed.
|
|
153
153
|
* @remarks Time O(log N), Space O(1)
|
|
@@ -132,8 +132,8 @@ export declare class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<
|
|
|
132
132
|
* @returns True if it's a AVLTreeMultiMapNode, false otherwise.
|
|
133
133
|
*/
|
|
134
134
|
isNode(keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined): keyNodeOrEntry is AVLTreeMultiMapNode<K, V>;
|
|
135
|
-
|
|
136
|
-
|
|
135
|
+
set(keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined): boolean;
|
|
136
|
+
set(key: K, value: V): boolean;
|
|
137
137
|
/**
|
|
138
138
|
* Delete a single value from the bucket at a given key. Removes the key if the bucket becomes empty.
|
|
139
139
|
* @remarks Time O(log N), Space O(1)
|
|
@@ -112,7 +112,7 @@ export declare class AVLTreeNode<K = any, V = any> {
|
|
|
112
112
|
}
|
|
113
113
|
/**
|
|
114
114
|
* Represents a self-balancing AVL (Adelson-Velsky and Landis) Tree.
|
|
115
|
-
* This tree extends BST and performs rotations on
|
|
115
|
+
* This tree extends BST and performs rotations on set/delete to maintain balance.
|
|
116
116
|
*
|
|
117
117
|
* @template K - The type of the key.
|
|
118
118
|
* @template V - The type of the value.
|
|
@@ -145,7 +145,7 @@ export declare class AVLTreeNode<K = any, V = any> {
|
|
|
145
145
|
* console.log(tree.size); // 5;
|
|
146
146
|
*
|
|
147
147
|
* // Add a new element
|
|
148
|
-
* tree.
|
|
148
|
+
* tree.set(3);
|
|
149
149
|
* console.log(tree.size); // 6;
|
|
150
150
|
* console.log([...tree.keys()]); // [1, 2, 3, 5, 8, 9];
|
|
151
151
|
* @example
|
|
@@ -208,7 +208,7 @@ export declare class AVLTreeNode<K = any, V = any> {
|
|
|
208
208
|
* console.log(universityTree.isAVLBalanced()); // true;
|
|
209
209
|
*
|
|
210
210
|
* // Add more universities
|
|
211
|
-
* universityTree.
|
|
211
|
+
* universityTree.set(6, { name: 'Oxford', rank: 6, students: 2000 });
|
|
212
212
|
* console.log(universityTree.isAVLBalanced()); // true;
|
|
213
213
|
*
|
|
214
214
|
* // Delete and verify balance is maintained
|
|
@@ -288,9 +288,9 @@ export declare class AVLTreeNode<K = any, V = any> {
|
|
|
288
288
|
export declare class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements IBinaryTree<K, V, R> {
|
|
289
289
|
/**
|
|
290
290
|
* Creates an instance of AVLTree.
|
|
291
|
-
* @remarks Time O(N log N) (from `
|
|
291
|
+
* @remarks Time O(N log N) (from `setMany` with balanced set). Space O(N).
|
|
292
292
|
*
|
|
293
|
-
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to
|
|
293
|
+
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
|
|
294
294
|
* @param [options] - Configuration options for the AVL tree.
|
|
295
295
|
*/
|
|
296
296
|
constructor(keysNodesEntriesOrRaws?: Iterable<K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, options?: AVLTreeOptions<K, V, R>);
|
|
@@ -312,14 +312,14 @@ export declare class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> imp
|
|
|
312
312
|
*/
|
|
313
313
|
isNode(keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): keyNodeOrEntry is AVLTreeNode<K, V>;
|
|
314
314
|
/**
|
|
315
|
-
*
|
|
316
|
-
* @remarks Time O(log N) (O(H) for BST
|
|
315
|
+
* Sets a new node to the AVL tree and balances the tree path.
|
|
316
|
+
* @remarks Time O(log N) (O(H) for BST set + O(H) for `_balancePath`). Space O(H) for path/recursion.
|
|
317
317
|
*
|
|
318
|
-
* @param keyNodeOrEntry - The key, node, or entry to
|
|
318
|
+
* @param keyNodeOrEntry - The key, node, or entry to set.
|
|
319
319
|
* @param [value] - The value, if providing just a key.
|
|
320
320
|
* @returns True if the addition was successful, false otherwise.
|
|
321
321
|
*/
|
|
322
|
-
|
|
322
|
+
set(keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): boolean;
|
|
323
323
|
/**
|
|
324
324
|
* Deletes a node from the AVL tree and re-balances the tree.
|
|
325
325
|
* @remarks Time O(log N) (O(H) for BST delete + O(H) for `_balancePath`). Space O(H) for path/recursion.
|
|
@@ -339,7 +339,7 @@ export declare class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> imp
|
|
|
339
339
|
perfectlyBalance(iterationType?: IterationType): boolean;
|
|
340
340
|
/**
|
|
341
341
|
* Creates a new AVLTree by mapping each [key, value] pair.
|
|
342
|
-
* @remarks Time O(N log N) (O(N) iteration + O(log M) `
|
|
342
|
+
* @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.
|
|
343
343
|
*
|
|
344
344
|
* @template MK - New key type.
|
|
345
345
|
* @template MV - New value type.
|
|
@@ -113,7 +113,7 @@ export declare class BinaryTreeNode<K = any, V = any> {
|
|
|
113
113
|
*
|
|
114
114
|
* @remarks
|
|
115
115
|
* This class implements a basic Binary Tree, not a Binary Search Tree.
|
|
116
|
-
* The `
|
|
116
|
+
* The `set` operation inserts nodes level-by-level (BFS) into the first available slot.
|
|
117
117
|
*
|
|
118
118
|
* @template K - The type of the key.
|
|
119
119
|
* @template V - The type of the value.
|
|
@@ -145,7 +145,7 @@ export declare class BinaryTreeNode<K = any, V = any> {
|
|
|
145
145
|
* console.log(tree.size); // 9;
|
|
146
146
|
*
|
|
147
147
|
* // Add new element
|
|
148
|
-
* tree.
|
|
148
|
+
* tree.set(10, 'ten');
|
|
149
149
|
* console.log(tree.size); // 10;
|
|
150
150
|
* @example
|
|
151
151
|
* // BinaryTree get and has operations
|
|
@@ -269,9 +269,9 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
269
269
|
iterationType: IterationType;
|
|
270
270
|
/**
|
|
271
271
|
* Creates an instance of BinaryTree.
|
|
272
|
-
* @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) `
|
|
272
|
+
* @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.
|
|
273
273
|
*
|
|
274
|
-
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to
|
|
274
|
+
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
|
|
275
275
|
* @param [options] - Configuration options for the tree.
|
|
276
276
|
*/
|
|
277
277
|
constructor(keysNodesEntriesOrRaws?: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, options?: BinaryTreeOptions<K, V, R>);
|
|
@@ -434,49 +434,47 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
434
434
|
* @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).
|
|
435
435
|
*
|
|
436
436
|
* @param keyNodeOrEntry - The key, node, or entry to add.
|
|
437
|
-
* @param [value] - The value, if providing just a key.
|
|
438
437
|
* @returns True if the addition was successful, false otherwise.
|
|
439
438
|
*/
|
|
440
|
-
add(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
|
|
439
|
+
add(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): boolean;
|
|
441
440
|
/**
|
|
442
441
|
* Adds or updates a new node to the tree.
|
|
443
|
-
* @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation
|
|
442
|
+
* @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).
|
|
444
443
|
*
|
|
445
|
-
* @param keyNodeOrEntry - The key, node, or entry to
|
|
444
|
+
* @param keyNodeOrEntry - The key, node, or entry to set or update.
|
|
446
445
|
* @param [value] - The value, if providing just a key.
|
|
447
446
|
* @returns True if the addition was successful, false otherwise.
|
|
448
447
|
*/
|
|
449
448
|
set(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): boolean;
|
|
450
449
|
/**
|
|
451
450
|
* Adds multiple items to the tree.
|
|
452
|
-
* @remarks Time O(N * M), where N is the number of items to
|
|
451
|
+
* @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).
|
|
453
452
|
*
|
|
454
|
-
* @param keysNodesEntriesOrRaws - An iterable of items to
|
|
455
|
-
* @
|
|
456
|
-
* @returns An array of booleans indicating the success of each individual `add` operation.
|
|
453
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set.
|
|
454
|
+
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
457
455
|
*/
|
|
458
|
-
addMany(keysNodesEntriesOrRaws: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R
|
|
456
|
+
addMany(keysNodesEntriesOrRaws: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>): boolean[];
|
|
459
457
|
/**
|
|
460
458
|
* Adds or updates multiple items to the tree.
|
|
461
|
-
* @remarks Time O(N * M), where N is the number of items to
|
|
459
|
+
* @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).
|
|
462
460
|
*
|
|
463
|
-
* @param keysNodesEntriesOrRaws - An iterable of items to
|
|
461
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set or update.
|
|
464
462
|
* @param [values] - An optional parallel iterable of values.
|
|
465
|
-
* @returns An array of booleans indicating the success of each individual `
|
|
463
|
+
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
466
464
|
*/
|
|
467
465
|
setMany(keysNodesEntriesOrRaws: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, values?: Iterable<V | undefined>): boolean[];
|
|
468
466
|
/**
|
|
469
|
-
* Merges another tree into this one by
|
|
470
|
-
* @remarks Time O(N * M), same as `
|
|
467
|
+
* Merges another tree into this one by seting all its nodes.
|
|
468
|
+
* @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`).
|
|
471
469
|
*
|
|
472
470
|
* @param anotherTree - The tree to merge.
|
|
473
471
|
*/
|
|
474
472
|
merge(anotherTree: BinaryTree<K, V, R>): void;
|
|
475
473
|
/**
|
|
476
474
|
* Clears the tree and refills it with new items.
|
|
477
|
-
* @remarks Time O(N) (for `clear`) + O(N * M) (for `
|
|
475
|
+
* @remarks Time O(N) (for `clear`) + O(N * M) (for `setMany`) = O(N * M). Space O(M) (from `setMany`).
|
|
478
476
|
*
|
|
479
|
-
* @param keysNodesEntriesOrRaws - An iterable of items to
|
|
477
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set.
|
|
480
478
|
* @param [values] - An optional parallel iterable of values.
|
|
481
479
|
*/
|
|
482
480
|
refill(keysNodesEntriesOrRaws: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, values?: Iterable<V | undefined>): void;
|
|
@@ -624,14 +622,14 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
624
622
|
morris<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback?: C, pattern?: DFSOrderPattern, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): ReturnType<C>[];
|
|
625
623
|
/**
|
|
626
624
|
* Clones the tree.
|
|
627
|
-
* @remarks Time O(N * M), where N is the number of nodes and M is the tree size during insertion (due to `bfs` + `
|
|
625
|
+
* @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.
|
|
628
626
|
*
|
|
629
627
|
* @returns A new, cloned instance of the tree.
|
|
630
628
|
*/
|
|
631
629
|
clone(): this;
|
|
632
630
|
/**
|
|
633
631
|
* Creates a new tree containing only the entries that satisfy the predicate.
|
|
634
|
-
* @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) `
|
|
632
|
+
* @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.
|
|
635
633
|
*
|
|
636
634
|
* @param predicate - A function to test each [key, value] pair.
|
|
637
635
|
* @param [thisArg] - `this` context for the predicate.
|
|
@@ -722,8 +720,8 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
722
720
|
*/
|
|
723
721
|
protected _keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): [BinaryTreeNode<K, V> | null | undefined, V | undefined];
|
|
724
722
|
/**
|
|
725
|
-
* (Protected) Helper for cloning. Performs a BFS and
|
|
726
|
-
* @remarks Time O(N * M) (O(N) BFS + O(M) `
|
|
723
|
+
* (Protected) Helper for cloning. Performs a BFS and sets all nodes to the new tree.
|
|
724
|
+
* @remarks Time O(N * M) (O(N) BFS + O(M) `set` for each node).
|
|
727
725
|
*
|
|
728
726
|
* @param cloned - The new, empty tree instance to populate.
|
|
729
727
|
*/
|
|
@@ -145,8 +145,8 @@ export declare class BSTNode<K = any, V = any> {
|
|
|
145
145
|
* console.log(bst.size); // 16;
|
|
146
146
|
*
|
|
147
147
|
* // Add new elements
|
|
148
|
-
* bst.
|
|
149
|
-
* bst.
|
|
148
|
+
* bst.set(17);
|
|
149
|
+
* bst.set(0);
|
|
150
150
|
* console.log(bst.size); // 18;
|
|
151
151
|
*
|
|
152
152
|
* // Verify keys are searchable
|
|
@@ -192,7 +192,7 @@ export declare class BSTNode<K = any, V = any> {
|
|
|
192
192
|
*
|
|
193
193
|
* // Merge datasets into a single BinarySearchTree
|
|
194
194
|
* const merged = new BST<number, string>(dataset1);
|
|
195
|
-
* merged.
|
|
195
|
+
* merged.setMany(dataset2);
|
|
196
196
|
* merged.merge(dataset3);
|
|
197
197
|
*
|
|
198
198
|
* // Verify merged dataset is in sorted order
|
|
@@ -276,7 +276,7 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
276
276
|
* Creates an instance of BST.
|
|
277
277
|
* @remarks Time O(N log N) or O(N^2) depending on `isBalanceAdd` in `addMany` and input order. Space O(N).
|
|
278
278
|
*
|
|
279
|
-
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to
|
|
279
|
+
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
|
|
280
280
|
* @param [options] - Configuration options for the BST, including comparator.
|
|
281
281
|
*/
|
|
282
282
|
constructor(keysNodesEntriesOrRaws?: Iterable<K | BSTNode | [K | null | undefined, V | undefined] | null | undefined | R>, options?: BSTOptions<K, V, R>);
|
|
@@ -359,24 +359,24 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
359
359
|
* Adds a new node to the BST based on key comparison.
|
|
360
360
|
* @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
|
|
361
361
|
*
|
|
362
|
-
* @param keyNodeOrEntry - The key, node, or entry to
|
|
362
|
+
* @param keyNodeOrEntry - The key, node, or entry to set.
|
|
363
363
|
* @param [value] - The value, if providing just a key.
|
|
364
364
|
* @returns True if the addition was successful, false otherwise.
|
|
365
365
|
*/
|
|
366
|
-
|
|
366
|
+
set(keyNodeOrEntry: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): boolean;
|
|
367
367
|
/**
|
|
368
368
|
* Adds multiple items to the tree.
|
|
369
|
-
* @remarks If `isBalanceAdd` is true, sorts the input and builds a balanced tree. Time O(N log N) (due to sort and balanced
|
|
369
|
+
* @remarks If `isBalanceAdd` is true, sorts the input and builds a balanced tree. Time O(N log N) (due to sort and balanced set).
|
|
370
370
|
* If false, adds items one by one. Time O(N * H), which is O(N^2) worst-case.
|
|
371
371
|
* Space O(N) for sorting and recursion/iteration stack.
|
|
372
372
|
*
|
|
373
|
-
* @param keysNodesEntriesOrRaws - An iterable of items to
|
|
373
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set.
|
|
374
374
|
* @param [values] - An optional parallel iterable of values.
|
|
375
375
|
* @param [isBalanceAdd=true] - If true, builds a balanced tree from the items.
|
|
376
|
-
* @param [iterationType=this.iterationType] - The traversal method for balanced
|
|
377
|
-
* @returns An array of booleans indicating the success of each individual `
|
|
376
|
+
* @param [iterationType=this.iterationType] - The traversal method for balanced set (recursive or iterative).
|
|
377
|
+
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
378
378
|
*/
|
|
379
|
-
|
|
379
|
+
setMany(keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, BSTNode<K, V>>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): boolean[];
|
|
380
380
|
/**
|
|
381
381
|
* Returns the first key with a value >= target.
|
|
382
382
|
* Equivalent to Java TreeMap.ceiling.
|
|
@@ -242,7 +242,7 @@ export declare class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R
|
|
|
242
242
|
* @param [value]- See parameter type for details.
|
|
243
243
|
* @returns True if inserted or updated; false if ignored.
|
|
244
244
|
*/
|
|
245
|
-
|
|
245
|
+
set(keyNodeOrEntry: K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): boolean;
|
|
246
246
|
/**
|
|
247
247
|
* Delete a node by key/node/entry and rebalance as needed.
|
|
248
248
|
* @remarks Time O(log n), Space O(1)
|
|
@@ -154,7 +154,7 @@ export declare class TreeCounter<K = any, V = any, R = any> extends RedBlackTree
|
|
|
154
154
|
* @param [count] - How much to increase the node's count (default 1).
|
|
155
155
|
* @returns True if inserted/updated; false if ignored.
|
|
156
156
|
*/
|
|
157
|
-
|
|
157
|
+
set(keyNodeOrEntry: K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V, count?: number): boolean;
|
|
158
158
|
/**
|
|
159
159
|
* Delete a node (or decrement its count) and rebalance if needed.
|
|
160
160
|
* @remarks Time O(log N), Space O(1)
|
|
@@ -297,8 +297,8 @@ export declare class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTre
|
|
|
297
297
|
* @returns True if it's a TreeMultiMapNode, false otherwise.
|
|
298
298
|
*/
|
|
299
299
|
isNode(keyNodeOrEntry: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined): keyNodeOrEntry is TreeMultiMapNode<K, V>;
|
|
300
|
-
|
|
301
|
-
|
|
300
|
+
set(keyNodeOrEntry: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined): boolean;
|
|
301
|
+
set(key: K, value: V): boolean;
|
|
302
302
|
/**
|
|
303
303
|
* Delete a single value from the bucket at a given key. Removes the key if the bucket becomes empty.
|
|
304
304
|
* @remarks Time O(log N), Space O(1)
|
|
@@ -1195,9 +1195,9 @@ var binaryTreeTyped = (() => {
|
|
|
1195
1195
|
var BinaryTree = class extends IterableEntryBase {
|
|
1196
1196
|
/**
|
|
1197
1197
|
* Creates an instance of BinaryTree.
|
|
1198
|
-
* @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) `
|
|
1198
|
+
* @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.
|
|
1199
1199
|
*
|
|
1200
|
-
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to
|
|
1200
|
+
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
|
|
1201
1201
|
* @param [options] - Configuration options for the tree.
|
|
1202
1202
|
*/
|
|
1203
1203
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
@@ -1226,7 +1226,7 @@ var binaryTreeTyped = (() => {
|
|
|
1226
1226
|
if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
|
|
1227
1227
|
else if (toEntryFn) throw TypeError("toEntryFn must be a function type");
|
|
1228
1228
|
}
|
|
1229
|
-
if (keysNodesEntriesOrRaws) this.
|
|
1229
|
+
if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
|
|
1230
1230
|
}
|
|
1231
1231
|
/**
|
|
1232
1232
|
* Gets whether the tree is in Map mode.
|
|
@@ -1433,10 +1433,20 @@ var binaryTreeTyped = (() => {
|
|
|
1433
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 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).
|
|
1434
1434
|
*
|
|
1435
1435
|
* @param keyNodeOrEntry - The key, node, or entry to add.
|
|
1436
|
+
* @returns True if the addition was successful, false otherwise.
|
|
1437
|
+
*/
|
|
1438
|
+
add(keyNodeOrEntry) {
|
|
1439
|
+
return this.set(keyNodeOrEntry);
|
|
1440
|
+
}
|
|
1441
|
+
/**
|
|
1442
|
+
* Adds or updates a new node to the tree.
|
|
1443
|
+
* @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).
|
|
1444
|
+
*
|
|
1445
|
+
* @param keyNodeOrEntry - The key, node, or entry to set or update.
|
|
1436
1446
|
* @param [value] - The value, if providing just a key.
|
|
1437
1447
|
* @returns True if the addition was successful, false otherwise.
|
|
1438
1448
|
*/
|
|
1439
|
-
|
|
1449
|
+
set(keyNodeOrEntry, value) {
|
|
1440
1450
|
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
1441
1451
|
if (newNode === void 0) return false;
|
|
1442
1452
|
if (!this._root) {
|
|
@@ -1480,25 +1490,24 @@ var binaryTreeTyped = (() => {
|
|
|
1480
1490
|
return false;
|
|
1481
1491
|
}
|
|
1482
1492
|
/**
|
|
1483
|
-
* Adds
|
|
1484
|
-
* @remarks Time O(
|
|
1493
|
+
* Adds multiple items to the tree.
|
|
1494
|
+
* @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).
|
|
1485
1495
|
*
|
|
1486
|
-
* @param
|
|
1487
|
-
* @
|
|
1488
|
-
* @returns True if the addition was successful, false otherwise.
|
|
1496
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set.
|
|
1497
|
+
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
1489
1498
|
*/
|
|
1490
|
-
|
|
1491
|
-
return this.
|
|
1499
|
+
addMany(keysNodesEntriesOrRaws) {
|
|
1500
|
+
return this.setMany(keysNodesEntriesOrRaws);
|
|
1492
1501
|
}
|
|
1493
1502
|
/**
|
|
1494
|
-
* Adds multiple items to the tree.
|
|
1495
|
-
* @remarks Time O(N * M), where N is the number of items to
|
|
1503
|
+
* Adds or updates multiple items to the tree.
|
|
1504
|
+
* @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).
|
|
1496
1505
|
*
|
|
1497
|
-
* @param keysNodesEntriesOrRaws - An iterable of items to
|
|
1506
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set or update.
|
|
1498
1507
|
* @param [values] - An optional parallel iterable of values.
|
|
1499
|
-
* @returns An array of booleans indicating the success of each individual `
|
|
1508
|
+
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
1500
1509
|
*/
|
|
1501
|
-
|
|
1510
|
+
setMany(keysNodesEntriesOrRaws, values) {
|
|
1502
1511
|
const inserted = [];
|
|
1503
1512
|
let valuesIterator;
|
|
1504
1513
|
if (values) {
|
|
@@ -1513,40 +1522,29 @@ var binaryTreeTyped = (() => {
|
|
|
1513
1522
|
}
|
|
1514
1523
|
}
|
|
1515
1524
|
if (this.isRaw(keyNodeEntryOrRaw)) keyNodeEntryOrRaw = this._toEntryFn(keyNodeEntryOrRaw);
|
|
1516
|
-
inserted.push(this.
|
|
1525
|
+
inserted.push(this.set(keyNodeEntryOrRaw, value));
|
|
1517
1526
|
}
|
|
1518
1527
|
return inserted;
|
|
1519
1528
|
}
|
|
1520
1529
|
/**
|
|
1521
|
-
*
|
|
1522
|
-
* @remarks Time O(N * M), where N is the
|
|
1523
|
-
*
|
|
1524
|
-
* @param keysNodesEntriesOrRaws - An iterable of items to add or update.
|
|
1525
|
-
* @param [values] - An optional parallel iterable of values.
|
|
1526
|
-
* @returns An array of booleans indicating the success of each individual `add` operation.
|
|
1527
|
-
*/
|
|
1528
|
-
setMany(keysNodesEntriesOrRaws, values) {
|
|
1529
|
-
return this.addMany(keysNodesEntriesOrRaws, values);
|
|
1530
|
-
}
|
|
1531
|
-
/**
|
|
1532
|
-
* Merges another tree into this one by adding all its nodes.
|
|
1533
|
-
* @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`).
|
|
1530
|
+
* Merges another tree into this one by seting all its nodes.
|
|
1531
|
+
* @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`).
|
|
1534
1532
|
*
|
|
1535
1533
|
* @param anotherTree - The tree to merge.
|
|
1536
1534
|
*/
|
|
1537
1535
|
merge(anotherTree) {
|
|
1538
|
-
this.
|
|
1536
|
+
this.setMany(anotherTree, []);
|
|
1539
1537
|
}
|
|
1540
1538
|
/**
|
|
1541
1539
|
* Clears the tree and refills it with new items.
|
|
1542
|
-
* @remarks Time O(N) (for `clear`) + O(N * M) (for `
|
|
1540
|
+
* @remarks Time O(N) (for `clear`) + O(N * M) (for `setMany`) = O(N * M). Space O(M) (from `setMany`).
|
|
1543
1541
|
*
|
|
1544
|
-
* @param keysNodesEntriesOrRaws - An iterable of items to
|
|
1542
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set.
|
|
1545
1543
|
* @param [values] - An optional parallel iterable of values.
|
|
1546
1544
|
*/
|
|
1547
1545
|
refill(keysNodesEntriesOrRaws, values) {
|
|
1548
1546
|
this.clear();
|
|
1549
|
-
this.
|
|
1547
|
+
this.setMany(keysNodesEntriesOrRaws, values);
|
|
1550
1548
|
}
|
|
1551
1549
|
/**
|
|
1552
1550
|
* Deletes a node from the tree.
|
|
@@ -2212,7 +2210,7 @@ var binaryTreeTyped = (() => {
|
|
|
2212
2210
|
}
|
|
2213
2211
|
/**
|
|
2214
2212
|
* Clones the tree.
|
|
2215
|
-
* @remarks Time O(N * M), where N is the number of nodes and M is the tree size during insertion (due to `bfs` + `
|
|
2213
|
+
* @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.
|
|
2216
2214
|
*
|
|
2217
2215
|
* @returns A new, cloned instance of the tree.
|
|
2218
2216
|
*/
|
|
@@ -2223,7 +2221,7 @@ var binaryTreeTyped = (() => {
|
|
|
2223
2221
|
}
|
|
2224
2222
|
/**
|
|
2225
2223
|
* Creates a new tree containing only the entries that satisfy the predicate.
|
|
2226
|
-
* @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) `
|
|
2224
|
+
* @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.
|
|
2227
2225
|
*
|
|
2228
2226
|
* @param predicate - A function to test each [key, value] pair.
|
|
2229
2227
|
* @param [thisArg] - `this` context for the predicate.
|
|
@@ -2232,7 +2230,7 @@ var binaryTreeTyped = (() => {
|
|
|
2232
2230
|
filter(predicate, thisArg) {
|
|
2233
2231
|
const out = this._createInstance();
|
|
2234
2232
|
let i = 0;
|
|
2235
|
-
for (const [k, v] of this) if (predicate.call(thisArg, v, k, i++, this)) out.
|
|
2233
|
+
for (const [k, v] of this) if (predicate.call(thisArg, v, k, i++, this)) out.set([k, v]);
|
|
2236
2234
|
return out;
|
|
2237
2235
|
}
|
|
2238
2236
|
/**
|
|
@@ -2250,7 +2248,7 @@ var binaryTreeTyped = (() => {
|
|
|
2250
2248
|
map(cb, options, thisArg) {
|
|
2251
2249
|
const out = this._createLike([], options);
|
|
2252
2250
|
let i = 0;
|
|
2253
|
-
for (const [k, v] of this) out.
|
|
2251
|
+
for (const [k, v] of this) out.set(cb.call(thisArg, v, k, i++, this));
|
|
2254
2252
|
return out;
|
|
2255
2253
|
}
|
|
2256
2254
|
/**
|
|
@@ -2496,18 +2494,18 @@ var binaryTreeTyped = (() => {
|
|
|
2496
2494
|
return [this.createNode(keyNodeOrEntry, value), value];
|
|
2497
2495
|
}
|
|
2498
2496
|
/**
|
|
2499
|
-
* (Protected) Helper for cloning. Performs a BFS and
|
|
2500
|
-
* @remarks Time O(N * M) (O(N) BFS + O(M) `
|
|
2497
|
+
* (Protected) Helper for cloning. Performs a BFS and sets all nodes to the new tree.
|
|
2498
|
+
* @remarks Time O(N * M) (O(N) BFS + O(M) `set` for each node).
|
|
2501
2499
|
*
|
|
2502
2500
|
* @param cloned - The new, empty tree instance to populate.
|
|
2503
2501
|
*/
|
|
2504
2502
|
_clone(cloned) {
|
|
2505
2503
|
this.bfs(
|
|
2506
2504
|
(node) => {
|
|
2507
|
-
if (node === null) cloned.
|
|
2505
|
+
if (node === null) cloned.set(null);
|
|
2508
2506
|
else {
|
|
2509
|
-
if (this._isMapMode) cloned.
|
|
2510
|
-
else cloned.
|
|
2507
|
+
if (this._isMapMode) cloned.set([node.key, this._store.get(node.key)]);
|
|
2508
|
+
else cloned.set([node.key, node.value]);
|
|
2511
2509
|
}
|
|
2512
2510
|
},
|
|
2513
2511
|
this._root,
|