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