data-structure-typed 1.37.2 → 1.37.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 +3 -1
- package/dist/data-structures/binary-tree/avl-tree.d.ts +44 -38
- package/dist/data-structures/binary-tree/avl-tree.js +46 -40
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-tree.d.ts +305 -192
- package/dist/data-structures/binary-tree/binary-tree.js +304 -201
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/bst.d.ts +111 -64
- package/dist/data-structures/binary-tree/bst.js +132 -85
- package/dist/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +49 -41
- package/dist/data-structures/binary-tree/tree-multiset.js +49 -41
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/types/data-structures/binary-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree.js +6 -6
- package/dist/types/data-structures/binary-tree.js.map +1 -1
- package/lib/data-structures/binary-tree/avl-tree.d.ts +44 -38
- package/lib/data-structures/binary-tree/avl-tree.js +46 -40
- package/lib/data-structures/binary-tree/binary-tree.d.ts +305 -192
- package/lib/data-structures/binary-tree/binary-tree.js +305 -202
- package/lib/data-structures/binary-tree/bst.d.ts +111 -64
- package/lib/data-structures/binary-tree/bst.js +133 -86
- package/lib/data-structures/binary-tree/tree-multiset.d.ts +49 -41
- package/lib/data-structures/binary-tree/tree-multiset.js +50 -42
- package/lib/types/data-structures/binary-tree.d.ts +2 -2
- package/lib/types/data-structures/binary-tree.js +5 -5
- package/package.json +6 -6
- package/src/data-structures/binary-tree/avl-tree.ts +46 -40
- package/src/data-structures/binary-tree/binary-tree.ts +328 -207
- package/src/data-structures/binary-tree/bst.ts +135 -88
- package/src/data-structures/binary-tree/tree-multiset.ts +50 -42
- package/src/types/data-structures/binary-tree.ts +2 -2
- package/test/config.ts +1 -0
- package/test/integration/avl-tree.test.ts +7 -8
- package/test/integration/bst.test.ts +17 -16
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +50 -0
- package/test/unit/data-structures/binary-tree/bst.test.ts +8 -1
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +2 -1
- package/test/unit/data-structures/linked-list/linked-list.test.ts +1 -1
- package/test/utils/big-o.ts +2 -1
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.map +1 -1
|
@@ -5,33 +5,50 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { FamilyPosition,
|
|
8
|
+
import { FamilyPosition, IterationType } from '../../types';
|
|
9
9
|
import { trampoline } from '../../utils';
|
|
10
10
|
import { Queue } from '../queue';
|
|
11
|
+
/**
|
|
12
|
+
* Represents a node in a binary tree.
|
|
13
|
+
* @template V - The type of data stored in the node.
|
|
14
|
+
* @template FAMILY - The type of the family relationship in the binary tree.
|
|
15
|
+
*/
|
|
11
16
|
export class BinaryTreeNode {
|
|
12
17
|
/**
|
|
13
|
-
*
|
|
14
|
-
* @param {BinaryTreeNodeKey} key - The
|
|
15
|
-
*
|
|
16
|
-
* @param {V} [val] - The "val" parameter is an optional parameter of type V. It represents the value that will be
|
|
17
|
-
* stored in the binary tree node. If no value is provided, it will be set to undefined.
|
|
18
|
+
* Creates a new instance of BinaryTreeNode.
|
|
19
|
+
* @param {BinaryTreeNodeKey} key - The key associated with the node.
|
|
20
|
+
* @param {V} val - The value stored in the node.
|
|
18
21
|
*/
|
|
19
22
|
constructor(key, val) {
|
|
20
23
|
this.key = key;
|
|
21
24
|
this.val = val;
|
|
22
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Get the left child node.
|
|
28
|
+
*/
|
|
23
29
|
get left() {
|
|
24
30
|
return this._left;
|
|
25
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Set the left child node.
|
|
34
|
+
* @param {FAMILY | null | undefined} v - The left child node.
|
|
35
|
+
*/
|
|
26
36
|
set left(v) {
|
|
27
37
|
if (v) {
|
|
28
38
|
v.parent = this;
|
|
29
39
|
}
|
|
30
40
|
this._left = v;
|
|
31
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* Get the right child node.
|
|
44
|
+
*/
|
|
32
45
|
get right() {
|
|
33
46
|
return this._right;
|
|
34
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* Set the right child node.
|
|
50
|
+
* @param {FAMILY | null | undefined} v - The right child node.
|
|
51
|
+
*/
|
|
35
52
|
set right(v) {
|
|
36
53
|
if (v) {
|
|
37
54
|
v.parent = this;
|
|
@@ -39,8 +56,8 @@ export class BinaryTreeNode {
|
|
|
39
56
|
this._right = v;
|
|
40
57
|
}
|
|
41
58
|
/**
|
|
42
|
-
*
|
|
43
|
-
* @returns
|
|
59
|
+
* Get the position of the node within its family.
|
|
60
|
+
* @returns {FamilyPosition} - The family position of the node.
|
|
44
61
|
*/
|
|
45
62
|
get familyPosition() {
|
|
46
63
|
const that = this;
|
|
@@ -75,53 +92,64 @@ export class BinaryTreeNode {
|
|
|
75
92
|
}
|
|
76
93
|
}
|
|
77
94
|
}
|
|
95
|
+
/**
|
|
96
|
+
* Represents a binary tree data structure.
|
|
97
|
+
* @template N - The type of the binary tree's nodes.
|
|
98
|
+
*/
|
|
78
99
|
export class BinaryTree {
|
|
79
100
|
/**
|
|
80
|
-
*
|
|
81
|
-
* @param {BinaryTreeOptions} [options] - The
|
|
82
|
-
* constructor of the `BinaryTree` class. It allows you to customize the behavior of the binary tree by providing
|
|
83
|
-
* different configuration options.
|
|
101
|
+
* Creates a new instance of BinaryTree.
|
|
102
|
+
* @param {BinaryTreeOptions} [options] - The options for the binary tree.
|
|
84
103
|
*/
|
|
85
104
|
constructor(options) {
|
|
86
|
-
// TODO placeholder node may need redesigned
|
|
87
105
|
this._root = null;
|
|
88
106
|
this._size = 0;
|
|
89
|
-
this._loopType =
|
|
107
|
+
this._loopType = IterationType.ITERATIVE;
|
|
90
108
|
this._defaultCallbackByKey = node => node.key;
|
|
91
109
|
if (options !== undefined) {
|
|
92
|
-
const {
|
|
93
|
-
this._loopType =
|
|
110
|
+
const { iterationType = IterationType.ITERATIVE } = options;
|
|
111
|
+
this._loopType = iterationType;
|
|
94
112
|
}
|
|
95
113
|
}
|
|
96
114
|
/**
|
|
97
|
-
*
|
|
98
|
-
* @param {BinaryTreeNodeKey} key - The
|
|
99
|
-
*
|
|
100
|
-
* @
|
|
101
|
-
* stored in the node.
|
|
102
|
-
* @returns a new instance of a BinaryTreeNode with the specified key and value.
|
|
115
|
+
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
116
|
+
* @param {BinaryTreeNodeKey} key - The key for the new node.
|
|
117
|
+
* @param {N['val']} val - The value for the new node.
|
|
118
|
+
* @returns {N} - The newly created BinaryTreeNode.
|
|
103
119
|
*/
|
|
104
120
|
createNode(key, val) {
|
|
105
121
|
return new BinaryTreeNode(key, val);
|
|
106
122
|
}
|
|
123
|
+
/**
|
|
124
|
+
* Get the root node of the binary tree.
|
|
125
|
+
*/
|
|
107
126
|
get root() {
|
|
108
127
|
return this._root;
|
|
109
128
|
}
|
|
129
|
+
/**
|
|
130
|
+
* Get the number of nodes in the binary tree.
|
|
131
|
+
*/
|
|
110
132
|
get size() {
|
|
111
133
|
return this._size;
|
|
112
134
|
}
|
|
113
|
-
|
|
135
|
+
/**
|
|
136
|
+
* Get the iteration type used in the binary tree.
|
|
137
|
+
*/
|
|
138
|
+
get iterationType() {
|
|
114
139
|
return this._loopType;
|
|
115
140
|
}
|
|
116
|
-
|
|
141
|
+
/**
|
|
142
|
+
* Set the iteration type for the binary tree.
|
|
143
|
+
* @param {IterationType} v - The new iteration type to set.
|
|
144
|
+
*/
|
|
145
|
+
set iterationType(v) {
|
|
117
146
|
this._loopType = v;
|
|
118
147
|
}
|
|
119
148
|
/**
|
|
120
|
-
*
|
|
121
|
-
* @param {N} srcNode - The source node
|
|
122
|
-
* @param {N} destNode - The
|
|
123
|
-
*
|
|
124
|
-
* @returns The `destNode` is being returned.
|
|
149
|
+
* Swap the data of two nodes in the binary tree.
|
|
150
|
+
* @param {N} srcNode - The source node to swap.
|
|
151
|
+
* @param {N} destNode - The destination node to swap.
|
|
152
|
+
* @returns {N} - The destination node after the swap.
|
|
125
153
|
*/
|
|
126
154
|
_swap(srcNode, destNode) {
|
|
127
155
|
const { key, val } = destNode;
|
|
@@ -135,31 +163,24 @@ export class BinaryTree {
|
|
|
135
163
|
return destNode;
|
|
136
164
|
}
|
|
137
165
|
/**
|
|
138
|
-
*
|
|
166
|
+
* Clear the binary tree, removing all nodes.
|
|
139
167
|
*/
|
|
140
168
|
clear() {
|
|
141
169
|
this._root = null;
|
|
142
170
|
this._size = 0;
|
|
143
171
|
}
|
|
144
172
|
/**
|
|
145
|
-
*
|
|
146
|
-
* @returns
|
|
173
|
+
* Check if the binary tree is empty.
|
|
174
|
+
* @returns {boolean} - True if the binary tree is empty, false otherwise.
|
|
147
175
|
*/
|
|
148
176
|
isEmpty() {
|
|
149
177
|
return this.size === 0;
|
|
150
178
|
}
|
|
151
179
|
/**
|
|
152
|
-
*
|
|
153
|
-
*
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
* The `add` function adds a new node to a binary tree, either by ID or by creating a new node with a given value.
|
|
157
|
-
* @param {BinaryTreeNodeKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a `BinaryTreeNodeKey`, which
|
|
158
|
-
* is a number representing the ID of a binary tree node, or it can be a `N` object, which represents a binary tree
|
|
159
|
-
* node itself. It can also be `null` if no node is specified.
|
|
160
|
-
* @param [val] - The `val` parameter is an optional value that can be assigned to the `val` property of the new node
|
|
161
|
-
* being added to the binary tree.
|
|
162
|
-
* @returns The function `add` returns either the inserted node (`N`), `null`, or `undefined`.
|
|
180
|
+
* Add a node with the given key and value to the binary tree.
|
|
181
|
+
* @param {BinaryTreeNodeKey | N | null} keyOrNode - The key or node to add to the binary tree.
|
|
182
|
+
* @param {N['val']} val - The value for the new node (optional).
|
|
183
|
+
* @returns {N | null | undefined} - The inserted node, or null if nothing was inserted, or undefined if the operation failed.
|
|
163
184
|
*/
|
|
164
185
|
add(keyOrNode, val) {
|
|
165
186
|
const _bfs = (root, newNode) => {
|
|
@@ -222,12 +243,12 @@ export class BinaryTree {
|
|
|
222
243
|
* values, and adds them to the binary tree.
|
|
223
244
|
* @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of BinaryTreeNodeKey or BinaryTreeNode
|
|
224
245
|
* objects, or null values.
|
|
225
|
-
* @param {N['val'][]} [
|
|
226
|
-
* the nodes or node IDs being added. It is used to set the value of each node being added. If `
|
|
246
|
+
* @param {N['val'][]} [values] - The `values` parameter is an optional array of values (`N['val'][]`) that corresponds to
|
|
247
|
+
* the nodes or node IDs being added. It is used to set the value of each node being added. If `values` is not provided,
|
|
227
248
|
* the value of the nodes will be `undefined`.
|
|
228
249
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
229
250
|
*/
|
|
230
|
-
addMany(keysOrNodes,
|
|
251
|
+
addMany(keysOrNodes, values) {
|
|
231
252
|
// TODO not sure addMany not be run multi times
|
|
232
253
|
const inserted = [];
|
|
233
254
|
for (let i = 0; i < keysOrNodes.length; i++) {
|
|
@@ -240,7 +261,7 @@ export class BinaryTree {
|
|
|
240
261
|
inserted.push(this.add(null));
|
|
241
262
|
continue;
|
|
242
263
|
}
|
|
243
|
-
const val =
|
|
264
|
+
const val = values === null || values === void 0 ? void 0 : values[i];
|
|
244
265
|
inserted.push(this.add(keyOrNode, val));
|
|
245
266
|
}
|
|
246
267
|
return inserted;
|
|
@@ -259,11 +280,12 @@ export class BinaryTree {
|
|
|
259
280
|
return keysOrNodes.length === this.addMany(keysOrNodes, data).length;
|
|
260
281
|
}
|
|
261
282
|
/**
|
|
262
|
-
* The `delete` function
|
|
263
|
-
*
|
|
264
|
-
* @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node
|
|
265
|
-
*
|
|
266
|
-
*
|
|
283
|
+
* The `delete` function removes a node from a binary search tree and returns the deleted node along
|
|
284
|
+
* with the parent node that needs to be balanced.
|
|
285
|
+
* @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node (`N`) or
|
|
286
|
+
* a key (`BinaryTreeNodeKey`). If it is a key, the function will find the corresponding node in the
|
|
287
|
+
* binary tree.
|
|
288
|
+
* @returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
267
289
|
*/
|
|
268
290
|
delete(nodeOrKey) {
|
|
269
291
|
const bstDeletedResult = [];
|
|
@@ -309,10 +331,16 @@ export class BinaryTree {
|
|
|
309
331
|
return bstDeletedResult;
|
|
310
332
|
}
|
|
311
333
|
/**
|
|
312
|
-
* The function calculates the depth of a node in a binary tree
|
|
313
|
-
*
|
|
314
|
-
* @param {N | BinaryTreeNodeKey | null}
|
|
315
|
-
*
|
|
334
|
+
* The function `getDepth` calculates the depth of a given node in a binary tree relative to a
|
|
335
|
+
* specified root node.
|
|
336
|
+
* @param {N | BinaryTreeNodeKey | null} distNode - The `distNode` parameter represents the node
|
|
337
|
+
* whose depth we want to find in the binary tree. It can be either a node object (`N`), a key value
|
|
338
|
+
* of the node (`BinaryTreeNodeKey`), or `null`.
|
|
339
|
+
* @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter represents the
|
|
340
|
+
* starting node from which we want to calculate the depth. It can be either a node object or the key
|
|
341
|
+
* of a node in the binary tree. If no value is provided for `beginRoot`, it defaults to the root
|
|
342
|
+
* node of the binary tree.
|
|
343
|
+
* @returns the depth of the `distNode` relative to the `beginRoot`.
|
|
316
344
|
*/
|
|
317
345
|
getDepth(distNode, beginRoot = this.root) {
|
|
318
346
|
if (typeof distNode === 'number')
|
|
@@ -330,18 +358,23 @@ export class BinaryTree {
|
|
|
330
358
|
return depth;
|
|
331
359
|
}
|
|
332
360
|
/**
|
|
333
|
-
* The `getHeight` function calculates the maximum height of a binary tree
|
|
334
|
-
*
|
|
335
|
-
*
|
|
336
|
-
* node
|
|
361
|
+
* The `getHeight` function calculates the maximum height of a binary tree using either recursive or
|
|
362
|
+
* iterative approach.
|
|
363
|
+
* @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter represents the
|
|
364
|
+
* starting node from which the height of the binary tree is calculated. It can be either a node
|
|
365
|
+
* object (`N`), a key value of a node in the tree (`BinaryTreeNodeKey`), or `null` if no starting
|
|
366
|
+
* node is specified. If `
|
|
367
|
+
* @param iterationType - The `iterationType` parameter is used to determine whether to calculate the
|
|
368
|
+
* height of the binary tree using a recursive approach or an iterative approach. It can have two
|
|
369
|
+
* possible values:
|
|
337
370
|
* @returns the height of the binary tree.
|
|
338
371
|
*/
|
|
339
|
-
getHeight(beginRoot = this.root) {
|
|
372
|
+
getHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
340
373
|
if (typeof beginRoot === 'number')
|
|
341
374
|
beginRoot = this.get(beginRoot);
|
|
342
375
|
if (!beginRoot)
|
|
343
376
|
return -1;
|
|
344
|
-
if (
|
|
377
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
345
378
|
const _getMaxHeight = (cur) => {
|
|
346
379
|
if (!cur)
|
|
347
380
|
return -1;
|
|
@@ -371,18 +404,20 @@ export class BinaryTree {
|
|
|
371
404
|
}
|
|
372
405
|
}
|
|
373
406
|
/**
|
|
374
|
-
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
375
|
-
* approach.
|
|
376
|
-
* @param {N | null}
|
|
377
|
-
*
|
|
378
|
-
*
|
|
379
|
-
* @
|
|
407
|
+
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
408
|
+
* recursive or iterative approach.
|
|
409
|
+
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which we want to
|
|
410
|
+
* calculate the minimum height of the tree. It is optional and defaults to the root of the tree if
|
|
411
|
+
* not provided.
|
|
412
|
+
* @param iterationType - The `iterationType` parameter is used to determine the method of iteration
|
|
413
|
+
* to calculate the minimum height of a binary tree. It can have two possible values:
|
|
414
|
+
* @returns The function `getMinHeight` returns the minimum height of a binary tree.
|
|
380
415
|
*/
|
|
381
|
-
getMinHeight(beginRoot = this.root) {
|
|
416
|
+
getMinHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
382
417
|
var _a, _b, _c;
|
|
383
418
|
if (!beginRoot)
|
|
384
419
|
return -1;
|
|
385
|
-
if (
|
|
420
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
386
421
|
const _getMinHeight = (cur) => {
|
|
387
422
|
if (!cur)
|
|
388
423
|
return 0;
|
|
@@ -423,32 +458,41 @@ export class BinaryTree {
|
|
|
423
458
|
}
|
|
424
459
|
}
|
|
425
460
|
/**
|
|
426
|
-
* The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
|
|
427
|
-
* tree.
|
|
428
|
-
* @param {N | null}
|
|
429
|
-
* tree or null
|
|
461
|
+
* The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
|
|
462
|
+
* height of the tree.
|
|
463
|
+
* @param {N | null} beginRoot - The parameter `beginRoot` is of type `N | null`, which means it can
|
|
464
|
+
* either be of type `N` (representing a node in a tree) or `null` (representing an empty tree).
|
|
430
465
|
* @returns The method is returning a boolean value.
|
|
431
466
|
*/
|
|
432
467
|
isPerfectlyBalanced(beginRoot = this.root) {
|
|
433
468
|
return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
|
|
434
469
|
}
|
|
435
470
|
/**
|
|
436
|
-
* The function `getNodes` returns an array of nodes that match a given property
|
|
437
|
-
*
|
|
438
|
-
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter
|
|
439
|
-
* generic type `N`. It represents the property of the
|
|
440
|
-
*
|
|
441
|
-
* @param
|
|
442
|
-
*
|
|
443
|
-
*
|
|
444
|
-
*
|
|
445
|
-
* @
|
|
471
|
+
* The function `getNodes` returns an array of nodes that match a given node property, using either
|
|
472
|
+
* recursive or iterative traversal.
|
|
473
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is either a
|
|
474
|
+
* `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are
|
|
475
|
+
* searching for. It can be a specific key value or any other property of the node.
|
|
476
|
+
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
477
|
+
* value. This value is compared with the `nodeProperty` parameter to determine if the node should be
|
|
478
|
+
* included in the result. The `callback` parameter has a default value of
|
|
479
|
+
* `this._defaultCallbackByKey`, which
|
|
480
|
+
* @param [onlyOne=false] - A boolean value indicating whether to stop searching after finding the
|
|
481
|
+
* first node that matches the nodeProperty. If set to true, the function will return an array with
|
|
482
|
+
* only one element (or an empty array if no matching node is found). If set to false (default), the
|
|
483
|
+
* function will continue searching for all
|
|
484
|
+
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which the
|
|
485
|
+
* traversal of the binary tree will begin. It is optional and defaults to the root of the binary
|
|
486
|
+
* tree.
|
|
487
|
+
* @param iterationType - The `iterationType` parameter determines the type of iteration used to
|
|
488
|
+
* traverse the binary tree. It can have two possible values:
|
|
489
|
+
* @returns The function `getNodes` returns an array of nodes (`N[]`).
|
|
446
490
|
*/
|
|
447
|
-
getNodes(nodeProperty, callback = this._defaultCallbackByKey, onlyOne = false, beginRoot = this.root) {
|
|
491
|
+
getNodes(nodeProperty, callback = this._defaultCallbackByKey, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
448
492
|
if (!beginRoot)
|
|
449
493
|
return [];
|
|
450
494
|
const ans = [];
|
|
451
|
-
if (
|
|
495
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
452
496
|
const _traverse = (cur) => {
|
|
453
497
|
if (callback(cur) === nodeProperty) {
|
|
454
498
|
ans.push(cur);
|
|
@@ -480,42 +524,55 @@ export class BinaryTree {
|
|
|
480
524
|
return ans;
|
|
481
525
|
}
|
|
482
526
|
/**
|
|
483
|
-
* The function checks if a binary tree node
|
|
484
|
-
* @param
|
|
485
|
-
*
|
|
486
|
-
*
|
|
487
|
-
*
|
|
527
|
+
* The function checks if a binary tree has a node with a given property or key.
|
|
528
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is the key or value of
|
|
529
|
+
* the node that you want to find in the binary tree. It can be either a `BinaryTreeNodeKey` or a
|
|
530
|
+
* generic type `N`.
|
|
531
|
+
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
532
|
+
* matches the desired criteria. It takes a node as input and returns a boolean value indicating
|
|
533
|
+
* whether the node matches the criteria or not. The default callback function
|
|
534
|
+
* `this._defaultCallbackByKey` is used if no callback function is
|
|
535
|
+
* @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
|
|
536
|
+
* the node from which the search should begin. By default, it is set to `this.root`, which means the
|
|
537
|
+
* search will start from the root node of the binary tree. However, you can provide a different node
|
|
538
|
+
* as
|
|
539
|
+
* @param iterationType - The `iterationType` parameter specifies the type of iteration to be
|
|
540
|
+
* performed when searching for nodes in the binary tree. It can have one of the following values:
|
|
488
541
|
* @returns a boolean value.
|
|
489
542
|
*/
|
|
490
|
-
has(nodeProperty, callback = this._defaultCallbackByKey) {
|
|
543
|
+
has(nodeProperty, callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
|
|
491
544
|
// TODO may support finding node by value equal
|
|
492
|
-
return this.getNodes(nodeProperty, callback, true).length > 0;
|
|
545
|
+
return this.getNodes(nodeProperty, callback, true, beginRoot, iterationType).length > 0;
|
|
493
546
|
}
|
|
494
547
|
/**
|
|
495
|
-
* The function returns the first node that matches the given property
|
|
496
|
-
*
|
|
497
|
-
*
|
|
498
|
-
*
|
|
499
|
-
*
|
|
500
|
-
*
|
|
501
|
-
*
|
|
502
|
-
*
|
|
503
|
-
*
|
|
548
|
+
* The function `get` returns the first node in a binary tree that matches the given property or key.
|
|
549
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is the key or value of
|
|
550
|
+
* the node that you want to find in the binary tree. It can be either a `BinaryTreeNodeKey` or `N`
|
|
551
|
+
* type.
|
|
552
|
+
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
553
|
+
* matches the desired criteria. It takes a node as input and returns a boolean value indicating
|
|
554
|
+
* whether the node matches the criteria or not. The default callback function
|
|
555
|
+
* (`this._defaultCallbackByKey`) is used if no callback function is
|
|
556
|
+
* @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
|
|
557
|
+
* the root node from which the search should begin.
|
|
558
|
+
* @param iterationType - The `iterationType` parameter specifies the type of iteration to be
|
|
559
|
+
* performed when searching for a node in the binary tree. It can have one of the following values:
|
|
560
|
+
* @returns either the found node (of type N) or null if no node is found.
|
|
504
561
|
*/
|
|
505
|
-
get(nodeProperty, callback = this._defaultCallbackByKey) {
|
|
562
|
+
get(nodeProperty, callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
|
|
506
563
|
var _a;
|
|
507
564
|
// TODO may support finding node by value equal
|
|
508
|
-
return (_a = this.getNodes(nodeProperty, callback, true)[0]) !== null && _a !== void 0 ? _a : null;
|
|
565
|
+
return (_a = this.getNodes(nodeProperty, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : null;
|
|
509
566
|
}
|
|
510
567
|
/**
|
|
511
|
-
* The function `getPathToRoot` returns an array of nodes
|
|
512
|
-
*
|
|
513
|
-
*
|
|
514
|
-
*
|
|
515
|
-
* @param
|
|
516
|
-
* path should be reversed or not. If `isReverse` is set to `true`, the path will be
|
|
517
|
-
* `isReverse` is set to `false` or not provided, the path will
|
|
518
|
-
* @returns The function `getPathToRoot` returns an array of
|
|
568
|
+
* The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
|
|
569
|
+
* up to the root node, with the option to reverse the order of the nodes.
|
|
570
|
+
* @param {N} beginRoot - The `beginRoot` parameter represents the starting node from which you want
|
|
571
|
+
* to find the path to the root node.
|
|
572
|
+
* @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
|
|
573
|
+
* resulting path should be reversed or not. If `isReverse` is set to `true`, the path will be
|
|
574
|
+
* reversed before returning it. If `isReverse` is set to `false` or not provided, the path will
|
|
575
|
+
* @returns The function `getPathToRoot` returns an array of type `N[]`.
|
|
519
576
|
*/
|
|
520
577
|
getPathToRoot(beginRoot, isReverse = true) {
|
|
521
578
|
// TODO to support get path through passing key
|
|
@@ -530,22 +587,22 @@ export class BinaryTree {
|
|
|
530
587
|
return isReverse ? result.reverse() : result;
|
|
531
588
|
}
|
|
532
589
|
/**
|
|
533
|
-
* The function `getLeftMost` returns the leftmost node in a binary tree,
|
|
534
|
-
*
|
|
535
|
-
* @param {N | BinaryTreeNodeKey | null}
|
|
536
|
-
*
|
|
537
|
-
* node), or `null
|
|
538
|
-
* @
|
|
539
|
-
*
|
|
540
|
-
*
|
|
541
|
-
* node
|
|
590
|
+
* The function `getLeftMost` returns the leftmost node in a binary tree, either using recursive or
|
|
591
|
+
* iterative traversal.
|
|
592
|
+
* @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter is the starting point
|
|
593
|
+
* for finding the leftmost node in a binary tree. It can be either a node object (`N`), a key value
|
|
594
|
+
* of a node (`BinaryTreeNodeKey`), or `null` if the tree is empty.
|
|
595
|
+
* @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
|
|
596
|
+
* be performed when finding the leftmost node in a binary tree. It can have two possible values:
|
|
597
|
+
* @returns The function `getLeftMost` returns the leftmost node (`N`) in a binary tree. If there is
|
|
598
|
+
* no leftmost node, it returns `null`.
|
|
542
599
|
*/
|
|
543
|
-
getLeftMost(beginRoot = this.root) {
|
|
600
|
+
getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
544
601
|
if (typeof beginRoot === 'number')
|
|
545
602
|
beginRoot = this.get(beginRoot);
|
|
546
603
|
if (!beginRoot)
|
|
547
604
|
return beginRoot;
|
|
548
|
-
if (
|
|
605
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
549
606
|
const _traverse = (cur) => {
|
|
550
607
|
if (!cur.left)
|
|
551
608
|
return cur;
|
|
@@ -564,20 +621,21 @@ export class BinaryTree {
|
|
|
564
621
|
}
|
|
565
622
|
}
|
|
566
623
|
/**
|
|
567
|
-
* The `getRightMost`
|
|
568
|
-
*
|
|
569
|
-
* @param {N | null}
|
|
570
|
-
*
|
|
571
|
-
*
|
|
572
|
-
* @
|
|
573
|
-
*
|
|
574
|
-
*
|
|
624
|
+
* The function `getRightMost` returns the rightmost node in a binary tree, either recursively or
|
|
625
|
+
* iteratively.
|
|
626
|
+
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which we want to
|
|
627
|
+
* find the rightmost node. It is of type `N | null`, which means it can either be a node of type `N`
|
|
628
|
+
* or `null`. If it is `null`, it means there is no starting node
|
|
629
|
+
* @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
|
|
630
|
+
* be performed when finding the rightmost node in a binary tree. It can have two possible values:
|
|
631
|
+
* @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If the
|
|
632
|
+
* `beginRoot` parameter is `null`, it returns `null`.
|
|
575
633
|
*/
|
|
576
|
-
getRightMost(beginRoot = this.root) {
|
|
634
|
+
getRightMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
577
635
|
// TODO support get right most by passing key in
|
|
578
636
|
if (!beginRoot)
|
|
579
637
|
return beginRoot;
|
|
580
|
-
if (
|
|
638
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
581
639
|
const _traverse = (cur) => {
|
|
582
640
|
if (!cur.right)
|
|
583
641
|
return cur;
|
|
@@ -596,15 +654,19 @@ export class BinaryTree {
|
|
|
596
654
|
}
|
|
597
655
|
}
|
|
598
656
|
/**
|
|
599
|
-
* The function checks if a binary
|
|
600
|
-
* @param {N
|
|
601
|
-
*
|
|
657
|
+
* The function `isSubtreeBST` checks if a given binary tree is a valid binary search tree.
|
|
658
|
+
* @param {N} beginRoot - The `beginRoot` parameter is the root node of the binary tree that you want
|
|
659
|
+
* to check if it is a binary search tree (BST) subtree.
|
|
660
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
661
|
+
* type of iteration to use when checking if a subtree is a binary search tree (BST). It can have two
|
|
662
|
+
* possible values:
|
|
663
|
+
* @returns The function `isSubtreeBST` returns a boolean value.
|
|
602
664
|
*/
|
|
603
|
-
isSubtreeBST(
|
|
665
|
+
isSubtreeBST(beginRoot, iterationType = this.iterationType) {
|
|
604
666
|
// TODO there is a bug
|
|
605
|
-
if (!
|
|
667
|
+
if (!beginRoot)
|
|
606
668
|
return true;
|
|
607
|
-
if (
|
|
669
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
608
670
|
const dfs = (cur, min, max) => {
|
|
609
671
|
if (!cur)
|
|
610
672
|
return true;
|
|
@@ -612,11 +674,11 @@ export class BinaryTree {
|
|
|
612
674
|
return false;
|
|
613
675
|
return dfs(cur.left, min, cur.key) && dfs(cur.right, cur.key, max);
|
|
614
676
|
};
|
|
615
|
-
return dfs(
|
|
677
|
+
return dfs(beginRoot, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
|
|
616
678
|
}
|
|
617
679
|
else {
|
|
618
680
|
const stack = [];
|
|
619
|
-
let prev = Number.MIN_SAFE_INTEGER, curr =
|
|
681
|
+
let prev = Number.MIN_SAFE_INTEGER, curr = beginRoot;
|
|
620
682
|
while (curr || stack.length > 0) {
|
|
621
683
|
while (curr) {
|
|
622
684
|
stack.push(curr);
|
|
@@ -632,36 +694,48 @@ export class BinaryTree {
|
|
|
632
694
|
}
|
|
633
695
|
}
|
|
634
696
|
/**
|
|
635
|
-
* The function
|
|
636
|
-
* @
|
|
697
|
+
* The function checks if a binary tree is a binary search tree.
|
|
698
|
+
* @param iterationType - The parameter "iterationType" is used to specify the type of iteration to
|
|
699
|
+
* be used when checking if the binary tree is a binary search tree (BST). It is an optional
|
|
700
|
+
* parameter with a default value of "this.iterationType". The value of "this.iterationType" is not
|
|
701
|
+
* provided in
|
|
702
|
+
* @returns a boolean value.
|
|
637
703
|
*/
|
|
638
|
-
isBST() {
|
|
639
|
-
|
|
704
|
+
isBST(iterationType = this.iterationType) {
|
|
705
|
+
if (this.root === null)
|
|
706
|
+
return true;
|
|
707
|
+
return this.isSubtreeBST(this.root, iterationType);
|
|
640
708
|
}
|
|
641
709
|
/**
|
|
642
|
-
* The function `subTreeTraverse`
|
|
643
|
-
*
|
|
644
|
-
*
|
|
645
|
-
*
|
|
646
|
-
*
|
|
647
|
-
*
|
|
710
|
+
* The function `subTreeTraverse` traverses a binary tree and applies a callback function to each
|
|
711
|
+
* node, either recursively or iteratively.
|
|
712
|
+
* @param callback - The `callback` parameter is a function that will be called on each node in the
|
|
713
|
+
* subtree traversal. It takes a single argument, which is the current node being traversed, and
|
|
714
|
+
* returns a value. The return values from each callback invocation will be collected and returned as
|
|
715
|
+
* an array.
|
|
716
|
+
* @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter is the starting point
|
|
717
|
+
* for traversing the subtree. It can be either a node object, a key value of a node, or `null` to
|
|
718
|
+
* start from the root of the tree.
|
|
719
|
+
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
720
|
+
* performed on the binary tree. It can have two possible values:
|
|
721
|
+
* @returns The function `subTreeTraverse` returns an array of `MapCallbackReturn<N>`.
|
|
648
722
|
*/
|
|
649
|
-
subTreeTraverse(callback = this._defaultCallbackByKey,
|
|
650
|
-
if (typeof
|
|
651
|
-
|
|
723
|
+
subTreeTraverse(callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
|
|
724
|
+
if (typeof beginRoot === 'number')
|
|
725
|
+
beginRoot = this.get(beginRoot);
|
|
652
726
|
const ans = [];
|
|
653
|
-
if (!
|
|
727
|
+
if (!beginRoot)
|
|
654
728
|
return ans;
|
|
655
|
-
if (
|
|
729
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
656
730
|
const _traverse = (cur) => {
|
|
657
731
|
ans.push(callback(cur));
|
|
658
732
|
cur.left && _traverse(cur.left);
|
|
659
733
|
cur.right && _traverse(cur.right);
|
|
660
734
|
};
|
|
661
|
-
_traverse(
|
|
735
|
+
_traverse(beginRoot);
|
|
662
736
|
}
|
|
663
737
|
else {
|
|
664
|
-
const stack = [
|
|
738
|
+
const stack = [beginRoot];
|
|
665
739
|
while (stack.length > 0) {
|
|
666
740
|
const cur = stack.pop();
|
|
667
741
|
ans.push(callback(cur));
|
|
@@ -672,19 +746,25 @@ export class BinaryTree {
|
|
|
672
746
|
return ans;
|
|
673
747
|
}
|
|
674
748
|
/**
|
|
675
|
-
* The dfs function performs a depth-first search traversal on a binary tree
|
|
676
|
-
* each node
|
|
677
|
-
* @param callback
|
|
678
|
-
*
|
|
679
|
-
*
|
|
680
|
-
* @param
|
|
681
|
-
*
|
|
749
|
+
* The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback
|
|
750
|
+
* function on each node according to a specified order pattern.
|
|
751
|
+
* @param callback - The `callback` parameter is a function that will be called on each node during
|
|
752
|
+
* the depth-first search traversal. It takes a node as input and returns a value. The default value
|
|
753
|
+
* is `this._defaultCallbackByKey`, which is a callback function defined elsewhere in the code.
|
|
754
|
+
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter determines the order in which the
|
|
755
|
+
* nodes are visited during the depth-first search. There are three possible values for `pattern`:
|
|
756
|
+
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the depth-first
|
|
757
|
+
* search. It determines where the search will begin in the tree or graph structure. If `beginRoot`
|
|
758
|
+
* is `null`, an empty array will be returned.
|
|
759
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
760
|
+
* iteration used in the depth-first search algorithm. It can have two possible values:
|
|
761
|
+
* @returns The function `dfs` returns an array of `MapCallbackReturn<N>` values.
|
|
682
762
|
*/
|
|
683
|
-
dfs(callback = this._defaultCallbackByKey, pattern = 'in', beginRoot = this.root,
|
|
763
|
+
dfs(callback = this._defaultCallbackByKey, pattern = 'in', beginRoot = this.root, iterationType = IterationType.ITERATIVE) {
|
|
684
764
|
if (!beginRoot)
|
|
685
765
|
return [];
|
|
686
766
|
const ans = [];
|
|
687
|
-
if (
|
|
767
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
688
768
|
const _traverse = (node) => {
|
|
689
769
|
switch (pattern) {
|
|
690
770
|
case 'in':
|
|
@@ -752,18 +832,27 @@ export class BinaryTree {
|
|
|
752
832
|
}
|
|
753
833
|
// --- start additional methods ---
|
|
754
834
|
/**
|
|
755
|
-
* The
|
|
756
|
-
*
|
|
757
|
-
* @param callback - The `callback` parameter is a function that
|
|
758
|
-
*
|
|
835
|
+
* The bfs function performs a breadth-first search traversal on a binary tree, executing a callback
|
|
836
|
+
* function on each node.
|
|
837
|
+
* @param callback - The `callback` parameter is a function that will be called for each node in the
|
|
838
|
+
* breadth-first search. It takes a node of type `N` as its argument and returns a value of type
|
|
839
|
+
* `BFSCallbackReturn<N>`. The default value for this parameter is `this._defaultCallbackByKey
|
|
840
|
+
* @param {boolean} [withLevel=false] - The `withLevel` parameter is a boolean flag that determines
|
|
841
|
+
* whether or not to include the level of each node in the callback function. If `withLevel` is set
|
|
842
|
+
* to `true`, the level of each node will be passed as an argument to the callback function. If
|
|
843
|
+
* `withLevel` is
|
|
844
|
+
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
|
|
845
|
+
* search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
|
|
846
|
+
* will not be performed and an empty array will be returned.
|
|
847
|
+
* @param iterationType - The `iterationType` parameter determines the type of iteration to be used
|
|
848
|
+
* in the breadth-first search (BFS) algorithm. It can have two possible values:
|
|
849
|
+
* @returns The function `bfs` returns an array of `BFSCallbackReturn<N>[]`.
|
|
759
850
|
*/
|
|
760
|
-
bfs(callback = this._defaultCallbackByKey, withLevel = false,
|
|
761
|
-
if (!
|
|
762
|
-
node = this.root;
|
|
763
|
-
if (!node)
|
|
851
|
+
bfs(callback = this._defaultCallbackByKey, withLevel = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
852
|
+
if (!beginRoot)
|
|
764
853
|
return [];
|
|
765
854
|
const ans = [];
|
|
766
|
-
if (
|
|
855
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
767
856
|
const _recursive = (node, level) => {
|
|
768
857
|
callback && ans.push(callback(node, withLevel ? level : undefined));
|
|
769
858
|
if (node.left)
|
|
@@ -771,10 +860,10 @@ export class BinaryTree {
|
|
|
771
860
|
if (node.right)
|
|
772
861
|
_recursive(node.right, level + 1);
|
|
773
862
|
};
|
|
774
|
-
_recursive(
|
|
863
|
+
_recursive(beginRoot, 0);
|
|
775
864
|
}
|
|
776
865
|
else {
|
|
777
|
-
const stack = [[
|
|
866
|
+
const stack = [[beginRoot, 0]];
|
|
778
867
|
while (stack.length > 0) {
|
|
779
868
|
const head = stack.pop();
|
|
780
869
|
const [node, level] = head;
|
|
@@ -788,9 +877,9 @@ export class BinaryTree {
|
|
|
788
877
|
return ans;
|
|
789
878
|
}
|
|
790
879
|
/**
|
|
791
|
-
* The function returns the predecessor of a given node in a binary tree.
|
|
792
|
-
* @param node - The parameter
|
|
793
|
-
* @returns the predecessor of the given node
|
|
880
|
+
* The function returns the predecessor node of a given node in a binary tree.
|
|
881
|
+
* @param {N} node - The parameter "node" represents a node in a binary tree.
|
|
882
|
+
* @returns The function `getPredecessor` returns the predecessor node of the given node `node`.
|
|
794
883
|
*/
|
|
795
884
|
getPredecessor(node) {
|
|
796
885
|
if (node.left) {
|
|
@@ -809,18 +898,29 @@ export class BinaryTree {
|
|
|
809
898
|
/**
|
|
810
899
|
* Time complexity is O(n)
|
|
811
900
|
* Space complexity of Iterative dfs equals to recursive dfs which is O(n) because of the stack
|
|
901
|
+
* The Morris algorithm only modifies the tree's structure during traversal; once the traversal is complete,
|
|
902
|
+
* the tree's structure should be restored to its original state to maintain the tree's integrity.
|
|
903
|
+
* This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
|
|
812
904
|
*/
|
|
813
905
|
/**
|
|
814
|
-
* The `morris` function performs
|
|
815
|
-
*
|
|
816
|
-
* @param callback
|
|
817
|
-
*
|
|
906
|
+
* The `morris` function performs a depth-first traversal of a binary tree using the Morris traversal
|
|
907
|
+
* algorithm and returns an array of values obtained by applying a callback function to each node.
|
|
908
|
+
* @param callback - The `callback` parameter is a function that will be called on each node in the
|
|
909
|
+
* tree. It takes a node of type `N` as input and returns a value of type `MapCallbackReturn<N>`. The
|
|
910
|
+
* default value for this parameter is `this._defaultCallbackByKey`.
|
|
911
|
+
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
|
|
912
|
+
* determines the order in which the nodes of a binary tree are traversed. It can have one of the
|
|
913
|
+
* following values:
|
|
914
|
+
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the Morris
|
|
915
|
+
* traversal. It specifies the root node of the tree from which the traversal should begin. If
|
|
916
|
+
* `beginRoot` is `null`, an empty array will be returned.
|
|
917
|
+
* @returns The `morris` function returns an array of `MapCallbackReturn<N>` values.
|
|
818
918
|
*/
|
|
819
|
-
morris(callback = this._defaultCallbackByKey, pattern = 'in') {
|
|
820
|
-
if (
|
|
919
|
+
morris(callback = this._defaultCallbackByKey, pattern = 'in', beginRoot = this.root) {
|
|
920
|
+
if (beginRoot === null)
|
|
821
921
|
return [];
|
|
822
922
|
const ans = [];
|
|
823
|
-
let cur =
|
|
923
|
+
let cur = beginRoot;
|
|
824
924
|
const _reverseEdge = (node) => {
|
|
825
925
|
let pre = null;
|
|
826
926
|
let next = null;
|
|
@@ -895,20 +995,21 @@ export class BinaryTree {
|
|
|
895
995
|
}
|
|
896
996
|
cur = cur.right;
|
|
897
997
|
}
|
|
898
|
-
_printEdge(
|
|
998
|
+
_printEdge(beginRoot);
|
|
899
999
|
break;
|
|
900
1000
|
}
|
|
901
1001
|
return ans;
|
|
902
1002
|
}
|
|
903
1003
|
/**
|
|
904
|
-
* The function adds a new node to a binary tree if there is an available position.
|
|
905
|
-
* @param {N | null} newNode - The `newNode` parameter
|
|
906
|
-
*
|
|
907
|
-
* @param {N} parent - The parent parameter
|
|
908
|
-
*
|
|
909
|
-
*
|
|
910
|
-
*
|
|
911
|
-
* undefined.
|
|
1004
|
+
* The function `_addTo` adds a new node to a binary tree if there is an available position.
|
|
1005
|
+
* @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to
|
|
1006
|
+
* the binary tree. It can be either a node object or `null`.
|
|
1007
|
+
* @param {N} parent - The `parent` parameter represents the parent node to which the new node will
|
|
1008
|
+
* be added as a child.
|
|
1009
|
+
* @returns either the left or right child node of the parent node, depending on which child is
|
|
1010
|
+
* available for adding the new node. If a new node is added, the function also updates the size of
|
|
1011
|
+
* the binary tree. If neither the left nor right child is available, the function returns undefined.
|
|
1012
|
+
* If the parent node is null, the function also returns undefined.
|
|
912
1013
|
*/
|
|
913
1014
|
_addTo(newNode, parent) {
|
|
914
1015
|
if (parent) {
|
|
@@ -937,9 +1038,10 @@ export class BinaryTree {
|
|
|
937
1038
|
}
|
|
938
1039
|
}
|
|
939
1040
|
/**
|
|
940
|
-
* The function sets the root property of an object to a given value, and if the value is not null,
|
|
941
|
-
* parent property of the value to undefined.
|
|
942
|
-
* @param {N | null} v - The parameter `v` is of type `N | null`, which means it can either be of
|
|
1041
|
+
* The function sets the root property of an object to a given value, and if the value is not null,
|
|
1042
|
+
* it also sets the parent property of the value to undefined.
|
|
1043
|
+
* @param {N | null} v - The parameter `v` is of type `N | null`, which means it can either be of
|
|
1044
|
+
* type `N` or `null`.
|
|
943
1045
|
*/
|
|
944
1046
|
_setRoot(v) {
|
|
945
1047
|
if (v) {
|
|
@@ -948,8 +1050,9 @@ export class BinaryTree {
|
|
|
948
1050
|
this._root = v;
|
|
949
1051
|
}
|
|
950
1052
|
/**
|
|
951
|
-
* The function sets the
|
|
952
|
-
* @param {number} v - number
|
|
1053
|
+
* The function sets the value of the protected property "_size" to the given number.
|
|
1054
|
+
* @param {number} v - The parameter "v" is a number that represents the size value that we want to
|
|
1055
|
+
* set.
|
|
953
1056
|
*/
|
|
954
1057
|
_setSize(v) {
|
|
955
1058
|
this._size = v;
|