data-structure-typed 1.39.2 → 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/CHANGELOG.md +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +11 -11
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +8 -6
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +50 -117
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +43 -51
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +23 -23
- package/dist/cjs/data-structures/binary-tree/bst.js +18 -18
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multiset.d.ts +15 -15
- package/dist/cjs/data-structures/binary-tree/tree-multiset.js +9 -9
- package/dist/cjs/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/tree-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/tree-set.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
- package/dist/cjs/data-structures/matrix/vector2d.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +4 -4
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/cjs/types/helpers.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +11 -11
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +8 -6
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +50 -117
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +43 -51
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +23 -23
- package/dist/mjs/data-structures/binary-tree/bst.js +18 -18
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/mjs/data-structures/binary-tree/tree-multiset.d.ts +15 -15
- package/dist/mjs/data-structures/binary-tree/tree-multiset.js +9 -9
- package/dist/mjs/interfaces/binary-tree.d.ts +4 -4
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/mjs/types/helpers.d.ts +1 -1
- package/dist/umd/data-structure-typed.min.js +1 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +5 -5
- package/src/data-structures/binary-tree/avl-tree.ts +16 -14
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +1 -1
- package/src/data-structures/binary-tree/binary-tree.ts +145 -84
- package/src/data-structures/binary-tree/bst.ts +37 -38
- package/src/data-structures/binary-tree/rb-tree.ts +7 -6
- package/src/data-structures/binary-tree/tree-multiset.ts +18 -17
- package/src/data-structures/graph/abstract-graph.ts +11 -10
- package/src/data-structures/graph/directed-graph.ts +2 -1
- package/src/data-structures/graph/undirected-graph.ts +5 -4
- package/src/data-structures/hash/hash-map.ts +1 -1
- package/src/data-structures/hash/tree-map.ts +1 -2
- package/src/data-structures/hash/tree-set.ts +1 -2
- package/src/data-structures/heap/heap.ts +2 -2
- package/src/data-structures/heap/max-heap.ts +1 -1
- package/src/data-structures/heap/min-heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
- package/src/data-structures/matrix/matrix.ts +1 -1
- package/src/data-structures/matrix/vector2d.ts +1 -2
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/data-structures/queue/deque.ts +4 -5
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/interfaces/binary-tree.ts +4 -4
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +2 -2
- package/src/types/data-structures/matrix/navigator.ts +1 -1
- package/src/types/helpers.ts +1 -1
- package/src/types/utils/utils.ts +1 -1
- package/src/types/utils/validate-type.ts +2 -2
- package/test/integration/bst.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +14 -9
- package/test/unit/data-structures/binary-tree/bst.test.ts +6 -6
- package/test/unit/data-structures/binary-tree/overall.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +2 -2
- package/test/unit/data-structures/hash/coordinate-map.test.ts +2 -2
- package/test/unit/data-structures/hash/coordinate-set.test.ts +2 -2
- package/test/unit/data-structures/heap/heap.test.ts +2 -2
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +2 -6
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +4 -7
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +2 -2
- package/test/utils/big-o.ts +1 -1
|
@@ -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 {
|
|
8
|
+
import type { BinaryTreeNodeNested, BinaryTreeOptions, BTNCallback, BTNKey } from '../../types';
|
|
9
9
|
import { BinaryTreeDeletedResult, DFSOrderPattern, FamilyPosition, IterationType } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
/**
|
|
@@ -17,7 +17,7 @@ export declare class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = Bi
|
|
|
17
17
|
/**
|
|
18
18
|
* The key associated with the node.
|
|
19
19
|
*/
|
|
20
|
-
key:
|
|
20
|
+
key: BTNKey;
|
|
21
21
|
/**
|
|
22
22
|
* The value stored in the node.
|
|
23
23
|
*/
|
|
@@ -28,10 +28,10 @@ export declare class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = Bi
|
|
|
28
28
|
parent: N | null | undefined;
|
|
29
29
|
/**
|
|
30
30
|
* Creates a new instance of BinaryTreeNode.
|
|
31
|
-
* @param {
|
|
31
|
+
* @param {BTNKey} key - The key associated with the node.
|
|
32
32
|
* @param {V} val - The value stored in the node.
|
|
33
33
|
*/
|
|
34
|
-
constructor(key:
|
|
34
|
+
constructor(key: BTNKey, val?: V);
|
|
35
35
|
private _left;
|
|
36
36
|
/**
|
|
37
37
|
* Get the left child node.
|
|
@@ -90,11 +90,11 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
90
90
|
get size(): number;
|
|
91
91
|
/**
|
|
92
92
|
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
93
|
-
* @param {
|
|
93
|
+
* @param {BTNKey} key - The key for the new node.
|
|
94
94
|
* @param {V} val - The value for the new node.
|
|
95
95
|
* @returns {N} - The newly created BinaryTreeNode.
|
|
96
96
|
*/
|
|
97
|
-
createNode(key:
|
|
97
|
+
createNode(key: BTNKey, val?: V): N;
|
|
98
98
|
/**
|
|
99
99
|
* Clear the binary tree, removing all nodes.
|
|
100
100
|
*/
|
|
@@ -106,73 +106,61 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
106
106
|
isEmpty(): boolean;
|
|
107
107
|
/**
|
|
108
108
|
* Add a node with the given key and value to the binary tree.
|
|
109
|
-
* @param {
|
|
109
|
+
* @param {BTNKey | N | null} keyOrNode - The key or node to add to the binary tree.
|
|
110
110
|
* @param {V} val - The value for the new node (optional).
|
|
111
111
|
* @returns {N | null | undefined} - The inserted node, or null if nothing was inserted, or undefined if the operation failed.
|
|
112
112
|
*/
|
|
113
|
-
add(keyOrNode:
|
|
113
|
+
add(keyOrNode: BTNKey | N | null, val?: V): N | null | undefined;
|
|
114
114
|
/**
|
|
115
115
|
* The `addMany` function takes an array of binary tree node IDs or nodes, and optionally an array of corresponding data
|
|
116
116
|
* values, and adds them to the binary tree.
|
|
117
|
-
* @param {(
|
|
117
|
+
* @param {(BTNKey | null)[] | (N | null)[]} keysOrNodes - An array of BTNKey or BinaryTreeNode
|
|
118
118
|
* objects, or null values.
|
|
119
119
|
* @param {V[]} [values] - The `values` parameter is an optional array of values (`V[]`) that corresponds to
|
|
120
120
|
* the nodes or node IDs being added. It is used to set the value of each node being added. If `values` is not provided,
|
|
121
121
|
* the value of the nodes will be `undefined`.
|
|
122
122
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
123
123
|
*/
|
|
124
|
-
addMany(keysOrNodes: (
|
|
124
|
+
addMany(keysOrNodes: (BTNKey | null)[] | (N | null)[], values?: V[]): (N | null | undefined)[];
|
|
125
125
|
/**
|
|
126
126
|
* The `refill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
|
|
127
|
-
* @param {(
|
|
128
|
-
* `
|
|
127
|
+
* @param {(BTNKey | N)[]} keysOrNodes - The `keysOrNodes` parameter is an array that can contain either
|
|
128
|
+
* `BTNKey` or `N` values.
|
|
129
129
|
* @param {N[] | Array<V>} [data] - The `data` parameter is an optional array of values that will be assigned to
|
|
130
130
|
* the nodes being added. If provided, the length of the `data` array should be equal to the length of the `keysOrNodes`
|
|
131
131
|
* array. Each value in the `data` array will be assigned to the
|
|
132
132
|
* @returns The method is returning a boolean value.
|
|
133
133
|
*/
|
|
134
|
-
refill(keysOrNodes: (
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
* a key (`BinaryTreeNodeKey`). If it is a key, the function will find the corresponding node in the
|
|
139
|
-
* binary tree.
|
|
140
|
-
* @returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
141
|
-
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a
|
|
142
|
-
* `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are
|
|
143
|
-
* searching for. It can be a specific key value or any other property of the node.
|
|
144
|
-
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
145
|
-
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
146
|
-
* included in the result. The `callback` parameter has a default value of
|
|
147
|
-
* `this._defaultCallbackByKey`, which
|
|
148
|
-
*/
|
|
149
|
-
delete<C extends OneParamCallback<N>>(identifier: ReturnType<C> | null, callback?: C): BinaryTreeDeletedResult<N>[];
|
|
134
|
+
refill(keysOrNodes: (BTNKey | null)[] | (N | null)[], data?: Array<V>): boolean;
|
|
135
|
+
delete<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C): BinaryTreeDeletedResult<N>[];
|
|
136
|
+
delete<C extends BTNCallback<N, N>>(identifier: N | null, callback?: C): BinaryTreeDeletedResult<N>[];
|
|
137
|
+
delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C): BinaryTreeDeletedResult<N>[];
|
|
150
138
|
/**
|
|
151
139
|
* The function `getDepth` calculates the depth of a given node in a binary tree relative to a
|
|
152
140
|
* specified root node.
|
|
153
|
-
* @param {
|
|
141
|
+
* @param {BTNKey | N | null} distNode - The `distNode` parameter represents the node
|
|
154
142
|
* whose depth we want to find in the binary tree. It can be either a node object (`N`), a key value
|
|
155
|
-
* of the node (`
|
|
156
|
-
* @param {
|
|
143
|
+
* of the node (`BTNKey`), or `null`.
|
|
144
|
+
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter represents the
|
|
157
145
|
* starting node from which we want to calculate the depth. It can be either a node object or the key
|
|
158
146
|
* of a node in the binary tree. If no value is provided for `beginRoot`, it defaults to the root
|
|
159
147
|
* node of the binary tree.
|
|
160
148
|
* @returns the depth of the `distNode` relative to the `beginRoot`.
|
|
161
149
|
*/
|
|
162
|
-
getDepth(distNode:
|
|
150
|
+
getDepth(distNode: BTNKey | N | null, beginRoot?: BTNKey | N | null): number;
|
|
163
151
|
/**
|
|
164
152
|
* The `getHeight` function calculates the maximum height of a binary tree using either recursive or
|
|
165
153
|
* iterative approach.
|
|
166
|
-
* @param {
|
|
154
|
+
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter represents the
|
|
167
155
|
* starting node from which the height of the binary tree is calculated. It can be either a node
|
|
168
|
-
* object (`N`), a key value of a node in the tree (`
|
|
156
|
+
* object (`N`), a key value of a node in the tree (`BTNKey`), or `null` if no starting
|
|
169
157
|
* node is specified. If `
|
|
170
158
|
* @param iterationType - The `iterationType` parameter is used to determine whether to calculate the
|
|
171
159
|
* height of the binary tree using a recursive approach or an iterative approach. It can have two
|
|
172
160
|
* possible values:
|
|
173
161
|
* @returns the height of the binary tree.
|
|
174
162
|
*/
|
|
175
|
-
getHeight(beginRoot?:
|
|
163
|
+
getHeight(beginRoot?: BTNKey | N | null, iterationType?: IterationType): number;
|
|
176
164
|
/**
|
|
177
165
|
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
178
166
|
* recursive or iterative approach.
|
|
@@ -192,62 +180,15 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
192
180
|
* @returns The method is returning a boolean value.
|
|
193
181
|
*/
|
|
194
182
|
isPerfectlyBalanced(beginRoot?: N | null): boolean;
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
* `this._defaultCallbackByKey`, which
|
|
205
|
-
* @param [onlyOne=false] - A boolean value indicating whether to stop searching after finding the
|
|
206
|
-
* first node that matches the identifier. If set to true, the function will return an array with
|
|
207
|
-
* only one element (or an empty array if no matching node is found). If set to false (default), the
|
|
208
|
-
* function will continue searching for all
|
|
209
|
-
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which the
|
|
210
|
-
* traversal of the binary tree will begin. It is optional and defaults to the root of the binary
|
|
211
|
-
* tree.
|
|
212
|
-
* @param iterationType - The `iterationType` parameter determines the type of iteration used to
|
|
213
|
-
* traverse the binary tree. It can have two possible values:
|
|
214
|
-
* @returns The function `getNodes` returns an array of nodes (`N[]`).
|
|
215
|
-
*/
|
|
216
|
-
getNodes<C extends OneParamCallback<N>>(identifier: ReturnType<C> | null, callback?: C, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[];
|
|
217
|
-
/**
|
|
218
|
-
* The function checks if a binary tree has a node with a given property or key.
|
|
219
|
-
* @param {BinaryTreeNodeKey | N} identifier - The `identifier` parameter is the key or value of
|
|
220
|
-
* the node that you want to find in the binary tree. It can be either a `BinaryTreeNodeKey` or a
|
|
221
|
-
* generic type `N`.
|
|
222
|
-
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
223
|
-
* matches the desired criteria. It takes a node as input and returns a boolean value indicating
|
|
224
|
-
* whether the node matches the criteria or not. The default callback function
|
|
225
|
-
* `this._defaultCallbackByKey` is used if no callback function is
|
|
226
|
-
* @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
|
|
227
|
-
* the node from which the search should begin. By default, it is set to `this.root`, which means the
|
|
228
|
-
* search will start from the root node of the binary tree. However, you can provide a different node
|
|
229
|
-
* as
|
|
230
|
-
* @param iterationType - The `iterationType` parameter specifies the type of iteration to be
|
|
231
|
-
* performed when searching for nodes in the binary tree. It can have one of the following values:
|
|
232
|
-
* @returns a boolean value.
|
|
233
|
-
*/
|
|
234
|
-
has<C extends OneParamCallback<N>>(identifier: ReturnType<C> | null, callback?: C, beginRoot?: N | null, iterationType?: IterationType): boolean;
|
|
235
|
-
/**
|
|
236
|
-
* The function `get` returns the first node in a binary tree that matches the given property or key.
|
|
237
|
-
* @param {BinaryTreeNodeKey | N} identifier - The `identifier` parameter is the key or value of
|
|
238
|
-
* the node that you want to find in the binary tree. It can be either a `BinaryTreeNodeKey` or `N`
|
|
239
|
-
* type.
|
|
240
|
-
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
241
|
-
* matches the desired criteria. It takes a node as input and returns a boolean value indicating
|
|
242
|
-
* whether the node matches the criteria or not. The default callback function
|
|
243
|
-
* (`this._defaultCallbackByKey`) is used if no callback function is
|
|
244
|
-
* @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
|
|
245
|
-
* the root node from which the search should begin.
|
|
246
|
-
* @param iterationType - The `iterationType` parameter specifies the type of iteration to be
|
|
247
|
-
* performed when searching for a node in the binary tree. It can have one of the following values:
|
|
248
|
-
* @returns either the found node (of type N) or null if no node is found.
|
|
249
|
-
*/
|
|
250
|
-
get<C extends OneParamCallback<N>>(identifier: ReturnType<C> | null, callback?: C, beginRoot?: N | null, iterationType?: IterationType): N | null;
|
|
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
|
+
getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[];
|
|
186
|
+
has<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C, beginRoot?: N, iterationType?: IterationType): boolean;
|
|
187
|
+
has<C extends BTNCallback<N, N>>(identifier: N | null, callback?: C, beginRoot?: N, iterationType?: IterationType): boolean;
|
|
188
|
+
has<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C, beginRoot?: N, iterationType?: IterationType): boolean;
|
|
189
|
+
get<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C, beginRoot?: N, iterationType?: IterationType): N | null;
|
|
190
|
+
get<C extends BTNCallback<N, N>>(identifier: N | null, callback?: C, beginRoot?: N, iterationType?: IterationType): N | null;
|
|
191
|
+
get<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, beginRoot?: N, iterationType?: IterationType): N | null;
|
|
251
192
|
/**
|
|
252
193
|
* The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
|
|
253
194
|
* up to the root node, with the option to reverse the order of the nodes.
|
|
@@ -262,15 +203,15 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
262
203
|
/**
|
|
263
204
|
* The function `getLeftMost` returns the leftmost node in a binary tree, either using recursive or
|
|
264
205
|
* iterative traversal.
|
|
265
|
-
* @param {
|
|
206
|
+
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter is the starting point
|
|
266
207
|
* for finding the leftmost node in a binary tree. It can be either a node object (`N`), a key value
|
|
267
|
-
* of a node (`
|
|
208
|
+
* of a node (`BTNKey`), or `null` if the tree is empty.
|
|
268
209
|
* @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
|
|
269
210
|
* be performed when finding the leftmost node in a binary tree. It can have two possible values:
|
|
270
211
|
* @returns The function `getLeftMost` returns the leftmost node (`N`) in a binary tree. If there is
|
|
271
212
|
* no leftmost node, it returns `null`.
|
|
272
213
|
*/
|
|
273
|
-
getLeftMost(beginRoot?:
|
|
214
|
+
getLeftMost(beginRoot?: BTNKey | N | null, iterationType?: IterationType): N | null;
|
|
274
215
|
/**
|
|
275
216
|
* The function `getRightMost` returns the rightmost node in a binary tree, either recursively or
|
|
276
217
|
* iteratively.
|
|
@@ -309,20 +250,20 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
309
250
|
* subtree traversal. It takes a single argument, which is the current node being traversed, and
|
|
310
251
|
* returns a value. The return values from each callback invocation will be collected and returned as
|
|
311
252
|
* an array.
|
|
312
|
-
* @param {
|
|
253
|
+
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter is the starting point
|
|
313
254
|
* for traversing the subtree. It can be either a node object, a key value of a node, or `null` to
|
|
314
255
|
* start from the root of the tree.
|
|
315
256
|
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
316
257
|
* performed on the binary tree. It can have two possible values:
|
|
317
|
-
* @returns The function `subTreeTraverse` returns an array of `ReturnType<
|
|
258
|
+
* @returns The function `subTreeTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
|
|
318
259
|
*/
|
|
319
|
-
subTreeTraverse<C extends
|
|
260
|
+
subTreeTraverse<C extends BTNCallback<N>>(callback?: C, beginRoot?: BTNKey | N | null, iterationType?: IterationType): ReturnType<C>[];
|
|
320
261
|
/**
|
|
321
262
|
* The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback
|
|
322
263
|
* function on each node according to a specified order pattern.
|
|
323
264
|
* @param callback - The `callback` parameter is a function that will be called on each node during
|
|
324
265
|
* the depth-first search traversal. It takes a node as input and returns a value. The default value
|
|
325
|
-
* is `
|
|
266
|
+
* is `((node: N) => node.key)`, which is a callback function defined elsewhere in the code.
|
|
326
267
|
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter determines the order in which the
|
|
327
268
|
* nodes are visited during the depth-first search. There are three possible values for `pattern`:
|
|
328
269
|
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the depth-first
|
|
@@ -330,23 +271,23 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
330
271
|
* is `null`, an empty array will be returned.
|
|
331
272
|
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
332
273
|
* iteration used in the depth-first search algorithm. It can have two possible values:
|
|
333
|
-
* @returns The function `dfs` returns an array of `ReturnType<
|
|
274
|
+
* @returns The function `dfs` returns an array of `ReturnType<BTNCallback<N>>` values.
|
|
334
275
|
*/
|
|
335
|
-
dfs<C extends
|
|
276
|
+
dfs<C extends BTNCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: N | null, iterationType?: IterationType): ReturnType<C>[];
|
|
336
277
|
/**
|
|
337
278
|
* The bfs function performs a breadth-first search traversal on a binary tree, executing a callback
|
|
338
279
|
* function on each node.
|
|
339
280
|
* @param callback - The `callback` parameter is a function that will be called for each node in the
|
|
340
281
|
* breadth-first search. It takes a node of type `N` as its argument and returns a value of type
|
|
341
|
-
* `ReturnType<
|
|
282
|
+
* `ReturnType<BTNCallback<N>>`. The default value for this parameter is `((node: N) => node.key)
|
|
342
283
|
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
|
|
343
284
|
* search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
|
|
344
285
|
* will not be performed and an empty array will be returned.
|
|
345
286
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be used
|
|
346
287
|
* in the breadth-first search (BFS) algorithm. It can have two possible values:
|
|
347
|
-
* @returns The function `bfs` returns an array of `ReturnType<
|
|
288
|
+
* @returns The function `bfs` returns an array of `ReturnType<BTNCallback<N>>[]`.
|
|
348
289
|
*/
|
|
349
|
-
bfs<C extends
|
|
290
|
+
bfs<C extends BTNCallback<N>>(callback?: C, beginRoot?: N | null, iterationType?: IterationType): ReturnType<C>[];
|
|
350
291
|
/**
|
|
351
292
|
* The `listLevels` function takes a binary tree node and a callback function, and returns an array
|
|
352
293
|
* of arrays representing the levels of the tree.
|
|
@@ -362,7 +303,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
362
303
|
* level in a binary tree. Each inner array contains the return type of the provided callback
|
|
363
304
|
* function `C` applied to the nodes at that level.
|
|
364
305
|
*/
|
|
365
|
-
listLevels<C extends
|
|
306
|
+
listLevels<C extends BTNCallback<N>>(callback?: C, beginRoot?: N | null, iterationType?: IterationType): ReturnType<C>[][];
|
|
366
307
|
/**
|
|
367
308
|
* The function returns the predecessor node of a given node in a binary tree.
|
|
368
309
|
* @param {N} node - The parameter "node" represents a node in a binary tree.
|
|
@@ -373,17 +314,17 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
373
314
|
* The `morris` function performs a depth-first traversal of a binary tree using the Morris traversal
|
|
374
315
|
* algorithm and returns an array of values obtained by applying a callback function to each node.
|
|
375
316
|
* @param callback - The `callback` parameter is a function that will be called on each node in the
|
|
376
|
-
* tree. It takes a node of type `N` as input and returns a value of type `ReturnType<
|
|
377
|
-
* default value for this parameter is `
|
|
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 `((node: N) => node.key)`.
|
|
378
319
|
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
|
|
379
320
|
* determines the order in which the nodes of a binary tree are traversed. It can have one of the
|
|
380
321
|
* following values:
|
|
381
322
|
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the Morris
|
|
382
323
|
* traversal. It specifies the root node of the tree from which the traversal should begin. If
|
|
383
324
|
* `beginRoot` is `null`, an empty array will be returned.
|
|
384
|
-
* @returns The `morris` function returns an array of `ReturnType<
|
|
325
|
+
* @returns The `morris` function returns an array of `ReturnType<BTNCallback<N>>` values.
|
|
385
326
|
*/
|
|
386
|
-
morris<C extends
|
|
327
|
+
morris<C extends BTNCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: N | null): ReturnType<C>[];
|
|
387
328
|
/**
|
|
388
329
|
* The above function is an iterator for a binary tree that can be used to traverse the tree in
|
|
389
330
|
* either an iterative or recursive manner.
|
|
@@ -393,7 +334,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
393
334
|
* @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
|
|
394
335
|
* binary tree nodes in a specific order.
|
|
395
336
|
*/
|
|
396
|
-
[Symbol.iterator](node?: N | null): Generator<
|
|
337
|
+
[Symbol.iterator](node?: N | null): Generator<BTNKey, void, undefined>;
|
|
397
338
|
/**
|
|
398
339
|
* Swap the data of two nodes in the binary tree.
|
|
399
340
|
* @param {N} srcNode - The source node to swap.
|
|
@@ -401,14 +342,6 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
401
342
|
* @returns {N} - The destination node after the swap.
|
|
402
343
|
*/
|
|
403
344
|
protected _swap(srcNode: N, destNode: N): N;
|
|
404
|
-
/**
|
|
405
|
-
* Time complexity is O(n)
|
|
406
|
-
* Space complexity of Iterative dfs equals to recursive dfs which is O(n) because of the stack
|
|
407
|
-
* The Morris algorithm only modifies the tree's structure during traversal; once the traversal is complete,
|
|
408
|
-
* the tree's structure should be restored to its original state to maintain the tree's integrity.
|
|
409
|
-
* This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
|
|
410
|
-
*/
|
|
411
|
-
protected _defaultCallbackByKey: OneParamCallback<N, BinaryTreeNodeKey>;
|
|
412
345
|
/**
|
|
413
346
|
* The function `_addTo` adds a new node to a binary tree if there is an available position.
|
|
414
347
|
* @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to
|
|
@@ -31,7 +31,7 @@ class BinaryTreeNode {
|
|
|
31
31
|
parent;
|
|
32
32
|
/**
|
|
33
33
|
* Creates a new instance of BinaryTreeNode.
|
|
34
|
-
* @param {
|
|
34
|
+
* @param {BTNKey} key - The key associated with the node.
|
|
35
35
|
* @param {V} val - The value stored in the node.
|
|
36
36
|
*/
|
|
37
37
|
constructor(key, val) {
|
|
@@ -136,7 +136,7 @@ class BinaryTree {
|
|
|
136
136
|
}
|
|
137
137
|
/**
|
|
138
138
|
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
139
|
-
* @param {
|
|
139
|
+
* @param {BTNKey} key - The key for the new node.
|
|
140
140
|
* @param {V} val - The value for the new node.
|
|
141
141
|
* @returns {N} - The newly created BinaryTreeNode.
|
|
142
142
|
*/
|
|
@@ -159,7 +159,7 @@ class BinaryTree {
|
|
|
159
159
|
}
|
|
160
160
|
/**
|
|
161
161
|
* Add a node with the given key and value to the binary tree.
|
|
162
|
-
* @param {
|
|
162
|
+
* @param {BTNKey | N | null} keyOrNode - The key or node to add to the binary tree.
|
|
163
163
|
* @param {V} val - The value for the new node (optional).
|
|
164
164
|
* @returns {N | null | undefined} - The inserted node, or null if nothing was inserted, or undefined if the operation failed.
|
|
165
165
|
*/
|
|
@@ -198,7 +198,7 @@ class BinaryTree {
|
|
|
198
198
|
return;
|
|
199
199
|
}
|
|
200
200
|
const key = typeof keyOrNode === 'number' ? keyOrNode : keyOrNode ? keyOrNode.key : undefined;
|
|
201
|
-
const existNode = key !== undefined ? this.get(key,
|
|
201
|
+
const existNode = key !== undefined ? this.get(key, (node) => node.key) : undefined;
|
|
202
202
|
if (this.root) {
|
|
203
203
|
if (existNode) {
|
|
204
204
|
existNode.val = val;
|
|
@@ -223,7 +223,7 @@ class BinaryTree {
|
|
|
223
223
|
/**
|
|
224
224
|
* The `addMany` function takes an array of binary tree node IDs or nodes, and optionally an array of corresponding data
|
|
225
225
|
* values, and adds them to the binary tree.
|
|
226
|
-
* @param {(
|
|
226
|
+
* @param {(BTNKey | null)[] | (N | null)[]} keysOrNodes - An array of BTNKey or BinaryTreeNode
|
|
227
227
|
* objects, or null values.
|
|
228
228
|
* @param {V[]} [values] - The `values` parameter is an optional array of values (`V[]`) that corresponds to
|
|
229
229
|
* the nodes or node IDs being added. It is used to set the value of each node being added. If `values` is not provided,
|
|
@@ -245,8 +245,8 @@ class BinaryTree {
|
|
|
245
245
|
}
|
|
246
246
|
/**
|
|
247
247
|
* The `refill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
|
|
248
|
-
* @param {(
|
|
249
|
-
* `
|
|
248
|
+
* @param {(BTNKey | N)[]} keysOrNodes - The `keysOrNodes` parameter is an array that can contain either
|
|
249
|
+
* `BTNKey` or `N` values.
|
|
250
250
|
* @param {N[] | Array<V>} [data] - The `data` parameter is an optional array of values that will be assigned to
|
|
251
251
|
* the nodes being added. If provided, the length of the `data` array should be equal to the length of the `keysOrNodes`
|
|
252
252
|
* array. Each value in the `data` array will be assigned to the
|
|
@@ -259,18 +259,18 @@ class BinaryTree {
|
|
|
259
259
|
/**
|
|
260
260
|
* The `delete` function removes a node from a binary search tree and returns the deleted node along
|
|
261
261
|
* with the parent node that needs to be balanced.
|
|
262
|
-
* a key (`
|
|
262
|
+
* a key (`BTNKey`). If it is a key, the function will find the corresponding node in the
|
|
263
263
|
* binary tree.
|
|
264
264
|
* @returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
265
265
|
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a
|
|
266
|
-
* `
|
|
266
|
+
* `BTNKey` or a generic type `N`. It represents the property of the node that we are
|
|
267
267
|
* searching for. It can be a specific key value or any other property of the node.
|
|
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)`, which
|
|
272
272
|
*/
|
|
273
|
-
delete(identifier, callback =
|
|
273
|
+
delete(identifier, callback = ((node) => node.key)) {
|
|
274
274
|
const bstDeletedResult = [];
|
|
275
275
|
if (!this.root)
|
|
276
276
|
return bstDeletedResult;
|
|
@@ -318,10 +318,10 @@ class BinaryTree {
|
|
|
318
318
|
/**
|
|
319
319
|
* The function `getDepth` calculates the depth of a given node in a binary tree relative to a
|
|
320
320
|
* specified root node.
|
|
321
|
-
* @param {
|
|
321
|
+
* @param {BTNKey | N | null} distNode - The `distNode` parameter represents the node
|
|
322
322
|
* whose depth we want to find in the binary tree. It can be either a node object (`N`), a key value
|
|
323
|
-
* of the node (`
|
|
324
|
-
* @param {
|
|
323
|
+
* of the node (`BTNKey`), or `null`.
|
|
324
|
+
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter represents the
|
|
325
325
|
* starting node from which we want to calculate the depth. It can be either a node object or the key
|
|
326
326
|
* of a node in the binary tree. If no value is provided for `beginRoot`, it defaults to the root
|
|
327
327
|
* node of the binary tree.
|
|
@@ -345,9 +345,9 @@ class BinaryTree {
|
|
|
345
345
|
/**
|
|
346
346
|
* The `getHeight` function calculates the maximum height of a binary tree using either recursive or
|
|
347
347
|
* iterative approach.
|
|
348
|
-
* @param {
|
|
348
|
+
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter represents the
|
|
349
349
|
* starting node from which the height of the binary tree is calculated. It can be either a node
|
|
350
|
-
* object (`N`), a key value of a node in the tree (`
|
|
350
|
+
* object (`N`), a key value of a node in the tree (`BTNKey`), or `null` if no starting
|
|
351
351
|
* node is specified. If `
|
|
352
352
|
* @param iterationType - The `iterationType` parameter is used to determine whether to calculate the
|
|
353
353
|
* height of the binary tree using a recursive approach or an iterative approach. It can have two
|
|
@@ -455,12 +455,12 @@ class BinaryTree {
|
|
|
455
455
|
* The function `getNodes` returns an array of nodes that match a given node property, using either
|
|
456
456
|
* recursive or iterative traversal.
|
|
457
457
|
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a
|
|
458
|
-
* `
|
|
458
|
+
* `BTNKey` or a generic type `N`. It represents the property of the node that we are
|
|
459
459
|
* searching for. It can be a specific key value or any other property of the node.
|
|
460
460
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
461
461
|
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
462
462
|
* included in the result. The `callback` parameter has a default value of
|
|
463
|
-
* `
|
|
463
|
+
* `((node: N) => node.key)`, which
|
|
464
464
|
* @param [onlyOne=false] - A boolean value indicating whether to stop searching after finding the
|
|
465
465
|
* first node that matches the identifier. If set to true, the function will return an array with
|
|
466
466
|
* only one element (or an empty array if no matching node is found). If set to false (default), the
|
|
@@ -472,7 +472,7 @@ class BinaryTree {
|
|
|
472
472
|
* traverse the binary tree. It can have two possible values:
|
|
473
473
|
* @returns The function `getNodes` returns an array of nodes (`N[]`).
|
|
474
474
|
*/
|
|
475
|
-
getNodes(identifier, callback =
|
|
475
|
+
getNodes(identifier, callback = ((node) => node.key), onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
476
476
|
if (!beginRoot)
|
|
477
477
|
return [];
|
|
478
478
|
if (identifier instanceof BinaryTreeNode)
|
|
@@ -511,13 +511,13 @@ class BinaryTree {
|
|
|
511
511
|
}
|
|
512
512
|
/**
|
|
513
513
|
* The function checks if a binary tree has a node with a given property or key.
|
|
514
|
-
* @param {
|
|
515
|
-
* the node that you want to find in the binary tree. It can be either a `
|
|
514
|
+
* @param {BTNKey | N} identifier - The `identifier` parameter is the key or value of
|
|
515
|
+
* the node that you want to find in the binary tree. It can be either a `BTNKey` or a
|
|
516
516
|
* generic type `N`.
|
|
517
517
|
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
518
518
|
* matches the desired criteria. It takes a node as input and returns a boolean value indicating
|
|
519
519
|
* whether the node matches the criteria or not. The default callback function
|
|
520
|
-
* `
|
|
520
|
+
* `((node: N) => node.key)` is used if no callback function is
|
|
521
521
|
* @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
|
|
522
522
|
* the node from which the search should begin. By default, it is set to `this.root`, which means the
|
|
523
523
|
* search will start from the root node of the binary tree. However, you can provide a different node
|
|
@@ -526,7 +526,7 @@ class BinaryTree {
|
|
|
526
526
|
* performed when searching for nodes in the binary tree. It can have one of the following values:
|
|
527
527
|
* @returns a boolean value.
|
|
528
528
|
*/
|
|
529
|
-
has(identifier, callback =
|
|
529
|
+
has(identifier, callback = ((node) => node.key), beginRoot = this.root, iterationType = this.iterationType) {
|
|
530
530
|
if (identifier instanceof BinaryTreeNode)
|
|
531
531
|
callback = (node => node);
|
|
532
532
|
// TODO may support finding node by value equal
|
|
@@ -534,20 +534,20 @@ class BinaryTree {
|
|
|
534
534
|
}
|
|
535
535
|
/**
|
|
536
536
|
* The function `get` returns the first node in a binary tree that matches the given property or key.
|
|
537
|
-
* @param {
|
|
538
|
-
* the node that you want to find in the binary tree. It can be either a `
|
|
537
|
+
* @param {BTNKey | N} identifier - The `identifier` parameter is the key or value of
|
|
538
|
+
* the node that you want to find in the binary tree. It can be either a `BTNKey` or `N`
|
|
539
539
|
* type.
|
|
540
540
|
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
541
541
|
* matches the desired criteria. It takes a node as input and returns a boolean value indicating
|
|
542
542
|
* whether the node matches the criteria or not. The default callback function
|
|
543
|
-
* (`
|
|
543
|
+
* (`((node: N) => node.key)`) is used if no callback function is
|
|
544
544
|
* @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
|
|
545
545
|
* the root node from which the search should begin.
|
|
546
546
|
* @param iterationType - The `iterationType` parameter specifies the type of iteration to be
|
|
547
547
|
* performed when searching for a node in the binary tree. It can have one of the following values:
|
|
548
548
|
* @returns either the found node (of type N) or null if no node is found.
|
|
549
549
|
*/
|
|
550
|
-
get(identifier, callback =
|
|
550
|
+
get(identifier, callback = ((node) => node.key), beginRoot = this.root, iterationType = this.iterationType) {
|
|
551
551
|
if (identifier instanceof BinaryTreeNode)
|
|
552
552
|
callback = (node => node);
|
|
553
553
|
// TODO may support finding node by value equal
|
|
@@ -578,9 +578,9 @@ class BinaryTree {
|
|
|
578
578
|
/**
|
|
579
579
|
* The function `getLeftMost` returns the leftmost node in a binary tree, either using recursive or
|
|
580
580
|
* iterative traversal.
|
|
581
|
-
* @param {
|
|
581
|
+
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter is the starting point
|
|
582
582
|
* for finding the leftmost node in a binary tree. It can be either a node object (`N`), a key value
|
|
583
|
-
* of a node (`
|
|
583
|
+
* of a node (`BTNKey`), or `null` if the tree is empty.
|
|
584
584
|
* @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
|
|
585
585
|
* be performed when finding the leftmost node in a binary tree. It can have two possible values:
|
|
586
586
|
* @returns The function `getLeftMost` returns the leftmost node (`N`) in a binary tree. If there is
|
|
@@ -702,14 +702,14 @@ class BinaryTree {
|
|
|
702
702
|
* subtree traversal. It takes a single argument, which is the current node being traversed, and
|
|
703
703
|
* returns a value. The return values from each callback invocation will be collected and returned as
|
|
704
704
|
* an array.
|
|
705
|
-
* @param {
|
|
705
|
+
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter is the starting point
|
|
706
706
|
* for traversing the subtree. It can be either a node object, a key value of a node, or `null` to
|
|
707
707
|
* start from the root of the tree.
|
|
708
708
|
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
709
709
|
* performed on the binary tree. It can have two possible values:
|
|
710
|
-
* @returns The function `subTreeTraverse` returns an array of `ReturnType<
|
|
710
|
+
* @returns The function `subTreeTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
|
|
711
711
|
*/
|
|
712
|
-
subTreeTraverse(callback =
|
|
712
|
+
subTreeTraverse(callback = ((node) => node.key), beginRoot = this.root, iterationType = this.iterationType) {
|
|
713
713
|
if (typeof beginRoot === 'number')
|
|
714
714
|
beginRoot = this.get(beginRoot);
|
|
715
715
|
const ans = [];
|
|
@@ -739,7 +739,7 @@ class BinaryTree {
|
|
|
739
739
|
* function on each node according to a specified order pattern.
|
|
740
740
|
* @param callback - The `callback` parameter is a function that will be called on each node during
|
|
741
741
|
* the depth-first search traversal. It takes a node as input and returns a value. The default value
|
|
742
|
-
* is `
|
|
742
|
+
* is `((node: N) => node.key)`, which is a callback function defined elsewhere in the code.
|
|
743
743
|
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter determines the order in which the
|
|
744
744
|
* nodes are visited during the depth-first search. There are three possible values for `pattern`:
|
|
745
745
|
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the depth-first
|
|
@@ -747,9 +747,9 @@ class BinaryTree {
|
|
|
747
747
|
* is `null`, an empty array will be returned.
|
|
748
748
|
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
749
749
|
* iteration used in the depth-first search algorithm. It can have two possible values:
|
|
750
|
-
* @returns The function `dfs` returns an array of `ReturnType<
|
|
750
|
+
* @returns The function `dfs` returns an array of `ReturnType<BTNCallback<N>>` values.
|
|
751
751
|
*/
|
|
752
|
-
dfs(callback =
|
|
752
|
+
dfs(callback = ((node) => node.key), pattern = 'in', beginRoot = this.root, iterationType = types_1.IterationType.ITERATIVE) {
|
|
753
753
|
if (!beginRoot)
|
|
754
754
|
return [];
|
|
755
755
|
const ans = [];
|
|
@@ -824,15 +824,15 @@ class BinaryTree {
|
|
|
824
824
|
* function on each node.
|
|
825
825
|
* @param callback - The `callback` parameter is a function that will be called for each node in the
|
|
826
826
|
* breadth-first search. It takes a node of type `N` as its argument and returns a value of type
|
|
827
|
-
* `ReturnType<
|
|
827
|
+
* `ReturnType<BTNCallback<N>>`. The default value for this parameter is `((node: N) => node.key)
|
|
828
828
|
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
|
|
829
829
|
* search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
|
|
830
830
|
* will not be performed and an empty array will be returned.
|
|
831
831
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be used
|
|
832
832
|
* in the breadth-first search (BFS) algorithm. It can have two possible values:
|
|
833
|
-
* @returns The function `bfs` returns an array of `ReturnType<
|
|
833
|
+
* @returns The function `bfs` returns an array of `ReturnType<BTNCallback<N>>[]`.
|
|
834
834
|
*/
|
|
835
|
-
bfs(callback =
|
|
835
|
+
bfs(callback = ((node) => node.key), beginRoot = this.root, iterationType = this.iterationType) {
|
|
836
836
|
if (!beginRoot)
|
|
837
837
|
return [];
|
|
838
838
|
const ans = [];
|
|
@@ -882,7 +882,7 @@ class BinaryTree {
|
|
|
882
882
|
* level in a binary tree. Each inner array contains the return type of the provided callback
|
|
883
883
|
* function `C` applied to the nodes at that level.
|
|
884
884
|
*/
|
|
885
|
-
listLevels(callback =
|
|
885
|
+
listLevels(callback = ((node) => node.key), beginRoot = this.root, iterationType = this.iterationType) {
|
|
886
886
|
if (!beginRoot)
|
|
887
887
|
return [];
|
|
888
888
|
const levelsNodes = [];
|
|
@@ -938,17 +938,17 @@ class BinaryTree {
|
|
|
938
938
|
* The `morris` function performs a depth-first traversal of a binary tree using the Morris traversal
|
|
939
939
|
* algorithm and returns an array of values obtained by applying a callback function to each node.
|
|
940
940
|
* @param callback - The `callback` parameter is a function that will be called on each node in the
|
|
941
|
-
* tree. It takes a node of type `N` as input and returns a value of type `ReturnType<
|
|
942
|
-
* default value for this parameter is `
|
|
941
|
+
* tree. It takes a node of type `N` as input and returns a value of type `ReturnType<BTNCallback<N>>`. The
|
|
942
|
+
* default value for this parameter is `((node: N) => node.key)`.
|
|
943
943
|
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
|
|
944
944
|
* determines the order in which the nodes of a binary tree are traversed. It can have one of the
|
|
945
945
|
* following values:
|
|
946
946
|
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the Morris
|
|
947
947
|
* traversal. It specifies the root node of the tree from which the traversal should begin. If
|
|
948
948
|
* `beginRoot` is `null`, an empty array will be returned.
|
|
949
|
-
* @returns The `morris` function returns an array of `ReturnType<
|
|
949
|
+
* @returns The `morris` function returns an array of `ReturnType<BTNCallback<N>>` values.
|
|
950
950
|
*/
|
|
951
|
-
morris(callback =
|
|
951
|
+
morris(callback = ((node) => node.key), pattern = 'in', beginRoot = this.root) {
|
|
952
952
|
if (beginRoot === null)
|
|
953
953
|
return [];
|
|
954
954
|
const ans = [];
|
|
@@ -1087,14 +1087,6 @@ class BinaryTree {
|
|
|
1087
1087
|
}
|
|
1088
1088
|
return destNode;
|
|
1089
1089
|
}
|
|
1090
|
-
/**
|
|
1091
|
-
* Time complexity is O(n)
|
|
1092
|
-
* Space complexity of Iterative dfs equals to recursive dfs which is O(n) because of the stack
|
|
1093
|
-
* The Morris algorithm only modifies the tree's structure during traversal; once the traversal is complete,
|
|
1094
|
-
* the tree's structure should be restored to its original state to maintain the tree's integrity.
|
|
1095
|
-
* This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
|
|
1096
|
-
*/
|
|
1097
|
-
_defaultCallbackByKey = node => node.key;
|
|
1098
1090
|
/**
|
|
1099
1091
|
* The function `_addTo` adds a new node to a binary tree if there is an available position.
|
|
1100
1092
|
* @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to
|