mirror of
https://github.com/zrwusa/data-structure-typed.git
synced 2024-11-23 12:54:04 +00:00
[binary-tree] To adhere to best practices, rename certain methods and replace optional parameters with default values.
This commit is contained in:
parent
31aa92e761
commit
8b9ef598e5
|
@ -364,7 +364,6 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|||
* @returns the height of the binary tree.
|
||||
*/
|
||||
getHeight(beginRoot: N | BinaryTreeNodeKey | null = this.root): number {
|
||||
|
||||
if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot, 'key');
|
||||
if (!beginRoot) return -1;
|
||||
|
||||
|
@ -475,7 +474,11 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|||
* function will stop traversing the tree and return the first matching node. If `only
|
||||
* @returns an array of nodes (type N).
|
||||
*/
|
||||
getNodes(nodeProperty: BinaryTreeNodeKey | N, propertyName: BinaryTreeNodePropertyName = 'key', onlyOne: boolean = false): N[] {
|
||||
getNodes(
|
||||
nodeProperty: BinaryTreeNodeKey | N,
|
||||
propertyName: BinaryTreeNodePropertyName = 'key',
|
||||
onlyOne: boolean = false
|
||||
): N[] {
|
||||
if (!this.root) return [];
|
||||
|
||||
const result: N[] = [];
|
||||
|
@ -936,7 +939,10 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|||
* @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'`.
|
||||
* @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.
|
||||
*/
|
||||
dfs(pattern: DFSOrderPattern = 'in', nodeOrPropertyName: NodeOrPropertyName = 'key'): AbstractBinaryTreeNodeProperties<N> {
|
||||
dfs(
|
||||
pattern: DFSOrderPattern = 'in',
|
||||
nodeOrPropertyName: NodeOrPropertyName = 'key'
|
||||
): AbstractBinaryTreeNodeProperties<N> {
|
||||
this._clearResults();
|
||||
const _traverse = (node: N) => {
|
||||
switch (pattern) {
|
||||
|
@ -1087,7 +1093,10 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|||
* accumulating results based on the 'key' property.
|
||||
* @returns An object of type `AbstractBinaryTreeNodeProperties<N>`.
|
||||
*/
|
||||
levelIterative(node: N | null = this.root, nodeOrPropertyName: NodeOrPropertyName = 'key'): AbstractBinaryTreeNodeProperties<N> {
|
||||
levelIterative(
|
||||
node: N | null = this.root,
|
||||
nodeOrPropertyName: NodeOrPropertyName = 'key'
|
||||
): AbstractBinaryTreeNodeProperties<N> {
|
||||
if (!node) return [];
|
||||
|
||||
this._clearResults();
|
||||
|
@ -1146,7 +1155,10 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|||
* @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'.
|
||||
* @returns A 2D array of `AbstractBinaryTreeNodeProperty<N>` objects.
|
||||
*/
|
||||
listLevels(node: N | null = this.root, nodeOrPropertyName: NodeOrPropertyName = 'key'): AbstractBinaryTreeNodeProperty<N>[][] {
|
||||
listLevels(
|
||||
node: N | null = this.root,
|
||||
nodeOrPropertyName: NodeOrPropertyName = 'key'
|
||||
): AbstractBinaryTreeNodeProperty<N>[][] {
|
||||
if (!node) return [];
|
||||
|
||||
const levelsNodes: AbstractBinaryTreeNodeProperty<N>[][] = [];
|
||||
|
@ -1492,7 +1504,9 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|||
* can accept either a `NodeOrPropertyName` type or be undefined.
|
||||
* @returns The method `_getResultByPropertyName` returns an instance of `AbstractBinaryTreeNodeProperties<N>`.
|
||||
*/
|
||||
protected _getResultByPropertyName(nodeOrPropertyName: NodeOrPropertyName = 'key'): AbstractBinaryTreeNodeProperties<N> {
|
||||
protected _getResultByPropertyName(
|
||||
nodeOrPropertyName: NodeOrPropertyName = 'key'
|
||||
): AbstractBinaryTreeNodeProperties<N> {
|
||||
switch (nodeOrPropertyName) {
|
||||
case 'key':
|
||||
return this.visitedKey;
|
||||
|
|
|
@ -216,8 +216,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|||
* 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<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|||
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<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|||
* @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<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|||
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;
|
||||
|
|
|
@ -105,8 +105,7 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = 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<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|||
* not be taken into account when removing it. If `ignoreCount` is set to `false
|
||||
* @returns The function `remove` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
||||
*/
|
||||
override remove(nodeOrKey: N | BinaryTreeNodeKey, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[] {
|
||||
override remove(nodeOrKey: N | BinaryTreeNodeKey, ignoreCount: boolean = false): BinaryTreeDeletedResult<N>[] {
|
||||
const bstDeletedResult: BinaryTreeDeletedResult<N>[] = [];
|
||||
if (!this.root) return bstDeletedResult;
|
||||
|
||||
|
@ -475,7 +474,7 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = 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<N extends TreeMultisetNode<N['val'], N> = 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);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue