min-heap-typed 1.42.8 → 1.43.0
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 +415 -236
- 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 +465 -256
- 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,33 +117,40 @@ 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) => {
|
|
127
139
|
const queue = new queue_1.Queue([root]);
|
|
128
140
|
while (queue.size > 0) {
|
|
129
141
|
const cur = queue.shift();
|
|
130
|
-
if (cur) {
|
|
131
|
-
|
|
132
|
-
cur.value = newNode.value;
|
|
133
|
-
return;
|
|
134
|
-
}
|
|
135
|
-
const inserted = this._addTo(newNode, cur);
|
|
136
|
-
if (inserted !== undefined)
|
|
137
|
-
return inserted;
|
|
138
|
-
if (cur.left)
|
|
139
|
-
queue.push(cur.left);
|
|
140
|
-
if (cur.right)
|
|
141
|
-
queue.push(cur.right);
|
|
142
|
-
}
|
|
143
|
-
else
|
|
142
|
+
if (newNode && cur.key === newNode.key) {
|
|
143
|
+
cur.value = newNode.value;
|
|
144
144
|
return;
|
|
145
|
+
}
|
|
146
|
+
const inserted = this._addTo(newNode, cur);
|
|
147
|
+
if (inserted !== undefined)
|
|
148
|
+
return inserted;
|
|
149
|
+
if (cur.left)
|
|
150
|
+
queue.push(cur.left);
|
|
151
|
+
if (cur.right)
|
|
152
|
+
queue.push(cur.right);
|
|
145
153
|
}
|
|
146
|
-
return;
|
|
147
154
|
};
|
|
148
155
|
let inserted, needInsert;
|
|
149
156
|
if (keyOrNode === null) {
|
|
@@ -174,13 +181,21 @@ class BinaryTree {
|
|
|
174
181
|
return inserted;
|
|
175
182
|
}
|
|
176
183
|
/**
|
|
177
|
-
*
|
|
178
|
-
*
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
*
|
|
182
|
-
*
|
|
183
|
-
*
|
|
184
|
+
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
185
|
+
* Space Complexity: O(1)
|
|
186
|
+
*/
|
|
187
|
+
/**
|
|
188
|
+
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
189
|
+
* Space Complexity: O(1)
|
|
190
|
+
*
|
|
191
|
+
* The `addMany` function takes an array of keys or nodes and an optional array of values, and adds
|
|
192
|
+
* each key-value pair to a data structure.
|
|
193
|
+
* @param {(BTNKey | N |null | undefined)[]} keysOrNodes - An array of keys or nodes to be added to
|
|
194
|
+
* the binary search tree. Each element can be of type `BTNKey` (a key value), `N` (a node), `null`,
|
|
195
|
+
* or `undefined`.
|
|
196
|
+
* @param {(V | undefined)[]} [values] - The `values` parameter is an optional array of values that
|
|
197
|
+
* correspond to the keys or nodes being added. If provided, the values will be associated with the
|
|
198
|
+
* keys or nodes during the add operation.
|
|
184
199
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
185
200
|
*/
|
|
186
201
|
addMany(keysOrNodes, values) {
|
|
@@ -197,6 +212,13 @@ class BinaryTree {
|
|
|
197
212
|
});
|
|
198
213
|
}
|
|
199
214
|
/**
|
|
215
|
+
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
216
|
+
* Space Complexity: O(1)
|
|
217
|
+
*/
|
|
218
|
+
/**
|
|
219
|
+
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
220
|
+
* Space Complexity: O(1)
|
|
221
|
+
*
|
|
200
222
|
* The `refill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
|
|
201
223
|
* @param {(BTNKey | N)[]} keysOrNodes - The `keysOrNodes` parameter is an array that can contain either
|
|
202
224
|
* `BTNKey` or `N` values.
|
|
@@ -210,24 +232,29 @@ class BinaryTree {
|
|
|
210
232
|
return keysOrNodes.length === this.addMany(keysOrNodes, values).length;
|
|
211
233
|
}
|
|
212
234
|
/**
|
|
213
|
-
*
|
|
214
|
-
*
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
*
|
|
218
|
-
*
|
|
219
|
-
*
|
|
220
|
-
*
|
|
221
|
-
*
|
|
222
|
-
*
|
|
223
|
-
*
|
|
224
|
-
*
|
|
235
|
+
* Time Complexity: O(n)
|
|
236
|
+
* Space Complexity: O(1)
|
|
237
|
+
*/
|
|
238
|
+
/**
|
|
239
|
+
* Time Complexity: O(n)
|
|
240
|
+
* Space Complexity: O(1)
|
|
241
|
+
*
|
|
242
|
+
* The function deletes a node from a binary tree and returns an array of the deleted nodes along
|
|
243
|
+
* with the nodes that need to be balanced.
|
|
244
|
+
* @param {ReturnType<C> | null | undefined} identifier - The identifier parameter is the value or
|
|
245
|
+
* object that you want to delete from the binary tree. It can be of any type that is compatible with
|
|
246
|
+
* the callback function's return type. It can also be null or undefined if you want to delete a
|
|
247
|
+
* specific node based on its value or object.
|
|
248
|
+
* @param {C} callback - The `callback` parameter is a function that is used to determine the
|
|
249
|
+
* identifier of the node to be deleted. It is optional and has a default value of
|
|
250
|
+
* `this._defaultOneParamCallback`. The `callback` function should return the identifier of the node.
|
|
251
|
+
* @returns an array of `BiTreeDeleteResult<N>`.
|
|
225
252
|
*/
|
|
226
253
|
delete(identifier, callback = this._defaultOneParamCallback) {
|
|
227
254
|
const deletedResult = [];
|
|
228
255
|
if (!this.root)
|
|
229
256
|
return deletedResult;
|
|
230
|
-
if (identifier instanceof BinaryTreeNode)
|
|
257
|
+
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
231
258
|
callback = (node => node);
|
|
232
259
|
const curr = this.getNode(identifier, callback);
|
|
233
260
|
if (!curr)
|
|
@@ -252,16 +279,18 @@ class BinaryTree {
|
|
|
252
279
|
}
|
|
253
280
|
}
|
|
254
281
|
else {
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
if (parentOfLeftSubTreeMax
|
|
261
|
-
parentOfLeftSubTreeMax.right
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
282
|
+
if (curr.left) {
|
|
283
|
+
const leftSubTreeRightMost = this.getRightMost(curr.left);
|
|
284
|
+
if (leftSubTreeRightMost) {
|
|
285
|
+
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
286
|
+
orgCurrent = this._swap(curr, leftSubTreeRightMost);
|
|
287
|
+
if (parentOfLeftSubTreeMax) {
|
|
288
|
+
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
289
|
+
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
290
|
+
else
|
|
291
|
+
parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
|
|
292
|
+
needBalanced = parentOfLeftSubTreeMax;
|
|
293
|
+
}
|
|
265
294
|
}
|
|
266
295
|
}
|
|
267
296
|
}
|
|
@@ -270,15 +299,20 @@ class BinaryTree {
|
|
|
270
299
|
return deletedResult;
|
|
271
300
|
}
|
|
272
301
|
/**
|
|
273
|
-
*
|
|
274
|
-
*
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
*
|
|
278
|
-
*
|
|
279
|
-
*
|
|
280
|
-
* of a node in
|
|
281
|
-
*
|
|
302
|
+
* Time Complexity: O(n)
|
|
303
|
+
* Space Complexity: O(1)
|
|
304
|
+
*/
|
|
305
|
+
/**
|
|
306
|
+
* Time Complexity: O(n)
|
|
307
|
+
* Space Complexity: O(1)
|
|
308
|
+
*
|
|
309
|
+
* The function calculates the depth of a given node in a binary tree.
|
|
310
|
+
* @param {BTNKey | N | null | undefined} distNode - The `distNode` parameter represents the node in
|
|
311
|
+
* the binary tree whose depth we want to find. It can be of type `BTNKey`, `N`, `null`, or
|
|
312
|
+
* `undefined`.
|
|
313
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
|
|
314
|
+
* from which we want to calculate the depth. It can be either a `BTNKey` (binary tree node key) or
|
|
315
|
+
* `N` (binary tree node) or `null` or `undefined`. If no value is provided for `beginRoot
|
|
282
316
|
* @returns the depth of the `distNode` relative to the `beginRoot`.
|
|
283
317
|
*/
|
|
284
318
|
getDepth(distNode, beginRoot = this.root) {
|
|
@@ -295,15 +329,22 @@ class BinaryTree {
|
|
|
295
329
|
return depth;
|
|
296
330
|
}
|
|
297
331
|
/**
|
|
298
|
-
*
|
|
299
|
-
*
|
|
332
|
+
* Time Complexity: O(n)
|
|
333
|
+
* Space Complexity: O(log n)
|
|
334
|
+
* Best Case - O(log n) (when using recursive iterationType), Worst Case - O(n) (when using iterative iterationType)
|
|
335
|
+
*/
|
|
336
|
+
/**
|
|
337
|
+
* Time Complexity: O(n)
|
|
338
|
+
* Space Complexity: O(log n)
|
|
339
|
+
*
|
|
340
|
+
* The function `getHeight` calculates the maximum height of a binary tree using either recursive or
|
|
341
|
+
* iterative traversal.
|
|
300
342
|
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
301
|
-
* starting node from which
|
|
302
|
-
*
|
|
303
|
-
* node is specified. If `
|
|
343
|
+
* starting node of the binary tree from which we want to calculate the height. It can be of type
|
|
344
|
+
* `BTNKey`, `N`, `null`, or `undefined`. If not provided, it defaults to `this.root`.
|
|
304
345
|
* @param iterationType - The `iterationType` parameter is used to determine whether to calculate the
|
|
305
|
-
* height of the
|
|
306
|
-
*
|
|
346
|
+
* height of the tree using a recursive approach or an iterative approach. It can have two possible
|
|
347
|
+
* values:
|
|
307
348
|
* @returns the height of the binary tree.
|
|
308
349
|
*/
|
|
309
350
|
getHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
@@ -321,9 +362,6 @@ class BinaryTree {
|
|
|
321
362
|
return _getMaxHeight(beginRoot);
|
|
322
363
|
}
|
|
323
364
|
else {
|
|
324
|
-
if (!beginRoot) {
|
|
325
|
-
return -1;
|
|
326
|
-
}
|
|
327
365
|
const stack = [{ node: beginRoot, depth: 0 }];
|
|
328
366
|
let maxHeight = 0;
|
|
329
367
|
while (stack.length > 0) {
|
|
@@ -338,11 +376,19 @@ class BinaryTree {
|
|
|
338
376
|
}
|
|
339
377
|
}
|
|
340
378
|
/**
|
|
379
|
+
* Time Complexity: O(n)
|
|
380
|
+
* Space Complexity: O(log n)
|
|
381
|
+
* Best Case - O(log n) (when using recursive iterationType), Worst Case - O(n) (when using iterative iterationType)
|
|
382
|
+
*/
|
|
383
|
+
/**
|
|
384
|
+
* Time Complexity: O(n)
|
|
385
|
+
* Space Complexity: O(log n)
|
|
386
|
+
*
|
|
341
387
|
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
342
388
|
* recursive or iterative approach.
|
|
343
|
-
* @param {N | null | undefined} beginRoot - The `beginRoot` parameter
|
|
344
|
-
*
|
|
345
|
-
*
|
|
389
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
390
|
+
* starting node of the binary tree from which we want to calculate the minimum height. It can be of
|
|
391
|
+
* type `BTNKey`, `N`, `null`, or `undefined`. If no value is provided, it defaults to `this.root`.
|
|
346
392
|
* @param iterationType - The `iterationType` parameter is used to determine the method of iteration
|
|
347
393
|
* to calculate the minimum height of a binary tree. It can have two possible values:
|
|
348
394
|
* @returns The function `getMinHeight` returns the minimum height of a binary tree.
|
|
@@ -393,40 +439,54 @@ class BinaryTree {
|
|
|
393
439
|
}
|
|
394
440
|
}
|
|
395
441
|
/**
|
|
442
|
+
* Time Complexity: O(n)
|
|
443
|
+
* Space Complexity: O(log n)
|
|
444
|
+
*/
|
|
445
|
+
/**
|
|
446
|
+
* Time Complexity: O(n)
|
|
447
|
+
* Space Complexity: O(log n)
|
|
448
|
+
*
|
|
396
449
|
* The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
|
|
397
450
|
* height of the tree.
|
|
398
|
-
* @param {N | null | undefined} beginRoot - The
|
|
399
|
-
*
|
|
400
|
-
*
|
|
451
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
|
|
452
|
+
* for calculating the height and minimum height of a binary tree. It can be either a `BTNKey` (a key
|
|
453
|
+
* value of a binary tree node), `N` (a node of a binary tree), `null`, or `undefined`. If
|
|
454
|
+
* @returns a boolean value.
|
|
401
455
|
*/
|
|
402
456
|
isPerfectlyBalanced(beginRoot = this.root) {
|
|
403
457
|
return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
|
|
404
458
|
}
|
|
405
459
|
/**
|
|
406
|
-
*
|
|
407
|
-
*
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
*
|
|
411
|
-
*
|
|
412
|
-
*
|
|
413
|
-
*
|
|
414
|
-
*
|
|
415
|
-
* @param
|
|
416
|
-
*
|
|
417
|
-
*
|
|
418
|
-
*
|
|
419
|
-
* @param {
|
|
420
|
-
*
|
|
421
|
-
*
|
|
460
|
+
* Time Complexity: O(n)
|
|
461
|
+
* Space Complexity: O(log n).
|
|
462
|
+
*/
|
|
463
|
+
/**
|
|
464
|
+
* Time Complexity: O(n)
|
|
465
|
+
* Space Complexity: O(log n).
|
|
466
|
+
*
|
|
467
|
+
* The function `getNodes` retrieves nodes from a binary tree based on a given identifier and
|
|
468
|
+
* callback function.
|
|
469
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
470
|
+
* that you want to search for in the binary tree. It can be of any type that is returned by the
|
|
471
|
+
* callback function `C`. It can also be `null` or `undefined` if you don't want to search for a
|
|
472
|
+
* specific value.
|
|
473
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node of type `N` as
|
|
474
|
+
* input and returns a value of type `C`. It is used to determine if a node matches the given
|
|
475
|
+
* identifier. If no callback is provided, the `_defaultOneParamCallback` function is used as the
|
|
476
|
+
* default
|
|
477
|
+
* @param [onlyOne=false] - A boolean value indicating whether to only return the first node that
|
|
478
|
+
* matches the identifier. If set to true, the function will stop iterating once it finds a matching
|
|
479
|
+
* node and return that node. If set to false (default), the function will continue iterating and
|
|
480
|
+
* return all nodes that match the identifier.
|
|
481
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
482
|
+
* starting node for the traversal. It can be either a key, a node object, or `null`/`undefined`. If
|
|
483
|
+
* it is `null` or `undefined`, an empty array will be returned.
|
|
422
484
|
* @param iterationType - The `iterationType` parameter determines the type of iteration used to
|
|
423
485
|
* traverse the binary tree. It can have two possible values:
|
|
424
|
-
* @returns
|
|
486
|
+
* @returns an array of nodes of type `N`.
|
|
425
487
|
*/
|
|
426
488
|
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
427
|
-
if (!
|
|
428
|
-
return [];
|
|
429
|
-
if (identifier instanceof BinaryTreeNode)
|
|
489
|
+
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
430
490
|
callback = (node => node);
|
|
431
491
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
432
492
|
if (!beginRoot)
|
|
@@ -464,49 +524,73 @@ class BinaryTree {
|
|
|
464
524
|
return ans;
|
|
465
525
|
}
|
|
466
526
|
/**
|
|
467
|
-
*
|
|
468
|
-
*
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
*
|
|
472
|
-
*
|
|
473
|
-
*
|
|
474
|
-
*
|
|
475
|
-
*
|
|
476
|
-
*
|
|
477
|
-
*
|
|
478
|
-
*
|
|
479
|
-
*
|
|
480
|
-
*
|
|
527
|
+
* Time Complexity: O(n)
|
|
528
|
+
* Space Complexity: O(log n).
|
|
529
|
+
*/
|
|
530
|
+
/**
|
|
531
|
+
* Time Complexity: O(n)
|
|
532
|
+
*
|
|
533
|
+
* The function checks if a Binary Tree Node with a specific identifier exists in the tree.
|
|
534
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
535
|
+
* that you want to search for in the binary tree. It can be of any type that is returned by the
|
|
536
|
+
* callback function `C`. It can also be `null` or `undefined` if you don't want to specify a
|
|
537
|
+
* specific identifier.
|
|
538
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
539
|
+
* the binary tree. It is used to filter the nodes based on certain conditions. The `callback`
|
|
540
|
+
* function should return a boolean value indicating whether the node should be included in the
|
|
541
|
+
* result or not.
|
|
542
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
|
|
543
|
+
* for the search in the binary tree. It can be specified as a `BTNKey` (a unique identifier for a
|
|
544
|
+
* node in the binary tree), a node object (`N`), or `null`/`undefined` to start the search from
|
|
545
|
+
* @param iterationType - The `iterationType` parameter is a variable that determines the type of
|
|
546
|
+
* iteration to be performed on the binary tree. It is used to specify whether the iteration should
|
|
547
|
+
* be performed in a pre-order, in-order, or post-order manner.
|
|
481
548
|
* @returns a boolean value.
|
|
482
549
|
*/
|
|
483
550
|
has(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
484
|
-
if (identifier instanceof BinaryTreeNode)
|
|
551
|
+
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
485
552
|
callback = (node => node);
|
|
486
553
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
|
|
487
554
|
}
|
|
488
555
|
/**
|
|
489
|
-
*
|
|
490
|
-
*
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
*
|
|
494
|
-
*
|
|
495
|
-
*
|
|
496
|
-
*
|
|
497
|
-
*
|
|
498
|
-
*
|
|
499
|
-
*
|
|
500
|
-
*
|
|
501
|
-
*
|
|
556
|
+
* Time Complexity: O(n)
|
|
557
|
+
* Space Complexity: O(log n)
|
|
558
|
+
*/
|
|
559
|
+
/**
|
|
560
|
+
* Time Complexity: O(n)
|
|
561
|
+
* Space Complexity: O(log n)
|
|
562
|
+
*
|
|
563
|
+
* The function `getNode` returns the first node that matches the given identifier and callback
|
|
564
|
+
* function.
|
|
565
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
566
|
+
* used to identify the node you want to retrieve. It can be of any type that is returned by the
|
|
567
|
+
* callback function `C`. It can also be `null` or `undefined` if you don't have a specific
|
|
568
|
+
* identifier.
|
|
569
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
570
|
+
* the binary tree. It is used to determine if a node matches the given identifier. The `callback`
|
|
571
|
+
* function should take a single parameter of type `N` (the type of the nodes in the binary tree) and
|
|
572
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
|
|
573
|
+
* for searching the binary tree. It can be either a key value, a node object, or `null`/`undefined`.
|
|
574
|
+
* If `null` or `undefined` is passed, the search will start from the root of the binary tree.
|
|
575
|
+
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
576
|
+
* be performed when searching for nodes in the binary tree. It determines the order in which the
|
|
577
|
+
* nodes are visited during the search.
|
|
578
|
+
* @returns a value of type `N | null | undefined`.
|
|
502
579
|
*/
|
|
503
580
|
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
504
581
|
var _a;
|
|
505
|
-
if (identifier instanceof BinaryTreeNode)
|
|
582
|
+
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
506
583
|
callback = (node => node);
|
|
507
584
|
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : null;
|
|
508
585
|
}
|
|
509
586
|
/**
|
|
587
|
+
* Time Complexity: O(n)
|
|
588
|
+
* Space Complexity: O(log n)
|
|
589
|
+
*/
|
|
590
|
+
/**
|
|
591
|
+
* Time Complexity: O(n)
|
|
592
|
+
* Space Complexity: O(log n)
|
|
593
|
+
*
|
|
510
594
|
* The function `getNodeByKey` searches for a node in a binary tree by its key, using either
|
|
511
595
|
* recursive or iterative iteration.
|
|
512
596
|
* @param {BTNKey} key - The `key` parameter is the key value that we are searching for in the tree.
|
|
@@ -561,23 +645,34 @@ class BinaryTree {
|
|
|
561
645
|
return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
562
646
|
}
|
|
563
647
|
/**
|
|
564
|
-
*
|
|
565
|
-
*
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
*
|
|
569
|
-
*
|
|
570
|
-
*
|
|
571
|
-
*
|
|
572
|
-
*
|
|
573
|
-
*
|
|
574
|
-
*
|
|
575
|
-
*
|
|
576
|
-
* @
|
|
648
|
+
* Time Complexity: O(n)
|
|
649
|
+
* Space Complexity: O(log n)
|
|
650
|
+
*/
|
|
651
|
+
/**
|
|
652
|
+
* Time Complexity: O(n)
|
|
653
|
+
* Space Complexity: O(log n)
|
|
654
|
+
*
|
|
655
|
+
* The function `get` retrieves the value of a node in a binary tree based on the provided identifier
|
|
656
|
+
* and callback function.
|
|
657
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
658
|
+
* used to identify the node in the binary tree. It can be of any type that is the return type of the
|
|
659
|
+
* callback function `C`. It can also be `null` or `undefined` if no identifier is provided.
|
|
660
|
+
* @param {C} callback - The `callback` parameter is a function that will be called with each node in
|
|
661
|
+
* the binary tree. It is used to determine whether a node matches the given identifier. The callback
|
|
662
|
+
* function should return a value that can be compared to the identifier to determine if it is a
|
|
663
|
+
* match.
|
|
664
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
|
|
665
|
+
* for the search in the binary tree. It can be specified as a `BTNKey` (a unique identifier for a
|
|
666
|
+
* node), a node object of type `N`, or `null`/`undefined` to start the search from the root of
|
|
667
|
+
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
668
|
+
* be performed when searching for a node in the binary tree. It is an optional parameter with a
|
|
669
|
+
* default value specified by `this.iterationType`.
|
|
670
|
+
* @returns The value of the node with the given identifier is being returned. If the node is not
|
|
671
|
+
* found, `undefined` is returned.
|
|
577
672
|
*/
|
|
578
673
|
get(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
579
674
|
var _a, _b;
|
|
580
|
-
if (identifier instanceof BinaryTreeNode)
|
|
675
|
+
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
581
676
|
callback = (node => node);
|
|
582
677
|
return (_b = (_a = this.getNode(identifier, callback, beginRoot, iterationType)) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : undefined;
|
|
583
678
|
}
|
|
@@ -596,14 +691,22 @@ class BinaryTree {
|
|
|
596
691
|
return this.size === 0;
|
|
597
692
|
}
|
|
598
693
|
/**
|
|
599
|
-
*
|
|
600
|
-
*
|
|
601
|
-
|
|
602
|
-
|
|
694
|
+
* Time Complexity: O(log n)
|
|
695
|
+
* Space Complexity: O(log n)
|
|
696
|
+
*/
|
|
697
|
+
/**
|
|
698
|
+
* Time Complexity: O(log n)
|
|
699
|
+
* Space Complexity: O(log n)
|
|
700
|
+
*
|
|
701
|
+
* The function `getPathToRoot` returns an array of nodes from a given node to the root of a tree
|
|
702
|
+
* structure, with the option to reverse the order of the nodes.
|
|
703
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
704
|
+
* starting node from which you want to find the path to the root. It can be of type `BTNKey`, `N`,
|
|
705
|
+
* `null`, or `undefined`.
|
|
603
706
|
* @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
|
|
604
707
|
* 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
|
|
708
|
+
* reversed before returning it. If `isReverse` is set to `false`, the path will be returned as is
|
|
709
|
+
* @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
|
|
607
710
|
*/
|
|
608
711
|
getPathToRoot(beginRoot, isReverse = true) {
|
|
609
712
|
// TODO to support get path through passing key
|
|
@@ -621,15 +724,22 @@ class BinaryTree {
|
|
|
621
724
|
return isReverse ? result.reverse() : result;
|
|
622
725
|
}
|
|
623
726
|
/**
|
|
624
|
-
*
|
|
625
|
-
*
|
|
727
|
+
* Time Complexity: O(log n)
|
|
728
|
+
* Space Complexity: O(1)
|
|
729
|
+
*/
|
|
730
|
+
/**
|
|
731
|
+
* Time Complexity: O(log n)
|
|
732
|
+
* Space Complexity: O(1)
|
|
733
|
+
*
|
|
734
|
+
* The function `getLeftMost` returns the leftmost node in a binary tree, either recursively or
|
|
735
|
+
* iteratively.
|
|
626
736
|
* @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
|
-
*
|
|
737
|
+
* for finding the leftmost node in a binary tree. It can be either a `BTNKey` (a key value), `N` (a
|
|
738
|
+
* node), `null`, or `undefined`. If not provided, it defaults to `this.root`,
|
|
629
739
|
* @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
|
|
630
740
|
* 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
|
|
741
|
+
* @returns The function `getLeftMost` returns the leftmost node (`N`) in the binary tree. If there
|
|
742
|
+
* is no leftmost node, it returns `null` or `undefined` depending on the input.
|
|
633
743
|
*/
|
|
634
744
|
getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
635
745
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
@@ -654,15 +764,23 @@ class BinaryTree {
|
|
|
654
764
|
}
|
|
655
765
|
}
|
|
656
766
|
/**
|
|
767
|
+
* Time Complexity: O(log n)
|
|
768
|
+
* Space Complexity: O(1)
|
|
769
|
+
*/
|
|
770
|
+
/**
|
|
771
|
+
* Time Complexity: O(log n)
|
|
772
|
+
* Space Complexity: O(1)
|
|
773
|
+
*
|
|
657
774
|
* The function `getRightMost` returns the rightmost node in a binary tree, either recursively or
|
|
658
775
|
* iteratively.
|
|
659
|
-
* @param {N | null | undefined} beginRoot - The `beginRoot` parameter
|
|
660
|
-
*
|
|
661
|
-
* or `
|
|
662
|
-
*
|
|
663
|
-
*
|
|
664
|
-
*
|
|
665
|
-
* `
|
|
776
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
777
|
+
* starting node from which we want to find the rightmost node. It can be of type `BTNKey`, `N`,
|
|
778
|
+
* `null`, or `undefined`. If not provided, it defaults to `this.root`, which is a property of the
|
|
779
|
+
* current object.
|
|
780
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
781
|
+
* type of iteration to use when finding the rightmost node. It can have one of two values:
|
|
782
|
+
* @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If there
|
|
783
|
+
* is no rightmost node, it returns `null` or `undefined`, depending on the input.
|
|
666
784
|
*/
|
|
667
785
|
getRightMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
668
786
|
// TODO support get right most by passing key in
|
|
@@ -688,13 +806,20 @@ class BinaryTree {
|
|
|
688
806
|
}
|
|
689
807
|
}
|
|
690
808
|
/**
|
|
809
|
+
* Time Complexity: O(n)
|
|
810
|
+
* Space Complexity: O(1)
|
|
811
|
+
*/
|
|
812
|
+
/**
|
|
813
|
+
* Time Complexity: O(n)
|
|
814
|
+
* Space Complexity: O(1)
|
|
815
|
+
*
|
|
691
816
|
* 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
|
|
817
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the root
|
|
818
|
+
* node of the binary search tree (BST) that you want to check if it is a subtree of another BST.
|
|
694
819
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
695
820
|
* type of iteration to use when checking if a subtree is a binary search tree (BST). It can have two
|
|
696
821
|
* possible values:
|
|
697
|
-
* @returns
|
|
822
|
+
* @returns a boolean value.
|
|
698
823
|
*/
|
|
699
824
|
isSubtreeBST(beginRoot, iterationType = this.iterationType) {
|
|
700
825
|
// TODO there is a bug
|
|
@@ -729,11 +854,18 @@ class BinaryTree {
|
|
|
729
854
|
}
|
|
730
855
|
}
|
|
731
856
|
/**
|
|
857
|
+
* Time Complexity: O(n)
|
|
858
|
+
* Space Complexity: O(1)
|
|
859
|
+
*/
|
|
860
|
+
/**
|
|
861
|
+
* Time Complexity: O(n)
|
|
862
|
+
* Space Complexity: O(1)
|
|
863
|
+
*
|
|
732
864
|
* The function checks if a binary tree is a binary search tree.
|
|
733
865
|
* @param iterationType - The parameter "iterationType" is used to specify the type of iteration to
|
|
734
866
|
* 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
|
-
*
|
|
867
|
+
* parameter with a default value of "this.iterationType". The value of "this.iterationType" is
|
|
868
|
+
* expected to be
|
|
737
869
|
* @returns a boolean value.
|
|
738
870
|
*/
|
|
739
871
|
isBST(iterationType = this.iterationType) {
|
|
@@ -742,19 +874,30 @@ class BinaryTree {
|
|
|
742
874
|
return this.isSubtreeBST(this.root, iterationType);
|
|
743
875
|
}
|
|
744
876
|
/**
|
|
877
|
+
* Time complexity: O(n)
|
|
878
|
+
* Space complexity: O(log n)
|
|
879
|
+
*/
|
|
880
|
+
/**
|
|
881
|
+
* Time complexity: O(n)
|
|
882
|
+
* Space complexity: O(log n)
|
|
883
|
+
*
|
|
745
884
|
* The function `subTreeTraverse` traverses a binary tree and applies a callback function to each
|
|
746
885
|
* 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
|
-
*
|
|
886
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
887
|
+
* the subtree traversal. It takes a single parameter, which is the current node being traversed, and
|
|
888
|
+
* returns a value of any type.
|
|
889
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
890
|
+
* starting node or key from which the subtree traversal should begin. It can be of type `BTNKey`,
|
|
891
|
+
* `N`, `null`, or `undefined`. If not provided, the `root` property of the current object is used as
|
|
892
|
+
* the default value.
|
|
754
893
|
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
755
|
-
* performed on the
|
|
756
|
-
* @param includeNull - The
|
|
757
|
-
*
|
|
894
|
+
* performed on the subtree. It can have two possible values:
|
|
895
|
+
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
896
|
+
* whether or not to include null values in the traversal. If `includeNull` is set to `true`, the
|
|
897
|
+
* traversal will include null values, otherwise it will skip them.
|
|
898
|
+
* @returns The function `subTreeTraverse` returns an array of values that are the result of invoking
|
|
899
|
+
* the `callback` function on each node in the subtree. The type of the array elements is determined
|
|
900
|
+
* by the return type of the `callback` function.
|
|
758
901
|
*/
|
|
759
902
|
subTreeTraverse(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
760
903
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
@@ -831,20 +974,31 @@ class BinaryTree {
|
|
|
831
974
|
return typeof potentialKey === 'number';
|
|
832
975
|
}
|
|
833
976
|
/**
|
|
834
|
-
*
|
|
835
|
-
*
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
*
|
|
977
|
+
* Time complexity: O(n)
|
|
978
|
+
* Space complexity: O(n)
|
|
979
|
+
*/
|
|
980
|
+
/**
|
|
981
|
+
* Time complexity: O(n)
|
|
982
|
+
* Space complexity: O(n)
|
|
983
|
+
*
|
|
984
|
+
* The `dfs` function performs a depth-first search traversal on a binary tree or graph, based on the
|
|
985
|
+
* specified pattern and iteration type, and returns an array of values obtained from applying a
|
|
986
|
+
* callback function to each visited node.
|
|
987
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
988
|
+
* the tree during the depth-first search. It takes a single parameter, which can be of type `N`,
|
|
989
|
+
* `null`, or `undefined`, and returns a value of any type. The default value for this parameter is
|
|
839
990
|
* @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
|
-
*
|
|
991
|
+
* nodes are traversed during the depth-first search. It can have one of the following values:
|
|
992
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
|
|
993
|
+
* for the depth-first search traversal. It can be specified as a key, a node object, or
|
|
994
|
+
* `null`/`undefined`. If not provided, the `beginRoot` will default to the root node of the tree.
|
|
844
995
|
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
845
|
-
* iteration
|
|
846
|
-
* @param includeNull - The
|
|
847
|
-
*
|
|
996
|
+
* iteration to use when traversing the tree. It can have one of the following values:
|
|
997
|
+
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
998
|
+
* whether null or undefined nodes should be included in the traversal. If `includeNull` is set to
|
|
999
|
+
* `true`, null or undefined nodes will be included in the traversal. If `includeNull` is set to
|
|
1000
|
+
* `false`, null or undefined
|
|
1001
|
+
* @returns an array of values that are the return values of the callback function.
|
|
848
1002
|
*/
|
|
849
1003
|
dfs(callback = this._defaultOneParamCallback, pattern = 'in', beginRoot = this.root, iterationType = types_1.IterationType.ITERATIVE, includeNull = false) {
|
|
850
1004
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
@@ -953,18 +1107,29 @@ class BinaryTree {
|
|
|
953
1107
|
return ans;
|
|
954
1108
|
}
|
|
955
1109
|
/**
|
|
956
|
-
*
|
|
957
|
-
*
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
*
|
|
961
|
-
*
|
|
962
|
-
*
|
|
963
|
-
*
|
|
964
|
-
*
|
|
965
|
-
*
|
|
966
|
-
*
|
|
967
|
-
*
|
|
1110
|
+
* Time complexity: O(n)
|
|
1111
|
+
* Space complexity: O(n)
|
|
1112
|
+
*/
|
|
1113
|
+
/**
|
|
1114
|
+
* Time complexity: O(n)
|
|
1115
|
+
* Space complexity: O(n)
|
|
1116
|
+
*
|
|
1117
|
+
* The `bfs` function performs a breadth-first search traversal on a binary tree, executing a
|
|
1118
|
+
* callback function on each node.
|
|
1119
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
1120
|
+
* the breadth-first search traversal. It takes a single parameter, which is the current node being
|
|
1121
|
+
* visited, and returns a value of any type.
|
|
1122
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
1123
|
+
* starting node for the breadth-first search traversal. It can be specified as a key, a node object,
|
|
1124
|
+
* or `null`/`undefined` to indicate the root of the tree. If not provided, the `root` property of
|
|
1125
|
+
* the class is used as
|
|
1126
|
+
* @param iterationType - The `iterationType` parameter determines the type of iteration to be
|
|
1127
|
+
* performed during the breadth-first search (BFS). It can have two possible values:
|
|
1128
|
+
* @param [includeNull=false] - The `includeNull` parameter is a boolean flag that determines whether
|
|
1129
|
+
* or not to include null values in the breadth-first search traversal. If `includeNull` is set to
|
|
1130
|
+
* `true`, null values will be included in the traversal, otherwise they will be skipped.
|
|
1131
|
+
* @returns an array of values that are the result of invoking the callback function on each node in
|
|
1132
|
+
* the breadth-first traversal of a binary tree.
|
|
968
1133
|
*/
|
|
969
1134
|
bfs(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
970
1135
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
@@ -1019,20 +1184,29 @@ class BinaryTree {
|
|
|
1019
1184
|
return ans;
|
|
1020
1185
|
}
|
|
1021
1186
|
/**
|
|
1022
|
-
*
|
|
1023
|
-
*
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
*
|
|
1027
|
-
*
|
|
1028
|
-
*
|
|
1029
|
-
*
|
|
1030
|
-
*
|
|
1031
|
-
*
|
|
1032
|
-
* @param
|
|
1033
|
-
*
|
|
1034
|
-
*
|
|
1035
|
-
*
|
|
1187
|
+
* Time complexity: O(n)
|
|
1188
|
+
* Space complexity: O(n)
|
|
1189
|
+
*/
|
|
1190
|
+
/**
|
|
1191
|
+
* Time complexity: O(n)
|
|
1192
|
+
* Space complexity: O(n)
|
|
1193
|
+
*
|
|
1194
|
+
* The `listLevels` function returns an array of arrays, where each inner array represents a level in
|
|
1195
|
+
* a binary tree and contains the values returned by a callback function applied to the nodes at that
|
|
1196
|
+
* level.
|
|
1197
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
1198
|
+
* the tree. It takes a single parameter, which can be of type `N`, `null`, or `undefined`, and
|
|
1199
|
+
* returns a value of any type.
|
|
1200
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
1201
|
+
* starting node for traversing the tree. It can be either a node object (`N`), a key value
|
|
1202
|
+
* (`BTNKey`), `null`, or `undefined`. If not provided, it defaults to the root node of the tree.
|
|
1203
|
+
* @param iterationType - The `iterationType` parameter determines the type of iteration to be
|
|
1204
|
+
* performed on the tree. It can have two possible values:
|
|
1205
|
+
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
1206
|
+
* whether or not to include null values in the resulting levels. If `includeNull` is set to `true`,
|
|
1207
|
+
* null values will be included in the levels. If `includeNull` is set to `false`, null values will
|
|
1208
|
+
* be excluded
|
|
1209
|
+
* @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
|
|
1036
1210
|
*/
|
|
1037
1211
|
listLevels(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
1038
1212
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
@@ -1084,9 +1258,10 @@ class BinaryTree {
|
|
|
1084
1258
|
return levelsNodes;
|
|
1085
1259
|
}
|
|
1086
1260
|
/**
|
|
1087
|
-
* The function returns the predecessor node of a given node in a binary tree.
|
|
1088
|
-
* @param {N} node - The
|
|
1089
|
-
*
|
|
1261
|
+
* The function `getPredecessor` returns the predecessor node of a given node in a binary tree.
|
|
1262
|
+
* @param {BTNKey | N | null | undefined} node - The `node` parameter can be of type `BTNKey`, `N`,
|
|
1263
|
+
* `null`, or `undefined`.
|
|
1264
|
+
* @returns The function `getPredecessor` returns a value of type `N | undefined`.
|
|
1090
1265
|
*/
|
|
1091
1266
|
getPredecessor(node) {
|
|
1092
1267
|
node = this.ensureNotKey(node);
|
|
@@ -1106,11 +1281,10 @@ class BinaryTree {
|
|
|
1106
1281
|
}
|
|
1107
1282
|
}
|
|
1108
1283
|
/**
|
|
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`.
|
|
1284
|
+
* The function `getSuccessor` returns the next node in a binary tree given a current node.
|
|
1285
|
+
* @param {BTNKey | N | null} [x] - The parameter `x` can be of type `BTNKey`, `N`, or `null`.
|
|
1286
|
+
* @returns the successor of the given node or key. The successor is the node that comes immediately
|
|
1287
|
+
* after the given node in the inorder traversal of the binary tree.
|
|
1114
1288
|
*/
|
|
1115
1289
|
getSuccessor(x) {
|
|
1116
1290
|
x = this.ensureNotKey(x);
|
|
@@ -1127,18 +1301,26 @@ class BinaryTree {
|
|
|
1127
1301
|
return y;
|
|
1128
1302
|
}
|
|
1129
1303
|
/**
|
|
1130
|
-
*
|
|
1131
|
-
*
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
*
|
|
1304
|
+
* Time complexity: O(n)
|
|
1305
|
+
* Space complexity: O(1)
|
|
1306
|
+
*/
|
|
1307
|
+
/**
|
|
1308
|
+
* Time complexity: O(n)
|
|
1309
|
+
* Space complexity: O(1)
|
|
1310
|
+
* The `morris` function performs a depth-first traversal on a binary tree using the Morris traversal
|
|
1311
|
+
* algorithm.
|
|
1312
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
1313
|
+
* the tree. It takes a single parameter of type `N` (the type of the nodes in the tree) and returns
|
|
1314
|
+
* a value of any type.
|
|
1135
1315
|
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
|
|
1136
1316
|
* determines the order in which the nodes of a binary tree are traversed. It can have one of the
|
|
1137
1317
|
* following values:
|
|
1138
|
-
* @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
|
|
1139
|
-
* traversal. It
|
|
1140
|
-
*
|
|
1141
|
-
* @returns The `morris`
|
|
1318
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
|
|
1319
|
+
* for the traversal. It can be specified as a key, a node object, or `null`/`undefined` to indicate
|
|
1320
|
+
* the root of the tree. If no value is provided, the default value is the root of the tree.
|
|
1321
|
+
* @returns The function `morris` returns an array of values that are the result of invoking the
|
|
1322
|
+
* `callback` function on each node in the binary tree. The type of the array elements is determined
|
|
1323
|
+
* by the return type of the `callback` function.
|
|
1142
1324
|
*/
|
|
1143
1325
|
morris(callback = this._defaultOneParamCallback, pattern = 'in', beginRoot = this.root) {
|
|
1144
1326
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
@@ -1225,7 +1407,6 @@ class BinaryTree {
|
|
|
1225
1407
|
}
|
|
1226
1408
|
return ans;
|
|
1227
1409
|
}
|
|
1228
|
-
// --- start additional methods ---
|
|
1229
1410
|
/**
|
|
1230
1411
|
* The above function is an iterator for a binary tree that can be used to traverse the tree in
|
|
1231
1412
|
* either an iterative or recursive manner.
|
|
@@ -1256,12 +1437,10 @@ class BinaryTree {
|
|
|
1256
1437
|
}
|
|
1257
1438
|
else {
|
|
1258
1439
|
if (node.left) {
|
|
1259
|
-
// @ts-ignore
|
|
1260
1440
|
yield* this[Symbol.iterator](node.left);
|
|
1261
1441
|
}
|
|
1262
1442
|
yield node.key;
|
|
1263
1443
|
if (node.right) {
|
|
1264
|
-
// @ts-ignore
|
|
1265
1444
|
yield* this[Symbol.iterator](node.right);
|
|
1266
1445
|
}
|
|
1267
1446
|
}
|
|
@@ -1341,9 +1520,9 @@ class BinaryTree {
|
|
|
1341
1520
|
}
|
|
1342
1521
|
/**
|
|
1343
1522
|
* 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
|
-
*
|
|
1523
|
+
* @param {N | null | undefined} root - The `root` parameter is of type `BTNKey | N | null |
|
|
1524
|
+
* undefined`. It represents the root node of a binary tree. The root node can have one of the
|
|
1525
|
+
* following types:
|
|
1347
1526
|
*/
|
|
1348
1527
|
print(beginRoot = this.root) {
|
|
1349
1528
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
@@ -1356,17 +1535,17 @@ class BinaryTree {
|
|
|
1356
1535
|
}
|
|
1357
1536
|
};
|
|
1358
1537
|
const _displayAux = (node) => {
|
|
1359
|
-
if (node
|
|
1538
|
+
if (!this.isRealNode(node)) {
|
|
1360
1539
|
return [[], 0, 0, 0];
|
|
1361
1540
|
}
|
|
1362
|
-
if (node && node.right
|
|
1541
|
+
if (this.isRealNode(node) && !this.isRealNode(node.right) && !this.isRealNode(node.left)) {
|
|
1363
1542
|
const line = `${node.key}`;
|
|
1364
1543
|
const width = line.length;
|
|
1365
1544
|
const height = 1;
|
|
1366
1545
|
const middle = Math.floor(width / 2);
|
|
1367
1546
|
return [[line], width, height, middle];
|
|
1368
1547
|
}
|
|
1369
|
-
if (node && node.right
|
|
1548
|
+
if (this.isRealNode(node) && !this.isRealNode(node.right)) {
|
|
1370
1549
|
const [lines, n, p, x] = _displayAux(node.left);
|
|
1371
1550
|
const s = `${node.key}`;
|
|
1372
1551
|
const u = s.length;
|
|
@@ -1375,7 +1554,7 @@ class BinaryTree {
|
|
|
1375
1554
|
const shifted_lines = lines.map(line => line + ' '.repeat(u));
|
|
1376
1555
|
return [[first_line, second_line, ...shifted_lines], n + u, p + 2, n + Math.floor(u / 2)];
|
|
1377
1556
|
}
|
|
1378
|
-
if (node && node.left
|
|
1557
|
+
if (this.isRealNode(node) && !this.isRealNode(node.left)) {
|
|
1379
1558
|
const [lines, n, p, u] = _displayAux(node.right);
|
|
1380
1559
|
const s = `${node.key}`;
|
|
1381
1560
|
const x = s.length;
|