data-structure-typed 1.42.8 → 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/CHANGELOG.md +1 -1
- package/README.md +12 -12
- package/benchmark/report.html +12 -12
- package/benchmark/report.json +106 -106
- package/dist/cjs/src/data-structures/binary-tree/avl-tree.d.ts +88 -23
- package/dist/cjs/src/data-structures/binary-tree/avl-tree.js +88 -23
- package/dist/cjs/src/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/src/data-structures/binary-tree/binary-tree.d.ts +180 -74
- package/dist/cjs/src/data-structures/binary-tree/binary-tree.js +388 -201
- package/dist/cjs/src/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/src/data-structures/binary-tree/bst.d.ts +121 -66
- package/dist/cjs/src/data-structures/binary-tree/bst.js +121 -67
- package/dist/cjs/src/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/src/data-structures/binary-tree/rb-tree.d.ts +72 -5
- package/dist/cjs/src/data-structures/binary-tree/rb-tree.js +95 -18
- package/dist/cjs/src/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/src/data-structures/binary-tree/tree-multimap.d.ts +82 -43
- package/dist/cjs/src/data-structures/binary-tree/tree-multimap.js +82 -43
- package/dist/cjs/src/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/src/data-structures/graph/abstract-graph.d.ts +139 -36
- package/dist/cjs/src/data-structures/graph/abstract-graph.js +147 -36
- package/dist/cjs/src/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/src/data-structures/graph/directed-graph.d.ts +126 -0
- package/dist/cjs/src/data-structures/graph/directed-graph.js +126 -0
- package/dist/cjs/src/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/src/data-structures/graph/undirected-graph.d.ts +63 -0
- package/dist/cjs/src/data-structures/graph/undirected-graph.js +63 -0
- package/dist/cjs/src/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/src/data-structures/heap/heap.d.ts +175 -12
- package/dist/cjs/src/data-structures/heap/heap.js +175 -12
- package/dist/cjs/src/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/src/data-structures/linked-list/doubly-linked-list.d.ts +203 -0
- package/dist/cjs/src/data-structures/linked-list/doubly-linked-list.js +203 -0
- package/dist/cjs/src/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/src/data-structures/linked-list/singly-linked-list.d.ts +182 -0
- package/dist/cjs/src/data-structures/linked-list/singly-linked-list.js +182 -0
- package/dist/cjs/src/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/src/data-structures/linked-list/skip-linked-list.d.ts +64 -0
- package/dist/cjs/src/data-structures/linked-list/skip-linked-list.js +64 -0
- package/dist/cjs/src/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/src/data-structures/queue/deque.d.ts +113 -3
- package/dist/cjs/src/data-structures/queue/deque.js +113 -3
- package/dist/cjs/src/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/src/data-structures/queue/queue.d.ts +87 -0
- package/dist/cjs/src/data-structures/queue/queue.js +87 -0
- package/dist/cjs/src/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/src/data-structures/stack/stack.d.ts +42 -0
- package/dist/cjs/src/data-structures/stack/stack.js +42 -0
- package/dist/cjs/src/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/src/data-structures/trie/trie.d.ts +76 -0
- package/dist/cjs/src/data-structures/trie/trie.js +76 -1
- package/dist/cjs/src/data-structures/trie/trie.js.map +1 -1
- package/dist/mjs/src/data-structures/binary-tree/avl-tree.d.ts +88 -23
- package/dist/mjs/src/data-structures/binary-tree/avl-tree.js +88 -23
- package/dist/mjs/src/data-structures/binary-tree/binary-tree.d.ts +180 -74
- package/dist/mjs/src/data-structures/binary-tree/binary-tree.js +388 -201
- package/dist/mjs/src/data-structures/binary-tree/bst.d.ts +121 -66
- package/dist/mjs/src/data-structures/binary-tree/bst.js +121 -67
- package/dist/mjs/src/data-structures/binary-tree/rb-tree.d.ts +72 -5
- package/dist/mjs/src/data-structures/binary-tree/rb-tree.js +95 -18
- package/dist/mjs/src/data-structures/binary-tree/tree-multimap.d.ts +82 -43
- package/dist/mjs/src/data-structures/binary-tree/tree-multimap.js +82 -43
- package/dist/mjs/src/data-structures/graph/abstract-graph.d.ts +139 -36
- package/dist/mjs/src/data-structures/graph/abstract-graph.js +147 -36
- package/dist/mjs/src/data-structures/graph/directed-graph.d.ts +126 -0
- package/dist/mjs/src/data-structures/graph/directed-graph.js +126 -0
- package/dist/mjs/src/data-structures/graph/undirected-graph.d.ts +63 -0
- package/dist/mjs/src/data-structures/graph/undirected-graph.js +63 -0
- package/dist/mjs/src/data-structures/heap/heap.d.ts +175 -12
- package/dist/mjs/src/data-structures/heap/heap.js +175 -12
- package/dist/mjs/src/data-structures/linked-list/doubly-linked-list.d.ts +203 -0
- package/dist/mjs/src/data-structures/linked-list/doubly-linked-list.js +203 -0
- package/dist/mjs/src/data-structures/linked-list/singly-linked-list.d.ts +182 -0
- package/dist/mjs/src/data-structures/linked-list/singly-linked-list.js +182 -0
- package/dist/mjs/src/data-structures/linked-list/skip-linked-list.d.ts +64 -0
- package/dist/mjs/src/data-structures/linked-list/skip-linked-list.js +64 -0
- package/dist/mjs/src/data-structures/queue/deque.d.ts +113 -3
- package/dist/mjs/src/data-structures/queue/deque.js +113 -3
- package/dist/mjs/src/data-structures/queue/queue.d.ts +87 -0
- package/dist/mjs/src/data-structures/queue/queue.js +87 -0
- package/dist/mjs/src/data-structures/stack/stack.d.ts +42 -0
- package/dist/mjs/src/data-structures/stack/stack.js +42 -0
- package/dist/mjs/src/data-structures/trie/trie.d.ts +76 -0
- package/dist/mjs/src/data-structures/trie/trie.js +76 -1
- package/dist/umd/data-structure-typed.min.js +1 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- 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
- package/test/integration/index.html +24 -2
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +19 -2
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +0 -1
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +0 -1
|
@@ -132,10 +132,22 @@ class BinaryTree {
|
|
|
132
132
|
return new BinaryTreeNode(key, value);
|
|
133
133
|
}
|
|
134
134
|
/**
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
|
|
135
|
+
* Time Complexity: O(n)
|
|
136
|
+
* Space Complexity: O(1)
|
|
137
|
+
* 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.
|
|
138
|
+
*/
|
|
139
|
+
/**
|
|
140
|
+
* Time Complexity: O(n)
|
|
141
|
+
* Space Complexity: O(1)
|
|
142
|
+
*
|
|
143
|
+
* The `add` function adds a new node with a key and value to a binary tree, or updates the value of
|
|
144
|
+
* an existing node with the same key.
|
|
145
|
+
* @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
|
|
146
|
+
* following types:
|
|
147
|
+
* @param {V} [value] - The value to be associated with the key or node being added to the binary
|
|
148
|
+
* tree.
|
|
149
|
+
* @returns The function `add` returns a node (`N`) if it was successfully inserted into the binary
|
|
150
|
+
* tree, or `null` or `undefined` if the insertion was not successful.
|
|
139
151
|
*/
|
|
140
152
|
add(keyOrNode, value) {
|
|
141
153
|
const _bfs = (root, newNode) => {
|
|
@@ -189,13 +201,21 @@ class BinaryTree {
|
|
|
189
201
|
return inserted;
|
|
190
202
|
}
|
|
191
203
|
/**
|
|
192
|
-
*
|
|
193
|
-
*
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
*
|
|
197
|
-
*
|
|
198
|
-
*
|
|
204
|
+
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
205
|
+
* Space Complexity: O(1)
|
|
206
|
+
*/
|
|
207
|
+
/**
|
|
208
|
+
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
209
|
+
* Space Complexity: O(1)
|
|
210
|
+
*
|
|
211
|
+
* The `addMany` function takes an array of keys or nodes and an optional array of values, and adds
|
|
212
|
+
* each key-value pair to a data structure.
|
|
213
|
+
* @param {(BTNKey | N |null | undefined)[]} keysOrNodes - An array of keys or nodes to be added to
|
|
214
|
+
* the binary search tree. Each element can be of type `BTNKey` (a key value), `N` (a node), `null`,
|
|
215
|
+
* or `undefined`.
|
|
216
|
+
* @param {(V | undefined)[]} [values] - The `values` parameter is an optional array of values that
|
|
217
|
+
* correspond to the keys or nodes being added. If provided, the values will be associated with the
|
|
218
|
+
* keys or nodes during the add operation.
|
|
199
219
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
200
220
|
*/
|
|
201
221
|
addMany(keysOrNodes, values) {
|
|
@@ -212,6 +232,13 @@ class BinaryTree {
|
|
|
212
232
|
});
|
|
213
233
|
}
|
|
214
234
|
/**
|
|
235
|
+
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
236
|
+
* Space Complexity: O(1)
|
|
237
|
+
*/
|
|
238
|
+
/**
|
|
239
|
+
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
240
|
+
* Space Complexity: O(1)
|
|
241
|
+
*
|
|
215
242
|
* The `refill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
|
|
216
243
|
* @param {(BTNKey | N)[]} keysOrNodes - The `keysOrNodes` parameter is an array that can contain either
|
|
217
244
|
* `BTNKey` or `N` values.
|
|
@@ -225,18 +252,23 @@ class BinaryTree {
|
|
|
225
252
|
return keysOrNodes.length === this.addMany(keysOrNodes, values).length;
|
|
226
253
|
}
|
|
227
254
|
/**
|
|
228
|
-
*
|
|
229
|
-
*
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
*
|
|
233
|
-
*
|
|
234
|
-
*
|
|
235
|
-
*
|
|
236
|
-
*
|
|
237
|
-
*
|
|
238
|
-
*
|
|
239
|
-
*
|
|
255
|
+
* Time Complexity: O(n)
|
|
256
|
+
* Space Complexity: O(1)
|
|
257
|
+
*/
|
|
258
|
+
/**
|
|
259
|
+
* Time Complexity: O(n)
|
|
260
|
+
* Space Complexity: O(1)
|
|
261
|
+
*
|
|
262
|
+
* The function deletes a node from a binary tree and returns an array of the deleted nodes along
|
|
263
|
+
* with the nodes that need to be balanced.
|
|
264
|
+
* @param {ReturnType<C> | null | undefined} identifier - The identifier parameter is the value or
|
|
265
|
+
* object that you want to delete from the binary tree. It can be of any type that is compatible with
|
|
266
|
+
* the callback function's return type. It can also be null or undefined if you want to delete a
|
|
267
|
+
* specific node based on its value or object.
|
|
268
|
+
* @param {C} callback - The `callback` parameter is a function that is used to determine the
|
|
269
|
+
* identifier of the node to be deleted. It is optional and has a default value of
|
|
270
|
+
* `this._defaultOneParamCallback`. The `callback` function should return the identifier of the node.
|
|
271
|
+
* @returns an array of `BiTreeDeleteResult<N>`.
|
|
240
272
|
*/
|
|
241
273
|
delete(identifier, callback = this._defaultOneParamCallback) {
|
|
242
274
|
const deletedResult = [];
|
|
@@ -285,15 +317,20 @@ class BinaryTree {
|
|
|
285
317
|
return deletedResult;
|
|
286
318
|
}
|
|
287
319
|
/**
|
|
288
|
-
*
|
|
289
|
-
*
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
*
|
|
293
|
-
*
|
|
294
|
-
*
|
|
295
|
-
* of a node in
|
|
296
|
-
*
|
|
320
|
+
* Time Complexity: O(n)
|
|
321
|
+
* Space Complexity: O(1)
|
|
322
|
+
*/
|
|
323
|
+
/**
|
|
324
|
+
* Time Complexity: O(n)
|
|
325
|
+
* Space Complexity: O(1)
|
|
326
|
+
*
|
|
327
|
+
* The function calculates the depth of a given node in a binary tree.
|
|
328
|
+
* @param {BTNKey | N | null | undefined} distNode - The `distNode` parameter represents the node in
|
|
329
|
+
* the binary tree whose depth we want to find. It can be of type `BTNKey`, `N`, `null`, or
|
|
330
|
+
* `undefined`.
|
|
331
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
|
|
332
|
+
* from which we want to calculate the depth. It can be either a `BTNKey` (binary tree node key) or
|
|
333
|
+
* `N` (binary tree node) or `null` or `undefined`. If no value is provided for `beginRoot
|
|
297
334
|
* @returns the depth of the `distNode` relative to the `beginRoot`.
|
|
298
335
|
*/
|
|
299
336
|
getDepth(distNode, beginRoot = this.root) {
|
|
@@ -310,15 +347,22 @@ class BinaryTree {
|
|
|
310
347
|
return depth;
|
|
311
348
|
}
|
|
312
349
|
/**
|
|
313
|
-
*
|
|
314
|
-
*
|
|
350
|
+
* Time Complexity: O(n)
|
|
351
|
+
* Space Complexity: O(log n)
|
|
352
|
+
* Best Case - O(log n) (when using recursive iterationType), Worst Case - O(n) (when using iterative iterationType)
|
|
353
|
+
*/
|
|
354
|
+
/**
|
|
355
|
+
* Time Complexity: O(n)
|
|
356
|
+
* Space Complexity: O(log n)
|
|
357
|
+
*
|
|
358
|
+
* The function `getHeight` calculates the maximum height of a binary tree using either recursive or
|
|
359
|
+
* iterative traversal.
|
|
315
360
|
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
316
|
-
* starting node from which
|
|
317
|
-
*
|
|
318
|
-
* node is specified. If `
|
|
361
|
+
* starting node of the binary tree from which we want to calculate the height. It can be of type
|
|
362
|
+
* `BTNKey`, `N`, `null`, or `undefined`. If not provided, it defaults to `this.root`.
|
|
319
363
|
* @param iterationType - The `iterationType` parameter is used to determine whether to calculate the
|
|
320
|
-
* height of the
|
|
321
|
-
*
|
|
364
|
+
* height of the tree using a recursive approach or an iterative approach. It can have two possible
|
|
365
|
+
* values:
|
|
322
366
|
* @returns the height of the binary tree.
|
|
323
367
|
*/
|
|
324
368
|
getHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
@@ -353,11 +397,19 @@ class BinaryTree {
|
|
|
353
397
|
}
|
|
354
398
|
}
|
|
355
399
|
/**
|
|
400
|
+
* Time Complexity: O(n)
|
|
401
|
+
* Space Complexity: O(log n)
|
|
402
|
+
* Best Case - O(log n) (when using recursive iterationType), Worst Case - O(n) (when using iterative iterationType)
|
|
403
|
+
*/
|
|
404
|
+
/**
|
|
405
|
+
* Time Complexity: O(n)
|
|
406
|
+
* Space Complexity: O(log n)
|
|
407
|
+
*
|
|
356
408
|
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
357
409
|
* recursive or iterative approach.
|
|
358
|
-
* @param {N | null | undefined} beginRoot - The `beginRoot` parameter
|
|
359
|
-
*
|
|
360
|
-
*
|
|
410
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
411
|
+
* starting node of the binary tree from which we want to calculate the minimum height. It can be of
|
|
412
|
+
* type `BTNKey`, `N`, `null`, or `undefined`. If no value is provided, it defaults to `this.root`.
|
|
361
413
|
* @param iterationType - The `iterationType` parameter is used to determine the method of iteration
|
|
362
414
|
* to calculate the minimum height of a binary tree. It can have two possible values:
|
|
363
415
|
* @returns The function `getMinHeight` returns the minimum height of a binary tree.
|
|
@@ -407,35 +459,51 @@ class BinaryTree {
|
|
|
407
459
|
}
|
|
408
460
|
}
|
|
409
461
|
/**
|
|
462
|
+
* Time Complexity: O(n)
|
|
463
|
+
* Space Complexity: O(log n)
|
|
464
|
+
*/
|
|
465
|
+
/**
|
|
466
|
+
* Time Complexity: O(n)
|
|
467
|
+
* Space Complexity: O(log n)
|
|
468
|
+
*
|
|
410
469
|
* The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
|
|
411
470
|
* height of the tree.
|
|
412
|
-
* @param {N | null | undefined} beginRoot - The
|
|
413
|
-
*
|
|
414
|
-
*
|
|
471
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
|
|
472
|
+
* for calculating the height and minimum height of a binary tree. It can be either a `BTNKey` (a key
|
|
473
|
+
* value of a binary tree node), `N` (a node of a binary tree), `null`, or `undefined`. If
|
|
474
|
+
* @returns a boolean value.
|
|
415
475
|
*/
|
|
416
476
|
isPerfectlyBalanced(beginRoot = this.root) {
|
|
417
477
|
return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
|
|
418
478
|
}
|
|
419
479
|
/**
|
|
420
|
-
*
|
|
421
|
-
*
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
*
|
|
425
|
-
*
|
|
426
|
-
*
|
|
427
|
-
*
|
|
428
|
-
*
|
|
429
|
-
* @param
|
|
430
|
-
*
|
|
431
|
-
*
|
|
432
|
-
*
|
|
433
|
-
* @param {
|
|
434
|
-
*
|
|
435
|
-
*
|
|
480
|
+
* Time Complexity: O(n)
|
|
481
|
+
* Space Complexity: O(log n).
|
|
482
|
+
*/
|
|
483
|
+
/**
|
|
484
|
+
* Time Complexity: O(n)
|
|
485
|
+
* Space Complexity: O(log n).
|
|
486
|
+
*
|
|
487
|
+
* The function `getNodes` retrieves nodes from a binary tree based on a given identifier and
|
|
488
|
+
* callback function.
|
|
489
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
490
|
+
* that you want to search for in the binary tree. It can be of any type that is returned by the
|
|
491
|
+
* callback function `C`. It can also be `null` or `undefined` if you don't want to search for a
|
|
492
|
+
* specific value.
|
|
493
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node of type `N` as
|
|
494
|
+
* input and returns a value of type `C`. It is used to determine if a node matches the given
|
|
495
|
+
* identifier. If no callback is provided, the `_defaultOneParamCallback` function is used as the
|
|
496
|
+
* default
|
|
497
|
+
* @param [onlyOne=false] - A boolean value indicating whether to only return the first node that
|
|
498
|
+
* matches the identifier. If set to true, the function will stop iterating once it finds a matching
|
|
499
|
+
* node and return that node. If set to false (default), the function will continue iterating and
|
|
500
|
+
* return all nodes that match the identifier.
|
|
501
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
502
|
+
* starting node for the traversal. It can be either a key, a node object, or `null`/`undefined`. If
|
|
503
|
+
* it is `null` or `undefined`, an empty array will be returned.
|
|
436
504
|
* @param iterationType - The `iterationType` parameter determines the type of iteration used to
|
|
437
505
|
* traverse the binary tree. It can have two possible values:
|
|
438
|
-
* @returns
|
|
506
|
+
* @returns an array of nodes of type `N`.
|
|
439
507
|
*/
|
|
440
508
|
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
441
509
|
if (!beginRoot)
|
|
@@ -478,20 +546,27 @@ class BinaryTree {
|
|
|
478
546
|
return ans;
|
|
479
547
|
}
|
|
480
548
|
/**
|
|
481
|
-
*
|
|
482
|
-
*
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
*
|
|
486
|
-
*
|
|
487
|
-
*
|
|
488
|
-
*
|
|
489
|
-
*
|
|
490
|
-
*
|
|
491
|
-
*
|
|
492
|
-
*
|
|
493
|
-
*
|
|
494
|
-
*
|
|
549
|
+
* Time Complexity: O(n)
|
|
550
|
+
* Space Complexity: O(log n).
|
|
551
|
+
*/
|
|
552
|
+
/**
|
|
553
|
+
* Time Complexity: O(n)
|
|
554
|
+
*
|
|
555
|
+
* The function checks if a Binary Tree Node with a specific identifier exists in the tree.
|
|
556
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
557
|
+
* that you want to search for in the binary tree. It can be of any type that is returned by the
|
|
558
|
+
* callback function `C`. It can also be `null` or `undefined` if you don't want to specify a
|
|
559
|
+
* specific identifier.
|
|
560
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
561
|
+
* the binary tree. It is used to filter the nodes based on certain conditions. The `callback`
|
|
562
|
+
* function should return a boolean value indicating whether the node should be included in the
|
|
563
|
+
* result or not.
|
|
564
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
|
|
565
|
+
* for the search in the binary tree. It can be specified as a `BTNKey` (a unique identifier for a
|
|
566
|
+
* node in the binary tree), a node object (`N`), or `null`/`undefined` to start the search from
|
|
567
|
+
* @param iterationType - The `iterationType` parameter is a variable that determines the type of
|
|
568
|
+
* iteration to be performed on the binary tree. It is used to specify whether the iteration should
|
|
569
|
+
* be performed in a pre-order, in-order, or post-order manner.
|
|
495
570
|
* @returns a boolean value.
|
|
496
571
|
*/
|
|
497
572
|
has(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
@@ -500,19 +575,29 @@ class BinaryTree {
|
|
|
500
575
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
|
|
501
576
|
}
|
|
502
577
|
/**
|
|
503
|
-
*
|
|
504
|
-
*
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
*
|
|
508
|
-
*
|
|
509
|
-
*
|
|
510
|
-
*
|
|
511
|
-
*
|
|
512
|
-
*
|
|
513
|
-
*
|
|
514
|
-
*
|
|
515
|
-
*
|
|
578
|
+
* Time Complexity: O(n)
|
|
579
|
+
* Space Complexity: O(log n)
|
|
580
|
+
*/
|
|
581
|
+
/**
|
|
582
|
+
* Time Complexity: O(n)
|
|
583
|
+
* Space Complexity: O(log n)
|
|
584
|
+
*
|
|
585
|
+
* The function `getNode` returns the first node that matches the given identifier and callback
|
|
586
|
+
* function.
|
|
587
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
588
|
+
* used to identify the node you want to retrieve. It can be of any type that is returned by the
|
|
589
|
+
* callback function `C`. It can also be `null` or `undefined` if you don't have a specific
|
|
590
|
+
* identifier.
|
|
591
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
592
|
+
* the binary tree. It is used to determine if a node matches the given identifier. The `callback`
|
|
593
|
+
* function should take a single parameter of type `N` (the type of the nodes in the binary tree) and
|
|
594
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
|
|
595
|
+
* for searching the binary tree. It can be either a key value, a node object, or `null`/`undefined`.
|
|
596
|
+
* If `null` or `undefined` is passed, the search will start from the root of the binary tree.
|
|
597
|
+
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
598
|
+
* be performed when searching for nodes in the binary tree. It determines the order in which the
|
|
599
|
+
* nodes are visited during the search.
|
|
600
|
+
* @returns a value of type `N | null | undefined`.
|
|
516
601
|
*/
|
|
517
602
|
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
518
603
|
if (identifier instanceof BinaryTreeNode)
|
|
@@ -520,6 +605,13 @@ class BinaryTree {
|
|
|
520
605
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
|
|
521
606
|
}
|
|
522
607
|
/**
|
|
608
|
+
* Time Complexity: O(n)
|
|
609
|
+
* Space Complexity: O(log n)
|
|
610
|
+
*/
|
|
611
|
+
/**
|
|
612
|
+
* Time Complexity: O(n)
|
|
613
|
+
* Space Complexity: O(log n)
|
|
614
|
+
*
|
|
523
615
|
* The function `getNodeByKey` searches for a node in a binary tree by its key, using either
|
|
524
616
|
* recursive or iterative iteration.
|
|
525
617
|
* @param {BTNKey} key - The `key` parameter is the key value that we are searching for in the tree.
|
|
@@ -574,19 +666,30 @@ class BinaryTree {
|
|
|
574
666
|
return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
575
667
|
}
|
|
576
668
|
/**
|
|
577
|
-
*
|
|
578
|
-
*
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
*
|
|
582
|
-
*
|
|
583
|
-
*
|
|
584
|
-
*
|
|
585
|
-
*
|
|
586
|
-
*
|
|
587
|
-
*
|
|
588
|
-
*
|
|
589
|
-
* @
|
|
669
|
+
* Time Complexity: O(n)
|
|
670
|
+
* Space Complexity: O(log n)
|
|
671
|
+
*/
|
|
672
|
+
/**
|
|
673
|
+
* Time Complexity: O(n)
|
|
674
|
+
* Space Complexity: O(log n)
|
|
675
|
+
*
|
|
676
|
+
* The function `get` retrieves the value of a node in a binary tree based on the provided identifier
|
|
677
|
+
* and callback function.
|
|
678
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
679
|
+
* used to identify the node in the binary tree. It can be of any type that is the return type of the
|
|
680
|
+
* callback function `C`. It can also be `null` or `undefined` if no identifier is provided.
|
|
681
|
+
* @param {C} callback - The `callback` parameter is a function that will be called with each node in
|
|
682
|
+
* the binary tree. It is used to determine whether a node matches the given identifier. The callback
|
|
683
|
+
* function should return a value that can be compared to the identifier to determine if it is a
|
|
684
|
+
* match.
|
|
685
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
|
|
686
|
+
* for the search in the binary tree. It can be specified as a `BTNKey` (a unique identifier for a
|
|
687
|
+
* node), a node object of type `N`, or `null`/`undefined` to start the search from the root of
|
|
688
|
+
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
689
|
+
* be performed when searching for a node in the binary tree. It is an optional parameter with a
|
|
690
|
+
* default value specified by `this.iterationType`.
|
|
691
|
+
* @returns The value of the node with the given identifier is being returned. If the node is not
|
|
692
|
+
* found, `undefined` is returned.
|
|
590
693
|
*/
|
|
591
694
|
get(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
592
695
|
if (identifier instanceof BinaryTreeNode)
|
|
@@ -608,14 +711,22 @@ class BinaryTree {
|
|
|
608
711
|
return this.size === 0;
|
|
609
712
|
}
|
|
610
713
|
/**
|
|
611
|
-
*
|
|
612
|
-
*
|
|
613
|
-
|
|
614
|
-
|
|
714
|
+
* Time Complexity: O(log n)
|
|
715
|
+
* Space Complexity: O(log n)
|
|
716
|
+
*/
|
|
717
|
+
/**
|
|
718
|
+
* Time Complexity: O(log n)
|
|
719
|
+
* Space Complexity: O(log n)
|
|
720
|
+
*
|
|
721
|
+
* The function `getPathToRoot` returns an array of nodes from a given node to the root of a tree
|
|
722
|
+
* structure, with the option to reverse the order of the nodes.
|
|
723
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
724
|
+
* starting node from which you want to find the path to the root. It can be of type `BTNKey`, `N`,
|
|
725
|
+
* `null`, or `undefined`.
|
|
615
726
|
* @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
|
|
616
727
|
* resulting path should be reversed or not. If `isReverse` is set to `true`, the path will be
|
|
617
|
-
* reversed before returning it. If `isReverse` is set to `false
|
|
618
|
-
* @returns The function `getPathToRoot` returns an array of
|
|
728
|
+
* reversed before returning it. If `isReverse` is set to `false`, the path will be returned as is
|
|
729
|
+
* @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
|
|
619
730
|
*/
|
|
620
731
|
getPathToRoot(beginRoot, isReverse = true) {
|
|
621
732
|
// TODO to support get path through passing key
|
|
@@ -633,15 +744,22 @@ class BinaryTree {
|
|
|
633
744
|
return isReverse ? result.reverse() : result;
|
|
634
745
|
}
|
|
635
746
|
/**
|
|
636
|
-
*
|
|
637
|
-
*
|
|
747
|
+
* Time Complexity: O(log n)
|
|
748
|
+
* Space Complexity: O(1)
|
|
749
|
+
*/
|
|
750
|
+
/**
|
|
751
|
+
* Time Complexity: O(log n)
|
|
752
|
+
* Space Complexity: O(1)
|
|
753
|
+
*
|
|
754
|
+
* The function `getLeftMost` returns the leftmost node in a binary tree, either recursively or
|
|
755
|
+
* iteratively.
|
|
638
756
|
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
|
|
639
|
-
* for finding the leftmost node in a binary tree. It can be either a
|
|
640
|
-
*
|
|
757
|
+
* for finding the leftmost node in a binary tree. It can be either a `BTNKey` (a key value), `N` (a
|
|
758
|
+
* node), `null`, or `undefined`. If not provided, it defaults to `this.root`,
|
|
641
759
|
* @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
|
|
642
760
|
* be performed when finding the leftmost node in a binary tree. It can have two possible values:
|
|
643
|
-
* @returns The function `getLeftMost` returns the leftmost node (`N`) in
|
|
644
|
-
* no leftmost node, it returns `null
|
|
761
|
+
* @returns The function `getLeftMost` returns the leftmost node (`N`) in the binary tree. If there
|
|
762
|
+
* is no leftmost node, it returns `null` or `undefined` depending on the input.
|
|
645
763
|
*/
|
|
646
764
|
getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
647
765
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
@@ -666,15 +784,23 @@ class BinaryTree {
|
|
|
666
784
|
}
|
|
667
785
|
}
|
|
668
786
|
/**
|
|
787
|
+
* Time Complexity: O(log n)
|
|
788
|
+
* Space Complexity: O(1)
|
|
789
|
+
*/
|
|
790
|
+
/**
|
|
791
|
+
* Time Complexity: O(log n)
|
|
792
|
+
* Space Complexity: O(1)
|
|
793
|
+
*
|
|
669
794
|
* The function `getRightMost` returns the rightmost node in a binary tree, either recursively or
|
|
670
795
|
* iteratively.
|
|
671
|
-
* @param {N | null | undefined} beginRoot - The `beginRoot` parameter
|
|
672
|
-
*
|
|
673
|
-
* or `
|
|
674
|
-
*
|
|
675
|
-
*
|
|
676
|
-
*
|
|
677
|
-
* `
|
|
796
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
797
|
+
* starting node from which we want to find the rightmost node. It can be of type `BTNKey`, `N`,
|
|
798
|
+
* `null`, or `undefined`. If not provided, it defaults to `this.root`, which is a property of the
|
|
799
|
+
* current object.
|
|
800
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
801
|
+
* type of iteration to use when finding the rightmost node. It can have one of two values:
|
|
802
|
+
* @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If there
|
|
803
|
+
* is no rightmost node, it returns `null` or `undefined`, depending on the input.
|
|
678
804
|
*/
|
|
679
805
|
getRightMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
680
806
|
// TODO support get right most by passing key in
|
|
@@ -700,13 +826,20 @@ class BinaryTree {
|
|
|
700
826
|
}
|
|
701
827
|
}
|
|
702
828
|
/**
|
|
829
|
+
* Time Complexity: O(n)
|
|
830
|
+
* Space Complexity: O(1)
|
|
831
|
+
*/
|
|
832
|
+
/**
|
|
833
|
+
* Time Complexity: O(n)
|
|
834
|
+
* Space Complexity: O(1)
|
|
835
|
+
*
|
|
703
836
|
* The function `isSubtreeBST` checks if a given binary tree is a valid binary search tree.
|
|
704
|
-
* @param {N} beginRoot - The `beginRoot` parameter
|
|
705
|
-
* to check if it is a
|
|
837
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the root
|
|
838
|
+
* node of the binary search tree (BST) that you want to check if it is a subtree of another BST.
|
|
706
839
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
707
840
|
* type of iteration to use when checking if a subtree is a binary search tree (BST). It can have two
|
|
708
841
|
* possible values:
|
|
709
|
-
* @returns
|
|
842
|
+
* @returns a boolean value.
|
|
710
843
|
*/
|
|
711
844
|
isSubtreeBST(beginRoot, iterationType = this.iterationType) {
|
|
712
845
|
// TODO there is a bug
|
|
@@ -741,11 +874,18 @@ class BinaryTree {
|
|
|
741
874
|
}
|
|
742
875
|
}
|
|
743
876
|
/**
|
|
877
|
+
* Time Complexity: O(n)
|
|
878
|
+
* Space Complexity: O(1)
|
|
879
|
+
*/
|
|
880
|
+
/**
|
|
881
|
+
* Time Complexity: O(n)
|
|
882
|
+
* Space Complexity: O(1)
|
|
883
|
+
*
|
|
744
884
|
* The function checks if a binary tree is a binary search tree.
|
|
745
885
|
* @param iterationType - The parameter "iterationType" is used to specify the type of iteration to
|
|
746
886
|
* be used when checking if the binary tree is a binary search tree (BST). It is an optional
|
|
747
|
-
* parameter with a default value of "this.iterationType". The value of "this.iterationType" is
|
|
748
|
-
*
|
|
887
|
+
* parameter with a default value of "this.iterationType". The value of "this.iterationType" is
|
|
888
|
+
* expected to be
|
|
749
889
|
* @returns a boolean value.
|
|
750
890
|
*/
|
|
751
891
|
isBST(iterationType = this.iterationType) {
|
|
@@ -754,19 +894,30 @@ class BinaryTree {
|
|
|
754
894
|
return this.isSubtreeBST(this.root, iterationType);
|
|
755
895
|
}
|
|
756
896
|
/**
|
|
897
|
+
* Time complexity: O(n)
|
|
898
|
+
* Space complexity: O(log n)
|
|
899
|
+
*/
|
|
900
|
+
/**
|
|
901
|
+
* Time complexity: O(n)
|
|
902
|
+
* Space complexity: O(log n)
|
|
903
|
+
*
|
|
757
904
|
* The function `subTreeTraverse` traverses a binary tree and applies a callback function to each
|
|
758
905
|
* node, either recursively or iteratively.
|
|
759
|
-
* @param callback - The `callback` parameter is a function that will be called
|
|
760
|
-
* subtree traversal. It takes a single
|
|
761
|
-
* returns a value
|
|
762
|
-
*
|
|
763
|
-
*
|
|
764
|
-
*
|
|
765
|
-
*
|
|
906
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
907
|
+
* the subtree traversal. It takes a single parameter, which is the current node being traversed, and
|
|
908
|
+
* returns a value of any type.
|
|
909
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
910
|
+
* starting node or key from which the subtree traversal should begin. It can be of type `BTNKey`,
|
|
911
|
+
* `N`, `null`, or `undefined`. If not provided, the `root` property of the current object is used as
|
|
912
|
+
* the default value.
|
|
766
913
|
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
767
|
-
* performed on the
|
|
768
|
-
* @param includeNull - The
|
|
769
|
-
*
|
|
914
|
+
* performed on the subtree. It can have two possible values:
|
|
915
|
+
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
916
|
+
* whether or not to include null values in the traversal. If `includeNull` is set to `true`, the
|
|
917
|
+
* traversal will include null values, otherwise it will skip them.
|
|
918
|
+
* @returns The function `subTreeTraverse` returns an array of values that are the result of invoking
|
|
919
|
+
* the `callback` function on each node in the subtree. The type of the array elements is determined
|
|
920
|
+
* by the return type of the `callback` function.
|
|
770
921
|
*/
|
|
771
922
|
subTreeTraverse(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
772
923
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
@@ -843,20 +994,31 @@ class BinaryTree {
|
|
|
843
994
|
return typeof potentialKey === 'number';
|
|
844
995
|
}
|
|
845
996
|
/**
|
|
846
|
-
*
|
|
847
|
-
*
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
*
|
|
997
|
+
* Time complexity: O(n)
|
|
998
|
+
* Space complexity: O(n)
|
|
999
|
+
*/
|
|
1000
|
+
/**
|
|
1001
|
+
* Time complexity: O(n)
|
|
1002
|
+
* Space complexity: O(n)
|
|
1003
|
+
*
|
|
1004
|
+
* The `dfs` function performs a depth-first search traversal on a binary tree or graph, based on the
|
|
1005
|
+
* specified pattern and iteration type, and returns an array of values obtained from applying a
|
|
1006
|
+
* callback function to each visited node.
|
|
1007
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
1008
|
+
* the tree during the depth-first search. It takes a single parameter, which can be of type `N`,
|
|
1009
|
+
* `null`, or `undefined`, and returns a value of any type. The default value for this parameter is
|
|
851
1010
|
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter determines the order in which the
|
|
852
|
-
* nodes are
|
|
853
|
-
* @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
|
|
854
|
-
* search. It
|
|
855
|
-
*
|
|
1011
|
+
* nodes are traversed during the depth-first search. It can have one of the following values:
|
|
1012
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
|
|
1013
|
+
* for the depth-first search traversal. It can be specified as a key, a node object, or
|
|
1014
|
+
* `null`/`undefined`. If not provided, the `beginRoot` will default to the root node of the tree.
|
|
856
1015
|
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
857
|
-
* iteration
|
|
858
|
-
* @param includeNull - The
|
|
859
|
-
*
|
|
1016
|
+
* iteration to use when traversing the tree. It can have one of the following values:
|
|
1017
|
+
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
1018
|
+
* whether null or undefined nodes should be included in the traversal. If `includeNull` is set to
|
|
1019
|
+
* `true`, null or undefined nodes will be included in the traversal. If `includeNull` is set to
|
|
1020
|
+
* `false`, null or undefined
|
|
1021
|
+
* @returns an array of values that are the return values of the callback function.
|
|
860
1022
|
*/
|
|
861
1023
|
dfs(callback = this._defaultOneParamCallback, pattern = 'in', beginRoot = this.root, iterationType = types_1.IterationType.ITERATIVE, includeNull = false) {
|
|
862
1024
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
@@ -965,18 +1127,29 @@ class BinaryTree {
|
|
|
965
1127
|
return ans;
|
|
966
1128
|
}
|
|
967
1129
|
/**
|
|
968
|
-
*
|
|
969
|
-
*
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
*
|
|
973
|
-
*
|
|
974
|
-
*
|
|
975
|
-
*
|
|
976
|
-
*
|
|
977
|
-
*
|
|
978
|
-
*
|
|
979
|
-
*
|
|
1130
|
+
* Time complexity: O(n)
|
|
1131
|
+
* Space complexity: O(n)
|
|
1132
|
+
*/
|
|
1133
|
+
/**
|
|
1134
|
+
* Time complexity: O(n)
|
|
1135
|
+
* Space complexity: O(n)
|
|
1136
|
+
*
|
|
1137
|
+
* The `bfs` function performs a breadth-first search traversal on a binary tree, executing a
|
|
1138
|
+
* callback function on each node.
|
|
1139
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
1140
|
+
* the breadth-first search traversal. It takes a single parameter, which is the current node being
|
|
1141
|
+
* visited, and returns a value of any type.
|
|
1142
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
1143
|
+
* starting node for the breadth-first search traversal. It can be specified as a key, a node object,
|
|
1144
|
+
* or `null`/`undefined` to indicate the root of the tree. If not provided, the `root` property of
|
|
1145
|
+
* the class is used as
|
|
1146
|
+
* @param iterationType - The `iterationType` parameter determines the type of iteration to be
|
|
1147
|
+
* performed during the breadth-first search (BFS). It can have two possible values:
|
|
1148
|
+
* @param [includeNull=false] - The `includeNull` parameter is a boolean flag that determines whether
|
|
1149
|
+
* or not to include null values in the breadth-first search traversal. If `includeNull` is set to
|
|
1150
|
+
* `true`, null values will be included in the traversal, otherwise they will be skipped.
|
|
1151
|
+
* @returns an array of values that are the result of invoking the callback function on each node in
|
|
1152
|
+
* the breadth-first traversal of a binary tree.
|
|
980
1153
|
*/
|
|
981
1154
|
bfs(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
982
1155
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
@@ -1031,20 +1204,29 @@ class BinaryTree {
|
|
|
1031
1204
|
return ans;
|
|
1032
1205
|
}
|
|
1033
1206
|
/**
|
|
1034
|
-
*
|
|
1035
|
-
*
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
*
|
|
1039
|
-
*
|
|
1040
|
-
*
|
|
1041
|
-
*
|
|
1042
|
-
*
|
|
1043
|
-
*
|
|
1044
|
-
* @param
|
|
1045
|
-
*
|
|
1046
|
-
*
|
|
1047
|
-
*
|
|
1207
|
+
* Time complexity: O(n)
|
|
1208
|
+
* Space complexity: O(n)
|
|
1209
|
+
*/
|
|
1210
|
+
/**
|
|
1211
|
+
* Time complexity: O(n)
|
|
1212
|
+
* Space complexity: O(n)
|
|
1213
|
+
*
|
|
1214
|
+
* The `listLevels` function returns an array of arrays, where each inner array represents a level in
|
|
1215
|
+
* a binary tree and contains the values returned by a callback function applied to the nodes at that
|
|
1216
|
+
* level.
|
|
1217
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
1218
|
+
* the tree. It takes a single parameter, which can be of type `N`, `null`, or `undefined`, and
|
|
1219
|
+
* returns a value of any type.
|
|
1220
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
1221
|
+
* starting node for traversing the tree. It can be either a node object (`N`), a key value
|
|
1222
|
+
* (`BTNKey`), `null`, or `undefined`. If not provided, it defaults to the root node of the tree.
|
|
1223
|
+
* @param iterationType - The `iterationType` parameter determines the type of iteration to be
|
|
1224
|
+
* performed on the tree. It can have two possible values:
|
|
1225
|
+
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
1226
|
+
* whether or not to include null values in the resulting levels. If `includeNull` is set to `true`,
|
|
1227
|
+
* null values will be included in the levels. If `includeNull` is set to `false`, null values will
|
|
1228
|
+
* be excluded
|
|
1229
|
+
* @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
|
|
1048
1230
|
*/
|
|
1049
1231
|
listLevels(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
1050
1232
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
@@ -1096,9 +1278,10 @@ class BinaryTree {
|
|
|
1096
1278
|
return levelsNodes;
|
|
1097
1279
|
}
|
|
1098
1280
|
/**
|
|
1099
|
-
* The function returns the predecessor node of a given node in a binary tree.
|
|
1100
|
-
* @param {N} node - The
|
|
1101
|
-
*
|
|
1281
|
+
* The function `getPredecessor` returns the predecessor node of a given node in a binary tree.
|
|
1282
|
+
* @param {BTNKey | N | null | undefined} node - The `node` parameter can be of type `BTNKey`, `N`,
|
|
1283
|
+
* `null`, or `undefined`.
|
|
1284
|
+
* @returns The function `getPredecessor` returns a value of type `N | undefined`.
|
|
1102
1285
|
*/
|
|
1103
1286
|
getPredecessor(node) {
|
|
1104
1287
|
node = this.ensureNotKey(node);
|
|
@@ -1118,11 +1301,10 @@ class BinaryTree {
|
|
|
1118
1301
|
}
|
|
1119
1302
|
}
|
|
1120
1303
|
/**
|
|
1121
|
-
* The function `getSuccessor` returns the next node in a binary tree given a node
|
|
1122
|
-
* `x`
|
|
1123
|
-
* @
|
|
1124
|
-
*
|
|
1125
|
-
* if there is no successor, or `undefined` if the input `x` is `undefined`.
|
|
1304
|
+
* The function `getSuccessor` returns the next node in a binary tree given a current node.
|
|
1305
|
+
* @param {BTNKey | N | null} [x] - The parameter `x` can be of type `BTNKey`, `N`, or `null`.
|
|
1306
|
+
* @returns the successor of the given node or key. The successor is the node that comes immediately
|
|
1307
|
+
* after the given node in the inorder traversal of the binary tree.
|
|
1126
1308
|
*/
|
|
1127
1309
|
getSuccessor(x) {
|
|
1128
1310
|
x = this.ensureNotKey(x);
|
|
@@ -1139,18 +1321,26 @@ class BinaryTree {
|
|
|
1139
1321
|
return y;
|
|
1140
1322
|
}
|
|
1141
1323
|
/**
|
|
1142
|
-
*
|
|
1143
|
-
*
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
*
|
|
1324
|
+
* Time complexity: O(n)
|
|
1325
|
+
* Space complexity: O(1)
|
|
1326
|
+
*/
|
|
1327
|
+
/**
|
|
1328
|
+
* Time complexity: O(n)
|
|
1329
|
+
* Space complexity: O(1)
|
|
1330
|
+
* The `morris` function performs a depth-first traversal on a binary tree using the Morris traversal
|
|
1331
|
+
* algorithm.
|
|
1332
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
1333
|
+
* the tree. It takes a single parameter of type `N` (the type of the nodes in the tree) and returns
|
|
1334
|
+
* a value of any type.
|
|
1147
1335
|
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
|
|
1148
1336
|
* determines the order in which the nodes of a binary tree are traversed. It can have one of the
|
|
1149
1337
|
* following values:
|
|
1150
|
-
* @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
|
|
1151
|
-
* traversal. It
|
|
1152
|
-
*
|
|
1153
|
-
* @returns The `morris`
|
|
1338
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
|
|
1339
|
+
* for the traversal. It can be specified as a key, a node object, or `null`/`undefined` to indicate
|
|
1340
|
+
* the root of the tree. If no value is provided, the default value is the root of the tree.
|
|
1341
|
+
* @returns The function `morris` returns an array of values that are the result of invoking the
|
|
1342
|
+
* `callback` function on each node in the binary tree. The type of the array elements is determined
|
|
1343
|
+
* by the return type of the `callback` function.
|
|
1154
1344
|
*/
|
|
1155
1345
|
morris(callback = this._defaultOneParamCallback, pattern = 'in', beginRoot = this.root) {
|
|
1156
1346
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
@@ -1237,7 +1427,6 @@ class BinaryTree {
|
|
|
1237
1427
|
}
|
|
1238
1428
|
return ans;
|
|
1239
1429
|
}
|
|
1240
|
-
// --- start additional methods ---
|
|
1241
1430
|
/**
|
|
1242
1431
|
* The above function is an iterator for a binary tree that can be used to traverse the tree in
|
|
1243
1432
|
* either an iterative or recursive manner.
|
|
@@ -1268,12 +1457,10 @@ class BinaryTree {
|
|
|
1268
1457
|
}
|
|
1269
1458
|
else {
|
|
1270
1459
|
if (node.left) {
|
|
1271
|
-
// @ts-ignore
|
|
1272
1460
|
yield* this[Symbol.iterator](node.left);
|
|
1273
1461
|
}
|
|
1274
1462
|
yield node.key;
|
|
1275
1463
|
if (node.right) {
|
|
1276
|
-
// @ts-ignore
|
|
1277
1464
|
yield* this[Symbol.iterator](node.right);
|
|
1278
1465
|
}
|
|
1279
1466
|
}
|
|
@@ -1354,9 +1541,9 @@ class BinaryTree {
|
|
|
1354
1541
|
}
|
|
1355
1542
|
/**
|
|
1356
1543
|
* The `print` function is used to display a binary tree structure in a visually appealing way.
|
|
1357
|
-
* @param {N | null | undefined} root - The `root` parameter
|
|
1358
|
-
* root node of a binary tree.
|
|
1359
|
-
*
|
|
1544
|
+
* @param {N | null | undefined} root - The `root` parameter is of type `BTNKey | N | null |
|
|
1545
|
+
* undefined`. It represents the root node of a binary tree. The root node can have one of the
|
|
1546
|
+
* following types:
|
|
1360
1547
|
*/
|
|
1361
1548
|
print(beginRoot = this.root) {
|
|
1362
1549
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
@@ -1369,17 +1556,17 @@ class BinaryTree {
|
|
|
1369
1556
|
}
|
|
1370
1557
|
};
|
|
1371
1558
|
const _displayAux = (node) => {
|
|
1372
|
-
if (node
|
|
1559
|
+
if (!this.isRealNode(node)) {
|
|
1373
1560
|
return [[], 0, 0, 0];
|
|
1374
1561
|
}
|
|
1375
|
-
if (node && node.right
|
|
1562
|
+
if (this.isRealNode(node) && !this.isRealNode(node.right) && !this.isRealNode(node.left)) {
|
|
1376
1563
|
const line = `${node.key}`;
|
|
1377
1564
|
const width = line.length;
|
|
1378
1565
|
const height = 1;
|
|
1379
1566
|
const middle = Math.floor(width / 2);
|
|
1380
1567
|
return [[line], width, height, middle];
|
|
1381
1568
|
}
|
|
1382
|
-
if (node && node.right
|
|
1569
|
+
if (this.isRealNode(node) && !this.isRealNode(node.right)) {
|
|
1383
1570
|
const [lines, n, p, x] = _displayAux(node.left);
|
|
1384
1571
|
const s = `${node.key}`;
|
|
1385
1572
|
const u = s.length;
|
|
@@ -1388,7 +1575,7 @@ class BinaryTree {
|
|
|
1388
1575
|
const shifted_lines = lines.map(line => line + ' '.repeat(u));
|
|
1389
1576
|
return [[first_line, second_line, ...shifted_lines], n + u, p + 2, n + Math.floor(u / 2)];
|
|
1390
1577
|
}
|
|
1391
|
-
if (node && node.left
|
|
1578
|
+
if (this.isRealNode(node) && !this.isRealNode(node.left)) {
|
|
1392
1579
|
const [lines, n, p, u] = _displayAux(node.right);
|
|
1393
1580
|
const s = `${node.key}`;
|
|
1394
1581
|
const x = s.length;
|