min-heap-typed 1.39.3 → 1.39.4
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/data-structures/binary-tree/avl-tree.d.ts +1 -1
- package/dist/data-structures/binary-tree/avl-tree.js +4 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +5 -13
- package/dist/data-structures/binary-tree/binary-tree.js +17 -25
- package/dist/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/data-structures/binary-tree/bst.js +6 -6
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.js +2 -2
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +3 -2
- package/src/data-structures/binary-tree/binary-tree.ts +19 -28
- package/src/data-structures/binary-tree/bst.ts +6 -6
- package/src/data-structures/binary-tree/tree-multiset.ts +2 -2
|
@@ -50,7 +50,7 @@ export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<
|
|
|
50
50
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
51
51
|
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
52
52
|
* included in the result. The `callback` parameter has a default value of
|
|
53
|
-
* `
|
|
53
|
+
* `((node: N) => node.key)`
|
|
54
54
|
* @returns The method is returning an array of `BinaryTreeDeletedResult<N>` objects.
|
|
55
55
|
*/
|
|
56
56
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback?: C): BinaryTreeDeletedResult<N>[];
|
|
@@ -63,10 +63,12 @@ class AVLTree extends bst_1.BST {
|
|
|
63
63
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
64
64
|
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
65
65
|
* included in the result. The `callback` parameter has a default value of
|
|
66
|
-
* `
|
|
66
|
+
* `((node: N) => node.key)`
|
|
67
67
|
* @returns The method is returning an array of `BinaryTreeDeletedResult<N>` objects.
|
|
68
68
|
*/
|
|
69
|
-
delete(identifier, callback =
|
|
69
|
+
delete(identifier, callback = ((node) => node.key)) {
|
|
70
|
+
if (identifier instanceof AVLTreeNode)
|
|
71
|
+
callback = (node => node);
|
|
70
72
|
const deletedResults = super.delete(identifier, callback);
|
|
71
73
|
for (const { needBalanced } of deletedResults) {
|
|
72
74
|
if (needBalanced) {
|
|
@@ -180,8 +180,8 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
180
180
|
* @returns The method is returning a boolean value.
|
|
181
181
|
*/
|
|
182
182
|
isPerfectlyBalanced(beginRoot?: N | null): boolean;
|
|
183
|
-
getNodes<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback
|
|
184
|
-
getNodes<C extends BTNCallback<N, N>>(identifier: N | null, callback
|
|
183
|
+
getNodes<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[];
|
|
184
|
+
getNodes<C extends BTNCallback<N, N>>(identifier: N | null, callback?: C, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[];
|
|
185
185
|
getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[];
|
|
186
186
|
has<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C, beginRoot?: N, iterationType?: IterationType): boolean;
|
|
187
187
|
has<C extends BTNCallback<N, N>>(identifier: N | null, callback?: C, beginRoot?: N, iterationType?: IterationType): boolean;
|
|
@@ -263,7 +263,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
263
263
|
* function on each node according to a specified order pattern.
|
|
264
264
|
* @param callback - The `callback` parameter is a function that will be called on each node during
|
|
265
265
|
* the depth-first search traversal. It takes a node as input and returns a value. The default value
|
|
266
|
-
* is `
|
|
266
|
+
* is `((node: N) => node.key)`, which is a callback function defined elsewhere in the code.
|
|
267
267
|
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter determines the order in which the
|
|
268
268
|
* nodes are visited during the depth-first search. There are three possible values for `pattern`:
|
|
269
269
|
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the depth-first
|
|
@@ -279,7 +279,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
279
279
|
* function on each node.
|
|
280
280
|
* @param callback - The `callback` parameter is a function that will be called for each node in the
|
|
281
281
|
* breadth-first search. It takes a node of type `N` as its argument and returns a value of type
|
|
282
|
-
* `ReturnType<BTNCallback<N>>`. The default value for this parameter is `
|
|
282
|
+
* `ReturnType<BTNCallback<N>>`. The default value for this parameter is `((node: N) => node.key)
|
|
283
283
|
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
|
|
284
284
|
* search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
|
|
285
285
|
* will not be performed and an empty array will be returned.
|
|
@@ -315,7 +315,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
315
315
|
* algorithm and returns an array of values obtained by applying a callback function to each node.
|
|
316
316
|
* @param callback - The `callback` parameter is a function that will be called on each node in the
|
|
317
317
|
* tree. It takes a node of type `N` as input and returns a value of type `ReturnType<BTNCallback<N>>`. The
|
|
318
|
-
* default value for this parameter is `
|
|
318
|
+
* default value for this parameter is `((node: N) => node.key)`.
|
|
319
319
|
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
|
|
320
320
|
* determines the order in which the nodes of a binary tree are traversed. It can have one of the
|
|
321
321
|
* following values:
|
|
@@ -342,14 +342,6 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
342
342
|
* @returns {N} - The destination node after the swap.
|
|
343
343
|
*/
|
|
344
344
|
protected _swap(srcNode: N, destNode: N): N;
|
|
345
|
-
/**
|
|
346
|
-
* Time complexity is O(n)
|
|
347
|
-
* Space complexity of Iterative dfs equals to recursive dfs which is O(n) because of the stack
|
|
348
|
-
* The Morris algorithm only modifies the tree's structure during traversal; once the traversal is complete,
|
|
349
|
-
* the tree's structure should be restored to its original state to maintain the tree's integrity.
|
|
350
|
-
* This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
|
|
351
|
-
*/
|
|
352
|
-
protected _defaultCallbackByKey: BTNCallback<N, BTNKey>;
|
|
353
345
|
/**
|
|
354
346
|
* The function `_addTo` adds a new node to a binary tree if there is an available position.
|
|
355
347
|
* @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to
|
|
@@ -90,14 +90,6 @@ class BinaryTree {
|
|
|
90
90
|
this._iterationType = types_1.IterationType.ITERATIVE;
|
|
91
91
|
this._root = null;
|
|
92
92
|
this._size = 0;
|
|
93
|
-
/**
|
|
94
|
-
* Time complexity is O(n)
|
|
95
|
-
* Space complexity of Iterative dfs equals to recursive dfs which is O(n) because of the stack
|
|
96
|
-
* The Morris algorithm only modifies the tree's structure during traversal; once the traversal is complete,
|
|
97
|
-
* the tree's structure should be restored to its original state to maintain the tree's integrity.
|
|
98
|
-
* This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
|
|
99
|
-
*/
|
|
100
|
-
this._defaultCallbackByKey = node => node.key;
|
|
101
93
|
if (options !== undefined) {
|
|
102
94
|
const { iterationType = types_1.IterationType.ITERATIVE } = options;
|
|
103
95
|
this._iterationType = iterationType;
|
|
@@ -192,7 +184,7 @@ class BinaryTree {
|
|
|
192
184
|
return;
|
|
193
185
|
}
|
|
194
186
|
const key = typeof keyOrNode === 'number' ? keyOrNode : keyOrNode ? keyOrNode.key : undefined;
|
|
195
|
-
const existNode = key !== undefined ? this.get(key,
|
|
187
|
+
const existNode = key !== undefined ? this.get(key, (node) => node.key) : undefined;
|
|
196
188
|
if (this.root) {
|
|
197
189
|
if (existNode) {
|
|
198
190
|
existNode.val = val;
|
|
@@ -262,9 +254,9 @@ class BinaryTree {
|
|
|
262
254
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
263
255
|
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
264
256
|
* included in the result. The `callback` parameter has a default value of
|
|
265
|
-
* `
|
|
257
|
+
* `((node: N) => node.key)`, which
|
|
266
258
|
*/
|
|
267
|
-
delete(identifier, callback =
|
|
259
|
+
delete(identifier, callback = ((node) => node.key)) {
|
|
268
260
|
const bstDeletedResult = [];
|
|
269
261
|
if (!this.root)
|
|
270
262
|
return bstDeletedResult;
|
|
@@ -455,7 +447,7 @@ class BinaryTree {
|
|
|
455
447
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
456
448
|
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
457
449
|
* included in the result. The `callback` parameter has a default value of
|
|
458
|
-
* `
|
|
450
|
+
* `((node: N) => node.key)`, which
|
|
459
451
|
* @param [onlyOne=false] - A boolean value indicating whether to stop searching after finding the
|
|
460
452
|
* first node that matches the identifier. If set to true, the function will return an array with
|
|
461
453
|
* only one element (or an empty array if no matching node is found). If set to false (default), the
|
|
@@ -467,7 +459,7 @@ class BinaryTree {
|
|
|
467
459
|
* traverse the binary tree. It can have two possible values:
|
|
468
460
|
* @returns The function `getNodes` returns an array of nodes (`N[]`).
|
|
469
461
|
*/
|
|
470
|
-
getNodes(identifier, callback =
|
|
462
|
+
getNodes(identifier, callback = ((node) => node.key), onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
471
463
|
if (!beginRoot)
|
|
472
464
|
return [];
|
|
473
465
|
if (identifier instanceof BinaryTreeNode)
|
|
@@ -512,7 +504,7 @@ class BinaryTree {
|
|
|
512
504
|
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
513
505
|
* matches the desired criteria. It takes a node as input and returns a boolean value indicating
|
|
514
506
|
* whether the node matches the criteria or not. The default callback function
|
|
515
|
-
* `
|
|
507
|
+
* `((node: N) => node.key)` is used if no callback function is
|
|
516
508
|
* @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
|
|
517
509
|
* the node from which the search should begin. By default, it is set to `this.root`, which means the
|
|
518
510
|
* search will start from the root node of the binary tree. However, you can provide a different node
|
|
@@ -521,7 +513,7 @@ class BinaryTree {
|
|
|
521
513
|
* performed when searching for nodes in the binary tree. It can have one of the following values:
|
|
522
514
|
* @returns a boolean value.
|
|
523
515
|
*/
|
|
524
|
-
has(identifier, callback =
|
|
516
|
+
has(identifier, callback = ((node) => node.key), beginRoot = this.root, iterationType = this.iterationType) {
|
|
525
517
|
if (identifier instanceof BinaryTreeNode)
|
|
526
518
|
callback = (node => node);
|
|
527
519
|
// TODO may support finding node by value equal
|
|
@@ -535,14 +527,14 @@ class BinaryTree {
|
|
|
535
527
|
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
536
528
|
* matches the desired criteria. It takes a node as input and returns a boolean value indicating
|
|
537
529
|
* whether the node matches the criteria or not. The default callback function
|
|
538
|
-
* (`
|
|
530
|
+
* (`((node: N) => node.key)`) is used if no callback function is
|
|
539
531
|
* @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
|
|
540
532
|
* the root node from which the search should begin.
|
|
541
533
|
* @param iterationType - The `iterationType` parameter specifies the type of iteration to be
|
|
542
534
|
* performed when searching for a node in the binary tree. It can have one of the following values:
|
|
543
535
|
* @returns either the found node (of type N) or null if no node is found.
|
|
544
536
|
*/
|
|
545
|
-
get(identifier, callback =
|
|
537
|
+
get(identifier, callback = ((node) => node.key), beginRoot = this.root, iterationType = this.iterationType) {
|
|
546
538
|
var _a;
|
|
547
539
|
if (identifier instanceof BinaryTreeNode)
|
|
548
540
|
callback = (node => node);
|
|
@@ -705,7 +697,7 @@ class BinaryTree {
|
|
|
705
697
|
* performed on the binary tree. It can have two possible values:
|
|
706
698
|
* @returns The function `subTreeTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
|
|
707
699
|
*/
|
|
708
|
-
subTreeTraverse(callback =
|
|
700
|
+
subTreeTraverse(callback = ((node) => node.key), beginRoot = this.root, iterationType = this.iterationType) {
|
|
709
701
|
if (typeof beginRoot === 'number')
|
|
710
702
|
beginRoot = this.get(beginRoot);
|
|
711
703
|
const ans = [];
|
|
@@ -735,7 +727,7 @@ class BinaryTree {
|
|
|
735
727
|
* function on each node according to a specified order pattern.
|
|
736
728
|
* @param callback - The `callback` parameter is a function that will be called on each node during
|
|
737
729
|
* the depth-first search traversal. It takes a node as input and returns a value. The default value
|
|
738
|
-
* is `
|
|
730
|
+
* is `((node: N) => node.key)`, which is a callback function defined elsewhere in the code.
|
|
739
731
|
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter determines the order in which the
|
|
740
732
|
* nodes are visited during the depth-first search. There are three possible values for `pattern`:
|
|
741
733
|
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the depth-first
|
|
@@ -745,7 +737,7 @@ class BinaryTree {
|
|
|
745
737
|
* iteration used in the depth-first search algorithm. It can have two possible values:
|
|
746
738
|
* @returns The function `dfs` returns an array of `ReturnType<BTNCallback<N>>` values.
|
|
747
739
|
*/
|
|
748
|
-
dfs(callback =
|
|
740
|
+
dfs(callback = ((node) => node.key), pattern = 'in', beginRoot = this.root, iterationType = types_1.IterationType.ITERATIVE) {
|
|
749
741
|
if (!beginRoot)
|
|
750
742
|
return [];
|
|
751
743
|
const ans = [];
|
|
@@ -820,7 +812,7 @@ class BinaryTree {
|
|
|
820
812
|
* function on each node.
|
|
821
813
|
* @param callback - The `callback` parameter is a function that will be called for each node in the
|
|
822
814
|
* breadth-first search. It takes a node of type `N` as its argument and returns a value of type
|
|
823
|
-
* `ReturnType<BTNCallback<N>>`. The default value for this parameter is `
|
|
815
|
+
* `ReturnType<BTNCallback<N>>`. The default value for this parameter is `((node: N) => node.key)
|
|
824
816
|
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
|
|
825
817
|
* search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
|
|
826
818
|
* will not be performed and an empty array will be returned.
|
|
@@ -828,7 +820,7 @@ class BinaryTree {
|
|
|
828
820
|
* in the breadth-first search (BFS) algorithm. It can have two possible values:
|
|
829
821
|
* @returns The function `bfs` returns an array of `ReturnType<BTNCallback<N>>[]`.
|
|
830
822
|
*/
|
|
831
|
-
bfs(callback =
|
|
823
|
+
bfs(callback = ((node) => node.key), beginRoot = this.root, iterationType = this.iterationType) {
|
|
832
824
|
if (!beginRoot)
|
|
833
825
|
return [];
|
|
834
826
|
const ans = [];
|
|
@@ -878,7 +870,7 @@ class BinaryTree {
|
|
|
878
870
|
* level in a binary tree. Each inner array contains the return type of the provided callback
|
|
879
871
|
* function `C` applied to the nodes at that level.
|
|
880
872
|
*/
|
|
881
|
-
listLevels(callback =
|
|
873
|
+
listLevels(callback = ((node) => node.key), beginRoot = this.root, iterationType = this.iterationType) {
|
|
882
874
|
if (!beginRoot)
|
|
883
875
|
return [];
|
|
884
876
|
const levelsNodes = [];
|
|
@@ -935,7 +927,7 @@ class BinaryTree {
|
|
|
935
927
|
* algorithm and returns an array of values obtained by applying a callback function to each node.
|
|
936
928
|
* @param callback - The `callback` parameter is a function that will be called on each node in the
|
|
937
929
|
* tree. It takes a node of type `N` as input and returns a value of type `ReturnType<BTNCallback<N>>`. The
|
|
938
|
-
* default value for this parameter is `
|
|
930
|
+
* default value for this parameter is `((node: N) => node.key)`.
|
|
939
931
|
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
|
|
940
932
|
* determines the order in which the nodes of a binary tree are traversed. It can have one of the
|
|
941
933
|
* following values:
|
|
@@ -944,7 +936,7 @@ class BinaryTree {
|
|
|
944
936
|
* `beginRoot` is `null`, an empty array will be returned.
|
|
945
937
|
* @returns The `morris` function returns an array of `ReturnType<BTNCallback<N>>` values.
|
|
946
938
|
*/
|
|
947
|
-
morris(callback =
|
|
939
|
+
morris(callback = ((node) => node.key), pattern = 'in', beginRoot = this.root) {
|
|
948
940
|
if (beginRoot === null)
|
|
949
941
|
return [];
|
|
950
942
|
const ans = [];
|
|
@@ -97,7 +97,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
97
97
|
* generic type `N`.
|
|
98
98
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
99
99
|
* value. This value is compared with the `nodeProperty` parameter to determine if the node should be
|
|
100
|
-
* included in the result. The default value for `callback` is `
|
|
100
|
+
* included in the result. The default value for `callback` is `((node: N) => node.key)`, which is
|
|
101
101
|
* a
|
|
102
102
|
* @param [onlyOne=false] - A boolean value indicating whether to stop the traversal after finding
|
|
103
103
|
* the first node that matches the nodeProperty. If set to true, the function will return an array
|
|
@@ -223,7 +223,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
223
223
|
* @returns either the first node that matches the given nodeProperty and callback, or null if no
|
|
224
224
|
* matching node is found.
|
|
225
225
|
*/
|
|
226
|
-
get(identifier, callback =
|
|
226
|
+
get(identifier, callback = ((node) => node.key), beginRoot = this.root, iterationType = this.iterationType) {
|
|
227
227
|
var _a;
|
|
228
228
|
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : null;
|
|
229
229
|
}
|
|
@@ -259,7 +259,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
259
259
|
* generic type `N`.
|
|
260
260
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
261
261
|
* value. This value is compared with the `nodeProperty` parameter to determine if the node should be
|
|
262
|
-
* included in the result. The default value for `callback` is `
|
|
262
|
+
* included in the result. The default value for `callback` is `((node: N) => node.key)`, which is
|
|
263
263
|
* a
|
|
264
264
|
* @param [onlyOne=false] - A boolean value indicating whether to stop the traversal after finding
|
|
265
265
|
* the first node that matches the nodeProperty. If set to true, the function will return an array
|
|
@@ -272,7 +272,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
272
272
|
* traverse the binary tree. It can have one of the following values:
|
|
273
273
|
* @returns an array of nodes (N[]).
|
|
274
274
|
*/
|
|
275
|
-
getNodes(identifier, callback =
|
|
275
|
+
getNodes(identifier, callback = ((node) => node.key), onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
276
276
|
if (!beginRoot)
|
|
277
277
|
return [];
|
|
278
278
|
const ans = [];
|
|
@@ -287,7 +287,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
287
287
|
if (!cur.left && !cur.right)
|
|
288
288
|
return;
|
|
289
289
|
// TODO potential bug
|
|
290
|
-
if (callback ===
|
|
290
|
+
if (callback === ((node) => node.key)) {
|
|
291
291
|
if (this._compare(cur.key, identifier) === types_1.CP.gt)
|
|
292
292
|
cur.left && _traverse(cur.left);
|
|
293
293
|
if (this._compare(cur.key, identifier) === types_1.CP.lt)
|
|
@@ -312,7 +312,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
312
312
|
return ans;
|
|
313
313
|
}
|
|
314
314
|
// TODO potential bug
|
|
315
|
-
if (callback ===
|
|
315
|
+
if (callback === ((node) => node.key)) {
|
|
316
316
|
if (this._compare(cur.key, identifier) === types_1.CP.gt)
|
|
317
317
|
cur.left && queue.push(cur.left);
|
|
318
318
|
if (this._compare(cur.key, identifier) === types_1.CP.lt)
|
|
@@ -345,7 +345,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
345
345
|
* done recursively or iteratively. It can have two possible values:
|
|
346
346
|
* @returns The function `lesserOrGreaterTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
|
|
347
347
|
*/
|
|
348
|
-
lesserOrGreaterTraverse(callback =
|
|
348
|
+
lesserOrGreaterTraverse(callback = ((node) => node.key), lesserOrGreater = types_1.CP.lt, targetNode = this.root, iterationType = this.iterationType) {
|
|
349
349
|
if (typeof targetNode === 'number')
|
|
350
350
|
targetNode = this.get(targetNode);
|
|
351
351
|
const ans = [];
|
|
@@ -98,7 +98,7 @@ export declare class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = Tr
|
|
|
98
98
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
99
99
|
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
100
100
|
* included in the result. The `callback` parameter has a default value of
|
|
101
|
-
* `
|
|
101
|
+
* `((node: N) => node.key)`
|
|
102
102
|
* @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
|
|
103
103
|
* being deleted. If set to true, the count of the node will not be considered and the node will be
|
|
104
104
|
* deleted regardless of its count. If set to false (default), the count of the node will be
|
|
@@ -251,14 +251,14 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
251
251
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
252
252
|
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
253
253
|
* included in the result. The `callback` parameter has a default value of
|
|
254
|
-
* `
|
|
254
|
+
* `((node: N) => node.key)`
|
|
255
255
|
* @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
|
|
256
256
|
* being deleted. If set to true, the count of the node will not be considered and the node will be
|
|
257
257
|
* deleted regardless of its count. If set to false (default), the count of the node will be
|
|
258
258
|
* decremented by 1 and
|
|
259
259
|
* @returns The method `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
260
260
|
*/
|
|
261
|
-
delete(identifier, callback =
|
|
261
|
+
delete(identifier, callback = ((node) => node.key), ignoreCount = false) {
|
|
262
262
|
const bstDeletedResult = [];
|
|
263
263
|
if (!this.root)
|
|
264
264
|
return bstDeletedResult;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "min-heap-typed",
|
|
3
|
-
"version": "1.39.
|
|
3
|
+
"version": "1.39.4",
|
|
4
4
|
"description": "Min Heap. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -131,6 +131,6 @@
|
|
|
131
131
|
"typescript": "^4.9.5"
|
|
132
132
|
},
|
|
133
133
|
"dependencies": {
|
|
134
|
-
"data-structure-typed": "^1.39.
|
|
134
|
+
"data-structure-typed": "^1.39.4"
|
|
135
135
|
}
|
|
136
136
|
}
|
|
@@ -71,13 +71,14 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|
|
71
71
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
72
72
|
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
73
73
|
* included in the result. The `callback` parameter has a default value of
|
|
74
|
-
* `
|
|
74
|
+
* `((node: N) => node.key)`
|
|
75
75
|
* @returns The method is returning an array of `BinaryTreeDeletedResult<N>` objects.
|
|
76
76
|
*/
|
|
77
77
|
override delete<C extends BTNCallback<N>>(
|
|
78
78
|
identifier: ReturnType<C>,
|
|
79
|
-
callback: C =
|
|
79
|
+
callback: C = ((node: N) => node.key) as C
|
|
80
80
|
): BinaryTreeDeletedResult<N>[] {
|
|
81
|
+
if ((identifier as any) instanceof AVLTreeNode) callback = (node => node) as C;
|
|
81
82
|
const deletedResults = super.delete(identifier, callback);
|
|
82
83
|
for (const {needBalanced} of deletedResults) {
|
|
83
84
|
if (needBalanced) {
|
|
@@ -217,7 +217,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
217
217
|
}
|
|
218
218
|
|
|
219
219
|
const key = typeof keyOrNode === 'number' ? keyOrNode : keyOrNode ? keyOrNode.key : undefined;
|
|
220
|
-
const existNode = key !== undefined ? this.get(key,
|
|
220
|
+
const existNode = key !== undefined ? this.get(key, (node: N) => node.key) : undefined;
|
|
221
221
|
|
|
222
222
|
if (this.root) {
|
|
223
223
|
if (existNode) {
|
|
@@ -296,11 +296,11 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
296
296
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
297
297
|
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
298
298
|
* included in the result. The `callback` parameter has a default value of
|
|
299
|
-
* `
|
|
299
|
+
* `((node: N) => node.key)`, which
|
|
300
300
|
*/
|
|
301
301
|
delete<C extends BTNCallback<N>>(
|
|
302
302
|
identifier: ReturnType<C> | null,
|
|
303
|
-
callback: C =
|
|
303
|
+
callback: C = ((node: N) => node.key) as C
|
|
304
304
|
): BinaryTreeDeletedResult<N>[] {
|
|
305
305
|
const bstDeletedResult: BinaryTreeDeletedResult<N>[] = [];
|
|
306
306
|
if (!this.root) return bstDeletedResult;
|
|
@@ -487,7 +487,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
487
487
|
|
|
488
488
|
getNodes<C extends BTNCallback<N, BTNKey>>(
|
|
489
489
|
identifier: BTNKey,
|
|
490
|
-
callback
|
|
490
|
+
callback?: C,
|
|
491
491
|
onlyOne?: boolean,
|
|
492
492
|
beginRoot?: N | null,
|
|
493
493
|
iterationType?: IterationType
|
|
@@ -495,7 +495,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
495
495
|
|
|
496
496
|
getNodes<C extends BTNCallback<N, N>>(
|
|
497
497
|
identifier: N | null,
|
|
498
|
-
callback
|
|
498
|
+
callback?: C,
|
|
499
499
|
onlyOne?: boolean,
|
|
500
500
|
beginRoot?: N | null,
|
|
501
501
|
iterationType?: IterationType
|
|
@@ -518,7 +518,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
518
518
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
519
519
|
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
520
520
|
* included in the result. The `callback` parameter has a default value of
|
|
521
|
-
* `
|
|
521
|
+
* `((node: N) => node.key)`, which
|
|
522
522
|
* @param [onlyOne=false] - A boolean value indicating whether to stop searching after finding the
|
|
523
523
|
* first node that matches the identifier. If set to true, the function will return an array with
|
|
524
524
|
* only one element (or an empty array if no matching node is found). If set to false (default), the
|
|
@@ -532,7 +532,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
532
532
|
*/
|
|
533
533
|
getNodes<C extends BTNCallback<N>>(
|
|
534
534
|
identifier: ReturnType<C> | null,
|
|
535
|
-
callback: C =
|
|
535
|
+
callback: C = ((node: N) => node.key) as C,
|
|
536
536
|
onlyOne = false,
|
|
537
537
|
beginRoot: N | null = this.root,
|
|
538
538
|
iterationType = this.iterationType
|
|
@@ -600,7 +600,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
600
600
|
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
601
601
|
* matches the desired criteria. It takes a node as input and returns a boolean value indicating
|
|
602
602
|
* whether the node matches the criteria or not. The default callback function
|
|
603
|
-
* `
|
|
603
|
+
* `((node: N) => node.key)` is used if no callback function is
|
|
604
604
|
* @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
|
|
605
605
|
* the node from which the search should begin. By default, it is set to `this.root`, which means the
|
|
606
606
|
* search will start from the root node of the binary tree. However, you can provide a different node
|
|
@@ -611,7 +611,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
611
611
|
*/
|
|
612
612
|
has<C extends BTNCallback<N>>(
|
|
613
613
|
identifier: ReturnType<C> | null,
|
|
614
|
-
callback: C =
|
|
614
|
+
callback: C = ((node: N) => node.key) as C,
|
|
615
615
|
beginRoot = this.root,
|
|
616
616
|
iterationType = this.iterationType
|
|
617
617
|
): boolean {
|
|
@@ -649,7 +649,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
649
649
|
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
650
650
|
* matches the desired criteria. It takes a node as input and returns a boolean value indicating
|
|
651
651
|
* whether the node matches the criteria or not. The default callback function
|
|
652
|
-
* (`
|
|
652
|
+
* (`((node: N) => node.key)`) is used if no callback function is
|
|
653
653
|
* @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
|
|
654
654
|
* the root node from which the search should begin.
|
|
655
655
|
* @param iterationType - The `iterationType` parameter specifies the type of iteration to be
|
|
@@ -658,7 +658,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
658
658
|
*/
|
|
659
659
|
get<C extends BTNCallback<N>>(
|
|
660
660
|
identifier: ReturnType<C> | null,
|
|
661
|
-
callback: C =
|
|
661
|
+
callback: C = ((node: N) => node.key) as C,
|
|
662
662
|
beginRoot = this.root,
|
|
663
663
|
iterationType = this.iterationType
|
|
664
664
|
): N | null {
|
|
@@ -824,7 +824,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
824
824
|
* @returns The function `subTreeTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
|
|
825
825
|
*/
|
|
826
826
|
subTreeTraverse<C extends BTNCallback<N>>(
|
|
827
|
-
callback: C =
|
|
827
|
+
callback: C = ((node: N) => node.key) as C,
|
|
828
828
|
beginRoot: BTNKey | N | null = this.root,
|
|
829
829
|
iterationType = this.iterationType
|
|
830
830
|
): ReturnType<C>[] {
|
|
@@ -860,7 +860,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
860
860
|
* function on each node according to a specified order pattern.
|
|
861
861
|
* @param callback - The `callback` parameter is a function that will be called on each node during
|
|
862
862
|
* the depth-first search traversal. It takes a node as input and returns a value. The default value
|
|
863
|
-
* is `
|
|
863
|
+
* is `((node: N) => node.key)`, which is a callback function defined elsewhere in the code.
|
|
864
864
|
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter determines the order in which the
|
|
865
865
|
* nodes are visited during the depth-first search. There are three possible values for `pattern`:
|
|
866
866
|
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the depth-first
|
|
@@ -871,7 +871,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
871
871
|
* @returns The function `dfs` returns an array of `ReturnType<BTNCallback<N>>` values.
|
|
872
872
|
*/
|
|
873
873
|
dfs<C extends BTNCallback<N>>(
|
|
874
|
-
callback: C =
|
|
874
|
+
callback: C = ((node: N) => node.key) as C,
|
|
875
875
|
pattern: DFSOrderPattern = 'in',
|
|
876
876
|
beginRoot: N | null = this.root,
|
|
877
877
|
iterationType: IterationType = IterationType.ITERATIVE
|
|
@@ -946,7 +946,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
946
946
|
* function on each node.
|
|
947
947
|
* @param callback - The `callback` parameter is a function that will be called for each node in the
|
|
948
948
|
* breadth-first search. It takes a node of type `N` as its argument and returns a value of type
|
|
949
|
-
* `ReturnType<BTNCallback<N>>`. The default value for this parameter is `
|
|
949
|
+
* `ReturnType<BTNCallback<N>>`. The default value for this parameter is `((node: N) => node.key)
|
|
950
950
|
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
|
|
951
951
|
* search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
|
|
952
952
|
* will not be performed and an empty array will be returned.
|
|
@@ -955,7 +955,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
955
955
|
* @returns The function `bfs` returns an array of `ReturnType<BTNCallback<N>>[]`.
|
|
956
956
|
*/
|
|
957
957
|
bfs<C extends BTNCallback<N>>(
|
|
958
|
-
callback: C =
|
|
958
|
+
callback: C = ((node: N) => node.key) as C,
|
|
959
959
|
beginRoot: N | null = this.root,
|
|
960
960
|
iterationType = this.iterationType
|
|
961
961
|
): ReturnType<C>[] {
|
|
@@ -1012,7 +1012,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1012
1012
|
* function `C` applied to the nodes at that level.
|
|
1013
1013
|
*/
|
|
1014
1014
|
listLevels<C extends BTNCallback<N>>(
|
|
1015
|
-
callback: C =
|
|
1015
|
+
callback: C = ((node: N) => node.key) as C,
|
|
1016
1016
|
beginRoot: N | null = this.root,
|
|
1017
1017
|
iterationType = this.iterationType
|
|
1018
1018
|
): ReturnType<C>[][] {
|
|
@@ -1071,7 +1071,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1071
1071
|
* algorithm and returns an array of values obtained by applying a callback function to each node.
|
|
1072
1072
|
* @param callback - The `callback` parameter is a function that will be called on each node in the
|
|
1073
1073
|
* tree. It takes a node of type `N` as input and returns a value of type `ReturnType<BTNCallback<N>>`. The
|
|
1074
|
-
* default value for this parameter is `
|
|
1074
|
+
* default value for this parameter is `((node: N) => node.key)`.
|
|
1075
1075
|
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
|
|
1076
1076
|
* determines the order in which the nodes of a binary tree are traversed. It can have one of the
|
|
1077
1077
|
* following values:
|
|
@@ -1081,7 +1081,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1081
1081
|
* @returns The `morris` function returns an array of `ReturnType<BTNCallback<N>>` values.
|
|
1082
1082
|
*/
|
|
1083
1083
|
morris<C extends BTNCallback<N>>(
|
|
1084
|
-
callback: C =
|
|
1084
|
+
callback: C = ((node: N) => node.key) as C,
|
|
1085
1085
|
pattern: DFSOrderPattern = 'in',
|
|
1086
1086
|
beginRoot: N | null = this.root
|
|
1087
1087
|
): ReturnType<C>[] {
|
|
@@ -1226,15 +1226,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1226
1226
|
return destNode;
|
|
1227
1227
|
}
|
|
1228
1228
|
|
|
1229
|
-
/**
|
|
1230
|
-
* Time complexity is O(n)
|
|
1231
|
-
* Space complexity of Iterative dfs equals to recursive dfs which is O(n) because of the stack
|
|
1232
|
-
* The Morris algorithm only modifies the tree's structure during traversal; once the traversal is complete,
|
|
1233
|
-
* the tree's structure should be restored to its original state to maintain the tree's integrity.
|
|
1234
|
-
* This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
|
|
1235
|
-
*/
|
|
1236
|
-
protected _defaultCallbackByKey: BTNCallback<N, BTNKey> = node => node.key;
|
|
1237
|
-
|
|
1238
1229
|
/**
|
|
1239
1230
|
* The function `_addTo` adds a new node to a binary tree if there is an available position.
|
|
1240
1231
|
* @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to
|
|
@@ -234,7 +234,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
234
234
|
*/
|
|
235
235
|
override get<C extends BTNCallback<N>>(
|
|
236
236
|
identifier: ReturnType<C> | null,
|
|
237
|
-
callback: C =
|
|
237
|
+
callback: C = ((node: N) => node.key) as C,
|
|
238
238
|
beginRoot = this.root,
|
|
239
239
|
iterationType = this.iterationType
|
|
240
240
|
): N | null {
|
|
@@ -270,7 +270,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
270
270
|
* generic type `N`.
|
|
271
271
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
272
272
|
* value. This value is compared with the `nodeProperty` parameter to determine if the node should be
|
|
273
|
-
* included in the result. The default value for `callback` is `
|
|
273
|
+
* included in the result. The default value for `callback` is `((node: N) => node.key)`, which is
|
|
274
274
|
* a
|
|
275
275
|
* @param [onlyOne=false] - A boolean value indicating whether to stop the traversal after finding
|
|
276
276
|
* the first node that matches the nodeProperty. If set to true, the function will return an array
|
|
@@ -285,7 +285,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
285
285
|
*/
|
|
286
286
|
override getNodes<C extends BTNCallback<N>>(
|
|
287
287
|
identifier: ReturnType<C> | null,
|
|
288
|
-
callback: C =
|
|
288
|
+
callback: C = ((node: N) => node.key) as C,
|
|
289
289
|
onlyOne = false,
|
|
290
290
|
beginRoot: N | null = this.root,
|
|
291
291
|
iterationType = this.iterationType
|
|
@@ -303,7 +303,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
303
303
|
|
|
304
304
|
if (!cur.left && !cur.right) return;
|
|
305
305
|
// TODO potential bug
|
|
306
|
-
if (callback ===
|
|
306
|
+
if (callback === ((node: N) => node.key)) {
|
|
307
307
|
if (this._compare(cur.key, identifier as number) === CP.gt) cur.left && _traverse(cur.left);
|
|
308
308
|
if (this._compare(cur.key, identifier as number) === CP.lt) cur.right && _traverse(cur.right);
|
|
309
309
|
} else {
|
|
@@ -324,7 +324,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
324
324
|
if (onlyOne) return ans;
|
|
325
325
|
}
|
|
326
326
|
// TODO potential bug
|
|
327
|
-
if (callback ===
|
|
327
|
+
if (callback === ((node: N) => node.key)) {
|
|
328
328
|
if (this._compare(cur.key, identifier as number) === CP.gt) cur.left && queue.push(cur.left);
|
|
329
329
|
if (this._compare(cur.key, identifier as number) === CP.lt) cur.right && queue.push(cur.right);
|
|
330
330
|
} else {
|
|
@@ -358,7 +358,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
358
358
|
* @returns The function `lesserOrGreaterTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
|
|
359
359
|
*/
|
|
360
360
|
lesserOrGreaterTraverse<C extends BTNCallback<N>>(
|
|
361
|
-
callback: C =
|
|
361
|
+
callback: C = ((node: N) => node.key) as C,
|
|
362
362
|
lesserOrGreater: CP = CP.lt,
|
|
363
363
|
targetNode: BTNKey | N | null = this.root,
|
|
364
364
|
iterationType = this.iterationType
|
|
@@ -268,7 +268,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
268
268
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
269
269
|
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
270
270
|
* included in the result. The `callback` parameter has a default value of
|
|
271
|
-
* `
|
|
271
|
+
* `((node: N) => node.key)`
|
|
272
272
|
* @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
|
|
273
273
|
* being deleted. If set to true, the count of the node will not be considered and the node will be
|
|
274
274
|
* deleted regardless of its count. If set to false (default), the count of the node will be
|
|
@@ -277,7 +277,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
277
277
|
*/
|
|
278
278
|
override delete<C extends BTNCallback<N>>(
|
|
279
279
|
identifier: ReturnType<C>,
|
|
280
|
-
callback: C =
|
|
280
|
+
callback: C = ((node: N) => node.key) as C,
|
|
281
281
|
ignoreCount = false
|
|
282
282
|
): BinaryTreeDeletedResult<N>[] {
|
|
283
283
|
const bstDeletedResult: BinaryTreeDeletedResult<N>[] = [];
|