diff --git a/src/data-structures/binary-tree/abstract-binary-tree.ts b/src/data-structures/binary-tree/abstract-binary-tree.ts index dcaea96..1aad936 100644 --- a/src/data-structures/binary-tree/abstract-binary-tree.ts +++ b/src/data-structures/binary-tree/abstract-binary-tree.ts @@ -364,7 +364,6 @@ export abstract class AbstractBinaryTree { + dfs( + pattern: DFSOrderPattern = 'in', + nodeOrPropertyName: NodeOrPropertyName = 'key' + ): AbstractBinaryTreeNodeProperties { this._clearResults(); const _traverse = (node: N) => { switch (pattern) { @@ -1087,7 +1093,10 @@ export abstract class AbstractBinaryTree`. */ - levelIterative(node: N | null = this.root, nodeOrPropertyName: NodeOrPropertyName = 'key'): AbstractBinaryTreeNodeProperties { + levelIterative( + node: N | null = this.root, + nodeOrPropertyName: NodeOrPropertyName = 'key' + ): AbstractBinaryTreeNodeProperties { if (!node) return []; this._clearResults(); @@ -1146,7 +1155,10 @@ export abstract class AbstractBinaryTree` objects. */ - listLevels(node: N | null = this.root, nodeOrPropertyName: NodeOrPropertyName = 'key'): AbstractBinaryTreeNodeProperty[][] { + listLevels( + node: N | null = this.root, + nodeOrPropertyName: NodeOrPropertyName = 'key' + ): AbstractBinaryTreeNodeProperty[][] { if (!node) return []; const levelsNodes: AbstractBinaryTreeNodeProperty[][] = []; @@ -1492,7 +1504,9 @@ export abstract class AbstractBinaryTree`. */ - protected _getResultByPropertyName(nodeOrPropertyName: NodeOrPropertyName = 'key'): AbstractBinaryTreeNodeProperties { + protected _getResultByPropertyName( + nodeOrPropertyName: NodeOrPropertyName = 'key' + ): AbstractBinaryTreeNodeProperties { switch (nodeOrPropertyName) { case 'key': return this.visitedKey; diff --git a/src/data-structures/binary-tree/bst.ts b/src/data-structures/binary-tree/bst.ts index 5ac85cd..b5b31e6 100644 --- a/src/data-structures/binary-tree/bst.ts +++ b/src/data-structures/binary-tree/bst.ts @@ -216,8 +216,7 @@ export class BST = BSTNode> extends BinaryTree * specifies the property name to use for searching the binary tree nodes. If not provided, it defaults to `'key'`. * @returns The method is returning either a BinaryTreeNodeKey or N (generic type) or null. */ - override get(nodeProperty: BinaryTreeNodeKey | N, propertyName?: BinaryTreeNodePropertyName): N | null { - propertyName = propertyName ?? 'key'; + override get(nodeProperty: BinaryTreeNodeKey | N, propertyName: BinaryTreeNodePropertyName = 'key'): N | null { return this.getNodes(nodeProperty, propertyName, true)[0] ?? null; } @@ -248,7 +247,7 @@ export class BST = BSTNode> extends BinaryTree override getNodes( nodeProperty: BinaryTreeNodeKey | N, propertyName: BinaryTreeNodePropertyName = 'key', - onlyOne?: boolean + onlyOne: boolean = false ): N[] { if (!this.root) return []; const result: N[] = []; @@ -298,8 +297,7 @@ export class BST = BSTNode> extends BinaryTree * @returns The function `lesserSum` returns a number, which represents the sum of the values of the nodes in the * binary tree that have a lesser value than the specified `beginNode` based on the `propertyName`. */ - lesserSum(beginNode: N | BinaryTreeNodeKey | null, propertyName?: BinaryTreeNodePropertyName): number { - propertyName = propertyName ?? 'key'; + lesserSum(beginNode: N | BinaryTreeNodeKey | null, propertyName: BinaryTreeNodePropertyName = 'key'): number { if (typeof beginNode === 'number') beginNode = this.get(beginNode, 'key'); if (!beginNode) return 0; if (!this.root) return 0; @@ -378,9 +376,8 @@ export class BST = BSTNode> extends BinaryTree allGreaterNodesAdd( node: N | BinaryTreeNodeKey | null, delta: number, - propertyName?: BinaryTreeNodePropertyName + propertyName: BinaryTreeNodePropertyName = 'key' ): boolean { - propertyName = propertyName ?? 'key'; if (typeof node === 'number') node = this.get(node, 'key'); if (!node) return false; const key = node.key; diff --git a/src/data-structures/binary-tree/tree-multiset.ts b/src/data-structures/binary-tree/tree-multiset.ts index cdcd741..48a989a 100644 --- a/src/data-structures/binary-tree/tree-multiset.ts +++ b/src/data-structures/binary-tree/tree-multiset.ts @@ -105,8 +105,7 @@ export class TreeMultiset = TreeMultiset * value should be added to the binary tree. If the `count` parameter is not provided, it defaults to 1. * @returns The method `add` returns either the inserted node (`N`), `null`, or `undefined`. */ - override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val'], count?: number): N | null | undefined { - count = count ?? 1; + override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val'], count: number = 1): N | null | undefined { let inserted: N | null | undefined = undefined, newNode: N | null; if (keyOrNode instanceof TreeMultisetNode) { @@ -294,7 +293,7 @@ export class TreeMultiset = TreeMultiset * not be taken into account when removing it. If `ignoreCount` is set to `false * @returns The function `remove` returns an array of `BinaryTreeDeletedResult` objects. */ - override remove(nodeOrKey: N | BinaryTreeNodeKey, ignoreCount?: boolean): BinaryTreeDeletedResult[] { + override remove(nodeOrKey: N | BinaryTreeNodeKey, ignoreCount: boolean = false): BinaryTreeDeletedResult[] { const bstDeletedResult: BinaryTreeDeletedResult[] = []; if (!this.root) return bstDeletedResult; @@ -475,7 +474,7 @@ export class TreeMultiset = TreeMultiset * to `true`, the function will return only one node. If `onlyOne` * @returns an array of nodes that match the given nodeProperty. */ - getNodesByCount(nodeProperty: BinaryTreeNodeKey | N, onlyOne?: boolean): N[] { + getNodesByCount(nodeProperty: BinaryTreeNodeKey | N, onlyOne: boolean = false): N[] { if (!this.root) return []; const result: N[] = []; @@ -541,35 +540,32 @@ export class TreeMultiset = TreeMultiset * traversal pattern for the Morris traversal algorithm. It can have one of three values: 'in', 'pre', or 'post'. * @returns The function `morrisCount` returns an array of numbers. */ - morrisCount(pattern?: 'in' | 'pre' | 'post'): number[] { - pattern = pattern || 'in'; + morrisCount(pattern: DFSOrderPattern = 'in'): number[] { const nodes = super.morris(pattern, 'node'); return nodes.map(node => node.count); } /** - * The function DFSIterativeCount performs an iterative depth-first search and returns an array of node counts based on + * The function dfsCountIterative performs an iterative depth-first search and returns an array of node counts based on * the specified traversal pattern. * @param {'in' | 'pre' | 'post'} [pattern] - The pattern parameter is a string that specifies the traversal order for * the Depth-First Search (dfs) algorithm. It can have three possible values: 'in', 'pre', or 'post'. - * @returns The DFSIterativeCount function returns an array of numbers, which represents the count property of each node + * @returns The dfsCountIterative function returns an array of numbers, which represents the count property of each node * in the dfs traversal. */ - DFSIterativeCount(pattern?: 'in' | 'pre' | 'post'): number[] { - pattern = pattern ?? 'in'; + dfsCountIterative(pattern: DFSOrderPattern = 'in'): number[] { const nodes = super.dfsIterative(pattern, 'node'); return nodes.map(node => node.count); } /** - * The DFSCount function returns an array of counts for each node in a depth-first search traversal. + * The dfsCount function returns an array of counts for each node in a depth-first search traversal. * @param {DFSOrderPattern} [pattern] - The pattern parameter is an optional parameter that specifies the order in which * the Depth-First Search (dfs) algorithm should traverse the nodes. It can have one of the following values: - * @returns The DFSCount function returns an array of numbers, specifically the count property of each node in the dfs + * @returns The dfsCount function returns an array of numbers, specifically the count property of each node in the dfs * traversal. */ - DFSCount(pattern?: DFSOrderPattern): number[] { - pattern = pattern ?? 'in'; + dfsCount(pattern: DFSOrderPattern = 'in'): number[] { const nodes = super.dfs(pattern, 'node'); return nodes.map(node => node.count); }