data-structure-typed 1.34.7 → 1.34.8
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/dist/data-structures/binary-tree/abstract-binary-tree.js +70 -70
- package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/avl-tree.js +8 -8
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-tree.js +4 -4
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/bst.js +59 -59
- package/dist/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/data-structures/binary-tree/rb-tree.js +4 -4
- package/dist/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.js +39 -39
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/data-structures/graph/abstract-graph.js +49 -49
- package/dist/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/data-structures/graph/directed-graph.js +33 -33
- package/dist/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/data-structures/graph/map-graph.js +4 -4
- package/dist/data-structures/graph/map-graph.js.map +1 -1
- package/dist/data-structures/graph/undirected-graph.js +14 -14
- package/dist/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/data-structures/tree/tree.js +5 -5
- package/dist/data-structures/tree/tree.js.map +1 -1
- package/lib/data-structures/binary-tree/abstract-binary-tree.d.ts +71 -71
- package/lib/data-structures/binary-tree/abstract-binary-tree.js +110 -110
- package/lib/data-structures/binary-tree/avl-tree.d.ts +10 -10
- package/lib/data-structures/binary-tree/avl-tree.js +13 -13
- package/lib/data-structures/binary-tree/binary-tree.d.ts +6 -6
- package/lib/data-structures/binary-tree/binary-tree.js +7 -7
- package/lib/data-structures/binary-tree/bst.d.ts +34 -34
- package/lib/data-structures/binary-tree/bst.js +80 -80
- package/lib/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/lib/data-structures/binary-tree/rb-tree.js +4 -4
- package/lib/data-structures/binary-tree/tree-multiset.d.ts +27 -27
- package/lib/data-structures/binary-tree/tree-multiset.js +55 -55
- package/lib/data-structures/graph/abstract-graph.d.ts +60 -60
- package/lib/data-structures/graph/abstract-graph.js +81 -81
- package/lib/data-structures/graph/directed-graph.d.ts +51 -51
- package/lib/data-structures/graph/directed-graph.js +63 -63
- package/lib/data-structures/graph/map-graph.d.ts +13 -13
- package/lib/data-structures/graph/map-graph.js +12 -12
- package/lib/data-structures/graph/undirected-graph.d.ts +30 -30
- package/lib/data-structures/graph/undirected-graph.js +32 -32
- package/lib/data-structures/tree/tree.d.ts +4 -4
- package/lib/data-structures/tree/tree.js +6 -6
- package/lib/interfaces/abstract-binary-tree.d.ts +24 -24
- package/lib/interfaces/abstract-graph.d.ts +13 -13
- package/lib/interfaces/avl-tree.d.ts +3 -3
- package/lib/interfaces/bst.d.ts +8 -8
- package/lib/interfaces/directed-graph.d.ts +5 -5
- package/lib/interfaces/rb-tree.d.ts +2 -2
- package/lib/interfaces/undirected-graph.d.ts +2 -2
- package/lib/types/data-structures/abstract-binary-tree.d.ts +3 -3
- package/lib/types/data-structures/abstract-graph.d.ts +2 -2
- package/lib/types/data-structures/bst.d.ts +2 -2
- package/lib/types/data-structures/tree-multiset.d.ts +1 -1
- package/lib/types/utils/validate-type.d.ts +8 -8
- package/package.json +1 -1
- package/scripts/rename_clear_files.sh +29 -0
- package/src/data-structures/binary-tree/abstract-binary-tree.ts +147 -147
- package/src/data-structures/binary-tree/avl-tree.ts +14 -14
- package/src/data-structures/binary-tree/binary-tree.ts +8 -8
- package/src/data-structures/binary-tree/bst.ts +98 -90
- package/src/data-structures/binary-tree/rb-tree.ts +9 -9
- package/src/data-structures/binary-tree/tree-multiset.ts +62 -62
- package/src/data-structures/graph/abstract-graph.ts +109 -104
- package/src/data-structures/graph/directed-graph.ts +77 -77
- package/src/data-structures/graph/map-graph.ts +20 -15
- package/src/data-structures/graph/undirected-graph.ts +39 -39
- package/src/data-structures/tree/tree.ts +7 -7
- package/src/interfaces/abstract-binary-tree.ts +24 -24
- package/src/interfaces/abstract-graph.ts +13 -13
- package/src/interfaces/avl-tree.ts +3 -3
- package/src/interfaces/bst.ts +8 -8
- package/src/interfaces/directed-graph.ts +5 -5
- package/src/interfaces/rb-tree.ts +2 -2
- package/src/interfaces/undirected-graph.ts +2 -2
- package/src/types/data-structures/abstract-binary-tree.ts +3 -3
- package/src/types/data-structures/abstract-graph.ts +2 -2
- package/src/types/data-structures/bst.ts +2 -2
- package/src/types/data-structures/tree-multiset.ts +1 -1
- package/src/types/utils/validate-type.ts +10 -10
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +24 -24
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/bst.test.ts +71 -71
- package/test/unit/data-structures/binary-tree/overall.test.ts +19 -19
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +72 -72
- package/test/unit/data-structures/graph/directed-graph.test.ts +8 -8
- package/test/unit/data-structures/graph/map-graph.test.ts +4 -4
- package/test/unit/data-structures/graph/overall.test.ts +2 -2
- package/test/unit/data-structures/graph/undirected-graph.test.ts +1 -1
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +0 -1
- package/test/unit/data-structures/tree/tree.test.ts +2 -2
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.map +1 -1
|
@@ -12,7 +12,7 @@ import type {
|
|
|
12
12
|
AbstractBinaryTreeNodeProperties,
|
|
13
13
|
AbstractBinaryTreeNodeProperty,
|
|
14
14
|
BinaryTreeDeletedResult,
|
|
15
|
-
|
|
15
|
+
BinaryTreeNodeKey,
|
|
16
16
|
BinaryTreeNodePropertyName,
|
|
17
17
|
DFSOrderPattern,
|
|
18
18
|
NodeOrPropertyName
|
|
@@ -26,25 +26,25 @@ export abstract class AbstractBinaryTreeNode<
|
|
|
26
26
|
> implements IAbstractBinaryTreeNode<V, NEIGHBOR>
|
|
27
27
|
{
|
|
28
28
|
/**
|
|
29
|
-
* The constructor function initializes a BinaryTreeNode object with an
|
|
30
|
-
* @param {
|
|
29
|
+
* The constructor function initializes a BinaryTreeNode object with an key and an optional value.
|
|
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
|
-
protected constructor(
|
|
36
|
-
this.
|
|
35
|
+
protected constructor(key: BinaryTreeNodeKey, val?: V) {
|
|
36
|
+
this._key = key;
|
|
37
37
|
this._val = val;
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
private
|
|
40
|
+
private _key: BinaryTreeNodeKey;
|
|
41
41
|
|
|
42
|
-
get
|
|
43
|
-
return this.
|
|
42
|
+
get key(): BinaryTreeNodeKey {
|
|
43
|
+
return this._key;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
set
|
|
47
|
-
this.
|
|
46
|
+
set key(v: BinaryTreeNodeKey) {
|
|
47
|
+
this._key = v;
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
private _val: V | undefined;
|
|
@@ -169,10 +169,10 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
169
169
|
return this._loopType;
|
|
170
170
|
}
|
|
171
171
|
|
|
172
|
-
private
|
|
172
|
+
private _visitedKey: BinaryTreeNodeKey[] = [];
|
|
173
173
|
|
|
174
|
-
get
|
|
175
|
-
return this.
|
|
174
|
+
get visitedKey(): BinaryTreeNodeKey[] {
|
|
175
|
+
return this._visitedKey;
|
|
176
176
|
}
|
|
177
177
|
|
|
178
178
|
private _visitedVal: N['val'][] = [];
|
|
@@ -187,7 +187,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
187
187
|
return this._visitedNode;
|
|
188
188
|
}
|
|
189
189
|
|
|
190
|
-
abstract createNode(
|
|
190
|
+
abstract createNode(key: BinaryTreeNodeKey, val?: N['val']): N | null;
|
|
191
191
|
|
|
192
192
|
/**
|
|
193
193
|
* The `swapLocation` function swaps the location of two nodes in a binary tree.
|
|
@@ -197,17 +197,17 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
197
197
|
* @returns The `destNode` is being returned.
|
|
198
198
|
*/
|
|
199
199
|
swapLocation(srcNode: N, destNode: N): N {
|
|
200
|
-
const {
|
|
201
|
-
const tempNode = this.createNode(
|
|
200
|
+
const {key, val, height} = destNode;
|
|
201
|
+
const tempNode = this.createNode(key, val);
|
|
202
202
|
|
|
203
203
|
if (tempNode) {
|
|
204
204
|
tempNode.height = height;
|
|
205
205
|
|
|
206
|
-
destNode.
|
|
206
|
+
destNode.key = srcNode.key;
|
|
207
207
|
destNode.val = srcNode.val;
|
|
208
208
|
destNode.height = srcNode.height;
|
|
209
209
|
|
|
210
|
-
srcNode.
|
|
210
|
+
srcNode.key = tempNode.key;
|
|
211
211
|
srcNode.val = tempNode.val;
|
|
212
212
|
srcNode.height = tempNode.height;
|
|
213
213
|
}
|
|
@@ -216,7 +216,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
216
216
|
}
|
|
217
217
|
|
|
218
218
|
/**
|
|
219
|
-
* The clear() function resets the root, size, and
|
|
219
|
+
* The clear() function resets the root, size, and maxKey properties to their initial values.
|
|
220
220
|
*/
|
|
221
221
|
clear() {
|
|
222
222
|
this._root = null;
|
|
@@ -239,20 +239,20 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
239
239
|
|
|
240
240
|
/**
|
|
241
241
|
* The `add` function adds a new node to a binary tree, either by ID or by creating a new node with a given value.
|
|
242
|
-
* @param {
|
|
242
|
+
* @param {BinaryTreeNodeKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a `BinaryTreeNodeKey`, which
|
|
243
243
|
* is a number representing the ID of a binary tree node, or it can be a `N` object, which represents a binary tree
|
|
244
244
|
* node itself. It can also be `null` if no node is specified.
|
|
245
245
|
* @param [val] - The `val` parameter is an optional value that can be assigned to the `val` property of the new node
|
|
246
246
|
* being added to the binary tree.
|
|
247
247
|
* @returns The function `add` returns either the inserted node (`N`), `null`, or `undefined`.
|
|
248
248
|
*/
|
|
249
|
-
add(
|
|
249
|
+
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined {
|
|
250
250
|
const _bfs = (root: N, newNode: N | null): N | undefined | null => {
|
|
251
251
|
const queue: Array<N | null> = [root];
|
|
252
252
|
while (queue.length > 0) {
|
|
253
253
|
const cur = queue.shift();
|
|
254
254
|
if (cur) {
|
|
255
|
-
if (newNode && cur.
|
|
255
|
+
if (newNode && cur.key === newNode.key) return;
|
|
256
256
|
const inserted = this._addTo(newNode, cur);
|
|
257
257
|
if (inserted !== undefined) return inserted;
|
|
258
258
|
if (cur.left) queue.push(cur.left);
|
|
@@ -264,17 +264,17 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
264
264
|
|
|
265
265
|
let inserted: N | null | undefined, needInsert: N | null;
|
|
266
266
|
|
|
267
|
-
if (
|
|
267
|
+
if (keyOrNode === null) {
|
|
268
268
|
needInsert = null;
|
|
269
|
-
} else if (typeof
|
|
270
|
-
needInsert = this.createNode(
|
|
271
|
-
} else if (
|
|
272
|
-
needInsert =
|
|
269
|
+
} else if (typeof keyOrNode === 'number') {
|
|
270
|
+
needInsert = this.createNode(keyOrNode, val);
|
|
271
|
+
} else if (keyOrNode instanceof AbstractBinaryTreeNode) {
|
|
272
|
+
needInsert = keyOrNode;
|
|
273
273
|
} else {
|
|
274
274
|
return;
|
|
275
275
|
}
|
|
276
276
|
|
|
277
|
-
const existNode =
|
|
277
|
+
const existNode = keyOrNode ? this.get(keyOrNode, 'key') : undefined;
|
|
278
278
|
|
|
279
279
|
if (this.root) {
|
|
280
280
|
if (existNode) {
|
|
@@ -298,45 +298,45 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
298
298
|
/**
|
|
299
299
|
* The `addMany` function takes an array of binary tree node IDs or nodes, and optionally an array of corresponding data
|
|
300
300
|
* values, and adds them to the binary tree.
|
|
301
|
-
* @param {(
|
|
301
|
+
* @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} idsOrNodes - An array of BinaryTreeNodeKey or BinaryTreeNode
|
|
302
302
|
* objects, or null values.
|
|
303
303
|
* @param {N['val'][]} [data] - The `data` parameter is an optional array of values (`N['val'][]`) that corresponds to
|
|
304
304
|
* 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
305
|
* the value of the nodes will be `undefined`.
|
|
306
306
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
307
307
|
*/
|
|
308
|
-
addMany(idsOrNodes: (
|
|
308
|
+
addMany(idsOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: N['val'][]): (N | null | undefined)[] {
|
|
309
309
|
// TODO not sure addMany not be run multi times
|
|
310
310
|
const inserted: (N | null | undefined)[] = [];
|
|
311
311
|
|
|
312
312
|
for (let i = 0; i < idsOrNodes.length; i++) {
|
|
313
|
-
const
|
|
314
|
-
if (
|
|
315
|
-
inserted.push(this.add(
|
|
313
|
+
const keyOrNode = idsOrNodes[i];
|
|
314
|
+
if (keyOrNode instanceof AbstractBinaryTreeNode) {
|
|
315
|
+
inserted.push(this.add(keyOrNode.key, keyOrNode.val));
|
|
316
316
|
continue;
|
|
317
317
|
}
|
|
318
318
|
|
|
319
|
-
if (
|
|
319
|
+
if (keyOrNode === null) {
|
|
320
320
|
inserted.push(this.add(null));
|
|
321
321
|
continue;
|
|
322
322
|
}
|
|
323
323
|
|
|
324
324
|
const val = data?.[i];
|
|
325
|
-
inserted.push(this.add(
|
|
325
|
+
inserted.push(this.add(keyOrNode, val));
|
|
326
326
|
}
|
|
327
327
|
return inserted;
|
|
328
328
|
}
|
|
329
329
|
|
|
330
330
|
/**
|
|
331
331
|
* The `fill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
|
|
332
|
-
* @param {(
|
|
333
|
-
* `
|
|
332
|
+
* @param {(BinaryTreeNodeKey | N)[]} idsOrNodes - The `idsOrNodes` parameter is an array that can contain either
|
|
333
|
+
* `BinaryTreeNodeKey` or `N` values.
|
|
334
334
|
* @param {N[] | Array<N['val']>} [data] - The `data` parameter is an optional array of values that will be assigned to
|
|
335
335
|
* the nodes being added. If provided, the length of the `data` array should be equal to the length of the `idsOrNodes`
|
|
336
336
|
* array. Each value in the `data` array will be assigned to the
|
|
337
337
|
* @returns The method is returning a boolean value.
|
|
338
338
|
*/
|
|
339
|
-
fill(idsOrNodes: (
|
|
339
|
+
fill(idsOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: N[] | Array<N['val']>): boolean {
|
|
340
340
|
this.clear();
|
|
341
341
|
return idsOrNodes.length === this.addMany(idsOrNodes, data).length;
|
|
342
342
|
}
|
|
@@ -344,15 +344,15 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
344
344
|
/**
|
|
345
345
|
* The `remove` function in TypeScript is used to delete a node from a binary search tree and returns an array of objects
|
|
346
346
|
* containing the deleted node and the node that needs to be balanced.
|
|
347
|
-
* @param {N |
|
|
348
|
-
* node ID (`
|
|
347
|
+
* @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node object (`N`) or a binary tree
|
|
348
|
+
* node ID (`BinaryTreeNodeKey`).
|
|
349
349
|
* @returns The function `remove` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
350
350
|
*/
|
|
351
|
-
remove(
|
|
351
|
+
remove(nodeOrKey: N | BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[] {
|
|
352
352
|
const bstDeletedResult: BinaryTreeDeletedResult<N>[] = [];
|
|
353
353
|
if (!this.root) return bstDeletedResult;
|
|
354
354
|
|
|
355
|
-
const curr: N | null = typeof
|
|
355
|
+
const curr: N | null = typeof nodeOrKey === 'number' ? this.get(nodeOrKey) : nodeOrKey;
|
|
356
356
|
if (!curr) return bstDeletedResult;
|
|
357
357
|
|
|
358
358
|
const parent: N | null = curr?.parent ? curr.parent : null;
|
|
@@ -392,11 +392,11 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
392
392
|
|
|
393
393
|
/**
|
|
394
394
|
* The function calculates the depth of a node in a binary tree.
|
|
395
|
-
* @param {N |
|
|
395
|
+
* @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter can be one of the following:
|
|
396
396
|
* @returns the depth of the given node or binary tree.
|
|
397
397
|
*/
|
|
398
|
-
getDepth(beginRoot: N |
|
|
399
|
-
if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot, '
|
|
398
|
+
getDepth(beginRoot: N | BinaryTreeNodeKey | null): number {
|
|
399
|
+
if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot, 'key');
|
|
400
400
|
|
|
401
401
|
let depth = 0;
|
|
402
402
|
while (beginRoot?.parent) {
|
|
@@ -408,15 +408,15 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
408
408
|
|
|
409
409
|
/**
|
|
410
410
|
* The `getHeight` function calculates the maximum height of a binary tree, either recursively or iteratively.
|
|
411
|
-
* @param {N |
|
|
412
|
-
* generic type representing a node in a binary tree), `
|
|
411
|
+
* @param {N | BinaryTreeNodeKey | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
|
|
412
|
+
* generic type representing a node in a binary tree), `BinaryTreeNodeKey` (a type representing the ID of a binary tree
|
|
413
413
|
* node), or `null`.
|
|
414
414
|
* @returns the height of the binary tree.
|
|
415
415
|
*/
|
|
416
|
-
getHeight(beginRoot?: N |
|
|
416
|
+
getHeight(beginRoot?: N | BinaryTreeNodeKey | null): number {
|
|
417
417
|
beginRoot = beginRoot ?? this.root;
|
|
418
418
|
|
|
419
|
-
if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot, '
|
|
419
|
+
if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot, 'key');
|
|
420
420
|
if (!beginRoot) return -1;
|
|
421
421
|
|
|
422
422
|
if (this._loopType === LoopType.RECURSIVE) {
|
|
@@ -518,18 +518,18 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
518
518
|
|
|
519
519
|
/**
|
|
520
520
|
* The function `getNodes` returns an array of nodes that match a given property name and value in a binary tree.
|
|
521
|
-
* @param {
|
|
521
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a
|
|
522
522
|
* generic type `N`. It represents the property of the binary tree node that you want to search for.
|
|
523
523
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
524
|
-
* specifies the property name to use when searching for nodes. If not provided, it defaults to '
|
|
524
|
+
* specifies the property name to use when searching for nodes. If not provided, it defaults to 'key'.
|
|
525
525
|
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
|
|
526
526
|
* return only one node that matches the given `nodeProperty` or `propertyName`. If `onlyOne` is set to `true`, the
|
|
527
527
|
* function will stop traversing the tree and return the first matching node. If `only
|
|
528
528
|
* @returns an array of nodes (type N).
|
|
529
529
|
*/
|
|
530
|
-
getNodes(nodeProperty:
|
|
530
|
+
getNodes(nodeProperty: BinaryTreeNodeKey | N, propertyName?: BinaryTreeNodePropertyName, onlyOne?: boolean): N[] {
|
|
531
531
|
if (!this.root) return [];
|
|
532
|
-
propertyName = propertyName ?? '
|
|
532
|
+
propertyName = propertyName ?? 'key';
|
|
533
533
|
|
|
534
534
|
const result: N[] = [];
|
|
535
535
|
|
|
@@ -559,14 +559,14 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
559
559
|
|
|
560
560
|
/**
|
|
561
561
|
* The function checks if a binary tree node has a specific property.
|
|
562
|
-
* @param {
|
|
562
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or `N`.
|
|
563
563
|
* It represents the property of the binary tree node that you want to check.
|
|
564
564
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
565
|
-
* specifies the name of the property to be checked in the nodes. If not provided, it defaults to '
|
|
565
|
+
* specifies the name of the property to be checked in the nodes. If not provided, it defaults to 'key'.
|
|
566
566
|
* @returns a boolean value.
|
|
567
567
|
*/
|
|
568
|
-
has(nodeProperty:
|
|
569
|
-
propertyName = propertyName ?? '
|
|
568
|
+
has(nodeProperty: BinaryTreeNodeKey | N, propertyName?: BinaryTreeNodePropertyName): boolean {
|
|
569
|
+
propertyName = propertyName ?? 'key';
|
|
570
570
|
// TODO may support finding node by value equal
|
|
571
571
|
return this.getNodes(nodeProperty, propertyName).length > 0;
|
|
572
572
|
}
|
|
@@ -574,16 +574,16 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
574
574
|
/**
|
|
575
575
|
* The function returns the first node that matches the given property name and value, or null if no matching node is
|
|
576
576
|
* found.
|
|
577
|
-
* @param {
|
|
577
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or `N`.
|
|
578
578
|
* It represents the property of the binary tree node that you want to search for.
|
|
579
579
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
580
580
|
* specifies the property name to be used for searching the binary tree nodes. If this parameter is not provided, the
|
|
581
|
-
* default value is set to `'
|
|
581
|
+
* default value is set to `'key'`.
|
|
582
582
|
* @returns either the value of the specified property of the node, or the node itself if no property name is provided.
|
|
583
583
|
* If no matching node is found, it returns null.
|
|
584
584
|
*/
|
|
585
|
-
get(nodeProperty:
|
|
586
|
-
propertyName = propertyName ?? '
|
|
585
|
+
get(nodeProperty: BinaryTreeNodeKey | N, propertyName?: BinaryTreeNodePropertyName): N | null {
|
|
586
|
+
propertyName = propertyName ?? 'key';
|
|
587
587
|
// TODO may support finding node by value equal
|
|
588
588
|
return this.getNodes(nodeProperty, propertyName, true)[0] ?? null;
|
|
589
589
|
}
|
|
@@ -599,7 +599,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
599
599
|
* @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
|
|
600
600
|
*/
|
|
601
601
|
getPathToRoot(node: N, isReverse = true): N[] {
|
|
602
|
-
// TODO to support get path through passing
|
|
602
|
+
// TODO to support get path through passing key
|
|
603
603
|
const result: N[] = [];
|
|
604
604
|
while (node.parent) {
|
|
605
605
|
// Array.push + Array.reverse is more efficient than Array.unshift
|
|
@@ -614,7 +614,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
614
614
|
/**
|
|
615
615
|
* The function `getLeftMost` returns the leftmost node in a binary tree, starting from a specified node or the root if
|
|
616
616
|
* no node is specified.
|
|
617
|
-
* generic type representing a node in a binary tree), `
|
|
617
|
+
* generic type representing a node in a binary tree), `BinaryTreeNodeKey` (a type representing the ID of a binary tree
|
|
618
618
|
* node), or `null`.
|
|
619
619
|
* @returns The function `getLeftMost` returns the leftmost node in a binary tree. If the `beginRoot` parameter is
|
|
620
620
|
* provided, it starts the traversal from that node. If `beginRoot` is not provided or is `null`, it starts the traversal
|
|
@@ -626,8 +626,8 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
626
626
|
/**
|
|
627
627
|
* The function `getLeftMost` returns the leftmost node in a binary tree, starting from a specified node or the root if
|
|
628
628
|
* no node is specified.
|
|
629
|
-
* @param {N |
|
|
630
|
-
* generic type representing a node in a binary tree), `
|
|
629
|
+
* @param {N | BinaryTreeNodeKey | null} [node] - The `beginRoot` parameter is optional and can be of type `N` (a
|
|
630
|
+
* generic type representing a node in a binary tree), `BinaryTreeNodeKey` (a type representing the ID of a binary tree
|
|
631
631
|
* node).
|
|
632
632
|
* @returns The function `getLeftMost` returns the leftmost node in a binary tree. If the `beginRoot` parameter is
|
|
633
633
|
* provided, it starts the traversal from that node. If `beginRoot` is not provided or is `null`, it starts the traversal
|
|
@@ -639,16 +639,16 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
639
639
|
/**
|
|
640
640
|
* The function `getLeftMost` returns the leftmost node in a binary tree, starting from a specified node or the root if
|
|
641
641
|
* no node is specified.
|
|
642
|
-
* @param {N |
|
|
643
|
-
* generic type representing a node in a binary tree), `
|
|
642
|
+
* @param {N | BinaryTreeNodeKey | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
|
|
643
|
+
* generic type representing a node in a binary tree), `BinaryTreeNodeKey` (a type representing the ID of a binary tree
|
|
644
644
|
* node), or `null`.
|
|
645
645
|
* @returns The function `getLeftMost` returns the leftmost node in a binary tree. If the `beginRoot` parameter is
|
|
646
646
|
* provided, it starts the traversal from that node. If `beginRoot` is not provided or is `null`, it starts the traversal
|
|
647
647
|
* from the root of the binary tree. The function returns the leftmost node found during the traversal. If no leftmost
|
|
648
648
|
* node is found (
|
|
649
649
|
*/
|
|
650
|
-
getLeftMost(beginRoot?: N |
|
|
651
|
-
if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot, '
|
|
650
|
+
getLeftMost(beginRoot?: N | BinaryTreeNodeKey | null): N | null {
|
|
651
|
+
if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot, 'key');
|
|
652
652
|
|
|
653
653
|
beginRoot = beginRoot ?? this.root;
|
|
654
654
|
if (!beginRoot) return beginRoot;
|
|
@@ -701,7 +701,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
701
701
|
* rightmost node starting from the root of the binary tree.
|
|
702
702
|
*/
|
|
703
703
|
getRightMost(node?: N | null): N | null {
|
|
704
|
-
// TODO support get right most by passing
|
|
704
|
+
// TODO support get right most by passing key in
|
|
705
705
|
node = node ?? this.root;
|
|
706
706
|
if (!node) return node;
|
|
707
707
|
|
|
@@ -733,10 +733,10 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
733
733
|
if (!node) return true;
|
|
734
734
|
|
|
735
735
|
if (this._loopType === LoopType.RECURSIVE) {
|
|
736
|
-
const dfs = (cur: N | null | undefined, min:
|
|
736
|
+
const dfs = (cur: N | null | undefined, min: BinaryTreeNodeKey, max: BinaryTreeNodeKey): boolean => {
|
|
737
737
|
if (!cur) return true;
|
|
738
|
-
if (cur.
|
|
739
|
-
return dfs(cur.left, min, cur.
|
|
738
|
+
if (cur.key <= min || cur.key >= max) return false;
|
|
739
|
+
return dfs(cur.left, min, cur.key) && dfs(cur.right, cur.key, max);
|
|
740
740
|
};
|
|
741
741
|
|
|
742
742
|
return dfs(node, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
|
|
@@ -750,8 +750,8 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
750
750
|
curr = curr.left;
|
|
751
751
|
}
|
|
752
752
|
curr = stack.pop()!;
|
|
753
|
-
if (!curr || prev >= curr.
|
|
754
|
-
prev = curr.
|
|
753
|
+
if (!curr || prev >= curr.key) return false;
|
|
754
|
+
prev = curr.key;
|
|
755
755
|
curr = curr.right;
|
|
756
756
|
}
|
|
757
757
|
return true;
|
|
@@ -773,7 +773,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
773
773
|
* @returns the size of the subtree rooted at `subTreeRoot`.
|
|
774
774
|
*/
|
|
775
775
|
getSubTreeSize(subTreeRoot: N | null | undefined) {
|
|
776
|
-
// TODO support
|
|
776
|
+
// TODO support key passed in
|
|
777
777
|
let size = 0;
|
|
778
778
|
if (!subTreeRoot) return size;
|
|
779
779
|
|
|
@@ -802,16 +802,16 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
802
802
|
|
|
803
803
|
/**
|
|
804
804
|
* The function `subTreeSum` calculates the sum of a specified property in a binary tree or subtree.
|
|
805
|
-
* @param {N |
|
|
805
|
+
* @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
|
|
806
806
|
* tree or the ID of a binary tree node. It can also be `null` if there is no subtree.
|
|
807
807
|
* @param {BinaryTreeNodePropertyName} [propertyName] - propertyName is an optional parameter that specifies the
|
|
808
|
-
* property of the binary tree node to use for calculating the sum. It can be either '
|
|
809
|
-
* not provided, it defaults to '
|
|
808
|
+
* property of the binary tree node to use for calculating the sum. It can be either 'key' or 'val'. If propertyName is
|
|
809
|
+
* not provided, it defaults to 'key'.
|
|
810
810
|
* @returns a number, which is the sum of the values of the specified property in the subtree rooted at `subTreeRoot`.
|
|
811
811
|
*/
|
|
812
|
-
subTreeSum(subTreeRoot: N |
|
|
813
|
-
propertyName = propertyName ?? '
|
|
814
|
-
if (typeof subTreeRoot === 'number') subTreeRoot = this.get(subTreeRoot, '
|
|
812
|
+
subTreeSum(subTreeRoot: N | BinaryTreeNodeKey | null, propertyName?: BinaryTreeNodePropertyName): number {
|
|
813
|
+
propertyName = propertyName ?? 'key';
|
|
814
|
+
if (typeof subTreeRoot === 'number') subTreeRoot = this.get(subTreeRoot, 'key');
|
|
815
815
|
|
|
816
816
|
if (!subTreeRoot) return 0;
|
|
817
817
|
|
|
@@ -820,14 +820,14 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
820
820
|
const _sumByProperty = (cur: N) => {
|
|
821
821
|
let needSum: number;
|
|
822
822
|
switch (propertyName) {
|
|
823
|
-
case '
|
|
824
|
-
needSum = cur.
|
|
823
|
+
case 'key':
|
|
824
|
+
needSum = cur.key;
|
|
825
825
|
break;
|
|
826
826
|
case 'val':
|
|
827
827
|
needSum = typeof cur.val === 'number' ? cur.val : 0;
|
|
828
828
|
break;
|
|
829
829
|
default:
|
|
830
|
-
needSum = cur.
|
|
830
|
+
needSum = cur.key;
|
|
831
831
|
break;
|
|
832
832
|
}
|
|
833
833
|
return needSum;
|
|
@@ -857,31 +857,31 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
857
857
|
|
|
858
858
|
/**
|
|
859
859
|
* The function `subTreeAdd` adds a delta value to a specified property of each node in a subtree.
|
|
860
|
-
* @param {N |
|
|
860
|
+
* @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
|
|
861
861
|
* tree or the ID of a node in the binary tree. It can also be `null` if there is no subtree to add to.
|
|
862
862
|
* @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
|
|
863
863
|
* each node in the subtree should be incremented.
|
|
864
864
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
865
|
-
* specifies the property of the binary tree node that should be modified. If not provided, it defaults to '
|
|
865
|
+
* specifies the property of the binary tree node that should be modified. If not provided, it defaults to 'key'.
|
|
866
866
|
* @returns a boolean value.
|
|
867
867
|
*/
|
|
868
868
|
subTreeAdd(
|
|
869
|
-
subTreeRoot: N |
|
|
869
|
+
subTreeRoot: N | BinaryTreeNodeKey | null,
|
|
870
870
|
delta: number,
|
|
871
871
|
propertyName?: BinaryTreeNodePropertyName
|
|
872
872
|
): boolean {
|
|
873
|
-
propertyName = propertyName ?? '
|
|
874
|
-
if (typeof subTreeRoot === 'number') subTreeRoot = this.get(subTreeRoot, '
|
|
873
|
+
propertyName = propertyName ?? 'key';
|
|
874
|
+
if (typeof subTreeRoot === 'number') subTreeRoot = this.get(subTreeRoot, 'key');
|
|
875
875
|
|
|
876
876
|
if (!subTreeRoot) return false;
|
|
877
877
|
|
|
878
878
|
const _addByProperty = (cur: N) => {
|
|
879
879
|
switch (propertyName) {
|
|
880
|
-
case '
|
|
881
|
-
cur.
|
|
880
|
+
case 'key':
|
|
881
|
+
cur.key += delta;
|
|
882
882
|
break;
|
|
883
883
|
default:
|
|
884
|
-
cur.
|
|
884
|
+
cur.key += delta;
|
|
885
885
|
break;
|
|
886
886
|
}
|
|
887
887
|
};
|
|
@@ -909,17 +909,17 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
909
909
|
}
|
|
910
910
|
|
|
911
911
|
/**
|
|
912
|
-
* Performs a breadth-first search (BFS) on a binary tree, accumulating properties of each node based on their '
|
|
912
|
+
* Performs a breadth-first search (BFS) on a binary tree, accumulating properties of each node based on their 'key' property.
|
|
913
913
|
* @returns An array of binary tree node IDs.
|
|
914
914
|
*/
|
|
915
|
-
BFS():
|
|
915
|
+
BFS(): BinaryTreeNodeKey[];
|
|
916
916
|
|
|
917
917
|
/**
|
|
918
918
|
* Performs a breadth-first search (BFS) on a binary tree, accumulating properties of each node based on the specified property name.
|
|
919
|
-
* @param {'
|
|
919
|
+
* @param {'key'} nodeOrPropertyName - The name of the property to accumulate.
|
|
920
920
|
* @returns An array of values corresponding to the specified property.
|
|
921
921
|
*/
|
|
922
|
-
BFS(nodeOrPropertyName: '
|
|
922
|
+
BFS(nodeOrPropertyName: 'key'): BinaryTreeNodeKey[];
|
|
923
923
|
|
|
924
924
|
/**
|
|
925
925
|
* Performs a breadth-first search (BFS) on a binary tree, accumulating the 'val' property of each node.
|
|
@@ -943,7 +943,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
943
943
|
* @returns An instance of the `AbstractBinaryTreeNodeProperties` class with generic type `N`.
|
|
944
944
|
*/
|
|
945
945
|
BFS(nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N> {
|
|
946
|
-
nodeOrPropertyName = nodeOrPropertyName ?? '
|
|
946
|
+
nodeOrPropertyName = nodeOrPropertyName ?? 'key';
|
|
947
947
|
this._clearResults();
|
|
948
948
|
const queue: Array<N | null | undefined> = [this.root];
|
|
949
949
|
|
|
@@ -960,10 +960,10 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
960
960
|
}
|
|
961
961
|
|
|
962
962
|
/**
|
|
963
|
-
* Performs a depth-first search (DFS) traversal on a binary tree and accumulates properties of each node based on their '
|
|
963
|
+
* Performs a depth-first search (DFS) traversal on a binary tree and accumulates properties of each node based on their 'key' property.
|
|
964
964
|
* @returns An array of binary tree node IDs.
|
|
965
965
|
*/
|
|
966
|
-
DFS():
|
|
966
|
+
DFS(): BinaryTreeNodeKey[];
|
|
967
967
|
|
|
968
968
|
/**
|
|
969
969
|
* Performs a depth-first search (DFS) traversal on a binary tree and accumulates properties of each node based on the specified property name.
|
|
@@ -971,7 +971,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
971
971
|
* @param {string} nodeOrPropertyName - The name of the property to accumulate.
|
|
972
972
|
* @returns An array of values corresponding to the specified property.
|
|
973
973
|
*/
|
|
974
|
-
DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: '
|
|
974
|
+
DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'key'): BinaryTreeNodeKey[];
|
|
975
975
|
|
|
976
976
|
/**
|
|
977
977
|
* Performs a depth-first search (DFS) traversal on a binary tree and accumulates the 'val' property of each node.
|
|
@@ -993,12 +993,12 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
993
993
|
* The DFS function performs a depth-first search traversal on a binary tree and returns the accumulated properties of
|
|
994
994
|
* each node based on the specified pattern and property name.
|
|
995
995
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
996
|
-
* @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 `'
|
|
996
|
+
* @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
997
|
* @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
998
|
*/
|
|
999
999
|
DFS(pattern?: 'in' | 'pre' | 'post', nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N> {
|
|
1000
1000
|
pattern = pattern ?? 'in';
|
|
1001
|
-
nodeOrPropertyName = nodeOrPropertyName ?? '
|
|
1001
|
+
nodeOrPropertyName = nodeOrPropertyName ?? 'key';
|
|
1002
1002
|
this._clearResults();
|
|
1003
1003
|
const _traverse = (node: N) => {
|
|
1004
1004
|
switch (pattern) {
|
|
@@ -1027,10 +1027,10 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1027
1027
|
// --- start additional methods ---
|
|
1028
1028
|
|
|
1029
1029
|
/**
|
|
1030
|
-
* Performs an iterative depth-first search (DFS) traversal on a binary tree and accumulates properties of each node based on their '
|
|
1030
|
+
* Performs an iterative depth-first search (DFS) traversal on a binary tree and accumulates properties of each node based on their 'key' property.
|
|
1031
1031
|
* @returns An array of binary tree node IDs.
|
|
1032
1032
|
*/
|
|
1033
|
-
DFSIterative():
|
|
1033
|
+
DFSIterative(): BinaryTreeNodeKey[];
|
|
1034
1034
|
|
|
1035
1035
|
/**
|
|
1036
1036
|
* Performs an iterative depth-first search (DFS) traversal on a binary tree and accumulates properties of each node based on the specified property name.
|
|
@@ -1038,7 +1038,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1038
1038
|
* @param {string} nodeOrPropertyName - The name of the property to accumulate.
|
|
1039
1039
|
* @returns An array of values corresponding to the specified property.
|
|
1040
1040
|
*/
|
|
1041
|
-
DFSIterative(pattern?: DFSOrderPattern, nodeOrPropertyName?: '
|
|
1041
|
+
DFSIterative(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'key'): BinaryTreeNodeKey[];
|
|
1042
1042
|
|
|
1043
1043
|
/**
|
|
1044
1044
|
* Performs an iterative depth-first search (DFS) traversal on a binary tree and accumulates the 'val' property of each node.
|
|
@@ -1060,7 +1060,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1060
1060
|
* The DFSIterative function performs an iterative depth-first search traversal on a binary tree, with the option to
|
|
1061
1061
|
* specify the traversal pattern and the property name to accumulate results by.
|
|
1062
1062
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
1063
|
-
* @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 `'
|
|
1063
|
+
* @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
1064
|
* @returns An object of type AbstractBinaryTreeNodeProperties<N>.
|
|
1065
1065
|
*/
|
|
1066
1066
|
DFSIterative(
|
|
@@ -1068,7 +1068,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1068
1068
|
nodeOrPropertyName?: NodeOrPropertyName
|
|
1069
1069
|
): AbstractBinaryTreeNodeProperties<N> {
|
|
1070
1070
|
pattern = pattern || 'in';
|
|
1071
|
-
nodeOrPropertyName = nodeOrPropertyName || '
|
|
1071
|
+
nodeOrPropertyName = nodeOrPropertyName || 'key';
|
|
1072
1072
|
this._clearResults();
|
|
1073
1073
|
if (!this.root) return this._getResultByPropertyName(nodeOrPropertyName);
|
|
1074
1074
|
// 0: visit, 1: print
|
|
@@ -1109,11 +1109,11 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1109
1109
|
}
|
|
1110
1110
|
|
|
1111
1111
|
/**
|
|
1112
|
-
* Performs a level-order traversal on a binary tree starting from the specified node and accumulates properties of each node based on their '
|
|
1112
|
+
* 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
1113
|
* @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.
|
|
1114
1114
|
* @returns An array of binary tree node IDs.
|
|
1115
1115
|
*/
|
|
1116
|
-
levelIterative(node: N | null):
|
|
1116
|
+
levelIterative(node: N | null): BinaryTreeNodeKey[];
|
|
1117
1117
|
|
|
1118
1118
|
/**
|
|
1119
1119
|
* Performs a level-order traversal on a binary tree starting from the specified node and accumulates properties of each node based on the specified property name.
|
|
@@ -1121,7 +1121,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1121
1121
|
* @param {string} nodeOrPropertyName - The name of the property to accumulate.
|
|
1122
1122
|
* @returns An array of values corresponding to the specified property.
|
|
1123
1123
|
*/
|
|
1124
|
-
levelIterative(node: N | null, nodeOrPropertyName?: '
|
|
1124
|
+
levelIterative(node: N | null, nodeOrPropertyName?: 'key'): BinaryTreeNodeKey[];
|
|
1125
1125
|
|
|
1126
1126
|
/**
|
|
1127
1127
|
* Performs a level-order traversal on a binary tree starting from the specified node and accumulates the 'val' property of each node.
|
|
@@ -1146,13 +1146,13 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1146
1146
|
* node for the level order traversal. It can be null if no specific node is provided, in which case the root node of
|
|
1147
1147
|
* the tree is used as the starting node.
|
|
1148
1148
|
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
|
|
1149
|
-
* can be either a `BinaryTreeNode` property name or the string `'
|
|
1149
|
+
* can be either a `BinaryTreeNode` property name or the string `'key'`. If a property name is provided, the function
|
|
1150
1150
|
* will accumulate results based on that property. If no property name is provided, the function will default to
|
|
1151
|
-
* accumulating results based on the '
|
|
1151
|
+
* accumulating results based on the 'key' property.
|
|
1152
1152
|
* @returns An object of type `AbstractBinaryTreeNodeProperties<N>`.
|
|
1153
1153
|
*/
|
|
1154
1154
|
levelIterative(node: N | null, nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N> {
|
|
1155
|
-
nodeOrPropertyName = nodeOrPropertyName || '
|
|
1155
|
+
nodeOrPropertyName = nodeOrPropertyName || 'key';
|
|
1156
1156
|
node = node || this.root;
|
|
1157
1157
|
if (!node) return [];
|
|
1158
1158
|
|
|
@@ -1180,15 +1180,15 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1180
1180
|
* @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.
|
|
1181
1181
|
* @returns A 2D array of AbstractBinaryTreeNodeProperty<N> objects.
|
|
1182
1182
|
*/
|
|
1183
|
-
listLevels(node: N | null):
|
|
1183
|
+
listLevels(node: N | null): BinaryTreeNodeKey[][];
|
|
1184
1184
|
|
|
1185
1185
|
/**
|
|
1186
1186
|
* Collects nodes from a binary tree by a specified property and organizes them into levels.
|
|
1187
1187
|
* @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
|
-
* @param {'
|
|
1188
|
+
* @param {'key} nodeOrPropertyName - The property of the BinaryTreeNode object to collect at each level.
|
|
1189
1189
|
* @returns A 2D array of values corresponding to the specified property.
|
|
1190
1190
|
*/
|
|
1191
|
-
listLevels(node: N | null, nodeOrPropertyName?: '
|
|
1191
|
+
listLevels(node: N | null, nodeOrPropertyName?: 'key'): BinaryTreeNodeKey[][];
|
|
1192
1192
|
|
|
1193
1193
|
/**
|
|
1194
1194
|
* Collects nodes from a binary tree by a specified property and organizes them into levels.
|
|
@@ -1209,11 +1209,11 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1209
1209
|
/**
|
|
1210
1210
|
* The `listLevels` function collects nodes from a binary tree by a specified property and organizes them into levels.
|
|
1211
1211
|
* @param {N | null} node - The `node` parameter is a BinaryTreeNode object or null. It represents the root node of a binary tree. If it is null, the function will use the root node of the current binary tree instance.
|
|
1212
|
-
* @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: '
|
|
1212
|
+
* @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
1213
|
* @returns A 2D array of `AbstractBinaryTreeNodeProperty<N>` objects.
|
|
1214
1214
|
*/
|
|
1215
1215
|
listLevels(node: N | null, nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperty<N>[][] {
|
|
1216
|
-
nodeOrPropertyName = nodeOrPropertyName || '
|
|
1216
|
+
nodeOrPropertyName = nodeOrPropertyName || 'key';
|
|
1217
1217
|
node = node || this.root;
|
|
1218
1218
|
if (!node) return [];
|
|
1219
1219
|
|
|
@@ -1221,8 +1221,8 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1221
1221
|
|
|
1222
1222
|
const collectByProperty = (node: N, level: number) => {
|
|
1223
1223
|
switch (nodeOrPropertyName) {
|
|
1224
|
-
case '
|
|
1225
|
-
levelsNodes[level].push(node.
|
|
1224
|
+
case 'key':
|
|
1225
|
+
levelsNodes[level].push(node.key);
|
|
1226
1226
|
break;
|
|
1227
1227
|
case 'val':
|
|
1228
1228
|
levelsNodes[level].push(node.val);
|
|
@@ -1231,7 +1231,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1231
1231
|
levelsNodes[level].push(node);
|
|
1232
1232
|
break;
|
|
1233
1233
|
default:
|
|
1234
|
-
levelsNodes[level].push(node.
|
|
1234
|
+
levelsNodes[level].push(node.key);
|
|
1235
1235
|
break;
|
|
1236
1236
|
}
|
|
1237
1237
|
};
|
|
@@ -1290,15 +1290,15 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1290
1290
|
* Performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm.
|
|
1291
1291
|
* @returns An array of binary tree node IDs.
|
|
1292
1292
|
*/
|
|
1293
|
-
morris():
|
|
1293
|
+
morris(): BinaryTreeNodeKey[];
|
|
1294
1294
|
|
|
1295
1295
|
/**
|
|
1296
1296
|
* 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.
|
|
1297
1297
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
1298
|
-
* @param {'
|
|
1298
|
+
* @param {'key'} nodeOrPropertyName - The name of the property to accumulate.
|
|
1299
1299
|
* @returns An array of values corresponding to the specified property.
|
|
1300
1300
|
*/
|
|
1301
|
-
morris(pattern?: DFSOrderPattern, nodeOrPropertyName?: '
|
|
1301
|
+
morris(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'key'): BinaryTreeNodeKey[];
|
|
1302
1302
|
|
|
1303
1303
|
/**
|
|
1304
1304
|
* 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.
|
|
@@ -1319,7 +1319,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1319
1319
|
/**
|
|
1320
1320
|
* The `morris` function performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm.
|
|
1321
1321
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
1322
|
-
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The property name of the nodes to retrieve or perform operations on during the traversal. It can be any valid property name of the nodes in the binary tree. If not provided, it defaults to '
|
|
1322
|
+
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The property name of the nodes to retrieve or perform operations on during the traversal. It can be any valid property name of the nodes in the binary tree. If not provided, it defaults to 'key'.
|
|
1323
1323
|
* @returns An array of AbstractBinaryTreeNodeProperties<N> objects.
|
|
1324
1324
|
*/
|
|
1325
1325
|
morris(
|
|
@@ -1329,7 +1329,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1329
1329
|
if (this.root === null) return [];
|
|
1330
1330
|
|
|
1331
1331
|
pattern = pattern || 'in';
|
|
1332
|
-
nodeOrPropertyName = nodeOrPropertyName || '
|
|
1332
|
+
nodeOrPropertyName = nodeOrPropertyName || 'key';
|
|
1333
1333
|
|
|
1334
1334
|
this._clearResults();
|
|
1335
1335
|
|
|
@@ -1454,11 +1454,11 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1454
1454
|
}
|
|
1455
1455
|
|
|
1456
1456
|
/**
|
|
1457
|
-
* The function sets the value of the `
|
|
1458
|
-
* @param {
|
|
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
1459
|
*/
|
|
1460
|
-
protected
|
|
1461
|
-
this.
|
|
1460
|
+
protected _setVisitedKey(value: BinaryTreeNodeKey[]) {
|
|
1461
|
+
this._visitedKey = value;
|
|
1462
1462
|
}
|
|
1463
1463
|
|
|
1464
1464
|
/**
|
|
@@ -1502,7 +1502,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1502
1502
|
* properties.
|
|
1503
1503
|
*/
|
|
1504
1504
|
protected _clearResults() {
|
|
1505
|
-
this.
|
|
1505
|
+
this._visitedKey = [];
|
|
1506
1506
|
this._visitedVal = [];
|
|
1507
1507
|
this._visitedNode = [];
|
|
1508
1508
|
}
|
|
@@ -1512,11 +1512,11 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1512
1512
|
* a result array.
|
|
1513
1513
|
* @param {N} cur - The current node being processed.
|
|
1514
1514
|
* @param {(N | null | undefined)[]} result - An array that stores the matching nodes.
|
|
1515
|
-
* @param {
|
|
1515
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is either a `BinaryTreeNodeKey` or a `N`
|
|
1516
1516
|
* type. It represents the property value that we are comparing against in the switch statement.
|
|
1517
1517
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
1518
|
-
* specifies the property name to compare against when pushing nodes into the `result` array. It can be either `'
|
|
1519
|
-
* or `'val'`. If it is not provided or is not equal to `'
|
|
1518
|
+
* specifies the property name to compare against when pushing nodes into the `result` array. It can be either `'key'`
|
|
1519
|
+
* or `'val'`. If it is not provided or is not equal to `'key'` or `'val'`, the
|
|
1520
1520
|
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
|
|
1521
1521
|
* stop after finding the first matching node or continue searching for all matching nodes. If `onlyOne` is set to
|
|
1522
1522
|
* `true`, the function will stop after finding the first matching node and return `true`. If `onlyOne
|
|
@@ -1525,13 +1525,13 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1525
1525
|
protected _pushByPropertyNameStopOrNot(
|
|
1526
1526
|
cur: N,
|
|
1527
1527
|
result: (N | null | undefined)[],
|
|
1528
|
-
nodeProperty:
|
|
1528
|
+
nodeProperty: BinaryTreeNodeKey | N,
|
|
1529
1529
|
propertyName?: BinaryTreeNodePropertyName,
|
|
1530
1530
|
onlyOne?: boolean
|
|
1531
1531
|
) {
|
|
1532
1532
|
switch (propertyName) {
|
|
1533
|
-
case '
|
|
1534
|
-
if (cur.
|
|
1533
|
+
case 'key':
|
|
1534
|
+
if (cur.key === nodeProperty) {
|
|
1535
1535
|
result.push(cur);
|
|
1536
1536
|
return !!onlyOne;
|
|
1537
1537
|
}
|
|
@@ -1543,7 +1543,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1543
1543
|
}
|
|
1544
1544
|
break;
|
|
1545
1545
|
default:
|
|
1546
|
-
if (cur.
|
|
1546
|
+
if (cur.key === nodeProperty) {
|
|
1547
1547
|
result.push(cur);
|
|
1548
1548
|
return !!onlyOne;
|
|
1549
1549
|
}
|
|
@@ -1559,11 +1559,11 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1559
1559
|
* specifies the property name to be used for accumulating values. If it is a `Node` object, it specifies
|
|
1560
1560
|
*/
|
|
1561
1561
|
protected _accumulatedByPropertyName(node: N, nodeOrPropertyName?: NodeOrPropertyName) {
|
|
1562
|
-
nodeOrPropertyName = nodeOrPropertyName ?? '
|
|
1562
|
+
nodeOrPropertyName = nodeOrPropertyName ?? 'key';
|
|
1563
1563
|
|
|
1564
1564
|
switch (nodeOrPropertyName) {
|
|
1565
|
-
case '
|
|
1566
|
-
this.
|
|
1565
|
+
case 'key':
|
|
1566
|
+
this._visitedKey.push(node.key);
|
|
1567
1567
|
break;
|
|
1568
1568
|
case 'val':
|
|
1569
1569
|
this._visitedVal.push(node.val);
|
|
@@ -1572,7 +1572,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1572
1572
|
this._visitedNode.push(node);
|
|
1573
1573
|
break;
|
|
1574
1574
|
default:
|
|
1575
|
-
this.
|
|
1575
|
+
this._visitedKey.push(node.key);
|
|
1576
1576
|
break;
|
|
1577
1577
|
}
|
|
1578
1578
|
}
|
|
@@ -1590,17 +1590,17 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|
|
1590
1590
|
* @returns The method `_getResultByPropertyName` returns an instance of `AbstractBinaryTreeNodeProperties<N>`.
|
|
1591
1591
|
*/
|
|
1592
1592
|
protected _getResultByPropertyName(nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N> {
|
|
1593
|
-
nodeOrPropertyName = nodeOrPropertyName ?? '
|
|
1593
|
+
nodeOrPropertyName = nodeOrPropertyName ?? 'key';
|
|
1594
1594
|
|
|
1595
1595
|
switch (nodeOrPropertyName) {
|
|
1596
|
-
case '
|
|
1597
|
-
return this.
|
|
1596
|
+
case 'key':
|
|
1597
|
+
return this._visitedKey;
|
|
1598
1598
|
case 'val':
|
|
1599
1599
|
return this._visitedVal;
|
|
1600
1600
|
case 'node':
|
|
1601
1601
|
return this._visitedNode;
|
|
1602
1602
|
default:
|
|
1603
|
-
return this.
|
|
1603
|
+
return this._visitedKey;
|
|
1604
1604
|
}
|
|
1605
1605
|
}
|
|
1606
1606
|
|