min-heap-typed 1.38.1 → 1.38.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.
@@ -1,63 +1,63 @@
1
-
2
1
  <!doctype html>
3
2
  <html lang="en">
4
3
 
5
4
  <head>
6
5
  <title>Code coverage report for index.ts</title>
7
- <meta charset="utf-8" />
8
- <link rel="stylesheet" href="prettify.css" />
9
- <link rel="stylesheet" href="base.css" />
10
- <link rel="shortcut icon" type="image/x-icon" href="favicon.png" />
11
- <meta name="viewport" content="width=device-width, initial-scale=1" />
6
+ <meta charset="utf-8"/>
7
+ <link href="prettify.css" rel="stylesheet"/>
8
+ <link href="base.css" rel="stylesheet"/>
9
+ <link href="favicon.png" rel="shortcut icon" type="image/x-icon"/>
10
+ <meta content="width=device-width, initial-scale=1" name="viewport"/>
12
11
  <style type='text/css'>
13
12
  .coverage-summary .sorter {
14
13
  background-image: url(sort-arrow-sprite.png);
15
14
  }
16
15
  </style>
17
16
  </head>
18
-
17
+
19
18
  <body>
20
19
  <div class='wrapper'>
21
20
  <div class='pad1'>
22
21
  <h1><a href="index.html">All files</a> index.ts</h1>
23
22
  <div class='clearfix'>
24
-
23
+
25
24
  <div class='fl pad1y space-right2'>
26
25
  <span class="strong">100% </span>
27
26
  <span class="quiet">Statements</span>
28
27
  <span class='fraction'>3/3</span>
29
28
  </div>
30
-
31
-
29
+
30
+
32
31
  <div class='fl pad1y space-right2'>
33
32
  <span class="strong">100% </span>
34
33
  <span class="quiet">Branches</span>
35
34
  <span class='fraction'>0/0</span>
36
35
  </div>
37
-
38
-
36
+
37
+
39
38
  <div class='fl pad1y space-right2'>
40
39
  <span class="strong">100% </span>
41
40
  <span class="quiet">Functions</span>
42
41
  <span class='fraction'>2/2</span>
43
42
  </div>
44
-
45
-
43
+
44
+
46
45
  <div class='fl pad1y space-right2'>
47
46
  <span class="strong">100% </span>
48
47
  <span class="quiet">Lines</span>
49
48
  <span class='fraction'>1/1</span>
50
49
  </div>
51
-
52
-
50
+
51
+
53
52
  </div>
54
53
  <p class="quiet">
55
- Press <em>n</em> or <em>j</em> to go to the next uncovered block, <em>b</em>, <em>p</em> or <em>k</em> for the previous block.
54
+ Press <em>n</em> or <em>j</em> to go to the next uncovered block, <em>b</em>, <em>p</em> or <em>k</em> for
55
+ the previous block.
56
56
  </p>
57
57
  <template id="filterTemplate">
58
58
  <div class="quiet">
59
59
  Filter:
60
- <input oninput="onInput()" type="search" id="fileSearch">
60
+ <input id="fileSearch" oninput="onInput()" type="search">
61
61
  </div>
62
62
  </template>
63
63
  </div>
@@ -71,7 +71,8 @@
71
71
  <a name='L6'></a><a href='#L6'>6</a>
72
72
  <a name='L7'></a><a href='#L7'>7</a>
73
73
  <a name='L8'></a><a href='#L8'>8</a>
74
- <a name='L9'></a><a href='#L9'>9</a></td><td class="line-coverage quiet"><span class="cline-any cline-neutral">&nbsp;</span>
74
+ <a name='L9'></a><a href='#L9'>9</a></td><td class="line-coverage quiet"><span
75
+ class="cline-any cline-neutral">&nbsp;</span>
75
76
  <span class="cline-any cline-neutral">&nbsp;</span>
76
77
  <span class="cline-any cline-neutral">&nbsp;</span>
77
78
  <span class="cline-any cline-neutral">&nbsp;</span>
@@ -89,21 +90,21 @@
89
90
  export { MinHeap, HeapItem } from 'data-structure-typed';
90
91
  &nbsp;</pre></td></tr></table></pre>
91
92
 
92
- <div class='push'></div><!-- for sticky footer -->
93
- </div><!-- /wrapper -->
94
- <div class='footer quiet pad2 space-top1 center small'>
95
- Code coverage generated by
96
- <a href="https://istanbul.js.org/" target="_blank" rel="noopener noreferrer">istanbul</a>
97
- at 2023-10-08T10:23:16.441Z
98
- </div>
99
- <script src="prettify.js"></script>
100
- <script>
101
- window.onload = function () {
102
- prettyPrint();
103
- };
104
- </script>
105
- <script src="sorter.js"></script>
106
- <script src="block-navigation.js"></script>
107
- </body>
93
+ <div class='push'></div><!-- for sticky footer -->
94
+ </div><!-- /wrapper -->
95
+ <div class='footer quiet pad2 space-top1 center small'>
96
+ Code coverage generated by
97
+ <a href="https://istanbul.js.org/" rel="noopener noreferrer" target="_blank">istanbul</a>
98
+ at 2023-10-08T10:23:16.441Z
99
+ </div>
100
+ <script src="prettify.js"></script>
101
+ <script>
102
+ window.onload = function () {
103
+ prettyPrint();
104
+ };
105
+ </script>
106
+ <script src="sorter.js"></script>
107
+ <script src="block-navigation.js"></script>
108
+ </body>
108
109
  </html>
109
110
 
@@ -1,5 +1,5 @@
1
1
  /* eslint-disable */
2
- var addSorting = (function() {
2
+ var addSorting = (function () {
3
3
  'use strict';
4
4
  var cols,
5
5
  currentSort = {
@@ -11,14 +11,17 @@ var addSorting = (function() {
11
11
  function getTable() {
12
12
  return document.querySelector('.coverage-summary');
13
13
  }
14
+
14
15
  // returns the thead element of the summary table
15
16
  function getTableHeader() {
16
17
  return getTable().querySelector('thead tr');
17
18
  }
19
+
18
20
  // returns the tbody element of the summary table
19
21
  function getTableBody() {
20
22
  return getTable().querySelector('tbody');
21
23
  }
24
+
22
25
  // returns the th element for nth column
23
26
  function getNthColumn(n) {
24
27
  return getTableHeader().querySelectorAll('th')[n];
@@ -73,6 +76,7 @@ var addSorting = (function() {
73
76
  }
74
77
  return cols;
75
78
  }
79
+
76
80
  // attaches a data attribute to every tr element with an object
77
81
  // of data values keyed by column name
78
82
  function loadRowData(tableRow) {
@@ -93,6 +97,7 @@ var addSorting = (function() {
93
97
  }
94
98
  return data;
95
99
  }
100
+
96
101
  // loads all row data
97
102
  function loadData() {
98
103
  var rows = getTableBody().querySelectorAll('tr'),
@@ -102,10 +107,11 @@ var addSorting = (function() {
102
107
  rows[i].data = loadRowData(rows[i]);
103
108
  }
104
109
  }
110
+
105
111
  // sorts the table using the data for the ith column
106
112
  function sortByIndex(index, desc) {
107
113
  var key = cols[index].key,
108
- sorter = function(a, b) {
114
+ sorter = function (a, b) {
109
115
  a = a.data[key];
110
116
  b = b.data[key];
111
117
  return a < b ? -1 : a > b ? 1 : 0;
@@ -117,7 +123,7 @@ var addSorting = (function() {
117
123
  i;
118
124
 
119
125
  if (desc) {
120
- finalSorter = function(a, b) {
126
+ finalSorter = function (a, b) {
121
127
  return -1 * sorter(a, b);
122
128
  };
123
129
  }
@@ -133,6 +139,7 @@ var addSorting = (function() {
133
139
  tableBody.appendChild(rows[i]);
134
140
  }
135
141
  }
142
+
136
143
  // removes sort indicators for current column being sorted
137
144
  function removeSortIndicators() {
138
145
  var col = getNthColumn(currentSort.index),
@@ -141,12 +148,14 @@ var addSorting = (function() {
141
148
  cls = cls.replace(/ sorted$/, '').replace(/ sorted-desc$/, '');
142
149
  col.className = cls;
143
150
  }
151
+
144
152
  // adds sort indicators for current column being sorted
145
153
  function addSortIndicators() {
146
154
  getNthColumn(currentSort.index).className += currentSort.desc
147
155
  ? ' sorted-desc'
148
156
  : ' sorted';
149
157
  }
158
+
150
159
  // adds event listeners for all sorter widgets
151
160
  function enableUI() {
152
161
  var i,
@@ -154,7 +163,7 @@ var addSorting = (function() {
154
163
  ithSorter = function ithSorter(i) {
155
164
  var col = cols[i];
156
165
 
157
- return function() {
166
+ return function () {
158
167
  var desc = col.defaultDescSort;
159
168
 
160
169
  if (currentSort.index === i) {
@@ -180,8 +189,9 @@ var addSorting = (function() {
180
189
  }
181
190
  }
182
191
  }
192
+
183
193
  // adds sorting functionality to the UI
184
- return function() {
194
+ return function () {
185
195
  if (!getTable()) {
186
196
  return;
187
197
  }
@@ -5,7 +5,7 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { BFSCallback, BFSCallbackReturn, BinaryTreeNodeKey, BinaryTreeNodeNested, BinaryTreeOptions, MapCallback, MapCallbackReturn } from '../../types';
8
+ import type { BFSCallback, BinaryTreeNodeKey, BinaryTreeNodeNested, BinaryTreeOptions, MapCallback } from '../../types';
9
9
  import { BinaryTreeDeletedResult, DFSOrderPattern, FamilyPosition, IterationType } from '../../types';
10
10
  import { IBinaryTree } from '../../interfaces';
11
11
  /**
@@ -207,7 +207,7 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
207
207
  * traverse the binary tree. It can have two possible values:
208
208
  * @returns The function `getNodes` returns an array of nodes (`N[]`).
209
209
  */
210
- getNodes(nodeProperty: BinaryTreeNodeKey | N, callback?: MapCallback<N>, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[];
210
+ getNodes<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(nodeProperty: BinaryTreeNodeKey | N, callback?: C, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[];
211
211
  /**
212
212
  * The function checks if a binary tree has a node with a given property or key.
213
213
  * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is the key or value of
@@ -225,7 +225,7 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
225
225
  * performed when searching for nodes in the binary tree. It can have one of the following values:
226
226
  * @returns a boolean value.
227
227
  */
228
- has(nodeProperty: BinaryTreeNodeKey | N, callback?: MapCallback<N>, beginRoot?: N | null, iterationType?: IterationType): boolean;
228
+ has<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(nodeProperty: BinaryTreeNodeKey | N, callback?: C, beginRoot?: N | null, iterationType?: IterationType): boolean;
229
229
  /**
230
230
  * The function `get` returns the first node in a binary tree that matches the given property or key.
231
231
  * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is the key or value of
@@ -241,7 +241,7 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
241
241
  * performed when searching for a node in the binary tree. It can have one of the following values:
242
242
  * @returns either the found node (of type N) or null if no node is found.
243
243
  */
244
- get(nodeProperty: BinaryTreeNodeKey | N, callback?: MapCallback<N>, beginRoot?: N | null, iterationType?: IterationType): N | null;
244
+ get<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(nodeProperty: BinaryTreeNodeKey | N, callback?: C, beginRoot?: N | null, iterationType?: IterationType): N | null;
245
245
  /**
246
246
  * The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
247
247
  * up to the root node, with the option to reverse the order of the nodes.
@@ -310,7 +310,7 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
310
310
  * performed on the binary tree. It can have two possible values:
311
311
  * @returns The function `subTreeTraverse` returns an array of `MapCallbackReturn<N>`.
312
312
  */
313
- subTreeTraverse(callback?: MapCallback<N>, beginRoot?: N | BinaryTreeNodeKey | null, iterationType?: IterationType): MapCallbackReturn<N>[];
313
+ subTreeTraverse<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(callback?: C, beginRoot?: N | BinaryTreeNodeKey | null, iterationType?: IterationType): ReturnType<C>[];
314
314
  /**
315
315
  * The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback
316
316
  * function on each node according to a specified order pattern.
@@ -326,7 +326,7 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
326
326
  * iteration used in the depth-first search algorithm. It can have two possible values:
327
327
  * @returns The function `dfs` returns an array of `MapCallbackReturn<N>` values.
328
328
  */
329
- dfs(callback?: MapCallback<N>, pattern?: DFSOrderPattern, beginRoot?: N | null, iterationType?: IterationType): MapCallbackReturn<N>[];
329
+ dfs<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: N | null, iterationType?: IterationType): ReturnType<C>[];
330
330
  /**
331
331
  * The bfs function performs a breadth-first search traversal on a binary tree, executing a callback
332
332
  * function on each node.
@@ -344,7 +344,7 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
344
344
  * in the breadth-first search (BFS) algorithm. It can have two possible values:
345
345
  * @returns The function `bfs` returns an array of `BFSCallbackReturn<N>[]`.
346
346
  */
347
- bfs(callback?: BFSCallback<N>, withLevel?: boolean, beginRoot?: N | null, iterationType?: IterationType): BFSCallbackReturn<N>[];
347
+ bfs<C extends BFSCallback<N> = BFSCallback<N, BinaryTreeNodeKey>>(callback?: C, withLevel?: boolean, beginRoot?: N | null, iterationType?: IterationType): ReturnType<C>[];
348
348
  /**
349
349
  * The function returns the predecessor node of a given node in a binary tree.
350
350
  * @param {N} node - The parameter "node" represents a node in a binary tree.
@@ -365,7 +365,7 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
365
365
  * `beginRoot` is `null`, an empty array will be returned.
366
366
  * @returns The `morris` function returns an array of `MapCallbackReturn<N>` values.
367
367
  */
368
- morris(callback?: MapCallback<N>, pattern?: DFSOrderPattern, beginRoot?: N | null): MapCallbackReturn<N>[];
368
+ morris<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: N | null): ReturnType<C>[];
369
369
  /**
370
370
  * Swap the data of two nodes in the binary tree.
371
371
  * @param {N} srcNode - The source node to swap.
@@ -380,7 +380,7 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
380
380
  * the tree's structure should be restored to its original state to maintain the tree's integrity.
381
381
  * This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
382
382
  */
383
- protected _defaultCallbackByKey: MapCallback<N>;
383
+ protected _defaultCallbackByKey: (node: N) => number;
384
384
  /**
385
385
  * The function `_addTo` adds a new node to a binary tree if there is an available position.
386
386
  * @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to
@@ -5,7 +5,7 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { BinaryTreeNodeKey, BSTComparator, BSTNodeNested, BSTOptions, MapCallback, MapCallbackReturn } from '../../types';
8
+ import type { BinaryTreeNodeKey, BSTComparator, BSTNodeNested, BSTOptions, MapCallback } from '../../types';
9
9
  import { CP, IterationType } from '../../types';
10
10
  import { BinaryTree, BinaryTreeNode } from './binary-tree';
11
11
  import { IBinaryTree } from '../../interfaces';
@@ -72,7 +72,7 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
72
72
  * @returns either the first node that matches the given nodeProperty and callback, or null if no
73
73
  * matching node is found.
74
74
  */
75
- get(nodeProperty: BinaryTreeNodeKey | N, callback?: MapCallback<N>, beginRoot?: N | null, iterationType?: IterationType): N | null;
75
+ get<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(nodeProperty: BinaryTreeNodeKey | N, callback?: C, beginRoot?: N | null, iterationType?: IterationType): N | null;
76
76
  /**
77
77
  * The function `lastKey` returns the key of the rightmost node if the comparison result is less
78
78
  * than, the key of the leftmost node if the comparison result is greater than, and the key of the
@@ -110,7 +110,7 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
110
110
  * traverse the binary tree. It can have one of the following values:
111
111
  * @returns an array of nodes (N[]).
112
112
  */
113
- getNodes(nodeProperty: BinaryTreeNodeKey | N, callback?: MapCallback<N>, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[];
113
+ getNodes<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(nodeProperty: BinaryTreeNodeKey | N, callback?: C, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[];
114
114
  /**
115
115
  * The `lesserOrGreaterTraverse` function traverses a binary tree and applies a callback function to
116
116
  * nodes that have a key value lesser or greater than a target key value.
@@ -128,7 +128,7 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
128
128
  * done recursively or iteratively. It can have two possible values:
129
129
  * @returns The function `lesserOrGreaterTraverse` returns an array of `MapCallbackReturn<N>`.
130
130
  */
131
- lesserOrGreaterTraverse(callback?: MapCallback<N>, lesserOrGreater?: CP, targetNode?: N | BinaryTreeNodeKey | null, iterationType?: IterationType): MapCallbackReturn<N>;
131
+ lesserOrGreaterTraverse<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(callback?: C, lesserOrGreater?: CP, targetNode?: N | BinaryTreeNodeKey | null, iterationType?: IterationType): ReturnType<C>[];
132
132
  /**
133
133
  * Balancing Adjustment:
134
134
  * Perfectly Balanced Binary Tree: Since the balance of a perfectly balanced binary tree is already fixed, no additional balancing adjustment is needed. Any insertion or deletion operation will disrupt the perfect balance, often requiring a complete reconstruction of the tree.
@@ -19,7 +19,7 @@ export declare enum FamilyPosition {
19
19
  MAL_NODE = "MAL_NODE"
20
20
  }
21
21
  export type BinaryTreeNodeKey = number;
22
- export type BFSCallback<N> = (node: N, level?: number) => any;
22
+ export type BFSCallback<N, D = any> = (node: N, level?: number) => D;
23
23
  export type BFSCallbackReturn<N> = ReturnType<BFSCallback<N>>;
24
24
  export type BinaryTreeDeletedResult<N> = {
25
25
  deleted: N | null | undefined;
@@ -1,6 +1,6 @@
1
1
  export type Comparator<T> = (a: T, b: T) => number;
2
2
  export type DFSOrderPattern = 'pre' | 'in' | 'post';
3
- export type MapCallback<N> = (node: N) => any;
3
+ export type MapCallback<N, D = any> = (node: N) => D;
4
4
  export type MapCallbackReturn<N> = ReturnType<MapCallback<N>>;
5
5
  export declare enum CP {
6
6
  lt = "lt",
package/jest.config.js CHANGED
@@ -1,8 +1,8 @@
1
1
  module.exports = {
2
- preset: 'ts-jest',
3
- testEnvironment: 'node',
4
- testMatch: ['<rootDir>/test/**/*.test.ts', '<rootDir>/test/**/*.test.js'],
5
- collectCoverage: true,
6
- coverageReporters: ['clover', 'json', 'lcov', ['text', {skipFull: true}], "json-summary"],
7
- coverageDirectory: 'coverage'
2
+ preset: 'ts-jest',
3
+ testEnvironment: 'node',
4
+ testMatch: ['<rootDir>/test/**/*.test.ts', '<rootDir>/test/**/*.test.js'],
5
+ collectCoverage: true,
6
+ coverageReporters: ['clover', 'json', 'lcov', ['text', {skipFull: true}], "json-summary"],
7
+ coverageDirectory: 'coverage'
8
8
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "min-heap-typed",
3
- "version": "1.38.1",
3
+ "version": "1.38.4",
4
4
  "description": "Min Heap. Javascript & Typescript Data Structure.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -129,5 +129,8 @@
129
129
  "ts-jest": "^29.1.1",
130
130
  "typedoc": "^0.25.1",
131
131
  "typescript": "^4.9.5"
132
+ },
133
+ "dependencies": {
134
+ "data-structure-typed": "^1.38.4"
132
135
  }
133
136
  }
@@ -512,9 +512,9 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
512
512
  * traverse the binary tree. It can have two possible values:
513
513
  * @returns The function `getNodes` returns an array of nodes (`N[]`).
514
514
  */
515
- getNodes(
515
+ getNodes<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(
516
516
  nodeProperty: BinaryTreeNodeKey | N,
517
- callback: MapCallback<N> = this._defaultCallbackByKey,
517
+ callback: C = this._defaultCallbackByKey as C,
518
518
  onlyOne = false,
519
519
  beginRoot: N | null = this.root,
520
520
  iterationType = this.iterationType
@@ -570,9 +570,9 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
570
570
  * performed when searching for nodes in the binary tree. It can have one of the following values:
571
571
  * @returns a boolean value.
572
572
  */
573
- has(
573
+ has<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(
574
574
  nodeProperty: BinaryTreeNodeKey | N,
575
- callback: MapCallback<N> = this._defaultCallbackByKey,
575
+ callback: C = this._defaultCallbackByKey as C,
576
576
  beginRoot = this.root,
577
577
  iterationType = this.iterationType
578
578
  ): boolean {
@@ -595,9 +595,9 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
595
595
  * performed when searching for a node in the binary tree. It can have one of the following values:
596
596
  * @returns either the found node (of type N) or null if no node is found.
597
597
  */
598
- get(
598
+ get<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(
599
599
  nodeProperty: BinaryTreeNodeKey | N,
600
- callback: MapCallback<N> = this._defaultCallbackByKey,
600
+ callback: C = this._defaultCallbackByKey as C,
601
601
  beginRoot = this.root,
602
602
  iterationType = this.iterationType
603
603
  ): N | null {
@@ -761,11 +761,11 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
761
761
  * performed on the binary tree. It can have two possible values:
762
762
  * @returns The function `subTreeTraverse` returns an array of `MapCallbackReturn<N>`.
763
763
  */
764
- subTreeTraverse(
765
- callback: MapCallback<N> = this._defaultCallbackByKey,
764
+ subTreeTraverse<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(
765
+ callback: C = this._defaultCallbackByKey as C,
766
766
  beginRoot: N | BinaryTreeNodeKey | null = this.root,
767
767
  iterationType = this.iterationType
768
- ): MapCallbackReturn<N>[] {
768
+ ): ReturnType<C>[] {
769
769
  if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot);
770
770
 
771
771
  const ans: MapCallbackReturn<N>[] = [];
@@ -808,12 +808,12 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
808
808
  * iteration used in the depth-first search algorithm. It can have two possible values:
809
809
  * @returns The function `dfs` returns an array of `MapCallbackReturn<N>` values.
810
810
  */
811
- dfs(
812
- callback: MapCallback<N> = this._defaultCallbackByKey,
811
+ dfs<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(
812
+ callback: C = this._defaultCallbackByKey as C,
813
813
  pattern: DFSOrderPattern = 'in',
814
814
  beginRoot: N | null = this.root,
815
815
  iterationType: IterationType = IterationType.ITERATIVE
816
- ): MapCallbackReturn<N>[] {
816
+ ): ReturnType<C>[] {
817
817
  if (!beginRoot) return [];
818
818
  const ans: MapCallbackReturn<N>[] = [];
819
819
  if (iterationType === IterationType.RECURSIVE) {
@@ -896,12 +896,12 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
896
896
  * in the breadth-first search (BFS) algorithm. It can have two possible values:
897
897
  * @returns The function `bfs` returns an array of `BFSCallbackReturn<N>[]`.
898
898
  */
899
- bfs(
900
- callback: BFSCallback<N> = this._defaultCallbackByKey,
899
+ bfs<C extends BFSCallback<N> = BFSCallback<N, BinaryTreeNodeKey>>(
900
+ callback: C = this._defaultCallbackByKey as C,
901
901
  withLevel: boolean = false,
902
902
  beginRoot: N | null = this.root,
903
903
  iterationType = this.iterationType
904
- ): BFSCallbackReturn<N>[] {
904
+ ): ReturnType<C>[] {
905
905
  if (!beginRoot) return [];
906
906
 
907
907
  const ans: BFSCallbackReturn<N>[] = [];
@@ -964,11 +964,11 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
964
964
  * `beginRoot` is `null`, an empty array will be returned.
965
965
  * @returns The `morris` function returns an array of `MapCallbackReturn<N>` values.
966
966
  */
967
- morris(
968
- callback: MapCallback<N> = this._defaultCallbackByKey,
967
+ morris<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(
968
+ callback: C = this._defaultCallbackByKey as C,
969
969
  pattern: DFSOrderPattern = 'in',
970
970
  beginRoot: N | null = this.root
971
- ): MapCallbackReturn<N>[] {
971
+ ): ReturnType<C>[] {
972
972
  if (beginRoot === null) return [];
973
973
  const ans: MapCallbackReturn<N>[] = [];
974
974
 
@@ -1078,7 +1078,7 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
1078
1078
  * This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
1079
1079
  */
1080
1080
 
1081
- protected _defaultCallbackByKey: MapCallback<N> = node => node.key;
1081
+ protected _defaultCallbackByKey: (node: N) => number = node => node.key;
1082
1082
 
1083
1083
  /**
1084
1084
  * The function `_addTo` adds a new node to a binary tree if there is an available position.
@@ -238,9 +238,9 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
238
238
  * @returns either the first node that matches the given nodeProperty and callback, or null if no
239
239
  * matching node is found.
240
240
  */
241
- override get(
241
+ override get<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(
242
242
  nodeProperty: BinaryTreeNodeKey | N,
243
- callback: MapCallback<N> = this._defaultCallbackByKey,
243
+ callback: C = this._defaultCallbackByKey as C,
244
244
  beginRoot = this.root,
245
245
  iterationType = this.iterationType
246
246
  ): N | null {
@@ -289,9 +289,9 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
289
289
  * traverse the binary tree. It can have one of the following values:
290
290
  * @returns an array of nodes (N[]).
291
291
  */
292
- override getNodes(
292
+ override getNodes<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(
293
293
  nodeProperty: BinaryTreeNodeKey | N,
294
- callback: MapCallback<N> = this._defaultCallbackByKey,
294
+ callback: C = this._defaultCallbackByKey as C,
295
295
  onlyOne = false,
296
296
  beginRoot: N | null = this.root,
297
297
  iterationType = this.iterationType
@@ -363,12 +363,12 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
363
363
  * done recursively or iteratively. It can have two possible values:
364
364
  * @returns The function `lesserOrGreaterTraverse` returns an array of `MapCallbackReturn<N>`.
365
365
  */
366
- lesserOrGreaterTraverse(
367
- callback: MapCallback<N> = this._defaultCallbackByKey,
366
+ lesserOrGreaterTraverse<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(
367
+ callback: C = this._defaultCallbackByKey as C,
368
368
  lesserOrGreater: CP = CP.lt,
369
369
  targetNode: N | BinaryTreeNodeKey | null = this.root,
370
370
  iterationType = this.iterationType
371
- ): MapCallbackReturn<N> {
371
+ ): ReturnType<C>[] {
372
372
  if (typeof targetNode === 'number') targetNode = this.get(targetNode);
373
373
  const ans: MapCallbackReturn<N>[] = [];
374
374
  if (!targetNode) return ans;
@@ -24,7 +24,7 @@ export enum FamilyPosition {
24
24
 
25
25
  export type BinaryTreeNodeKey = number;
26
26
 
27
- export type BFSCallback<N> = (node: N, level?: number) => any;
27
+ export type BFSCallback<N, D = any> = (node: N, level?: number) => D;
28
28
 
29
29
  export type BFSCallbackReturn<N> = ReturnType<BFSCallback<N>>;
30
30
 
@@ -2,7 +2,7 @@ export type Comparator<T> = (a: T, b: T) => number;
2
2
 
3
3
  export type DFSOrderPattern = 'pre' | 'in' | 'post';
4
4
 
5
- export type MapCallback<N> = (node: N) => any;
5
+ export type MapCallback<N, D = any> = (node: N) => D;
6
6
 
7
7
  export type MapCallbackReturn<N> = ReturnType<MapCallback<N>>;
8
8
 
package/tsconfig.json CHANGED
@@ -26,9 +26,8 @@
26
26
  "node_modules/@types"
27
27
  ]
28
28
  },
29
-
30
29
  "include": [
31
- "src",
30
+ "src"
32
31
  ],
33
32
  "exclude": [
34
33
  // "node_modules/data-structure-typed",