data-structure-typed 1.34.8 → 1.34.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/.eslintrc.js +1 -0
- package/CHANGELOG.md +1 -1
- package/CONTRIBUTING.md +0 -0
- package/README.md +8 -8
- package/dist/data-structures/binary-tree/aa-tree.js +2 -5
- package/dist/data-structures/binary-tree/aa-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +280 -423
- package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/avl-tree.js +63 -89
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/b-tree.js +2 -5
- package/dist/data-structures/binary-tree/b-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +17 -22
- package/dist/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-tree.js +11 -30
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/bst.js +127 -214
- package/dist/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/data-structures/binary-tree/rb-tree.js +19 -43
- package/dist/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/segment-tree.js +80 -122
- package/dist/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/splay-tree.js +2 -5
- package/dist/data-structures/binary-tree/splay-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.js +147 -218
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
- package/dist/data-structures/binary-tree/two-three-tree.js.map +1 -1
- package/dist/data-structures/graph/abstract-graph.js +338 -572
- package/dist/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/data-structures/graph/directed-graph.js +145 -275
- package/dist/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/data-structures/graph/map-graph.js +46 -87
- package/dist/data-structures/graph/map-graph.js.map +1 -1
- package/dist/data-structures/graph/undirected-graph.js +90 -179
- package/dist/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/data-structures/hash/coordinate-map.js +23 -45
- package/dist/data-structures/hash/coordinate-map.js.map +1 -1
- package/dist/data-structures/hash/coordinate-set.js +20 -42
- package/dist/data-structures/hash/coordinate-set.js.map +1 -1
- package/dist/data-structures/hash/hash-map.js +85 -247
- package/dist/data-structures/hash/hash-map.js.map +1 -1
- package/dist/data-structures/hash/hash-table.js +87 -128
- package/dist/data-structures/hash/hash-table.js.map +1 -1
- package/dist/data-structures/hash/pair.js +2 -5
- package/dist/data-structures/hash/pair.js.map +1 -1
- package/dist/data-structures/hash/tree-map.js +2 -5
- package/dist/data-structures/hash/tree-map.js.map +1 -1
- package/dist/data-structures/hash/tree-set.js +2 -5
- package/dist/data-structures/hash/tree-set.js.map +1 -1
- package/dist/data-structures/heap/heap.js +56 -80
- package/dist/data-structures/heap/heap.js.map +1 -1
- package/dist/data-structures/heap/max-heap.js +8 -26
- package/dist/data-structures/heap/max-heap.js.map +1 -1
- package/dist/data-structures/heap/min-heap.js +8 -26
- package/dist/data-structures/heap/min-heap.js.map +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.js +149 -218
- package/dist/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/data-structures/linked-list/singly-linked-list.js +119 -218
- package/dist/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/data-structures/linked-list/skip-linked-list.js +50 -70
- package/dist/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/data-structures/matrix/matrix.js +7 -8
- package/dist/data-structures/matrix/matrix.js.map +1 -1
- package/dist/data-structures/matrix/matrix2d.js +57 -70
- package/dist/data-structures/matrix/matrix2d.js.map +1 -1
- package/dist/data-structures/matrix/navigator.js +18 -37
- package/dist/data-structures/matrix/navigator.js.map +1 -1
- package/dist/data-structures/matrix/vector2d.js +63 -84
- package/dist/data-structures/matrix/vector2d.js.map +1 -1
- package/dist/data-structures/priority-queue/max-priority-queue.js +13 -41
- package/dist/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/min-priority-queue.js +13 -41
- package/dist/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/priority-queue.js +93 -139
- package/dist/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/data-structures/queue/deque.js +82 -128
- package/dist/data-structures/queue/deque.js.map +1 -1
- package/dist/data-structures/queue/queue.js +57 -157
- package/dist/data-structures/queue/queue.js.map +1 -1
- package/dist/data-structures/stack/stack.js +21 -22
- package/dist/data-structures/stack/stack.js.map +1 -1
- package/dist/data-structures/tree/tree.js +32 -45
- package/dist/data-structures/tree/tree.js.map +1 -1
- package/dist/data-structures/trie/trie.js +93 -200
- package/dist/data-structures/trie/trie.js.map +1 -1
- package/dist/types/data-structures/abstract-binary-tree.js.map +1 -1
- package/dist/utils/utils.js +22 -107
- package/dist/utils/utils.js.map +1 -1
- package/lib/data-structures/binary-tree/abstract-binary-tree.d.ts +79 -78
- package/lib/data-structures/binary-tree/abstract-binary-tree.js +61 -141
- package/lib/data-structures/binary-tree/avl-tree.d.ts +9 -0
- package/lib/data-structures/binary-tree/avl-tree.js +22 -0
- package/lib/data-structures/binary-tree/bst.d.ts +3 -3
- package/lib/data-structures/binary-tree/bst.js +12 -15
- package/lib/data-structures/binary-tree/tree-multiset.d.ts +16 -18
- package/lib/data-structures/binary-tree/tree-multiset.js +26 -36
- package/lib/data-structures/graph/abstract-graph.d.ts +2 -2
- package/lib/data-structures/graph/abstract-graph.js +5 -5
- package/lib/data-structures/priority-queue/priority-queue.d.ts +3 -3
- package/lib/data-structures/priority-queue/priority-queue.js +3 -3
- package/lib/interfaces/abstract-binary-tree.d.ts +23 -28
- package/lib/interfaces/avl-tree.d.ts +3 -1
- package/lib/interfaces/bst.d.ts +2 -1
- package/lib/types/data-structures/abstract-binary-tree.js +0 -1
- package/package.json +6 -6
- package/src/data-structures/binary-tree/abstract-binary-tree.ts +151 -203
- package/src/data-structures/binary-tree/avl-tree.ts +29 -0
- package/src/data-structures/binary-tree/bst.ts +12 -15
- package/src/data-structures/binary-tree/tree-multiset.ts +27 -39
- package/src/data-structures/graph/abstract-graph.ts +5 -5
- package/src/data-structures/priority-queue/priority-queue.ts +3 -3
- package/src/interfaces/abstract-binary-tree.ts +23 -36
- package/src/interfaces/avl-tree.ts +3 -1
- package/src/interfaces/bst.ts +1 -1
- package/src/types/data-structures/abstract-binary-tree.ts +0 -1
- package/test/integration/avl-tree.test.ts +24 -24
- package/test/integration/bst.test.ts +71 -71
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +4 -4
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +1 -9
- package/test/unit/data-structures/binary-tree/bst.test.ts +8 -8
- package/test/unit/data-structures/binary-tree/overall.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +9 -9
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +4 -4
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.map +1 -1
|
@@ -26,36 +26,20 @@ export abstract class AbstractBinaryTreeNode<
|
|
|
26
26
|
> implements IAbstractBinaryTreeNode<V, NEIGHBOR>
|
|
27
27
|
{
|
|
28
28
|
/**
|
|
29
|
-
* The constructor function initializes a BinaryTreeNode object with
|
|
29
|
+
* The constructor function initializes a BinaryTreeNode object with a key and an optional value.
|
|
30
30
|
* @param {BinaryTreeNodeKey} key - The `key` parameter is of type `BinaryTreeNodeKey` and represents the unique identifier
|
|
31
31
|
* of the binary tree node. It is used to distinguish one node from another in the binary tree.
|
|
32
32
|
* @param {V} [val] - The "val" parameter is an optional parameter of type V. It represents the value that will be
|
|
33
33
|
* stored in the binary tree node. If no value is provided, it will be set to undefined.
|
|
34
34
|
*/
|
|
35
35
|
protected constructor(key: BinaryTreeNodeKey, val?: V) {
|
|
36
|
-
this.
|
|
37
|
-
this.
|
|
36
|
+
this.key = key;
|
|
37
|
+
this.val = val;
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
|
|
40
|
+
key: BinaryTreeNodeKey;
|
|
41
41
|
|
|
42
|
-
|
|
43
|
-
return this._key;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
set key(v: BinaryTreeNodeKey) {
|
|
47
|
-
this._key = v;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
private _val: V | undefined;
|
|
51
|
-
|
|
52
|
-
get val(): V | undefined {
|
|
53
|
-
return this._val;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
set val(value: V | undefined) {
|
|
57
|
-
this._val = value;
|
|
58
|
-
}
|
|
42
|
+
val: V | undefined;
|
|
59
43
|
|
|
60
44
|
private _left: NEIGHBOR | null | undefined;
|
|
61
45
|
|
|
@@ -83,25 +67,7 @@ export abstract class AbstractBinaryTreeNode<
|
|
|
83
67
|
this._right = v;
|
|
84
68
|
}
|
|
85
69
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
get parent(): NEIGHBOR | null | undefined {
|
|
89
|
-
return this._parent;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
set parent(v: NEIGHBOR | null | undefined) {
|
|
93
|
-
this._parent = v;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
private _height = 0;
|
|
97
|
-
|
|
98
|
-
get height(): number {
|
|
99
|
-
return this._height;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
set height(v: number) {
|
|
103
|
-
this._height = v;
|
|
104
|
-
}
|
|
70
|
+
parent: NEIGHBOR | null | undefined;
|
|
105
71
|
|
|
106
72
|
/**
|
|
107
73
|
* The function determines the position of a node in a family tree structure.
|
|
@@ -169,23 +135,11 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
169
135
|
return this._loopType;
|
|
170
136
|
}
|
|
171
137
|
|
|
172
|
-
|
|
138
|
+
visitedKey: BinaryTreeNodeKey[] = [];
|
|
173
139
|
|
|
174
|
-
|
|
175
|
-
return this._visitedKey;
|
|
176
|
-
}
|
|
140
|
+
visitedVal: N['val'][] = [];
|
|
177
141
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
get visitedVal(): N['val'][] {
|
|
181
|
-
return this._visitedVal;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
private _visitedNode: N[] = [];
|
|
185
|
-
|
|
186
|
-
get visitedNode(): N[] {
|
|
187
|
-
return this._visitedNode;
|
|
188
|
-
}
|
|
142
|
+
visitedNode: N[] = [];
|
|
189
143
|
|
|
190
144
|
abstract createNode(key: BinaryTreeNodeKey, val?: N['val']): N | null;
|
|
191
145
|
|
|
@@ -197,19 +151,15 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
197
151
|
* @returns The `destNode` is being returned.
|
|
198
152
|
*/
|
|
199
153
|
swapLocation(srcNode: N, destNode: N): N {
|
|
200
|
-
const {key, val
|
|
154
|
+
const {key, val} = destNode;
|
|
201
155
|
const tempNode = this.createNode(key, val);
|
|
202
156
|
|
|
203
157
|
if (tempNode) {
|
|
204
|
-
tempNode.height = height;
|
|
205
|
-
|
|
206
158
|
destNode.key = srcNode.key;
|
|
207
159
|
destNode.val = srcNode.val;
|
|
208
|
-
destNode.height = srcNode.height;
|
|
209
160
|
|
|
210
161
|
srcNode.key = tempNode.key;
|
|
211
162
|
srcNode.val = tempNode.val;
|
|
212
|
-
srcNode.height = tempNode.height;
|
|
213
163
|
}
|
|
214
164
|
|
|
215
165
|
return destNode;
|
|
@@ -298,19 +248,19 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
298
248
|
/**
|
|
299
249
|
* The `addMany` function takes an array of binary tree node IDs or nodes, and optionally an array of corresponding data
|
|
300
250
|
* values, and adds them to the binary tree.
|
|
301
|
-
* @param {(BinaryTreeNodeKey | null)[] | (N | null)[]}
|
|
251
|
+
* @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of BinaryTreeNodeKey or BinaryTreeNode
|
|
302
252
|
* objects, or null values.
|
|
303
253
|
* @param {N['val'][]} [data] - The `data` parameter is an optional array of values (`N['val'][]`) that corresponds to
|
|
304
254
|
* the nodes or node IDs being added. It is used to set the value of each node being added. If `data` is not provided,
|
|
305
255
|
* the value of the nodes will be `undefined`.
|
|
306
256
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
307
257
|
*/
|
|
308
|
-
addMany(
|
|
258
|
+
addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: N['val'][]): (N | null | undefined)[] {
|
|
309
259
|
// TODO not sure addMany not be run multi times
|
|
310
260
|
const inserted: (N | null | undefined)[] = [];
|
|
311
261
|
|
|
312
|
-
for (let i = 0; i <
|
|
313
|
-
const keyOrNode =
|
|
262
|
+
for (let i = 0; i < keysOrNodes.length; i++) {
|
|
263
|
+
const keyOrNode = keysOrNodes[i];
|
|
314
264
|
if (keyOrNode instanceof AbstractBinaryTreeNode) {
|
|
315
265
|
inserted.push(this.add(keyOrNode.key, keyOrNode.val));
|
|
316
266
|
continue;
|
|
@@ -328,17 +278,17 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
328
278
|
}
|
|
329
279
|
|
|
330
280
|
/**
|
|
331
|
-
* The `
|
|
332
|
-
* @param {(BinaryTreeNodeKey | N)[]}
|
|
281
|
+
* The `refill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
|
|
282
|
+
* @param {(BinaryTreeNodeKey | N)[]} keysOrNodes - The `keysOrNodes` parameter is an array that can contain either
|
|
333
283
|
* `BinaryTreeNodeKey` or `N` values.
|
|
334
284
|
* @param {N[] | Array<N['val']>} [data] - The `data` parameter is an optional array of values that will be assigned to
|
|
335
|
-
* the nodes being added. If provided, the length of the `data` array should be equal to the length of the `
|
|
285
|
+
* the nodes being added. If provided, the length of the `data` array should be equal to the length of the `keysOrNodes`
|
|
336
286
|
* array. Each value in the `data` array will be assigned to the
|
|
337
287
|
* @returns The method is returning a boolean value.
|
|
338
288
|
*/
|
|
339
|
-
|
|
289
|
+
refill(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: N[] | Array<N['val']>): boolean {
|
|
340
290
|
this.clear();
|
|
341
|
-
return
|
|
291
|
+
return keysOrNodes.length === this.addMany(keysOrNodes, data).length;
|
|
342
292
|
}
|
|
343
293
|
|
|
344
294
|
/**
|
|
@@ -395,7 +345,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
395
345
|
* @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter can be one of the following:
|
|
396
346
|
* @returns the depth of the given node or binary tree.
|
|
397
347
|
*/
|
|
398
|
-
getDepth(beginRoot: N | BinaryTreeNodeKey | null): number {
|
|
348
|
+
getDepth(beginRoot: N | BinaryTreeNodeKey | null = this.root): number {
|
|
399
349
|
if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot, 'key');
|
|
400
350
|
|
|
401
351
|
let depth = 0;
|
|
@@ -413,9 +363,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
413
363
|
* node), or `null`.
|
|
414
364
|
* @returns the height of the binary tree.
|
|
415
365
|
*/
|
|
416
|
-
getHeight(beginRoot
|
|
417
|
-
beginRoot = beginRoot ?? this.root;
|
|
418
|
-
|
|
366
|
+
getHeight(beginRoot: N | BinaryTreeNodeKey | null = this.root): number {
|
|
419
367
|
if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot, 'key');
|
|
420
368
|
if (!beginRoot) return -1;
|
|
421
369
|
|
|
@@ -462,8 +410,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
462
410
|
* for `beginRoot`, the `this.root` property is used as the default value.
|
|
463
411
|
* @returns The function `getMinHeight` returns the minimum height of the binary tree.
|
|
464
412
|
*/
|
|
465
|
-
getMinHeight(beginRoot
|
|
466
|
-
beginRoot = beginRoot || this.root;
|
|
413
|
+
getMinHeight(beginRoot: N | null = this.root): number {
|
|
467
414
|
if (!beginRoot) return -1;
|
|
468
415
|
|
|
469
416
|
if (this._loopType === LoopType.RECURSIVE) {
|
|
@@ -512,7 +459,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
512
459
|
* tree or null if the tree is empty.
|
|
513
460
|
* @returns The method is returning a boolean value.
|
|
514
461
|
*/
|
|
515
|
-
isPerfectlyBalanced(beginRoot
|
|
462
|
+
isPerfectlyBalanced(beginRoot: N | null = this.root): boolean {
|
|
516
463
|
return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
|
|
517
464
|
}
|
|
518
465
|
|
|
@@ -527,9 +474,12 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
527
474
|
* function will stop traversing the tree and return the first matching node. If `only
|
|
528
475
|
* @returns an array of nodes (type N).
|
|
529
476
|
*/
|
|
530
|
-
getNodes(
|
|
477
|
+
getNodes(
|
|
478
|
+
nodeProperty: BinaryTreeNodeKey | N,
|
|
479
|
+
propertyName: BinaryTreeNodePropertyName = 'key',
|
|
480
|
+
onlyOne = false
|
|
481
|
+
): N[] {
|
|
531
482
|
if (!this.root) return [];
|
|
532
|
-
propertyName = propertyName ?? 'key';
|
|
533
483
|
|
|
534
484
|
const result: N[] = [];
|
|
535
485
|
|
|
@@ -565,8 +515,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
565
515
|
* specifies the name of the property to be checked in the nodes. If not provided, it defaults to 'key'.
|
|
566
516
|
* @returns a boolean value.
|
|
567
517
|
*/
|
|
568
|
-
has(nodeProperty: BinaryTreeNodeKey | N, propertyName
|
|
569
|
-
propertyName = propertyName ?? 'key';
|
|
518
|
+
has(nodeProperty: BinaryTreeNodeKey | N, propertyName: BinaryTreeNodePropertyName = 'key'): boolean {
|
|
570
519
|
// TODO may support finding node by value equal
|
|
571
520
|
return this.getNodes(nodeProperty, propertyName).length > 0;
|
|
572
521
|
}
|
|
@@ -582,8 +531,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
582
531
|
* @returns either the value of the specified property of the node, or the node itself if no property name is provided.
|
|
583
532
|
* If no matching node is found, it returns null.
|
|
584
533
|
*/
|
|
585
|
-
get(nodeProperty: BinaryTreeNodeKey | N, propertyName
|
|
586
|
-
propertyName = propertyName ?? 'key';
|
|
534
|
+
get(nodeProperty: BinaryTreeNodeKey | N, propertyName: BinaryTreeNodePropertyName = 'key'): N | null {
|
|
587
535
|
// TODO may support finding node by value equal
|
|
588
536
|
return this.getNodes(nodeProperty, propertyName, true)[0] ?? null;
|
|
589
537
|
}
|
|
@@ -647,10 +595,9 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
647
595
|
* from the root of the binary tree. The function returns the leftmost node found during the traversal. If no leftmost
|
|
648
596
|
* node is found (
|
|
649
597
|
*/
|
|
650
|
-
getLeftMost(beginRoot
|
|
598
|
+
getLeftMost(beginRoot: N | BinaryTreeNodeKey | null = this.root): N | null {
|
|
651
599
|
if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot, 'key');
|
|
652
600
|
|
|
653
|
-
beginRoot = beginRoot ?? this.root;
|
|
654
601
|
if (!beginRoot) return beginRoot;
|
|
655
602
|
|
|
656
603
|
if (this._loopType === LoopType.RECURSIVE) {
|
|
@@ -682,28 +629,27 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
682
629
|
/**
|
|
683
630
|
* The `getRightMost` function returns the rightmost node in a binary tree, either recursively or iteratively using tail
|
|
684
631
|
* recursion optimization.
|
|
685
|
-
* @param {N | null} [
|
|
632
|
+
* @param {N | null} [beginRoot] - The `node` parameter is an optional parameter of type `N` or `null`. It represents the
|
|
686
633
|
* starting node from which we want to find the rightmost node. If no node is provided, the function will default to
|
|
687
634
|
* using the root node of the data structure.
|
|
688
635
|
* @returns The `getRightMost` function returns the rightmost node in a binary tree. It returns the rightmost node
|
|
689
636
|
* starting from that node.
|
|
690
637
|
*/
|
|
691
|
-
getRightMost(
|
|
638
|
+
getRightMost(beginRoot: N): N;
|
|
692
639
|
|
|
693
640
|
/**
|
|
694
641
|
* The `getRightMost` function returns the rightmost node in a binary tree, either recursively or iteratively using tail
|
|
695
642
|
* recursion optimization.
|
|
696
|
-
* @param {N | null} [
|
|
643
|
+
* @param {N | null} [beginRoot] - The `node` parameter is an optional parameter of type `N` or `null`. It represents the
|
|
697
644
|
* starting node from which we want to find the rightmost node. If no node is provided, the function will default to
|
|
698
645
|
* using the root node of the data structure.
|
|
699
646
|
* @returns The `getRightMost` function returns the rightmost node in a binary tree. If the `node` parameter is provided,
|
|
700
647
|
* it returns the rightmost node starting from that node. If the `node` parameter is not provided, it returns the
|
|
701
648
|
* rightmost node starting from the root of the binary tree.
|
|
702
649
|
*/
|
|
703
|
-
getRightMost(
|
|
650
|
+
getRightMost(beginRoot: N | null = this.root): N | null {
|
|
704
651
|
// TODO support get right most by passing key in
|
|
705
|
-
|
|
706
|
-
if (!node) return node;
|
|
652
|
+
if (!beginRoot) return beginRoot;
|
|
707
653
|
|
|
708
654
|
if (this._loopType === LoopType.RECURSIVE) {
|
|
709
655
|
const _traverse = (cur: N): N => {
|
|
@@ -711,7 +657,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
711
657
|
return _traverse(cur.right);
|
|
712
658
|
};
|
|
713
659
|
|
|
714
|
-
return _traverse(
|
|
660
|
+
return _traverse(beginRoot);
|
|
715
661
|
} else {
|
|
716
662
|
// Indirect implementation of iteration using tail recursion optimization
|
|
717
663
|
const _traverse = trampoline((cur: N) => {
|
|
@@ -719,7 +665,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
719
665
|
return _traverse.cont(cur.right);
|
|
720
666
|
});
|
|
721
667
|
|
|
722
|
-
return _traverse(
|
|
668
|
+
return _traverse(beginRoot);
|
|
723
669
|
}
|
|
724
670
|
}
|
|
725
671
|
|
|
@@ -809,8 +755,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
809
755
|
* not provided, it defaults to 'key'.
|
|
810
756
|
* @returns a number, which is the sum of the values of the specified property in the subtree rooted at `subTreeRoot`.
|
|
811
757
|
*/
|
|
812
|
-
subTreeSum(subTreeRoot: N | BinaryTreeNodeKey | null, propertyName
|
|
813
|
-
propertyName = propertyName ?? 'key';
|
|
758
|
+
subTreeSum(subTreeRoot: N | BinaryTreeNodeKey | null, propertyName: BinaryTreeNodePropertyName = 'key'): number {
|
|
814
759
|
if (typeof subTreeRoot === 'number') subTreeRoot = this.get(subTreeRoot, 'key');
|
|
815
760
|
|
|
816
761
|
if (!subTreeRoot) return 0;
|
|
@@ -868,9 +813,8 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
868
813
|
subTreeAdd(
|
|
869
814
|
subTreeRoot: N | BinaryTreeNodeKey | null,
|
|
870
815
|
delta: number,
|
|
871
|
-
propertyName
|
|
816
|
+
propertyName: BinaryTreeNodePropertyName = 'key'
|
|
872
817
|
): boolean {
|
|
873
|
-
propertyName = propertyName ?? 'key';
|
|
874
818
|
if (typeof subTreeRoot === 'number') subTreeRoot = this.get(subTreeRoot, 'key');
|
|
875
819
|
|
|
876
820
|
if (!subTreeRoot) return false;
|
|
@@ -909,41 +853,40 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
909
853
|
}
|
|
910
854
|
|
|
911
855
|
/**
|
|
912
|
-
* Performs a breadth-first search (
|
|
856
|
+
* Performs a breadth-first search (bfs) on a binary tree, accumulating properties of each node based on their 'key' property.
|
|
913
857
|
* @returns An array of binary tree node IDs.
|
|
914
858
|
*/
|
|
915
|
-
|
|
859
|
+
bfs(): BinaryTreeNodeKey[];
|
|
916
860
|
|
|
917
861
|
/**
|
|
918
|
-
* Performs a breadth-first search (
|
|
862
|
+
* Performs a breadth-first search (bfs) on a binary tree, accumulating properties of each node based on the specified property name.
|
|
919
863
|
* @param {'key'} nodeOrPropertyName - The name of the property to accumulate.
|
|
920
864
|
* @returns An array of values corresponding to the specified property.
|
|
921
865
|
*/
|
|
922
|
-
|
|
866
|
+
bfs(nodeOrPropertyName: 'key'): BinaryTreeNodeKey[];
|
|
923
867
|
|
|
924
868
|
/**
|
|
925
|
-
* Performs a breadth-first search (
|
|
869
|
+
* Performs a breadth-first search (bfs) on a binary tree, accumulating the 'val' property of each node.
|
|
926
870
|
* @param {'val'} nodeOrPropertyName - The name of the property to accumulate.
|
|
927
871
|
* @returns An array of 'val' properties from each node.
|
|
928
872
|
*/
|
|
929
|
-
|
|
873
|
+
bfs(nodeOrPropertyName: 'val'): N['val'][];
|
|
930
874
|
|
|
931
875
|
/**
|
|
932
|
-
* Performs a breadth-first search (
|
|
876
|
+
* Performs a breadth-first search (bfs) on a binary tree, accumulating nodes themselves.
|
|
933
877
|
* @param {'node'} nodeOrPropertyName - The name of the property to accumulate.
|
|
934
878
|
* @returns An array of binary tree nodes.
|
|
935
879
|
*/
|
|
936
|
-
|
|
880
|
+
bfs(nodeOrPropertyName: 'node'): N[];
|
|
937
881
|
|
|
938
882
|
/**
|
|
939
|
-
* The
|
|
883
|
+
* The bfs function performs a breadth-first search on a binary tree, accumulating properties of each node based on a specified property name.
|
|
940
884
|
* @param {NodeOrPropertyName} [nodeOrPropertyName] - An optional parameter that represents either a node or a property name.
|
|
941
|
-
* If a node is provided, the
|
|
942
|
-
* If a property name is provided, the
|
|
885
|
+
* If a node is provided, the bfs algorithm will be performed starting from that node.
|
|
886
|
+
* If a property name is provided, the bfs algorithm will be performed starting from the root node, accumulating the specified property.
|
|
943
887
|
* @returns An instance of the `AbstractBinaryTreeNodeProperties` class with generic type `N`.
|
|
944
888
|
*/
|
|
945
|
-
|
|
946
|
-
nodeOrPropertyName = nodeOrPropertyName ?? 'key';
|
|
889
|
+
bfs(nodeOrPropertyName: NodeOrPropertyName = 'key'): AbstractBinaryTreeNodeProperties<N> {
|
|
947
890
|
this._clearResults();
|
|
948
891
|
const queue: Array<N | null | undefined> = [this.root];
|
|
949
892
|
|
|
@@ -960,45 +903,53 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
960
903
|
}
|
|
961
904
|
|
|
962
905
|
/**
|
|
963
|
-
* Performs a depth-first search (
|
|
906
|
+
* Performs a depth-first search (dfs) traversal on a binary tree and accumulates properties of each node based on their 'key' property.
|
|
964
907
|
* @returns An array of binary tree node IDs.
|
|
965
908
|
*/
|
|
966
|
-
|
|
909
|
+
dfs(): BinaryTreeNodeKey[];
|
|
967
910
|
|
|
968
911
|
/**
|
|
969
|
-
* Performs a depth-first search (
|
|
912
|
+
* Performs a depth-first search (dfs) traversal on a binary tree and accumulates properties of each node based on the specified property name.
|
|
913
|
+
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
914
|
+
* @returns An array of values corresponding to the specified property.
|
|
915
|
+
*/
|
|
916
|
+
dfs(pattern: DFSOrderPattern): BinaryTreeNodeKey[];
|
|
917
|
+
|
|
918
|
+
/**
|
|
919
|
+
* Performs a depth-first search (dfs) traversal on a binary tree and accumulates properties of each node based on the specified property name.
|
|
970
920
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
971
921
|
* @param {string} nodeOrPropertyName - The name of the property to accumulate.
|
|
972
922
|
* @returns An array of values corresponding to the specified property.
|
|
973
923
|
*/
|
|
974
|
-
|
|
924
|
+
dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'key'): BinaryTreeNodeKey[];
|
|
975
925
|
|
|
976
926
|
/**
|
|
977
|
-
* Performs a depth-first search (
|
|
927
|
+
* Performs a depth-first search (dfs) traversal on a binary tree and accumulates the 'val' property of each node.
|
|
978
928
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
979
929
|
* @param {'val'} nodeOrPropertyName - The name of the property to accumulate.
|
|
980
930
|
* @returns An array of 'val' properties from each node.
|
|
981
931
|
*/
|
|
982
|
-
|
|
932
|
+
dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'val'): N[];
|
|
983
933
|
|
|
984
934
|
/**
|
|
985
|
-
* Performs a depth-first search (
|
|
935
|
+
* Performs a depth-first search (dfs) traversal on a binary tree and accumulates nodes themselves.
|
|
986
936
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
987
937
|
* @param {'node'} nodeOrPropertyName - The name of the property to accumulate.
|
|
988
938
|
* @returns An array of binary tree nodes.
|
|
989
939
|
*/
|
|
990
|
-
|
|
940
|
+
dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'node'): N[];
|
|
991
941
|
|
|
992
942
|
/**
|
|
993
|
-
* The
|
|
943
|
+
* The dfs function performs a depth-first search traversal on a binary tree and returns the accumulated properties of
|
|
994
944
|
* each node based on the specified pattern and property name.
|
|
995
945
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
996
946
|
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The name of a property of the nodes in the binary tree. This property will be used to accumulate values during the depth-first search traversal. If no `nodeOrPropertyName` is provided, the default value is `'key'`.
|
|
997
947
|
* @returns an instance of the AbstractBinaryTreeNodeProperties class, which contains the accumulated properties of the binary tree nodes based on the specified pattern and node or property name.
|
|
998
948
|
*/
|
|
999
|
-
|
|
1000
|
-
pattern =
|
|
1001
|
-
nodeOrPropertyName =
|
|
949
|
+
dfs(
|
|
950
|
+
pattern: DFSOrderPattern = 'in',
|
|
951
|
+
nodeOrPropertyName: NodeOrPropertyName = 'key'
|
|
952
|
+
): AbstractBinaryTreeNodeProperties<N> {
|
|
1002
953
|
this._clearResults();
|
|
1003
954
|
const _traverse = (node: N) => {
|
|
1004
955
|
switch (pattern) {
|
|
@@ -1027,48 +978,53 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1027
978
|
// --- start additional methods ---
|
|
1028
979
|
|
|
1029
980
|
/**
|
|
1030
|
-
* Performs an iterative depth-first search (
|
|
981
|
+
* Performs an iterative depth-first search (dfs) traversal on a binary tree and accumulates properties of each node based on their 'key' property.
|
|
1031
982
|
* @returns An array of binary tree node IDs.
|
|
1032
983
|
*/
|
|
1033
|
-
|
|
984
|
+
dfsIterative(): BinaryTreeNodeKey[];
|
|
1034
985
|
|
|
1035
986
|
/**
|
|
1036
|
-
* Performs an iterative depth-first search (
|
|
987
|
+
* Performs an iterative depth-first search (dfs) traversal on a binary tree and accumulates properties of each node based on their 'key' property.
|
|
988
|
+
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
989
|
+
* @returns An array of values corresponding to the specified property.
|
|
990
|
+
*/
|
|
991
|
+
dfsIterative(pattern: DFSOrderPattern): BinaryTreeNodeKey[];
|
|
992
|
+
|
|
993
|
+
/**
|
|
994
|
+
* Performs an iterative depth-first search (dfs) traversal on a binary tree and accumulates properties of each node based on the specified property name.
|
|
1037
995
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
1038
996
|
* @param {string} nodeOrPropertyName - The name of the property to accumulate.
|
|
1039
997
|
* @returns An array of values corresponding to the specified property.
|
|
1040
998
|
*/
|
|
1041
|
-
|
|
999
|
+
dfsIterative(pattern: DFSOrderPattern, nodeOrPropertyName: 'key'): BinaryTreeNodeKey[];
|
|
1042
1000
|
|
|
1043
1001
|
/**
|
|
1044
|
-
* Performs an iterative depth-first search (
|
|
1002
|
+
* Performs an iterative depth-first search (dfs) traversal on a binary tree and accumulates the 'val' property of each node.
|
|
1045
1003
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
1046
1004
|
* @param {'val'} nodeOrPropertyName - The name of the property to accumulate.
|
|
1047
1005
|
* @returns An array of 'val' properties from each node.
|
|
1048
1006
|
*/
|
|
1049
|
-
|
|
1007
|
+
dfsIterative(pattern: DFSOrderPattern, nodeOrPropertyName: 'val'): N['val'][];
|
|
1050
1008
|
|
|
1051
1009
|
/**
|
|
1052
|
-
* Performs an iterative depth-first search (
|
|
1010
|
+
* Performs an iterative depth-first search (dfs) traversal on a binary tree and accumulates nodes themselves.
|
|
1053
1011
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
1054
1012
|
* @param {'node'} nodeOrPropertyName - The name of the property to accumulate.
|
|
1055
1013
|
* @returns An array of binary tree nodes.
|
|
1056
1014
|
*/
|
|
1057
|
-
|
|
1015
|
+
dfsIterative(pattern: DFSOrderPattern, nodeOrPropertyName: 'node'): N[];
|
|
1058
1016
|
|
|
1059
1017
|
/**
|
|
1060
|
-
* The
|
|
1018
|
+
* The dfsIterative function performs an iterative depth-first search traversal on a binary tree, with the option to
|
|
1061
1019
|
* specify the traversal pattern and the property name to accumulate results by.
|
|
1062
1020
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
1063
1021
|
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The name of a property of the nodes in the binary tree. This property will be used to accumulate values during the depth-first search traversal. By default, it is set to `'key'`.
|
|
1064
1022
|
* @returns An object of type AbstractBinaryTreeNodeProperties<N>.
|
|
1065
1023
|
*/
|
|
1066
|
-
|
|
1067
|
-
pattern
|
|
1068
|
-
nodeOrPropertyName
|
|
1024
|
+
dfsIterative(
|
|
1025
|
+
pattern: DFSOrderPattern = 'in',
|
|
1026
|
+
nodeOrPropertyName: NodeOrPropertyName = 'key'
|
|
1069
1027
|
): AbstractBinaryTreeNodeProperties<N> {
|
|
1070
|
-
pattern = pattern || 'in';
|
|
1071
|
-
nodeOrPropertyName = nodeOrPropertyName || 'key';
|
|
1072
1028
|
this._clearResults();
|
|
1073
1029
|
if (!this.root) return this._getResultByPropertyName(nodeOrPropertyName);
|
|
1074
1030
|
// 0: visit, 1: print
|
|
@@ -1108,6 +1064,12 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1108
1064
|
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
1109
1065
|
}
|
|
1110
1066
|
|
|
1067
|
+
/**
|
|
1068
|
+
* Performs a level-order traversal on a binary tree starting from the specified node and accumulates properties of each node based on their 'key' property.
|
|
1069
|
+
* @returns An array of binary tree node IDs.
|
|
1070
|
+
*/
|
|
1071
|
+
levelIterative(): BinaryTreeNodeKey[];
|
|
1072
|
+
|
|
1111
1073
|
/**
|
|
1112
1074
|
* Performs a level-order traversal on a binary tree starting from the specified node and accumulates properties of each node based on their 'key' property.
|
|
1113
1075
|
* @param {N | null} node - The starting node for the level order traversal. If null, the root node of the tree is used as the starting node.
|
|
@@ -1121,7 +1083,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1121
1083
|
* @param {string} nodeOrPropertyName - The name of the property to accumulate.
|
|
1122
1084
|
* @returns An array of values corresponding to the specified property.
|
|
1123
1085
|
*/
|
|
1124
|
-
levelIterative(node: N | null, nodeOrPropertyName
|
|
1086
|
+
levelIterative(node: N | null, nodeOrPropertyName: 'key'): BinaryTreeNodeKey[];
|
|
1125
1087
|
|
|
1126
1088
|
/**
|
|
1127
1089
|
* Performs a level-order traversal on a binary tree starting from the specified node and accumulates the 'val' property of each node.
|
|
@@ -1129,7 +1091,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1129
1091
|
* @param {'val'} nodeOrPropertyName - The name of the property to accumulate.
|
|
1130
1092
|
* @returns An array of 'val' properties from each node.
|
|
1131
1093
|
*/
|
|
1132
|
-
levelIterative(node: N | null, nodeOrPropertyName
|
|
1094
|
+
levelIterative(node: N | null, nodeOrPropertyName: 'val'): N['val'][];
|
|
1133
1095
|
|
|
1134
1096
|
/**
|
|
1135
1097
|
* Performs a level-order traversal on a binary tree starting from the specified node and accumulates nodes themselves.
|
|
@@ -1137,7 +1099,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1137
1099
|
* @param {'node'} nodeOrPropertyName - The name of the property to accumulate.
|
|
1138
1100
|
* @returns An array of binary tree nodes.
|
|
1139
1101
|
*/
|
|
1140
|
-
levelIterative(node: N | null, nodeOrPropertyName
|
|
1102
|
+
levelIterative(node: N | null, nodeOrPropertyName: 'node'): N[];
|
|
1141
1103
|
|
|
1142
1104
|
/**
|
|
1143
1105
|
* The `levelIterative` function performs a level-order traversal on a binary tree and returns the values of the nodes
|
|
@@ -1151,9 +1113,10 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1151
1113
|
* accumulating results based on the 'key' property.
|
|
1152
1114
|
* @returns An object of type `AbstractBinaryTreeNodeProperties<N>`.
|
|
1153
1115
|
*/
|
|
1154
|
-
levelIterative(
|
|
1155
|
-
|
|
1156
|
-
|
|
1116
|
+
levelIterative(
|
|
1117
|
+
node: N | null = this.root,
|
|
1118
|
+
nodeOrPropertyName: NodeOrPropertyName = 'key'
|
|
1119
|
+
): AbstractBinaryTreeNodeProperties<N> {
|
|
1157
1120
|
if (!node) return [];
|
|
1158
1121
|
|
|
1159
1122
|
this._clearResults();
|
|
@@ -1175,6 +1138,12 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1175
1138
|
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
1176
1139
|
}
|
|
1177
1140
|
|
|
1141
|
+
/**
|
|
1142
|
+
* Collects nodes from a binary tree by a specified property and organizes them into levels.
|
|
1143
|
+
* @returns A 2D array of AbstractBinaryTreeNodeProperty<N> objects.
|
|
1144
|
+
*/
|
|
1145
|
+
listLevels(): BinaryTreeNodeKey[][];
|
|
1146
|
+
|
|
1178
1147
|
/**
|
|
1179
1148
|
* Collects nodes from a binary tree by a specified property and organizes them into levels.
|
|
1180
1149
|
* @param {N | null} node - The root node of the binary tree or null. If null, the function will use the root node of the current binary tree instance.
|
|
@@ -1188,7 +1157,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1188
1157
|
* @param {'key} nodeOrPropertyName - The property of the BinaryTreeNode object to collect at each level.
|
|
1189
1158
|
* @returns A 2D array of values corresponding to the specified property.
|
|
1190
1159
|
*/
|
|
1191
|
-
listLevels(node: N | null, nodeOrPropertyName
|
|
1160
|
+
listLevels(node: N | null, nodeOrPropertyName: 'key'): BinaryTreeNodeKey[][];
|
|
1192
1161
|
|
|
1193
1162
|
/**
|
|
1194
1163
|
* Collects nodes from a binary tree by a specified property and organizes them into levels.
|
|
@@ -1196,7 +1165,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1196
1165
|
* @param {'val'} nodeOrPropertyName - The property of the BinaryTreeNode object to collect at each level.
|
|
1197
1166
|
* @returns A 2D array of 'val' properties from each node.
|
|
1198
1167
|
*/
|
|
1199
|
-
listLevels(node: N | null, nodeOrPropertyName
|
|
1168
|
+
listLevels(node: N | null, nodeOrPropertyName: 'val'): N['val'][][];
|
|
1200
1169
|
|
|
1201
1170
|
/**
|
|
1202
1171
|
* Collects nodes from a binary tree by a specified property and organizes them into levels.
|
|
@@ -1204,7 +1173,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1204
1173
|
* @param {'node'} nodeOrPropertyName - The property of the BinaryTreeNode object to collect at each level.
|
|
1205
1174
|
* @returns A 2D array of binary tree nodes.
|
|
1206
1175
|
*/
|
|
1207
|
-
listLevels(node: N | null, nodeOrPropertyName
|
|
1176
|
+
listLevels(node: N | null, nodeOrPropertyName: 'node'): N[][];
|
|
1208
1177
|
|
|
1209
1178
|
/**
|
|
1210
1179
|
* The `listLevels` function collects nodes from a binary tree by a specified property and organizes them into levels.
|
|
@@ -1212,9 +1181,10 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1212
1181
|
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that specifies the property of the `BinaryTreeNode` object to collect at each level. It can be one of the following values: 'key', 'val', or 'node'. If not provided, it defaults to 'key'.
|
|
1213
1182
|
* @returns A 2D array of `AbstractBinaryTreeNodeProperty<N>` objects.
|
|
1214
1183
|
*/
|
|
1215
|
-
listLevels(
|
|
1216
|
-
|
|
1217
|
-
|
|
1184
|
+
listLevels(
|
|
1185
|
+
node: N | null = this.root,
|
|
1186
|
+
nodeOrPropertyName: NodeOrPropertyName = 'key'
|
|
1187
|
+
): AbstractBinaryTreeNodeProperty<N>[][] {
|
|
1218
1188
|
if (!node) return [];
|
|
1219
1189
|
|
|
1220
1190
|
const levelsNodes: AbstractBinaryTreeNodeProperty<N>[][] = [];
|
|
@@ -1283,7 +1253,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1283
1253
|
|
|
1284
1254
|
/**
|
|
1285
1255
|
* Time complexity is O(n)
|
|
1286
|
-
* Space complexity of Iterative
|
|
1256
|
+
* Space complexity of Iterative dfs equals to recursive dfs which is O(n) because of the stack
|
|
1287
1257
|
*/
|
|
1288
1258
|
|
|
1289
1259
|
/**
|
|
@@ -1298,7 +1268,14 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1298
1268
|
* @param {'key'} nodeOrPropertyName - The name of the property to accumulate.
|
|
1299
1269
|
* @returns An array of values corresponding to the specified property.
|
|
1300
1270
|
*/
|
|
1301
|
-
morris(pattern
|
|
1271
|
+
morris(pattern: DFSOrderPattern, nodeOrPropertyName: 'key'): BinaryTreeNodeKey[];
|
|
1272
|
+
|
|
1273
|
+
/**
|
|
1274
|
+
* Performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm and accumulates properties of each node based on the specified property name.
|
|
1275
|
+
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
1276
|
+
* @returns An array of values corresponding to the specified property.
|
|
1277
|
+
*/
|
|
1278
|
+
morris(pattern: DFSOrderPattern): BinaryTreeNodeKey[];
|
|
1302
1279
|
|
|
1303
1280
|
/**
|
|
1304
1281
|
* Performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm and accumulates the 'val' property of each node.
|
|
@@ -1306,7 +1283,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1306
1283
|
* @param {'val'} nodeOrPropertyName - The property of the BinaryTreeNode object to collect at each level.
|
|
1307
1284
|
* @returns An array of 'val' properties from each node.
|
|
1308
1285
|
*/
|
|
1309
|
-
morris(pattern
|
|
1286
|
+
morris(pattern: DFSOrderPattern, nodeOrPropertyName: 'val'): N[];
|
|
1310
1287
|
|
|
1311
1288
|
/**
|
|
1312
1289
|
* Performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm and accumulates nodes themselves.
|
|
@@ -1314,7 +1291,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1314
1291
|
* @param {'node'} nodeOrPropertyName - The property of the BinaryTreeNode object to collect at each level.
|
|
1315
1292
|
* @returns An array of binary tree nodes.
|
|
1316
1293
|
*/
|
|
1317
|
-
morris(pattern
|
|
1294
|
+
morris(pattern: DFSOrderPattern, nodeOrPropertyName: 'node'): N[];
|
|
1318
1295
|
|
|
1319
1296
|
/**
|
|
1320
1297
|
* The `morris` function performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm.
|
|
@@ -1323,14 +1300,11 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1323
1300
|
* @returns An array of AbstractBinaryTreeNodeProperties<N> objects.
|
|
1324
1301
|
*/
|
|
1325
1302
|
morris(
|
|
1326
|
-
pattern
|
|
1327
|
-
nodeOrPropertyName
|
|
1303
|
+
pattern: DFSOrderPattern = 'in',
|
|
1304
|
+
nodeOrPropertyName: NodeOrPropertyName = 'key'
|
|
1328
1305
|
): AbstractBinaryTreeNodeProperties<N> {
|
|
1329
1306
|
if (this.root === null) return [];
|
|
1330
1307
|
|
|
1331
|
-
pattern = pattern || 'in';
|
|
1332
|
-
nodeOrPropertyName = nodeOrPropertyName || 'key';
|
|
1333
|
-
|
|
1334
1308
|
this._clearResults();
|
|
1335
1309
|
|
|
1336
1310
|
let cur: N | null | undefined = this.root;
|
|
@@ -1453,30 +1427,6 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1453
1427
|
this._loopType = value;
|
|
1454
1428
|
}
|
|
1455
1429
|
|
|
1456
|
-
/**
|
|
1457
|
-
* The function sets the value of the `_visitedKey` property in a protected manner.
|
|
1458
|
-
* @param {BinaryTreeNodeKey[]} value - value is an array of BinaryTreeNodeKey values.
|
|
1459
|
-
*/
|
|
1460
|
-
protected _setVisitedKey(value: BinaryTreeNodeKey[]) {
|
|
1461
|
-
this._visitedKey = value;
|
|
1462
|
-
}
|
|
1463
|
-
|
|
1464
|
-
/**
|
|
1465
|
-
* The function sets the value of the "_visitedVal" property to the given array.
|
|
1466
|
-
* @param value - An array of type N.
|
|
1467
|
-
*/
|
|
1468
|
-
protected _setVisitedVal(value: Array<N>) {
|
|
1469
|
-
this._visitedVal = value;
|
|
1470
|
-
}
|
|
1471
|
-
|
|
1472
|
-
/**
|
|
1473
|
-
* The function sets the value of the _visitedNode property.
|
|
1474
|
-
* @param {N[]} value - N[] is an array of elements of type N.
|
|
1475
|
-
*/
|
|
1476
|
-
protected _setVisitedNode(value: N[]) {
|
|
1477
|
-
this._visitedNode = value;
|
|
1478
|
-
}
|
|
1479
|
-
|
|
1480
1430
|
/**
|
|
1481
1431
|
* The function sets the root property of an object to a given value, and if the value is not null, it also sets the
|
|
1482
1432
|
* parent property of the value to undefined.
|
|
@@ -1502,9 +1452,9 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1502
1452
|
* properties.
|
|
1503
1453
|
*/
|
|
1504
1454
|
protected _clearResults() {
|
|
1505
|
-
this.
|
|
1506
|
-
this.
|
|
1507
|
-
this.
|
|
1455
|
+
this.visitedKey = [];
|
|
1456
|
+
this.visitedVal = [];
|
|
1457
|
+
this.visitedNode = [];
|
|
1508
1458
|
}
|
|
1509
1459
|
|
|
1510
1460
|
/**
|
|
@@ -1526,26 +1476,26 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1526
1476
|
cur: N,
|
|
1527
1477
|
result: (N | null | undefined)[],
|
|
1528
1478
|
nodeProperty: BinaryTreeNodeKey | N,
|
|
1529
|
-
propertyName
|
|
1530
|
-
onlyOne
|
|
1479
|
+
propertyName: BinaryTreeNodePropertyName = 'key',
|
|
1480
|
+
onlyOne = false
|
|
1531
1481
|
) {
|
|
1532
1482
|
switch (propertyName) {
|
|
1533
1483
|
case 'key':
|
|
1534
1484
|
if (cur.key === nodeProperty) {
|
|
1535
1485
|
result.push(cur);
|
|
1536
|
-
return
|
|
1486
|
+
return onlyOne;
|
|
1537
1487
|
}
|
|
1538
1488
|
break;
|
|
1539
1489
|
case 'val':
|
|
1540
1490
|
if (cur.val === nodeProperty) {
|
|
1541
1491
|
result.push(cur);
|
|
1542
|
-
return
|
|
1492
|
+
return onlyOne;
|
|
1543
1493
|
}
|
|
1544
1494
|
break;
|
|
1545
1495
|
default:
|
|
1546
1496
|
if (cur.key === nodeProperty) {
|
|
1547
1497
|
result.push(cur);
|
|
1548
|
-
return
|
|
1498
|
+
return onlyOne;
|
|
1549
1499
|
}
|
|
1550
1500
|
break;
|
|
1551
1501
|
}
|
|
@@ -1558,27 +1508,25 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1558
1508
|
* can be either a string representing a property name or a reference to a `Node` object. If it is a string, it
|
|
1559
1509
|
* specifies the property name to be used for accumulating values. If it is a `Node` object, it specifies
|
|
1560
1510
|
*/
|
|
1561
|
-
protected _accumulatedByPropertyName(node: N, nodeOrPropertyName
|
|
1562
|
-
nodeOrPropertyName = nodeOrPropertyName ?? 'key';
|
|
1563
|
-
|
|
1511
|
+
protected _accumulatedByPropertyName(node: N, nodeOrPropertyName: NodeOrPropertyName = 'key') {
|
|
1564
1512
|
switch (nodeOrPropertyName) {
|
|
1565
1513
|
case 'key':
|
|
1566
|
-
this.
|
|
1514
|
+
this.visitedKey.push(node.key);
|
|
1567
1515
|
break;
|
|
1568
1516
|
case 'val':
|
|
1569
|
-
this.
|
|
1517
|
+
this.visitedVal.push(node.val);
|
|
1570
1518
|
break;
|
|
1571
1519
|
case 'node':
|
|
1572
|
-
this.
|
|
1520
|
+
this.visitedNode.push(node);
|
|
1573
1521
|
break;
|
|
1574
1522
|
default:
|
|
1575
|
-
this.
|
|
1523
|
+
this.visitedKey.push(node.key);
|
|
1576
1524
|
break;
|
|
1577
1525
|
}
|
|
1578
1526
|
}
|
|
1579
1527
|
|
|
1580
1528
|
/**
|
|
1581
|
-
* The time complexity of Morris traversal is O(n), it
|
|
1529
|
+
* The time complexity of Morris traversal is O(n), it may slower than others
|
|
1582
1530
|
* The space complexity Morris traversal is O(1) because no using stack
|
|
1583
1531
|
*/
|
|
1584
1532
|
|
|
@@ -1589,18 +1537,18 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1589
1537
|
* can accept either a `NodeOrPropertyName` type or be undefined.
|
|
1590
1538
|
* @returns The method `_getResultByPropertyName` returns an instance of `AbstractBinaryTreeNodeProperties<N>`.
|
|
1591
1539
|
*/
|
|
1592
|
-
protected _getResultByPropertyName(
|
|
1593
|
-
nodeOrPropertyName =
|
|
1594
|
-
|
|
1540
|
+
protected _getResultByPropertyName(
|
|
1541
|
+
nodeOrPropertyName: NodeOrPropertyName = 'key'
|
|
1542
|
+
): AbstractBinaryTreeNodeProperties<N> {
|
|
1595
1543
|
switch (nodeOrPropertyName) {
|
|
1596
1544
|
case 'key':
|
|
1597
|
-
return this.
|
|
1545
|
+
return this.visitedKey;
|
|
1598
1546
|
case 'val':
|
|
1599
|
-
return this.
|
|
1547
|
+
return this.visitedVal;
|
|
1600
1548
|
case 'node':
|
|
1601
|
-
return this.
|
|
1549
|
+
return this.visitedNode;
|
|
1602
1550
|
default:
|
|
1603
|
-
return this.
|
|
1551
|
+
return this.visitedKey;
|
|
1604
1552
|
}
|
|
1605
1553
|
}
|
|
1606
1554
|
|