min-heap-typed 1.42.7 → 1.42.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/avl-tree.d.ts +88 -23
- package/dist/data-structures/binary-tree/avl-tree.js +88 -23
- package/dist/data-structures/binary-tree/binary-tree.d.ts +180 -74
- package/dist/data-structures/binary-tree/binary-tree.js +388 -201
- package/dist/data-structures/binary-tree/bst.d.ts +121 -66
- package/dist/data-structures/binary-tree/bst.js +121 -67
- package/dist/data-structures/binary-tree/rb-tree.d.ts +72 -5
- package/dist/data-structures/binary-tree/rb-tree.js +95 -18
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +82 -43
- package/dist/data-structures/binary-tree/tree-multimap.js +82 -43
- package/dist/data-structures/graph/abstract-graph.d.ts +139 -36
- package/dist/data-structures/graph/abstract-graph.js +147 -36
- package/dist/data-structures/graph/directed-graph.d.ts +126 -0
- package/dist/data-structures/graph/directed-graph.js +126 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +63 -0
- package/dist/data-structures/graph/undirected-graph.js +63 -0
- package/dist/data-structures/heap/heap.d.ts +175 -12
- package/dist/data-structures/heap/heap.js +175 -12
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +203 -0
- package/dist/data-structures/linked-list/doubly-linked-list.js +203 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +182 -0
- package/dist/data-structures/linked-list/singly-linked-list.js +182 -0
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +64 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +64 -0
- package/dist/data-structures/queue/deque.d.ts +113 -3
- package/dist/data-structures/queue/deque.js +113 -3
- package/dist/data-structures/queue/queue.d.ts +87 -0
- package/dist/data-structures/queue/queue.js +87 -0
- package/dist/data-structures/stack/stack.d.ts +42 -0
- package/dist/data-structures/stack/stack.js +42 -0
- package/dist/data-structures/trie/trie.d.ts +76 -0
- package/dist/data-structures/trie/trie.js +76 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +97 -23
- package/src/data-structures/binary-tree/binary-tree.ts +419 -204
- package/src/data-structures/binary-tree/bst.ts +130 -68
- package/src/data-structures/binary-tree/rb-tree.ts +106 -19
- package/src/data-structures/binary-tree/tree-multimap.ts +88 -44
- package/src/data-structures/graph/abstract-graph.ts +133 -7
- package/src/data-structures/graph/directed-graph.ts +145 -1
- package/src/data-structures/graph/undirected-graph.ts +72 -0
- package/src/data-structures/heap/heap.ts +201 -12
- package/src/data-structures/linked-list/doubly-linked-list.ts +232 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +208 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +74 -0
- package/src/data-structures/queue/deque.ts +127 -3
- package/src/data-structures/queue/queue.ts +99 -0
- package/src/data-structures/stack/stack.ts +48 -0
- package/src/data-structures/trie/trie.ts +87 -4
|
@@ -117,10 +117,22 @@ class BinaryTree {
|
|
|
117
117
|
return new BinaryTreeNode(key, value);
|
|
118
118
|
}
|
|
119
119
|
/**
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
|
|
120
|
+
* Time Complexity: O(n)
|
|
121
|
+
* Space Complexity: O(1)
|
|
122
|
+
* Comments: The time complexity for adding a node depends on the depth of the tree. In the best case (when the tree is empty), it's O(1). In the worst case (when the tree is a degenerate tree), it's O(n). The space complexity is constant.
|
|
123
|
+
*/
|
|
124
|
+
/**
|
|
125
|
+
* Time Complexity: O(n)
|
|
126
|
+
* Space Complexity: O(1)
|
|
127
|
+
*
|
|
128
|
+
* The `add` function adds a new node with a key and value to a binary tree, or updates the value of
|
|
129
|
+
* an existing node with the same key.
|
|
130
|
+
* @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
|
|
131
|
+
* following types:
|
|
132
|
+
* @param {V} [value] - The value to be associated with the key or node being added to the binary
|
|
133
|
+
* tree.
|
|
134
|
+
* @returns The function `add` returns a node (`N`) if it was successfully inserted into the binary
|
|
135
|
+
* tree, or `null` or `undefined` if the insertion was not successful.
|
|
124
136
|
*/
|
|
125
137
|
add(keyOrNode, value) {
|
|
126
138
|
const _bfs = (root, newNode) => {
|
|
@@ -174,13 +186,21 @@ class BinaryTree {
|
|
|
174
186
|
return inserted;
|
|
175
187
|
}
|
|
176
188
|
/**
|
|
177
|
-
*
|
|
178
|
-
*
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
*
|
|
182
|
-
*
|
|
183
|
-
*
|
|
189
|
+
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
190
|
+
* Space Complexity: O(1)
|
|
191
|
+
*/
|
|
192
|
+
/**
|
|
193
|
+
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
194
|
+
* Space Complexity: O(1)
|
|
195
|
+
*
|
|
196
|
+
* The `addMany` function takes an array of keys or nodes and an optional array of values, and adds
|
|
197
|
+
* each key-value pair to a data structure.
|
|
198
|
+
* @param {(BTNKey | N |null | undefined)[]} keysOrNodes - An array of keys or nodes to be added to
|
|
199
|
+
* the binary search tree. Each element can be of type `BTNKey` (a key value), `N` (a node), `null`,
|
|
200
|
+
* or `undefined`.
|
|
201
|
+
* @param {(V | undefined)[]} [values] - The `values` parameter is an optional array of values that
|
|
202
|
+
* correspond to the keys or nodes being added. If provided, the values will be associated with the
|
|
203
|
+
* keys or nodes during the add operation.
|
|
184
204
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
185
205
|
*/
|
|
186
206
|
addMany(keysOrNodes, values) {
|
|
@@ -197,6 +217,13 @@ class BinaryTree {
|
|
|
197
217
|
});
|
|
198
218
|
}
|
|
199
219
|
/**
|
|
220
|
+
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
221
|
+
* Space Complexity: O(1)
|
|
222
|
+
*/
|
|
223
|
+
/**
|
|
224
|
+
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
225
|
+
* Space Complexity: O(1)
|
|
226
|
+
*
|
|
200
227
|
* The `refill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
|
|
201
228
|
* @param {(BTNKey | N)[]} keysOrNodes - The `keysOrNodes` parameter is an array that can contain either
|
|
202
229
|
* `BTNKey` or `N` values.
|
|
@@ -210,18 +237,23 @@ class BinaryTree {
|
|
|
210
237
|
return keysOrNodes.length === this.addMany(keysOrNodes, values).length;
|
|
211
238
|
}
|
|
212
239
|
/**
|
|
213
|
-
*
|
|
214
|
-
*
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
*
|
|
218
|
-
*
|
|
219
|
-
*
|
|
220
|
-
*
|
|
221
|
-
*
|
|
222
|
-
*
|
|
223
|
-
*
|
|
224
|
-
*
|
|
240
|
+
* Time Complexity: O(n)
|
|
241
|
+
* Space Complexity: O(1)
|
|
242
|
+
*/
|
|
243
|
+
/**
|
|
244
|
+
* Time Complexity: O(n)
|
|
245
|
+
* Space Complexity: O(1)
|
|
246
|
+
*
|
|
247
|
+
* The function deletes a node from a binary tree and returns an array of the deleted nodes along
|
|
248
|
+
* with the nodes that need to be balanced.
|
|
249
|
+
* @param {ReturnType<C> | null | undefined} identifier - The identifier parameter is the value or
|
|
250
|
+
* object that you want to delete from the binary tree. It can be of any type that is compatible with
|
|
251
|
+
* the callback function's return type. It can also be null or undefined if you want to delete a
|
|
252
|
+
* specific node based on its value or object.
|
|
253
|
+
* @param {C} callback - The `callback` parameter is a function that is used to determine the
|
|
254
|
+
* identifier of the node to be deleted. It is optional and has a default value of
|
|
255
|
+
* `this._defaultOneParamCallback`. The `callback` function should return the identifier of the node.
|
|
256
|
+
* @returns an array of `BiTreeDeleteResult<N>`.
|
|
225
257
|
*/
|
|
226
258
|
delete(identifier, callback = this._defaultOneParamCallback) {
|
|
227
259
|
const deletedResult = [];
|
|
@@ -270,15 +302,20 @@ class BinaryTree {
|
|
|
270
302
|
return deletedResult;
|
|
271
303
|
}
|
|
272
304
|
/**
|
|
273
|
-
*
|
|
274
|
-
*
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
*
|
|
278
|
-
*
|
|
279
|
-
*
|
|
280
|
-
* of a node in
|
|
281
|
-
*
|
|
305
|
+
* Time Complexity: O(n)
|
|
306
|
+
* Space Complexity: O(1)
|
|
307
|
+
*/
|
|
308
|
+
/**
|
|
309
|
+
* Time Complexity: O(n)
|
|
310
|
+
* Space Complexity: O(1)
|
|
311
|
+
*
|
|
312
|
+
* The function calculates the depth of a given node in a binary tree.
|
|
313
|
+
* @param {BTNKey | N | null | undefined} distNode - The `distNode` parameter represents the node in
|
|
314
|
+
* the binary tree whose depth we want to find. It can be of type `BTNKey`, `N`, `null`, or
|
|
315
|
+
* `undefined`.
|
|
316
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
|
|
317
|
+
* from which we want to calculate the depth. It can be either a `BTNKey` (binary tree node key) or
|
|
318
|
+
* `N` (binary tree node) or `null` or `undefined`. If no value is provided for `beginRoot
|
|
282
319
|
* @returns the depth of the `distNode` relative to the `beginRoot`.
|
|
283
320
|
*/
|
|
284
321
|
getDepth(distNode, beginRoot = this.root) {
|
|
@@ -295,15 +332,22 @@ class BinaryTree {
|
|
|
295
332
|
return depth;
|
|
296
333
|
}
|
|
297
334
|
/**
|
|
298
|
-
*
|
|
299
|
-
*
|
|
335
|
+
* Time Complexity: O(n)
|
|
336
|
+
* Space Complexity: O(log n)
|
|
337
|
+
* Best Case - O(log n) (when using recursive iterationType), Worst Case - O(n) (when using iterative iterationType)
|
|
338
|
+
*/
|
|
339
|
+
/**
|
|
340
|
+
* Time Complexity: O(n)
|
|
341
|
+
* Space Complexity: O(log n)
|
|
342
|
+
*
|
|
343
|
+
* The function `getHeight` calculates the maximum height of a binary tree using either recursive or
|
|
344
|
+
* iterative traversal.
|
|
300
345
|
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
301
|
-
* starting node from which
|
|
302
|
-
*
|
|
303
|
-
* node is specified. If `
|
|
346
|
+
* starting node of the binary tree from which we want to calculate the height. It can be of type
|
|
347
|
+
* `BTNKey`, `N`, `null`, or `undefined`. If not provided, it defaults to `this.root`.
|
|
304
348
|
* @param iterationType - The `iterationType` parameter is used to determine whether to calculate the
|
|
305
|
-
* height of the
|
|
306
|
-
*
|
|
349
|
+
* height of the tree using a recursive approach or an iterative approach. It can have two possible
|
|
350
|
+
* values:
|
|
307
351
|
* @returns the height of the binary tree.
|
|
308
352
|
*/
|
|
309
353
|
getHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
@@ -338,11 +382,19 @@ class BinaryTree {
|
|
|
338
382
|
}
|
|
339
383
|
}
|
|
340
384
|
/**
|
|
385
|
+
* Time Complexity: O(n)
|
|
386
|
+
* Space Complexity: O(log n)
|
|
387
|
+
* Best Case - O(log n) (when using recursive iterationType), Worst Case - O(n) (when using iterative iterationType)
|
|
388
|
+
*/
|
|
389
|
+
/**
|
|
390
|
+
* Time Complexity: O(n)
|
|
391
|
+
* Space Complexity: O(log n)
|
|
392
|
+
*
|
|
341
393
|
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
342
394
|
* recursive or iterative approach.
|
|
343
|
-
* @param {N | null | undefined} beginRoot - The `beginRoot` parameter
|
|
344
|
-
*
|
|
345
|
-
*
|
|
395
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
396
|
+
* starting node of the binary tree from which we want to calculate the minimum height. It can be of
|
|
397
|
+
* type `BTNKey`, `N`, `null`, or `undefined`. If no value is provided, it defaults to `this.root`.
|
|
346
398
|
* @param iterationType - The `iterationType` parameter is used to determine the method of iteration
|
|
347
399
|
* to calculate the minimum height of a binary tree. It can have two possible values:
|
|
348
400
|
* @returns The function `getMinHeight` returns the minimum height of a binary tree.
|
|
@@ -393,35 +445,51 @@ class BinaryTree {
|
|
|
393
445
|
}
|
|
394
446
|
}
|
|
395
447
|
/**
|
|
448
|
+
* Time Complexity: O(n)
|
|
449
|
+
* Space Complexity: O(log n)
|
|
450
|
+
*/
|
|
451
|
+
/**
|
|
452
|
+
* Time Complexity: O(n)
|
|
453
|
+
* Space Complexity: O(log n)
|
|
454
|
+
*
|
|
396
455
|
* The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
|
|
397
456
|
* height of the tree.
|
|
398
|
-
* @param {N | null | undefined} beginRoot - The
|
|
399
|
-
*
|
|
400
|
-
*
|
|
457
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
|
|
458
|
+
* for calculating the height and minimum height of a binary tree. It can be either a `BTNKey` (a key
|
|
459
|
+
* value of a binary tree node), `N` (a node of a binary tree), `null`, or `undefined`. If
|
|
460
|
+
* @returns a boolean value.
|
|
401
461
|
*/
|
|
402
462
|
isPerfectlyBalanced(beginRoot = this.root) {
|
|
403
463
|
return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
|
|
404
464
|
}
|
|
405
465
|
/**
|
|
406
|
-
*
|
|
407
|
-
*
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
*
|
|
411
|
-
*
|
|
412
|
-
*
|
|
413
|
-
*
|
|
414
|
-
*
|
|
415
|
-
* @param
|
|
416
|
-
*
|
|
417
|
-
*
|
|
418
|
-
*
|
|
419
|
-
* @param {
|
|
420
|
-
*
|
|
421
|
-
*
|
|
466
|
+
* Time Complexity: O(n)
|
|
467
|
+
* Space Complexity: O(log n).
|
|
468
|
+
*/
|
|
469
|
+
/**
|
|
470
|
+
* Time Complexity: O(n)
|
|
471
|
+
* Space Complexity: O(log n).
|
|
472
|
+
*
|
|
473
|
+
* The function `getNodes` retrieves nodes from a binary tree based on a given identifier and
|
|
474
|
+
* callback function.
|
|
475
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
476
|
+
* that you want to search for in the binary tree. It can be of any type that is returned by the
|
|
477
|
+
* callback function `C`. It can also be `null` or `undefined` if you don't want to search for a
|
|
478
|
+
* specific value.
|
|
479
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node of type `N` as
|
|
480
|
+
* input and returns a value of type `C`. It is used to determine if a node matches the given
|
|
481
|
+
* identifier. If no callback is provided, the `_defaultOneParamCallback` function is used as the
|
|
482
|
+
* default
|
|
483
|
+
* @param [onlyOne=false] - A boolean value indicating whether to only return the first node that
|
|
484
|
+
* matches the identifier. If set to true, the function will stop iterating once it finds a matching
|
|
485
|
+
* node and return that node. If set to false (default), the function will continue iterating and
|
|
486
|
+
* return all nodes that match the identifier.
|
|
487
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
488
|
+
* starting node for the traversal. It can be either a key, a node object, or `null`/`undefined`. If
|
|
489
|
+
* it is `null` or `undefined`, an empty array will be returned.
|
|
422
490
|
* @param iterationType - The `iterationType` parameter determines the type of iteration used to
|
|
423
491
|
* traverse the binary tree. It can have two possible values:
|
|
424
|
-
* @returns
|
|
492
|
+
* @returns an array of nodes of type `N`.
|
|
425
493
|
*/
|
|
426
494
|
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
427
495
|
if (!beginRoot)
|
|
@@ -464,20 +532,27 @@ class BinaryTree {
|
|
|
464
532
|
return ans;
|
|
465
533
|
}
|
|
466
534
|
/**
|
|
467
|
-
*
|
|
468
|
-
*
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
*
|
|
472
|
-
*
|
|
473
|
-
*
|
|
474
|
-
*
|
|
475
|
-
*
|
|
476
|
-
*
|
|
477
|
-
*
|
|
478
|
-
*
|
|
479
|
-
*
|
|
480
|
-
*
|
|
535
|
+
* Time Complexity: O(n)
|
|
536
|
+
* Space Complexity: O(log n).
|
|
537
|
+
*/
|
|
538
|
+
/**
|
|
539
|
+
* Time Complexity: O(n)
|
|
540
|
+
*
|
|
541
|
+
* The function checks if a Binary Tree Node with a specific identifier exists in the tree.
|
|
542
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
543
|
+
* that you want to search for in the binary tree. It can be of any type that is returned by the
|
|
544
|
+
* callback function `C`. It can also be `null` or `undefined` if you don't want to specify a
|
|
545
|
+
* specific identifier.
|
|
546
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
547
|
+
* the binary tree. It is used to filter the nodes based on certain conditions. The `callback`
|
|
548
|
+
* function should return a boolean value indicating whether the node should be included in the
|
|
549
|
+
* result or not.
|
|
550
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
|
|
551
|
+
* for the search in the binary tree. It can be specified as a `BTNKey` (a unique identifier for a
|
|
552
|
+
* node in the binary tree), a node object (`N`), or `null`/`undefined` to start the search from
|
|
553
|
+
* @param iterationType - The `iterationType` parameter is a variable that determines the type of
|
|
554
|
+
* iteration to be performed on the binary tree. It is used to specify whether the iteration should
|
|
555
|
+
* be performed in a pre-order, in-order, or post-order manner.
|
|
481
556
|
* @returns a boolean value.
|
|
482
557
|
*/
|
|
483
558
|
has(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
@@ -486,19 +561,29 @@ class BinaryTree {
|
|
|
486
561
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
|
|
487
562
|
}
|
|
488
563
|
/**
|
|
489
|
-
*
|
|
490
|
-
*
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
*
|
|
494
|
-
*
|
|
495
|
-
*
|
|
496
|
-
*
|
|
497
|
-
*
|
|
498
|
-
*
|
|
499
|
-
*
|
|
500
|
-
*
|
|
501
|
-
*
|
|
564
|
+
* Time Complexity: O(n)
|
|
565
|
+
* Space Complexity: O(log n)
|
|
566
|
+
*/
|
|
567
|
+
/**
|
|
568
|
+
* Time Complexity: O(n)
|
|
569
|
+
* Space Complexity: O(log n)
|
|
570
|
+
*
|
|
571
|
+
* The function `getNode` returns the first node that matches the given identifier and callback
|
|
572
|
+
* function.
|
|
573
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
574
|
+
* used to identify the node you want to retrieve. It can be of any type that is returned by the
|
|
575
|
+
* callback function `C`. It can also be `null` or `undefined` if you don't have a specific
|
|
576
|
+
* identifier.
|
|
577
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
578
|
+
* the binary tree. It is used to determine if a node matches the given identifier. The `callback`
|
|
579
|
+
* function should take a single parameter of type `N` (the type of the nodes in the binary tree) and
|
|
580
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
|
|
581
|
+
* for searching the binary tree. It can be either a key value, a node object, or `null`/`undefined`.
|
|
582
|
+
* If `null` or `undefined` is passed, the search will start from the root of the binary tree.
|
|
583
|
+
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
584
|
+
* be performed when searching for nodes in the binary tree. It determines the order in which the
|
|
585
|
+
* nodes are visited during the search.
|
|
586
|
+
* @returns a value of type `N | null | undefined`.
|
|
502
587
|
*/
|
|
503
588
|
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
504
589
|
var _a;
|
|
@@ -507,6 +592,13 @@ class BinaryTree {
|
|
|
507
592
|
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : null;
|
|
508
593
|
}
|
|
509
594
|
/**
|
|
595
|
+
* Time Complexity: O(n)
|
|
596
|
+
* Space Complexity: O(log n)
|
|
597
|
+
*/
|
|
598
|
+
/**
|
|
599
|
+
* Time Complexity: O(n)
|
|
600
|
+
* Space Complexity: O(log n)
|
|
601
|
+
*
|
|
510
602
|
* The function `getNodeByKey` searches for a node in a binary tree by its key, using either
|
|
511
603
|
* recursive or iterative iteration.
|
|
512
604
|
* @param {BTNKey} key - The `key` parameter is the key value that we are searching for in the tree.
|
|
@@ -561,19 +653,30 @@ class BinaryTree {
|
|
|
561
653
|
return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
562
654
|
}
|
|
563
655
|
/**
|
|
564
|
-
*
|
|
565
|
-
*
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
*
|
|
569
|
-
*
|
|
570
|
-
*
|
|
571
|
-
*
|
|
572
|
-
*
|
|
573
|
-
*
|
|
574
|
-
*
|
|
575
|
-
*
|
|
576
|
-
* @
|
|
656
|
+
* Time Complexity: O(n)
|
|
657
|
+
* Space Complexity: O(log n)
|
|
658
|
+
*/
|
|
659
|
+
/**
|
|
660
|
+
* Time Complexity: O(n)
|
|
661
|
+
* Space Complexity: O(log n)
|
|
662
|
+
*
|
|
663
|
+
* The function `get` retrieves the value of a node in a binary tree based on the provided identifier
|
|
664
|
+
* and callback function.
|
|
665
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
666
|
+
* used to identify the node in the binary tree. It can be of any type that is the return type of the
|
|
667
|
+
* callback function `C`. It can also be `null` or `undefined` if no identifier is provided.
|
|
668
|
+
* @param {C} callback - The `callback` parameter is a function that will be called with each node in
|
|
669
|
+
* the binary tree. It is used to determine whether a node matches the given identifier. The callback
|
|
670
|
+
* function should return a value that can be compared to the identifier to determine if it is a
|
|
671
|
+
* match.
|
|
672
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
|
|
673
|
+
* for the search in the binary tree. It can be specified as a `BTNKey` (a unique identifier for a
|
|
674
|
+
* node), a node object of type `N`, or `null`/`undefined` to start the search from the root of
|
|
675
|
+
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
676
|
+
* be performed when searching for a node in the binary tree. It is an optional parameter with a
|
|
677
|
+
* default value specified by `this.iterationType`.
|
|
678
|
+
* @returns The value of the node with the given identifier is being returned. If the node is not
|
|
679
|
+
* found, `undefined` is returned.
|
|
577
680
|
*/
|
|
578
681
|
get(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
579
682
|
var _a, _b;
|
|
@@ -596,14 +699,22 @@ class BinaryTree {
|
|
|
596
699
|
return this.size === 0;
|
|
597
700
|
}
|
|
598
701
|
/**
|
|
599
|
-
*
|
|
600
|
-
*
|
|
601
|
-
|
|
602
|
-
|
|
702
|
+
* Time Complexity: O(log n)
|
|
703
|
+
* Space Complexity: O(log n)
|
|
704
|
+
*/
|
|
705
|
+
/**
|
|
706
|
+
* Time Complexity: O(log n)
|
|
707
|
+
* Space Complexity: O(log n)
|
|
708
|
+
*
|
|
709
|
+
* The function `getPathToRoot` returns an array of nodes from a given node to the root of a tree
|
|
710
|
+
* structure, with the option to reverse the order of the nodes.
|
|
711
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
712
|
+
* starting node from which you want to find the path to the root. It can be of type `BTNKey`, `N`,
|
|
713
|
+
* `null`, or `undefined`.
|
|
603
714
|
* @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
|
|
604
715
|
* resulting path should be reversed or not. If `isReverse` is set to `true`, the path will be
|
|
605
|
-
* reversed before returning it. If `isReverse` is set to `false
|
|
606
|
-
* @returns The function `getPathToRoot` returns an array of
|
|
716
|
+
* reversed before returning it. If `isReverse` is set to `false`, the path will be returned as is
|
|
717
|
+
* @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
|
|
607
718
|
*/
|
|
608
719
|
getPathToRoot(beginRoot, isReverse = true) {
|
|
609
720
|
// TODO to support get path through passing key
|
|
@@ -621,15 +732,22 @@ class BinaryTree {
|
|
|
621
732
|
return isReverse ? result.reverse() : result;
|
|
622
733
|
}
|
|
623
734
|
/**
|
|
624
|
-
*
|
|
625
|
-
*
|
|
735
|
+
* Time Complexity: O(log n)
|
|
736
|
+
* Space Complexity: O(1)
|
|
737
|
+
*/
|
|
738
|
+
/**
|
|
739
|
+
* Time Complexity: O(log n)
|
|
740
|
+
* Space Complexity: O(1)
|
|
741
|
+
*
|
|
742
|
+
* The function `getLeftMost` returns the leftmost node in a binary tree, either recursively or
|
|
743
|
+
* iteratively.
|
|
626
744
|
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
|
|
627
|
-
* for finding the leftmost node in a binary tree. It can be either a
|
|
628
|
-
*
|
|
745
|
+
* for finding the leftmost node in a binary tree. It can be either a `BTNKey` (a key value), `N` (a
|
|
746
|
+
* node), `null`, or `undefined`. If not provided, it defaults to `this.root`,
|
|
629
747
|
* @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
|
|
630
748
|
* be performed when finding the leftmost node in a binary tree. It can have two possible values:
|
|
631
|
-
* @returns The function `getLeftMost` returns the leftmost node (`N`) in
|
|
632
|
-
* no leftmost node, it returns `null
|
|
749
|
+
* @returns The function `getLeftMost` returns the leftmost node (`N`) in the binary tree. If there
|
|
750
|
+
* is no leftmost node, it returns `null` or `undefined` depending on the input.
|
|
633
751
|
*/
|
|
634
752
|
getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
635
753
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
@@ -654,15 +772,23 @@ class BinaryTree {
|
|
|
654
772
|
}
|
|
655
773
|
}
|
|
656
774
|
/**
|
|
775
|
+
* Time Complexity: O(log n)
|
|
776
|
+
* Space Complexity: O(1)
|
|
777
|
+
*/
|
|
778
|
+
/**
|
|
779
|
+
* Time Complexity: O(log n)
|
|
780
|
+
* Space Complexity: O(1)
|
|
781
|
+
*
|
|
657
782
|
* The function `getRightMost` returns the rightmost node in a binary tree, either recursively or
|
|
658
783
|
* iteratively.
|
|
659
|
-
* @param {N | null | undefined} beginRoot - The `beginRoot` parameter
|
|
660
|
-
*
|
|
661
|
-
* or `
|
|
662
|
-
*
|
|
663
|
-
*
|
|
664
|
-
*
|
|
665
|
-
* `
|
|
784
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
785
|
+
* starting node from which we want to find the rightmost node. It can be of type `BTNKey`, `N`,
|
|
786
|
+
* `null`, or `undefined`. If not provided, it defaults to `this.root`, which is a property of the
|
|
787
|
+
* current object.
|
|
788
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
789
|
+
* type of iteration to use when finding the rightmost node. It can have one of two values:
|
|
790
|
+
* @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If there
|
|
791
|
+
* is no rightmost node, it returns `null` or `undefined`, depending on the input.
|
|
666
792
|
*/
|
|
667
793
|
getRightMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
668
794
|
// TODO support get right most by passing key in
|
|
@@ -688,13 +814,20 @@ class BinaryTree {
|
|
|
688
814
|
}
|
|
689
815
|
}
|
|
690
816
|
/**
|
|
817
|
+
* Time Complexity: O(n)
|
|
818
|
+
* Space Complexity: O(1)
|
|
819
|
+
*/
|
|
820
|
+
/**
|
|
821
|
+
* Time Complexity: O(n)
|
|
822
|
+
* Space Complexity: O(1)
|
|
823
|
+
*
|
|
691
824
|
* The function `isSubtreeBST` checks if a given binary tree is a valid binary search tree.
|
|
692
|
-
* @param {N} beginRoot - The `beginRoot` parameter
|
|
693
|
-
* to check if it is a
|
|
825
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the root
|
|
826
|
+
* node of the binary search tree (BST) that you want to check if it is a subtree of another BST.
|
|
694
827
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
695
828
|
* type of iteration to use when checking if a subtree is a binary search tree (BST). It can have two
|
|
696
829
|
* possible values:
|
|
697
|
-
* @returns
|
|
830
|
+
* @returns a boolean value.
|
|
698
831
|
*/
|
|
699
832
|
isSubtreeBST(beginRoot, iterationType = this.iterationType) {
|
|
700
833
|
// TODO there is a bug
|
|
@@ -729,11 +862,18 @@ class BinaryTree {
|
|
|
729
862
|
}
|
|
730
863
|
}
|
|
731
864
|
/**
|
|
865
|
+
* Time Complexity: O(n)
|
|
866
|
+
* Space Complexity: O(1)
|
|
867
|
+
*/
|
|
868
|
+
/**
|
|
869
|
+
* Time Complexity: O(n)
|
|
870
|
+
* Space Complexity: O(1)
|
|
871
|
+
*
|
|
732
872
|
* The function checks if a binary tree is a binary search tree.
|
|
733
873
|
* @param iterationType - The parameter "iterationType" is used to specify the type of iteration to
|
|
734
874
|
* be used when checking if the binary tree is a binary search tree (BST). It is an optional
|
|
735
|
-
* parameter with a default value of "this.iterationType". The value of "this.iterationType" is
|
|
736
|
-
*
|
|
875
|
+
* parameter with a default value of "this.iterationType". The value of "this.iterationType" is
|
|
876
|
+
* expected to be
|
|
737
877
|
* @returns a boolean value.
|
|
738
878
|
*/
|
|
739
879
|
isBST(iterationType = this.iterationType) {
|
|
@@ -742,19 +882,30 @@ class BinaryTree {
|
|
|
742
882
|
return this.isSubtreeBST(this.root, iterationType);
|
|
743
883
|
}
|
|
744
884
|
/**
|
|
885
|
+
* Time complexity: O(n)
|
|
886
|
+
* Space complexity: O(log n)
|
|
887
|
+
*/
|
|
888
|
+
/**
|
|
889
|
+
* Time complexity: O(n)
|
|
890
|
+
* Space complexity: O(log n)
|
|
891
|
+
*
|
|
745
892
|
* The function `subTreeTraverse` traverses a binary tree and applies a callback function to each
|
|
746
893
|
* node, either recursively or iteratively.
|
|
747
|
-
* @param callback - The `callback` parameter is a function that will be called
|
|
748
|
-
* subtree traversal. It takes a single
|
|
749
|
-
* returns a value
|
|
750
|
-
*
|
|
751
|
-
*
|
|
752
|
-
*
|
|
753
|
-
*
|
|
894
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
895
|
+
* the subtree traversal. It takes a single parameter, which is the current node being traversed, and
|
|
896
|
+
* returns a value of any type.
|
|
897
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
898
|
+
* starting node or key from which the subtree traversal should begin. It can be of type `BTNKey`,
|
|
899
|
+
* `N`, `null`, or `undefined`. If not provided, the `root` property of the current object is used as
|
|
900
|
+
* the default value.
|
|
754
901
|
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
755
|
-
* performed on the
|
|
756
|
-
* @param includeNull - The
|
|
757
|
-
*
|
|
902
|
+
* performed on the subtree. It can have two possible values:
|
|
903
|
+
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
904
|
+
* whether or not to include null values in the traversal. If `includeNull` is set to `true`, the
|
|
905
|
+
* traversal will include null values, otherwise it will skip them.
|
|
906
|
+
* @returns The function `subTreeTraverse` returns an array of values that are the result of invoking
|
|
907
|
+
* the `callback` function on each node in the subtree. The type of the array elements is determined
|
|
908
|
+
* by the return type of the `callback` function.
|
|
758
909
|
*/
|
|
759
910
|
subTreeTraverse(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
760
911
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
@@ -831,20 +982,31 @@ class BinaryTree {
|
|
|
831
982
|
return typeof potentialKey === 'number';
|
|
832
983
|
}
|
|
833
984
|
/**
|
|
834
|
-
*
|
|
835
|
-
*
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
*
|
|
985
|
+
* Time complexity: O(n)
|
|
986
|
+
* Space complexity: O(n)
|
|
987
|
+
*/
|
|
988
|
+
/**
|
|
989
|
+
* Time complexity: O(n)
|
|
990
|
+
* Space complexity: O(n)
|
|
991
|
+
*
|
|
992
|
+
* The `dfs` function performs a depth-first search traversal on a binary tree or graph, based on the
|
|
993
|
+
* specified pattern and iteration type, and returns an array of values obtained from applying a
|
|
994
|
+
* callback function to each visited node.
|
|
995
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
996
|
+
* the tree during the depth-first search. It takes a single parameter, which can be of type `N`,
|
|
997
|
+
* `null`, or `undefined`, and returns a value of any type. The default value for this parameter is
|
|
839
998
|
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter determines the order in which the
|
|
840
|
-
* nodes are
|
|
841
|
-
* @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
|
|
842
|
-
* search. It
|
|
843
|
-
*
|
|
999
|
+
* nodes are traversed during the depth-first search. It can have one of the following values:
|
|
1000
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
|
|
1001
|
+
* for the depth-first search traversal. It can be specified as a key, a node object, or
|
|
1002
|
+
* `null`/`undefined`. If not provided, the `beginRoot` will default to the root node of the tree.
|
|
844
1003
|
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
845
|
-
* iteration
|
|
846
|
-
* @param includeNull - The
|
|
847
|
-
*
|
|
1004
|
+
* iteration to use when traversing the tree. It can have one of the following values:
|
|
1005
|
+
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
1006
|
+
* whether null or undefined nodes should be included in the traversal. If `includeNull` is set to
|
|
1007
|
+
* `true`, null or undefined nodes will be included in the traversal. If `includeNull` is set to
|
|
1008
|
+
* `false`, null or undefined
|
|
1009
|
+
* @returns an array of values that are the return values of the callback function.
|
|
848
1010
|
*/
|
|
849
1011
|
dfs(callback = this._defaultOneParamCallback, pattern = 'in', beginRoot = this.root, iterationType = types_1.IterationType.ITERATIVE, includeNull = false) {
|
|
850
1012
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
@@ -953,18 +1115,29 @@ class BinaryTree {
|
|
|
953
1115
|
return ans;
|
|
954
1116
|
}
|
|
955
1117
|
/**
|
|
956
|
-
*
|
|
957
|
-
*
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
*
|
|
961
|
-
*
|
|
962
|
-
*
|
|
963
|
-
*
|
|
964
|
-
*
|
|
965
|
-
*
|
|
966
|
-
*
|
|
967
|
-
*
|
|
1118
|
+
* Time complexity: O(n)
|
|
1119
|
+
* Space complexity: O(n)
|
|
1120
|
+
*/
|
|
1121
|
+
/**
|
|
1122
|
+
* Time complexity: O(n)
|
|
1123
|
+
* Space complexity: O(n)
|
|
1124
|
+
*
|
|
1125
|
+
* The `bfs` function performs a breadth-first search traversal on a binary tree, executing a
|
|
1126
|
+
* callback function on each node.
|
|
1127
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
1128
|
+
* the breadth-first search traversal. It takes a single parameter, which is the current node being
|
|
1129
|
+
* visited, and returns a value of any type.
|
|
1130
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
1131
|
+
* starting node for the breadth-first search traversal. It can be specified as a key, a node object,
|
|
1132
|
+
* or `null`/`undefined` to indicate the root of the tree. If not provided, the `root` property of
|
|
1133
|
+
* the class is used as
|
|
1134
|
+
* @param iterationType - The `iterationType` parameter determines the type of iteration to be
|
|
1135
|
+
* performed during the breadth-first search (BFS). It can have two possible values:
|
|
1136
|
+
* @param [includeNull=false] - The `includeNull` parameter is a boolean flag that determines whether
|
|
1137
|
+
* or not to include null values in the breadth-first search traversal. If `includeNull` is set to
|
|
1138
|
+
* `true`, null values will be included in the traversal, otherwise they will be skipped.
|
|
1139
|
+
* @returns an array of values that are the result of invoking the callback function on each node in
|
|
1140
|
+
* the breadth-first traversal of a binary tree.
|
|
968
1141
|
*/
|
|
969
1142
|
bfs(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
970
1143
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
@@ -1019,20 +1192,29 @@ class BinaryTree {
|
|
|
1019
1192
|
return ans;
|
|
1020
1193
|
}
|
|
1021
1194
|
/**
|
|
1022
|
-
*
|
|
1023
|
-
*
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
*
|
|
1027
|
-
*
|
|
1028
|
-
*
|
|
1029
|
-
*
|
|
1030
|
-
*
|
|
1031
|
-
*
|
|
1032
|
-
* @param
|
|
1033
|
-
*
|
|
1034
|
-
*
|
|
1035
|
-
*
|
|
1195
|
+
* Time complexity: O(n)
|
|
1196
|
+
* Space complexity: O(n)
|
|
1197
|
+
*/
|
|
1198
|
+
/**
|
|
1199
|
+
* Time complexity: O(n)
|
|
1200
|
+
* Space complexity: O(n)
|
|
1201
|
+
*
|
|
1202
|
+
* The `listLevels` function returns an array of arrays, where each inner array represents a level in
|
|
1203
|
+
* a binary tree and contains the values returned by a callback function applied to the nodes at that
|
|
1204
|
+
* level.
|
|
1205
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
1206
|
+
* the tree. It takes a single parameter, which can be of type `N`, `null`, or `undefined`, and
|
|
1207
|
+
* returns a value of any type.
|
|
1208
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
1209
|
+
* starting node for traversing the tree. It can be either a node object (`N`), a key value
|
|
1210
|
+
* (`BTNKey`), `null`, or `undefined`. If not provided, it defaults to the root node of the tree.
|
|
1211
|
+
* @param iterationType - The `iterationType` parameter determines the type of iteration to be
|
|
1212
|
+
* performed on the tree. It can have two possible values:
|
|
1213
|
+
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
1214
|
+
* whether or not to include null values in the resulting levels. If `includeNull` is set to `true`,
|
|
1215
|
+
* null values will be included in the levels. If `includeNull` is set to `false`, null values will
|
|
1216
|
+
* be excluded
|
|
1217
|
+
* @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
|
|
1036
1218
|
*/
|
|
1037
1219
|
listLevels(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
1038
1220
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
@@ -1084,9 +1266,10 @@ class BinaryTree {
|
|
|
1084
1266
|
return levelsNodes;
|
|
1085
1267
|
}
|
|
1086
1268
|
/**
|
|
1087
|
-
* The function returns the predecessor node of a given node in a binary tree.
|
|
1088
|
-
* @param {N} node - The
|
|
1089
|
-
*
|
|
1269
|
+
* The function `getPredecessor` returns the predecessor node of a given node in a binary tree.
|
|
1270
|
+
* @param {BTNKey | N | null | undefined} node - The `node` parameter can be of type `BTNKey`, `N`,
|
|
1271
|
+
* `null`, or `undefined`.
|
|
1272
|
+
* @returns The function `getPredecessor` returns a value of type `N | undefined`.
|
|
1090
1273
|
*/
|
|
1091
1274
|
getPredecessor(node) {
|
|
1092
1275
|
node = this.ensureNotKey(node);
|
|
@@ -1106,11 +1289,10 @@ class BinaryTree {
|
|
|
1106
1289
|
}
|
|
1107
1290
|
}
|
|
1108
1291
|
/**
|
|
1109
|
-
* The function `getSuccessor` returns the next node in a binary tree given a node
|
|
1110
|
-
* `x`
|
|
1111
|
-
* @
|
|
1112
|
-
*
|
|
1113
|
-
* if there is no successor, or `undefined` if the input `x` is `undefined`.
|
|
1292
|
+
* The function `getSuccessor` returns the next node in a binary tree given a current node.
|
|
1293
|
+
* @param {BTNKey | N | null} [x] - The parameter `x` can be of type `BTNKey`, `N`, or `null`.
|
|
1294
|
+
* @returns the successor of the given node or key. The successor is the node that comes immediately
|
|
1295
|
+
* after the given node in the inorder traversal of the binary tree.
|
|
1114
1296
|
*/
|
|
1115
1297
|
getSuccessor(x) {
|
|
1116
1298
|
x = this.ensureNotKey(x);
|
|
@@ -1127,18 +1309,26 @@ class BinaryTree {
|
|
|
1127
1309
|
return y;
|
|
1128
1310
|
}
|
|
1129
1311
|
/**
|
|
1130
|
-
*
|
|
1131
|
-
*
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
*
|
|
1312
|
+
* Time complexity: O(n)
|
|
1313
|
+
* Space complexity: O(1)
|
|
1314
|
+
*/
|
|
1315
|
+
/**
|
|
1316
|
+
* Time complexity: O(n)
|
|
1317
|
+
* Space complexity: O(1)
|
|
1318
|
+
* The `morris` function performs a depth-first traversal on a binary tree using the Morris traversal
|
|
1319
|
+
* algorithm.
|
|
1320
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
1321
|
+
* the tree. It takes a single parameter of type `N` (the type of the nodes in the tree) and returns
|
|
1322
|
+
* a value of any type.
|
|
1135
1323
|
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
|
|
1136
1324
|
* determines the order in which the nodes of a binary tree are traversed. It can have one of the
|
|
1137
1325
|
* following values:
|
|
1138
|
-
* @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
|
|
1139
|
-
* traversal. It
|
|
1140
|
-
*
|
|
1141
|
-
* @returns The `morris`
|
|
1326
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
|
|
1327
|
+
* for the traversal. It can be specified as a key, a node object, or `null`/`undefined` to indicate
|
|
1328
|
+
* the root of the tree. If no value is provided, the default value is the root of the tree.
|
|
1329
|
+
* @returns The function `morris` returns an array of values that are the result of invoking the
|
|
1330
|
+
* `callback` function on each node in the binary tree. The type of the array elements is determined
|
|
1331
|
+
* by the return type of the `callback` function.
|
|
1142
1332
|
*/
|
|
1143
1333
|
morris(callback = this._defaultOneParamCallback, pattern = 'in', beginRoot = this.root) {
|
|
1144
1334
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
@@ -1225,7 +1415,6 @@ class BinaryTree {
|
|
|
1225
1415
|
}
|
|
1226
1416
|
return ans;
|
|
1227
1417
|
}
|
|
1228
|
-
// --- start additional methods ---
|
|
1229
1418
|
/**
|
|
1230
1419
|
* The above function is an iterator for a binary tree that can be used to traverse the tree in
|
|
1231
1420
|
* either an iterative or recursive manner.
|
|
@@ -1256,12 +1445,10 @@ class BinaryTree {
|
|
|
1256
1445
|
}
|
|
1257
1446
|
else {
|
|
1258
1447
|
if (node.left) {
|
|
1259
|
-
// @ts-ignore
|
|
1260
1448
|
yield* this[Symbol.iterator](node.left);
|
|
1261
1449
|
}
|
|
1262
1450
|
yield node.key;
|
|
1263
1451
|
if (node.right) {
|
|
1264
|
-
// @ts-ignore
|
|
1265
1452
|
yield* this[Symbol.iterator](node.right);
|
|
1266
1453
|
}
|
|
1267
1454
|
}
|
|
@@ -1341,9 +1528,9 @@ class BinaryTree {
|
|
|
1341
1528
|
}
|
|
1342
1529
|
/**
|
|
1343
1530
|
* The `print` function is used to display a binary tree structure in a visually appealing way.
|
|
1344
|
-
* @param {N | null | undefined} root - The `root` parameter
|
|
1345
|
-
* root node of a binary tree.
|
|
1346
|
-
*
|
|
1531
|
+
* @param {N | null | undefined} root - The `root` parameter is of type `BTNKey | N | null |
|
|
1532
|
+
* undefined`. It represents the root node of a binary tree. The root node can have one of the
|
|
1533
|
+
* following types:
|
|
1347
1534
|
*/
|
|
1348
1535
|
print(beginRoot = this.root) {
|
|
1349
1536
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
@@ -1356,17 +1543,17 @@ class BinaryTree {
|
|
|
1356
1543
|
}
|
|
1357
1544
|
};
|
|
1358
1545
|
const _displayAux = (node) => {
|
|
1359
|
-
if (node
|
|
1546
|
+
if (!this.isRealNode(node)) {
|
|
1360
1547
|
return [[], 0, 0, 0];
|
|
1361
1548
|
}
|
|
1362
|
-
if (node && node.right
|
|
1549
|
+
if (this.isRealNode(node) && !this.isRealNode(node.right) && !this.isRealNode(node.left)) {
|
|
1363
1550
|
const line = `${node.key}`;
|
|
1364
1551
|
const width = line.length;
|
|
1365
1552
|
const height = 1;
|
|
1366
1553
|
const middle = Math.floor(width / 2);
|
|
1367
1554
|
return [[line], width, height, middle];
|
|
1368
1555
|
}
|
|
1369
|
-
if (node && node.right
|
|
1556
|
+
if (this.isRealNode(node) && !this.isRealNode(node.right)) {
|
|
1370
1557
|
const [lines, n, p, x] = _displayAux(node.left);
|
|
1371
1558
|
const s = `${node.key}`;
|
|
1372
1559
|
const u = s.length;
|
|
@@ -1375,7 +1562,7 @@ class BinaryTree {
|
|
|
1375
1562
|
const shifted_lines = lines.map(line => line + ' '.repeat(u));
|
|
1376
1563
|
return [[first_line, second_line, ...shifted_lines], n + u, p + 2, n + Math.floor(u / 2)];
|
|
1377
1564
|
}
|
|
1378
|
-
if (node && node.left
|
|
1565
|
+
if (this.isRealNode(node) && !this.isRealNode(node.left)) {
|
|
1379
1566
|
const [lines, n, p, u] = _displayAux(node.right);
|
|
1380
1567
|
const s = `${node.key}`;
|
|
1381
1568
|
const x = s.length;
|