From 0417ee50099877dfda209e78400a10d1ac88625a Mon Sep 17 00:00:00 2001 From: Revone Date: Mon, 23 Oct 2023 03:02:12 +0800 Subject: [PATCH] [binary-tree] Translate all traversal methods into callback function forms to enhance API convenience and readability. --- .../binary-tree/binary-tree.ts | 538 +++--------------- src/data-structures/binary-tree/bst.ts | 72 ++- .../binary-tree/tree-multiset.ts | 86 +-- src/data-structures/heap/heap.ts | 2 +- src/types/data-structures/binary-tree.ts | 4 + src/types/data-structures/index.ts | 3 + .../binary-tree/avl-tree.test.ts | 21 +- .../binary-tree/binary-tree.test.ts | 2 +- .../data-structures/binary-tree/bst.test.ts | 74 ++- .../binary-tree/overall.test.ts | 3 +- .../binary-tree/rb-tree.test.ts | 10 +- .../binary-tree/tree-multiset.test.ts | 37 +- 12 files changed, 235 insertions(+), 617 deletions(-) diff --git a/src/data-structures/binary-tree/binary-tree.ts b/src/data-structures/binary-tree/binary-tree.ts index 8f4037f..e49f9e2 100644 --- a/src/data-structures/binary-tree/binary-tree.ts +++ b/src/data-structures/binary-tree/binary-tree.ts @@ -7,19 +7,19 @@ */ import type { + BFSCallback, + BFSCallbackReturn, BinaryTreeNodeKey, BinaryTreeNodeNested, - BinaryTreeNodeProperties, - BinaryTreeNodeProperty, - BinaryTreeOptions + BinaryTreeOptions, + MapCallback, + MapCallbackReturn } from '../../types'; import { BinaryTreeDeletedResult, - BinaryTreeNodePropertyName, DFSOrderPattern, FamilyPosition, - LoopType, - NodeOrPropertyName + LoopType } from '../../types'; import {IBinaryTree} from '../../interfaces'; import {trampoline} from '../../utils'; @@ -127,6 +127,7 @@ export class BinaryTree = BinaryTreeNode> createNode(key: BinaryTreeNodeKey, val?: N['val']): N { return new BinaryTreeNode(key, val) as N; } + // TODO placeholder node may need redesigned private _root: N | null = null; @@ -150,12 +151,6 @@ export class BinaryTree = BinaryTreeNode> this._loopType = v; } - visitedKey: BinaryTreeNodeKey[] = []; - - visitedVal: N['val'][] = []; - - visitedNode: N[] = []; - /** * The `_swap` function swaps the location of two nodes in a binary tree. * @param {N} srcNode - The source node that you want to _swap with the destination node. @@ -184,7 +179,6 @@ export class BinaryTree = BinaryTreeNode> clear() { this._root = null; this._size = 0; - this._clearResults(); } /** @@ -237,7 +231,7 @@ export class BinaryTree = BinaryTreeNode> return; } - const existNode = keyOrNode ? this.get(keyOrNode, 'key') : undefined; + const existNode = keyOrNode ? this.get(keyOrNode, this._defaultCallbackByKey) : undefined; if (this.root) { if (existNode) { @@ -360,8 +354,8 @@ export class BinaryTree = BinaryTreeNode> * @returns the depth of the given node or binary tree. */ getDepth(distNode: N | BinaryTreeNodeKey | null, beginRoot: N | BinaryTreeNodeKey | null = this.root): number { - if (typeof distNode === 'number') distNode = this.get(distNode, 'key'); - if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot, 'key'); + if (typeof distNode === 'number') distNode = this.get(distNode); + if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot); let depth = 0; while (distNode?.parent) { if (distNode === beginRoot) { @@ -381,7 +375,7 @@ export class BinaryTree = BinaryTreeNode> * @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 (typeof beginRoot === 'number') beginRoot = this.get(beginRoot); if (!beginRoot) return -1; if (this._loopType === LoopType.RECURSIVE) { @@ -419,6 +413,8 @@ export class BinaryTree = BinaryTreeNode> } } + protected _defaultCallbackByKey: MapCallback = node => node.key; + /** * The `getMinHeight` function calculates the minimum height of a binary tree using either a recursive or iterative * approach. @@ -482,125 +478,108 @@ export class BinaryTree = BinaryTreeNode> /** * The function `getNodes` returns an array of nodes that match a given property name and value in a binary tree. + * @param callback * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a * generic type `N`. It represents the property of the binary tree node that you want to search for. - * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that * specifies the property name to use when searching for nodes. If not provided, it defaults to 'key'. * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to * return only one node that matches the given `nodeProperty` or `propertyName`. If `onlyOne` is set to `true`, the * function will stop traversing the tree and return the first matching node. If `only + * @param beginRoot * @returns an array of nodes (type N). */ getNodes( nodeProperty: BinaryTreeNodeKey | N, - propertyName: BinaryTreeNodePropertyName = 'key', - onlyOne = false + callback: MapCallback = this._defaultCallbackByKey, + onlyOne = false, + beginRoot: N | null = this.root ): N[] { - if (!this.root) return []; + if (!beginRoot) return []; - const result: N[] = []; + const ans: N[] = []; if (this.loopType === LoopType.RECURSIVE) { const _traverse = (cur: N) => { - if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne)) return; + if (callback(cur) === nodeProperty) { + ans.push(cur); + if (onlyOne) return; + } if (!cur.left && !cur.right) return; cur.left && _traverse(cur.left); cur.right && _traverse(cur.right); }; - _traverse(this.root); + _traverse(beginRoot); } else { - const queue = new Queue([this.root]); + const queue = new Queue([beginRoot]); while (queue.size > 0) { const cur = queue.shift(); if (cur) { - if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne)) return result; + if (callback(cur) === nodeProperty) { + ans.push(cur); + if (onlyOne) return ans; + } cur.left && queue.push(cur.left); cur.right && queue.push(cur.right); } } } - return result; + return ans; } /** * The function checks if a binary tree node has a specific property. + * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value. * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or `N`. * It represents the property of the binary tree node that you want to check. - * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that * specifies the name of the property to be checked in the nodes. If not provided, it defaults to 'key'. * @returns a boolean value. */ - has(nodeProperty: BinaryTreeNodeKey | N, propertyName: BinaryTreeNodePropertyName = 'key'): boolean { + has(nodeProperty: BinaryTreeNodeKey | N, callback: MapCallback = this._defaultCallbackByKey): boolean { // TODO may support finding node by value equal - return this.getNodes(nodeProperty, propertyName).length > 0; + return this.getNodes(nodeProperty, callback, true).length > 0; } /** * The function returns the first node that matches the given property name and value, or null if no matching node is * found. + * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value. * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or `N`. * It represents the property of the binary tree node that you want to search for. - * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that * specifies the property name to be used for searching the binary tree nodes. If this parameter is not provided, the * default value is set to `'key'`. * @returns either the value of the specified property of the node, or the node itself if no property name is provided. * If no matching node is found, it returns null. */ - get(nodeProperty: BinaryTreeNodeKey | N, propertyName: BinaryTreeNodePropertyName = 'key'): N | null { + get(nodeProperty: BinaryTreeNodeKey | N, callback: MapCallback = this._defaultCallbackByKey): N | null { // TODO may support finding node by value equal - return this.getNodes(nodeProperty, propertyName, true)[0] ?? null; + return this.getNodes(nodeProperty, callback, true)[0] ?? null; } /** * The function `getPathToRoot` returns an array of nodes representing the path from a given node to the root node, with * an option to reverse the order of the nodes. - * @param {N} node - The `node` parameter represents a node in a tree structure. It is of type `N`, which could be any * type that represents a node in your specific implementation. + * @param beginRoot - The `beginRoot` parameter is of type `N` and represents the starting node from which you want to * @param {boolean} [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the resulting * path should be reversed or not. If `isReverse` is set to `true`, the path will be reversed before returning it. If * `isReverse` is set to `false` or not provided, the path will * @returns The function `getPathToRoot` returns an array of nodes (`N[]`). */ - getPathToRoot(node: N, isReverse = true): N[] { + getPathToRoot(beginRoot: N, isReverse = true): N[] { // TODO to support get path through passing key const result: N[] = []; - while (node.parent) { + while (beginRoot.parent) { // Array.push + Array.reverse is more efficient than Array.unshift // TODO may consider using Deque, so far this is not the performance bottleneck - result.push(node); - node = node.parent; + result.push(beginRoot); + beginRoot = beginRoot.parent; } - result.push(node); + result.push(beginRoot); return isReverse ? result.reverse() : result; } - /** - * The function `getLeftMost` returns the leftmost node in a binary tree, starting from a specified node or the root if - * no node is specified. - * generic type representing a node in a binary tree, `BinaryTreeNodeKey` (a type representing the ID of a binary tree - * node), or `null`. - * @returns The function `getLeftMost` returns the leftmost node in a binary tree. If the `beginRoot` parameter is - * provided, it starts the traversal from that node. If `beginRoot` is not provided or is `null`, it starts the traversal - * from the root of the binary tree. The function returns the leftmost node found during the traversal. If no leftmost - * node is found ( - */ - getLeftMost(): N | null; - - /** - * The function `getLeftMost` returns the leftmost node in a binary tree, starting from a specified node or the root if - * no node is specified. - * @param {N | BinaryTreeNodeKey | null} [node] - The `beginRoot` parameter is optional and can be of type `N` (a - * generic type representing a node in a binary tree), `BinaryTreeNodeKey` (a type representing the ID of a binary tree - * node). - * @returns The function `getLeftMost` returns the leftmost node in a binary tree. If the `beginRoot` parameter is - * provided, it starts the traversal from that node. If `beginRoot` is not provided or is `null`, it starts the traversal - * from the root of the binary tree. The function returns the leftmost node found during the traversal. If no leftmost - * node is found ( - */ - getLeftMost(node: N): N; - /** * The function `getLeftMost` returns the leftmost node in a binary tree, starting from a specified node or the root if * no node is specified. @@ -613,7 +592,7 @@ export class BinaryTree = BinaryTreeNode> * node is found ( */ getLeftMost(beginRoot: N | BinaryTreeNodeKey | null = this.root): N | null { - if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot, 'key'); + if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot); if (!beginRoot) return beginRoot; @@ -635,25 +614,6 @@ export class BinaryTree = BinaryTreeNode> } } - /** - * The `getRightMost` function returns the rightmost node in a binary tree, either recursively or iteratively using tail - * recursion optimization. - * @returns The `getRightMost` function returns the rightmost node in a binary tree. It returns the - * rightmost node starting from the root of the binary tree. - */ - getRightMost(): N | null; - - /** - * The `getRightMost` function returns the rightmost node in a binary tree, either recursively or iteratively using tail - * recursion optimization. - * @param {N | null} [beginRoot] - The `node` parameter is an optional parameter of type `N` or `null`. It represents the - * starting node from which we want to find the rightmost node. If no node is provided, the function will default to - * using the root node of the data structure. - * @returns The `getRightMost` function returns the rightmost node in a binary tree. It returns the rightmost node - * starting from that node. - */ - getRightMost(beginRoot: N): N; - /** * The `getRightMost` function returns the rightmost node in a binary tree, either recursively or iteratively using tail * recursion optimization. @@ -730,55 +690,25 @@ export class BinaryTree = BinaryTreeNode> } /** - * The function calculates the size of a subtree by traversing it either recursively or iteratively. - * @param {N | null | undefined} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree in a - * binary tree. - * @returns the size of the subtree rooted at `subTreeRoot`. - */ - getSubTreeSize(subTreeRoot: N | null | undefined) { - // TODO support key passed in - let size = 0; - if (!subTreeRoot) return size; - - if (this._loopType === LoopType.RECURSIVE) { - const _traverse = (cur: N) => { - size++; - cur.left && _traverse(cur.left); - cur.right && _traverse(cur.right); - }; - - _traverse(subTreeRoot); - return size; - } else { - const stack: N[] = [subTreeRoot]; - - while (stack.length > 0) { - const cur = stack.pop()!; - size++; - cur.right && stack.push(cur.right); - cur.left && stack.push(cur.left); - } - - return size; - } - } - - /** - * The function `subTreeForeach` adds a delta value to a specified property of each node in a subtree. + * The function `subTreeTraverse` adds a delta value to a specified property of each node in a subtree. * @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary * tree or the ID of a node in the binary tree. It can also be `null` if there is no subtree to add to. * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value. * specifies the property of the binary tree node that should be modified. If not provided, it defaults to 'key'. * @returns a boolean value. */ - subTreeForeach(subTreeRoot: N | BinaryTreeNodeKey | null, callback: (node: N) => any): boolean { - if (typeof subTreeRoot === 'number') subTreeRoot = this.get(subTreeRoot, 'key'); + subTreeTraverse( + callback: MapCallback = this._defaultCallbackByKey, + subTreeRoot: N | BinaryTreeNodeKey | null = this.root + ): MapCallbackReturn[] { + if (typeof subTreeRoot === 'number') subTreeRoot = this.get(subTreeRoot); - if (!subTreeRoot) return false; + const ans: MapCallbackReturn[] = []; + if (!subTreeRoot) return ans; if (this._loopType === LoopType.RECURSIVE) { const _traverse = (cur: N) => { - callback(cur); + ans.push(callback(cur)); cur.left && _traverse(cur.left); cur.right && _traverse(cur.right); }; @@ -790,150 +720,64 @@ export class BinaryTree = BinaryTreeNode> while (stack.length > 0) { const cur = stack.pop()!; - callback(cur); + ans.push(callback(cur)); cur.right && stack.push(cur.right); cur.left && stack.push(cur.left); } } - return true; + return ans; } - /** - * Performs a breadth-first search (bfs) on a binary tree, accumulating properties of each node based on their 'key' property. - * @returns An array of binary tree node IDs. - */ - bfs(): BinaryTreeNodeKey[]; - - /** - * Performs a breadth-first search (bfs) on a binary tree, accumulating properties of each node based on the specified property name. - * @param {'key'} nodeOrPropertyName - The name of the property to accumulate. - * @returns An array of values corresponding to the specified property. - */ - bfs(nodeOrPropertyName: 'key'): BinaryTreeNodeKey[]; - - /** - * Performs a breadth-first search (bfs) on a binary tree, accumulating the 'val' property of each node. - * @param {'val'} nodeOrPropertyName - The name of the property to accumulate. - * @returns An array of 'val' properties from each node. - */ - bfs(nodeOrPropertyName: 'val'): N['val'][]; - - /** - * Performs a breadth-first search (bfs) on a binary tree, accumulating nodes themselves. - * @param {'node'} nodeOrPropertyName - The name of the property to accumulate. - * @returns An array of binary tree nodes. - */ - bfs(nodeOrPropertyName: 'node'): N[]; - - /** - * The bfs function performs a breadth-first search on a binary tree, accumulating properties of each node based on a specified property name. - * @param {NodeOrPropertyName} [nodeOrPropertyName] - An optional parameter that represents either a node or a property name. - * If a node is provided, the bfs algorithm will be performed starting from that node. - * If a property name is provided, the bfs algorithm will be performed starting from the root node, accumulating the specified property. - * @returns An instance of the `BinaryTreeNodeProperties` class with generic type `N`. - */ - bfs(nodeOrPropertyName: NodeOrPropertyName = 'key'): BinaryTreeNodeProperties { - this._clearResults(); - const queue = new Queue([this.root]); - - while (queue.size !== 0) { - const cur = queue.shift(); - if (cur) { - this._accumulatedByPropertyName(cur, nodeOrPropertyName); - if (cur?.left !== null) queue.push(cur.left); - if (cur?.right !== null) queue.push(cur.right); - } - } - - return this._getResultByPropertyName(nodeOrPropertyName); - } - - /** - * Performs a depth-first search (dfs) traversal on a binary tree and accumulates properties of each node based on their 'key' property. - * @returns An array of binary tree node IDs. - */ - dfs(): BinaryTreeNodeKey[]; - - /** - * Performs a depth-first search (dfs) traversal on a binary tree and accumulates properties of each node based on the specified property name. - * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order). - * @returns An array of values corresponding to the specified property. - */ - dfs(pattern: DFSOrderPattern): BinaryTreeNodeKey[]; - - /** - * Performs a depth-first search (dfs) traversal on a binary tree and accumulates properties of each node based on the specified property name. - * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order). - * @param {string} nodeOrPropertyName - The name of the property to accumulate. - * @param loopType - The type of loop to use for the depth-first search traversal. The default value is `LoopType.ITERATIVE`. - * @returns An array of values corresponding to the specified property. - */ - dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'key', loopType?: LoopType): BinaryTreeNodeKey[]; - - /** - * Performs a depth-first search (dfs) traversal on a binary tree and accumulates the 'val' property of each node. - * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order). - * @param {'val'} nodeOrPropertyName - The name of the property to accumulate. - * @param loopType - The type of loop to use for the depth-first search traversal. The default value is `LoopType.ITERATIVE`. - * @returns An array of 'val' properties from each node. - */ - dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'val', loopType?: LoopType): N[]; - - /** - * Performs a depth-first search (dfs) traversal on a binary tree and accumulates nodes themselves. - * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order). - * @param {'node'} nodeOrPropertyName - The name of the property to accumulate. - * @param loopType - The type of loop to use for the depth-first search traversal. The default value is `LoopType.ITERATIVE`. - * @returns An array of binary tree nodes. - */ - dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'node', loopType?: LoopType): N[]; - /** * The dfs function performs a depth-first search traversal on a binary tree and returns the accumulated properties of * each node based on the specified pattern and property name. + * @param callback + * @param beginRoot - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It represents the * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order). - * @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'`. * @param loopType - The type of loop to use for the depth-first search traversal. The default value is `LoopType.ITERATIVE`. * @returns an instance of the BinaryTreeNodeProperties class, which contains the accumulated properties of the binary tree nodes based on the specified pattern and node or property name. */ dfs( + callback: MapCallback = this._defaultCallbackByKey, pattern: DFSOrderPattern = 'in', - nodeOrPropertyName: NodeOrPropertyName = 'key', + beginRoot: N | null = this.root, loopType: LoopType = LoopType.ITERATIVE - ): BinaryTreeNodeProperties { - this._clearResults(); + ): MapCallbackReturn[] { + if (!beginRoot) return []; + const ans: MapCallbackReturn[] = []; if (loopType === LoopType.RECURSIVE) { const _traverse = (node: N) => { switch (pattern) { case 'in': if (node.left) _traverse(node.left); - this._accumulatedByPropertyName(node, nodeOrPropertyName); + ans.push(callback(node)); if (node.right) _traverse(node.right); break; case 'pre': - this._accumulatedByPropertyName(node, nodeOrPropertyName); + ans.push(callback(node)); + if (node.left) _traverse(node.left); if (node.right) _traverse(node.right); break; case 'post': if (node.left) _traverse(node.left); if (node.right) _traverse(node.right); - this._accumulatedByPropertyName(node, nodeOrPropertyName); + ans.push(callback(node)); + break; } }; - this.root && _traverse(this.root); + _traverse(beginRoot); } else { - if (!this.root) return this._getResultByPropertyName(nodeOrPropertyName); // 0: visit, 1: print - const stack: {opt: 0 | 1; node: N | null | undefined}[] = [{opt: 0, node: this.root}]; + const stack: {opt: 0 | 1; node: N | null | undefined}[] = [{opt: 0, node: beginRoot}]; while (stack.length > 0) { const cur = stack.pop(); if (!cur || !cur.node) continue; if (cur.opt === 1) { - this._accumulatedByPropertyName(cur.node, nodeOrPropertyName); + ans.push(callback(cur.node)); } else { switch (pattern) { case 'in': @@ -961,83 +805,30 @@ export class BinaryTree = BinaryTreeNode> } } - return this._getResultByPropertyName(nodeOrPropertyName); + return ans; } // --- start additional methods --- - /** - * Collects nodes from a binary tree by a specified property and organizes them into levels. - * @returns A 2D array of AbstractBinaryTreeNodeProperty objects. - */ - listLevels(): BinaryTreeNodeKey[][]; - - /** - * Collects nodes from a binary tree by a specified property and organizes them into levels. - * @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. - * @returns A 2D array of AbstractBinaryTreeNodeProperty objects. - */ - listLevels(node: N | null): BinaryTreeNodeKey[][]; - - /** - * Collects nodes from a binary tree by a specified property and organizes them into levels. - * @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. - * @param {'key} nodeOrPropertyName - The property of the BinaryTreeNode object to collect at each level. - * @returns A 2D array of values corresponding to the specified property. - */ - listLevels(node: N | null, nodeOrPropertyName: 'key'): BinaryTreeNodeKey[][]; - - /** - * Collects nodes from a binary tree by a specified property and organizes them into levels. - * @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. - * @param {'val'} nodeOrPropertyName - The property of the BinaryTreeNode object to collect at each level. - * @returns A 2D array of 'val' properties from each node. - */ - listLevels(node: N | null, nodeOrPropertyName: 'val'): N['val'][][]; - - /** - * Collects nodes from a binary tree by a specified property and organizes them into levels. - * @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. - * @param {'node'} nodeOrPropertyName - The property of the BinaryTreeNode object to collect at each level. - * @returns A 2D array of binary tree nodes. - */ - listLevels(node: N | null, nodeOrPropertyName: 'node'): N[][]; - /** * The `listLevels` function collects nodes from a binary tree by a specified property and organizes them into levels. * @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. - * @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` objects. + * @param callback - The `callback` parameter is a function that takes a node and a level as parameters and returns a value. + * @param withLevel - The `withLevel` parameter is a boolean flag that determines whether to include the level of each node in the result. If `withLevel` is set to `true`, the function will include the level of each node in the result. If `withLevel` is set to `false` or not provided, the function will not include the level of each node in the result. */ - listLevels( - node: N | null = this.root, - nodeOrPropertyName: NodeOrPropertyName = 'key' - ): BinaryTreeNodeProperty[][] { + bfs( + callback: BFSCallback = this._defaultCallbackByKey, + withLevel: boolean = false, + node?: N | null + ): BFSCallbackReturn[] { + if (!node) node = this.root; if (!node) return []; - const levelsNodes: BinaryTreeNodeProperty[][] = []; - - const collectByProperty = (node: N, level: number) => { - switch (nodeOrPropertyName) { - case 'key': - levelsNodes[level].push(node.key); - break; - case 'val': - levelsNodes[level].push(node.val); - break; - case 'node': - levelsNodes[level].push(node); - break; - default: - levelsNodes[level].push(node.key); - break; - } - }; + const ans: BFSCallbackReturn[] = []; if (this.loopType === LoopType.RECURSIVE) { const _recursive = (node: N, level: number) => { - if (!levelsNodes[level]) levelsNodes[level] = []; - collectByProperty(node, level); + callback && ans.push(callback(node, withLevel ? level : undefined)); if (node.left) _recursive(node.left, level + 1); if (node.right) _recursive(node.right, level + 1); }; @@ -1050,14 +841,12 @@ export class BinaryTree = BinaryTreeNode> const head = stack.pop()!; const [node, level] = head; - if (!levelsNodes[level]) levelsNodes[level] = []; - collectByProperty(node, level); + callback && ans.push(callback(node, withLevel ? level : undefined)); if (node.right) stack.push([node.right, level + 1]); if (node.left) stack.push([node.left, level + 1]); } } - - return levelsNodes; + return ans; } /** @@ -1084,53 +873,18 @@ export class BinaryTree = BinaryTreeNode> * Space complexity of Iterative dfs equals to recursive dfs which is O(n) because of the stack */ - /** - * Performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm. - * @returns An array of binary tree node IDs. - */ - morris(): BinaryTreeNodeKey[]; - - /** - * 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. - * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order). - * @param {'key'} nodeOrPropertyName - The name of the property to accumulate. - * @returns An array of values corresponding to the specified property. - */ - morris(pattern: DFSOrderPattern, nodeOrPropertyName: 'key'): BinaryTreeNodeKey[]; - - /** - * 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. - * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order). - * @returns An array of values corresponding to the specified property. - */ - morris(pattern: DFSOrderPattern): BinaryTreeNodeKey[]; - - /** - * 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. - * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order). - * @param {'val'} nodeOrPropertyName - The property of the BinaryTreeNode object to collect at each level. - * @returns An array of 'val' properties from each node. - */ - morris(pattern: DFSOrderPattern, nodeOrPropertyName: 'val'): N[]; - - /** - * Performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm and accumulates nodes themselves. - * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order). - * @param {'node'} nodeOrPropertyName - The property of the BinaryTreeNode object to collect at each level. - * @returns An array of binary tree nodes. - */ - morris(pattern: DFSOrderPattern, nodeOrPropertyName: 'node'): N[]; - /** * The `morris` function performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm. * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order). - * @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'. + * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value. * @returns An array of BinaryTreeNodeProperties objects. */ - morris(pattern: DFSOrderPattern = 'in', nodeOrPropertyName: NodeOrPropertyName = 'key'): BinaryTreeNodeProperties { + morris( + callback: MapCallback = this._defaultCallbackByKey, + pattern: DFSOrderPattern = 'in' + ): MapCallbackReturn[] { if (this.root === null) return []; - - this._clearResults(); + const ans: MapCallbackReturn[] = []; let cur: N | null | undefined = this.root; const _reverseEdge = (node: N | null | undefined) => { @@ -1148,7 +902,7 @@ export class BinaryTree = BinaryTreeNode> const tail: N | null | undefined = _reverseEdge(node); let cur: N | null | undefined = tail; while (cur) { - this._accumulatedByPropertyName(cur, nodeOrPropertyName); + ans.push(callback(cur)); cur = cur.right; } _reverseEdge(tail); @@ -1166,7 +920,7 @@ export class BinaryTree = BinaryTreeNode> predecessor.right = null; } } - this._accumulatedByPropertyName(cur, nodeOrPropertyName); + ans.push(callback(cur)); cur = cur.right; } break; @@ -1176,14 +930,14 @@ export class BinaryTree = BinaryTreeNode> const predecessor = this.getPredecessor(cur); if (!predecessor.right) { predecessor.right = cur; - this._accumulatedByPropertyName(cur, nodeOrPropertyName); + ans.push(callback(cur)); cur = cur.left; continue; } else { predecessor.right = null; } } else { - this._accumulatedByPropertyName(cur, nodeOrPropertyName); + ans.push(callback(cur)); } cur = cur.right; } @@ -1206,8 +960,7 @@ export class BinaryTree = BinaryTreeNode> _printEdge(this.root); break; } - - return this._getResultByPropertyName(nodeOrPropertyName); + return ans; } /** @@ -1264,108 +1017,5 @@ export class BinaryTree = BinaryTreeNode> this._size = v; } - /** - * The function `_clearResults` resets the values of several arrays used for tracking visited nodes and their - * properties. - */ - protected _clearResults() { - this.visitedKey = []; - this.visitedVal = []; - this.visitedNode = []; - } - - /** - * The function checks if a given property of a binary tree node matches a specified value, and if so, adds the node to - * a result array. - * @param {N} cur - The current node being processed. - * @param {(N | null | undefined)[]} result - An array that stores the matching nodes. - * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is either a `BinaryTreeNodeKey` or a `N` - * type. It represents the property value that we are comparing against in the switch statement. - * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that - * specifies the property name to compare against when pushing nodes into the `result` array. It can be either `'key'` - * or `'val'`. If it is not provided or is not equal to `'key'` or `'val'`, the - * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to - * stop after finding the first matching node or continue searching for all matching nodes. If `onlyOne` is set to - * `true`, the function will stop after finding the first matching node and return `true`. If `onlyOne - * @returns a boolean value indicating whether only one matching node should be pushed into the result array. - */ - protected _pushByPropertyNameStopOrNot( - cur: N, - result: (N | null | undefined)[], - nodeProperty: BinaryTreeNodeKey | N, - propertyName: BinaryTreeNodePropertyName = 'key', - onlyOne = false - ) { - switch (propertyName) { - case 'key': - if (cur.key === nodeProperty) { - result.push(cur); - return onlyOne; - } - break; - case 'val': - if (cur.val === nodeProperty) { - result.push(cur); - return onlyOne; - } - break; - default: - if (cur.key === nodeProperty) { - result.push(cur); - return onlyOne; - } - break; - } - } - - /** - * The function `_accumulatedByPropertyName` accumulates values from a given node based on the specified property name. - * @param {N} node - The `node` parameter is of type `N`, which represents a node in a data structure. - * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that - * can be either a string representing a property name or a reference to a `Node` object. If it is a string, it - * specifies the property name to be used for accumulating values. If it is a `Node` object, it specifies - */ - protected _accumulatedByPropertyName(node: N, nodeOrPropertyName: NodeOrPropertyName = 'key') { - switch (nodeOrPropertyName) { - case 'key': - this.visitedKey.push(node.key); - break; - case 'val': - this.visitedVal.push(node.val); - break; - case 'node': - this.visitedNode.push(node); - break; - default: - this.visitedKey.push(node.key); - break; - } - } - - /** - * The time complexity of Morris traversal is O(n), it may slower than others - * The space complexity Morris traversal is O(1) because no using stack - */ - - /** - * The function `_getResultByPropertyName` returns the corresponding property value based on the given node or property - * name. - * @param {NodeOrPropertyName} [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is an optional parameter that - * can accept either a `NodeOrPropertyName` type or be undefined. - * @returns The method `_getResultByPropertyName` returns an instance of `BinaryTreeNodeProperties`. - */ - protected _getResultByPropertyName(nodeOrPropertyName: NodeOrPropertyName = 'key'): BinaryTreeNodeProperties { - switch (nodeOrPropertyName) { - case 'key': - return this.visitedKey; - case 'val': - return this.visitedVal; - case 'node': - return this.visitedNode; - default: - return this.visitedKey; - } - } - // --- end additional methods --- } diff --git a/src/data-structures/binary-tree/bst.ts b/src/data-structures/binary-tree/bst.ts index c4b49ed..e5ae80c 100644 --- a/src/data-structures/binary-tree/bst.ts +++ b/src/data-structures/binary-tree/bst.ts @@ -7,10 +7,11 @@ */ import type { BinaryTreeNodeKey, - BinaryTreeNodePropertyName, BSTComparator, BSTNodeNested, - BSTOptions + BSTOptions, + MapCallback, + MapCallbackReturn } from '../../types'; import {CP, LoopType} from '../../types'; import {BinaryTree, BinaryTreeNode} from './binary-tree'; @@ -211,12 +212,12 @@ export class BST = BSTNode> extends BinaryTree * The function returns the first node in a binary tree that matches the given property name and value. * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a * generic type `N`. It represents the property of the binary tree node that you want to search for. - * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that + * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value. * 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 = 'key'): N | null { - return this.getNodes(nodeProperty, propertyName, true)[0] ?? null; + override get(nodeProperty: BinaryTreeNodeKey | N, callback: MapCallback = this._defaultCallbackByKey): N | null { + return this.getNodes(nodeProperty, callback, true)[0] ?? null; } /** @@ -236,27 +237,34 @@ export class BST = BSTNode> extends BinaryTree * The function `getNodes` returns an array of nodes in a binary tree that match a given property value. * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or an * `N` type. It represents the property of the binary tree node that you want to compare with. - * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that + * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value. * specifies the property name to use for comparison. If not provided, it defaults to `'key'`. * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to * return only one node that matches the given `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne` * is set to `true`, the function will return an array with only one node (if + * @param beginRoot - The `beginRoot` parameter is an optional parameter that specifies the root node from which to * @returns an array of nodes (type N). */ override getNodes( nodeProperty: BinaryTreeNodeKey | N, - propertyName: BinaryTreeNodePropertyName = 'key', - onlyOne = false + callback: MapCallback = this._defaultCallbackByKey, + onlyOne = false, + beginRoot: N | null = this.root ): N[] { - if (!this.root) return []; - const result: N[] = []; + if (!beginRoot) return []; + const ans: N[] = []; if (this.loopType === LoopType.RECURSIVE) { const _traverse = (cur: N) => { - if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne)) return; + const callbackResult = callback(cur); + if (callbackResult === nodeProperty) { + ans.push(cur); + if (onlyOne) return; + } if (!cur.left && !cur.right) return; - if (propertyName === 'key') { + // TODO potential bug + if (callback === this._defaultCallbackByKey) { if (this._compare(cur.key, nodeProperty as number) === CP.gt) cur.left && _traverse(cur.left); if (this._compare(cur.key, nodeProperty as number) === CP.lt) cur.right && _traverse(cur.right); } else { @@ -265,14 +273,19 @@ export class BST = BSTNode> extends BinaryTree } }; - _traverse(this.root); + _traverse(beginRoot); } else { - const queue = new Queue([this.root]); + const queue = new Queue([beginRoot]); while (queue.size > 0) { const cur = queue.shift(); if (cur) { - if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne)) return result; - if (propertyName === 'key') { + const callbackResult = callback(cur); + if (callbackResult === nodeProperty) { + ans.push(cur); + if (onlyOne) return ans; + } + // TODO potential bug + if (callback === this._defaultCallbackByKey) { if (this._compare(cur.key, nodeProperty as number) === CP.gt) cur.left && queue.push(cur.left); if (this._compare(cur.key, nodeProperty as number) === CP.lt) cur.right && queue.push(cur.right); } else { @@ -283,33 +296,34 @@ export class BST = BSTNode> extends BinaryTree } } - return result; + return ans; } // --- start additional functions /** - * The `lesserOrGreaterForeach` function adds a delta value to the specified property of all nodes in a binary tree that + * The `lesserOrGreaterTraverse` function adds a delta value to the specified property of all nodes in a binary tree that * have a greater value than a given node. + * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value. * @param {N | BinaryTreeNodeKey | null} node - The `node` parameter can be either of type `N` (a generic type), `BinaryTreeNodeKey`, or `null`. It * represents the node in the binary tree to which the delta value will be added. * @param lesserOrGreater - The `lesserOrGreater` parameter is an optional parameter that specifies whether the delta - * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a boolean */ - lesserOrGreaterForeach( - node: N | BinaryTreeNodeKey | null, + lesserOrGreaterTraverse( + callback: MapCallback = this._defaultCallbackByKey, lesserOrGreater: CP = CP.lt, - callback: (node: N) => void - ): boolean { - if (typeof node === 'number') node = this.get(node, 'key'); - if (!node) return false; + node: N | BinaryTreeNodeKey | null + ): MapCallbackReturn { + if (typeof node === 'number') node = this.get(node); + const ans: MapCallbackReturn[] = []; + if (!node) return []; const key = node.key; if (!this.root) return false; if (this.loopType === LoopType.RECURSIVE) { const _traverse = (cur: N) => { const compared = this._compare(cur.key, key); - if (compared === lesserOrGreater) callback(cur); + if (compared === lesserOrGreater) ans.push(callback(cur)); if (!cur.left && !cur.right) return; if (cur.left && this._compare(cur.left.key, key) === lesserOrGreater) _traverse(cur.left); @@ -324,13 +338,13 @@ export class BST = BSTNode> extends BinaryTree const cur = queue.shift(); if (cur) { const compared = this._compare(cur.key, key); - if (compared === lesserOrGreater) callback(cur); + if (compared === lesserOrGreater) ans.push(callback(cur)); if (cur.left && this._compare(cur.left.key, key) === lesserOrGreater) queue.push(cur.left); if (cur.right && this._compare(cur.right.key, key) === lesserOrGreater) queue.push(cur.right); } } - return true; + return ans; } } @@ -350,7 +364,7 @@ export class BST = BSTNode> extends BinaryTree * @returns The function `perfectlyBalance()` returns a boolean value. */ perfectlyBalance(): boolean { - const sorted = this.dfs('in', 'node'), + const sorted = this.dfs(node => node, 'in'), n = sorted.length; this.clear(); diff --git a/src/data-structures/binary-tree/tree-multiset.ts b/src/data-structures/binary-tree/tree-multiset.ts index 1e62b91..c2ea9fa 100644 --- a/src/data-structures/binary-tree/tree-multiset.ts +++ b/src/data-structures/binary-tree/tree-multiset.ts @@ -6,10 +6,9 @@ * @license MIT License */ import type {BinaryTreeNodeKey, TreeMultisetNodeNested, TreeMultisetOptions} from '../../types'; -import {BinaryTreeDeletedResult, CP, DFSOrderPattern, FamilyPosition, LoopType} from '../../types'; +import {BinaryTreeDeletedResult, CP, FamilyPosition, LoopType} from '../../types'; import {IBinaryTree} from '../../interfaces'; import {AVLTree, AVLTreeNode} from './avl-tree'; -import {Queue} from '../queue'; export class TreeMultisetNode< V = any, @@ -248,7 +247,7 @@ export class TreeMultiset = TreeMultiset * @returns The function `perfectlyBalance()` returns a boolean value. */ override perfectlyBalance(): boolean { - const sorted = this.dfs('in', 'node'), + const sorted = this.dfs(node => node, 'in'), n = sorted.length; if (sorted.length < 1) return false; @@ -350,87 +349,6 @@ export class TreeMultiset = TreeMultiset return bstDeletedResult; } - /** - * The function `getNodesByCount` returns an array of nodes that have a specific count property, either recursively or - * using a queue. - * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a - * `N`. It represents the property of the nodes that you want to search for. - * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to - * return only one node that matches the `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne` is set - * 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 = false): N[] { - if (!this.root) return []; - const result: N[] = []; - - if (this.loopType === LoopType.RECURSIVE) { - const _traverse = (cur: N) => { - if (cur.count === nodeProperty) { - result.push(cur); - if (onlyOne) return; - } - - if (!cur.left && !cur.right) return; - cur.left && _traverse(cur.left); - cur.right && _traverse(cur.right); - }; - - _traverse(this.root); - } else { - const queue = new Queue([this.root]); - while (queue.size > 0) { - const cur = queue.shift(); - if (cur) { - if (cur.count === nodeProperty) { - result.push(cur); - if (onlyOne) return result; - } - - cur.left && queue.push(cur.left); - cur.right && queue.push(cur.right); - } - } - } - - return result; - } - - /** - * The BFSCount function returns an array of counts from a breadth-first search of nodes. - * @returns The BFSCount() function returns an array of numbers, specifically the count property of each node in the - * bfs traversal. - */ - bfsCount(): number[] { - const nodes = super.bfs('node'); - return nodes.map(node => node.count); - } - - /** - * The function "listLevelsCount" takes a node and returns an array of arrays, where each inner array contains the - * count property of each node at that level. - * @param {N | null} node - The parameter `node` is of type `N | null`. This means that it can either be an instance of - * the class `N` or `null`. - * @returns a 2D array of numbers. Each inner array represents a level in the binary tree, and each number in the inner - * array represents the count property of a node in that level. - */ - listLevelsCount(node: N | null): number[][] { - const levels = super.listLevels(node, 'node'); - return levels.map(level => level.map(node => node.count)); - } - - /** - * The `morrisCount` function returns an array of counts for each node in a binary tree, based on a specified traversal - * pattern. - * @param {'in' | 'pre' | 'post'} [pattern] - The `pattern` parameter is an optional parameter that specifies the - * 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: DFSOrderPattern = 'in'): number[] { - const nodes = super.morris(pattern, 'node'); - return nodes.map(node => node.count); - } - /** * The clear() function clears the data and sets the count to 0. */ diff --git a/src/data-structures/heap/heap.ts b/src/data-structures/heap/heap.ts index 95b16ea..09cc63c 100644 --- a/src/data-structures/heap/heap.ts +++ b/src/data-structures/heap/heap.ts @@ -172,7 +172,7 @@ export class Heap { /** * Depth-first search (DFS) method, different traversal orders can be selected。 - * @param order - Traversal order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order). + * @param order - Traverse order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order). * @returns An array containing elements traversed in the specified order. */ dfs(order: DFSOrderPattern): E[] { diff --git a/src/types/data-structures/binary-tree.ts b/src/types/data-structures/binary-tree.ts index a296960..0371633 100644 --- a/src/types/data-structures/binary-tree.ts +++ b/src/types/data-structures/binary-tree.ts @@ -28,6 +28,10 @@ export type NodeOrPropertyName = 'node' | BinaryTreeNodePropertyName; export type BinaryTreeNodeKey = number; +export type BFSCallback = (node: N, level?: number) => any; + +export type BFSCallbackReturn = ReturnType>; + export type BinaryTreeNodeProperty> = | N['val'] | N diff --git a/src/types/data-structures/index.ts b/src/types/data-structures/index.ts index 8a60614..8ad00d7 100644 --- a/src/types/data-structures/index.ts +++ b/src/types/data-structures/index.ts @@ -12,3 +12,6 @@ export * from './singly-linked-list'; export * from './doubly-linked-list'; export * from './navigator'; export * from './hash'; + +export type MapCallback = (node: N) => any; +export type MapCallbackReturn = ReturnType>; diff --git a/test/unit/data-structures/binary-tree/avl-tree.test.ts b/test/unit/data-structures/binary-tree/avl-tree.test.ts index fd2fb41..e606423 100644 --- a/test/unit/data-structures/binary-tree/avl-tree.test.ts +++ b/test/unit/data-structures/binary-tree/avl-tree.test.ts @@ -1,9 +1,9 @@ -import {AVLTree, CP} from '../../../../src'; +import {AVLTree, CP, AVLTreeNode} from '../../../../src'; describe('AVL Tree Test', () => { it('should perform various operations on a AVL Tree', () => { const arr = [11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]; - const tree = new AVLTree(); + const tree = new AVLTree>(); for (const i of arr) tree.add(i, i); @@ -12,7 +12,7 @@ describe('AVL Tree Test', () => { expect(node6 && tree.getHeight(node6)).toBe(3); expect(node6 && tree.getDepth(node6)).toBe(1); - const getNodeById = tree.get(10, 'key'); + const getNodeById = tree.get(10); expect(getNodeById?.key).toBe(10); const getMinNodeByRoot = tree.getLeftMost(); @@ -23,22 +23,23 @@ describe('AVL Tree Test', () => { expect(getMinNodeBySpecificNode?.key).toBe(12); let subTreeSum = 0; - node15 && tree.subTreeForeach(node15, node => (subTreeSum += node.key)); + node15 && tree.subTreeTraverse(node => (subTreeSum += node.key), node15); expect(subTreeSum).toBe(70); let lesserSum = 0; - tree.lesserOrGreaterForeach(10, CP.lt, node => (lesserSum += node.key)); + tree.lesserOrGreaterTraverse(node => (lesserSum += node.key), CP.lt, 10); expect(lesserSum).toBe(45); // node15 has type problem. After the uniform design, the generics of containers (DirectedGraph, BST) are based on the type of value. However, this design has a drawback: when I attempt to inherit from the Vertex or BSTNode classes, the types of the results obtained by all methods are those of the parent class. expect(node15?.val).toBe(15); - const dfs = tree.dfs('in', 'node'); + const dfs = tree.dfs(node => node, 'in'); expect(dfs[0].key).toBe(1); expect(dfs[dfs.length - 1].key).toBe(16); tree.perfectlyBalance(); - const bfs = tree.bfs('node'); + const bfs: AVLTreeNode[] = []; + tree.bfs(node => bfs.push(node)); expect(tree.isPerfectlyBalanced()).toBe(true); expect(bfs[0].key).toBe(8); expect(bfs[bfs.length - 1].key).toBe(16); @@ -97,12 +98,14 @@ describe('AVL Tree Test', () => { expect(tree.getHeight()).toBe(1); expect(tree.isAVLBalanced()).toBe(true); - const lastBFSIds = tree.bfs(); + const lastBFSIds = new Array(); + tree.bfs(node => lastBFSIds.push(node.key)); expect(lastBFSIds[0]).toBe(12); expect(lastBFSIds[1]).toBe(2); expect(lastBFSIds[2]).toBe(16); - const lastBFSNodes = tree.bfs('node'); + const lastBFSNodes: AVLTreeNode[] = []; + tree.bfs(node => lastBFSNodes.push(node)); expect(lastBFSNodes[0].key).toBe(12); expect(lastBFSNodes[1].key).toBe(2); expect(lastBFSNodes[2].key).toBe(16); diff --git a/test/unit/data-structures/binary-tree/binary-tree.test.ts b/test/unit/data-structures/binary-tree/binary-tree.test.ts index 4574a9e..acd8368 100644 --- a/test/unit/data-structures/binary-tree/binary-tree.test.ts +++ b/test/unit/data-structures/binary-tree/binary-tree.test.ts @@ -127,7 +127,7 @@ describe('BinaryTree', () => { binaryTree.add(5); binaryTree.add(7); - const inOrder = binaryTree.dfs('in'); + const inOrder = binaryTree.dfs(node => node.key); expect(inOrder).toEqual([1, 2, 3, 4, 5, 6, 7]); }); diff --git a/test/unit/data-structures/binary-tree/bst.test.ts b/test/unit/data-structures/binary-tree/bst.test.ts index 84c0d79..75d4e2d 100644 --- a/test/unit/data-structures/binary-tree/bst.test.ts +++ b/test/unit/data-structures/binary-tree/bst.test.ts @@ -1,6 +1,6 @@ import {BST, BSTNode, CP} from '../../../../src'; -const isDebug = false; +const isDebug = true; describe('BST operations test', () => { it('should perform various operations on a Binary Search Tree with numeric values', () => { @@ -25,7 +25,7 @@ describe('BST operations test', () => { const nodeId10 = bst.get(10); expect(nodeId10?.key).toBe(10); - const nodeVal9 = bst.get(9, 'val'); + const nodeVal9 = bst.get(9, node => node.val); expect(nodeVal9?.key).toBe(9); const leftMost = bst.getLeftMost(); @@ -36,11 +36,11 @@ describe('BST operations test', () => { expect(minNodeBySpecificNode?.key).toBe(12); let subTreeSum = 0; - node15 && bst.subTreeForeach(15, node => (subTreeSum += node.key)); + node15 && bst.subTreeTraverse(node => (subTreeSum += node.key), 15); expect(subTreeSum).toBe(70); let lesserSum = 0; - bst.lesserOrGreaterForeach(10, CP.lt, node => (lesserSum += node.key)); + bst.lesserOrGreaterTraverse(node => (lesserSum += node.key), CP.lt, 10); expect(lesserSum).toBe(45); expect(node15).toBeInstanceOf(BSTNode); @@ -48,14 +48,15 @@ describe('BST operations test', () => { const node11 = bst.get(11); expect(node11).toBeInstanceOf(BSTNode); - const dfsInorderNodes = bst.dfs('in', 'node'); + const dfsInorderNodes = bst.dfs(node => node, 'in'); expect(dfsInorderNodes[0].key).toBe(1); expect(dfsInorderNodes[dfsInorderNodes.length - 1].key).toBe(16); bst.perfectlyBalance(); expect(bst.isPerfectlyBalanced()).toBe(true); - const bfsNodesAfterBalanced = bst.bfs('node'); + const bfsNodesAfterBalanced: BSTNode[] = []; + bst.bfs(node => bfsNodesAfterBalanced.push(node)); expect(bfsNodesAfterBalanced[0].key).toBe(8); expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].key).toBe(16); @@ -173,12 +174,14 @@ describe('BST operations test', () => { expect(bst.isAVLBalanced()).toBe(false); - const bfsIDs = bst.bfs(); + const bfsIDs: number[] = []; + bst.bfs(node => bfsIDs.push(node.key)); expect(bfsIDs[0]).toBe(2); expect(bfsIDs[1]).toBe(12); expect(bfsIDs[2]).toBe(16); - const bfsNodes = bst.bfs('node'); + const bfsNodes: BSTNode[] = []; + bst.bfs(node => bfsNodes.push(node)); expect(bfsNodes[0].key).toBe(2); expect(bfsNodes[1].key).toBe(12); expect(bfsNodes[2].key).toBe(16); @@ -222,10 +225,10 @@ describe('BST operations test', () => { expect(node6 && objBST.getHeight(node6)).toBe(2); expect(node6 && objBST.getDepth(node6)).toBe(3); - const nodeId10 = objBST.get(10, 'key'); + const nodeId10 = objBST.get(10); expect(nodeId10?.key).toBe(10); - const nodeVal9 = objBST.get(9, 'key'); + const nodeVal9 = objBST.get(9); expect(nodeVal9?.key).toBe(9); const leftMost = objBST.getLeftMost(); @@ -237,11 +240,11 @@ describe('BST operations test', () => { expect(minNodeBySpecificNode?.key).toBe(12); let subTreeSum = 0; - node15 && objBST.subTreeForeach(node15, node => (subTreeSum += node.key)); + node15 && objBST.subTreeTraverse(node => (subTreeSum += node.key), node15); expect(subTreeSum).toBe(70); let lesserSum = 0; - objBST.lesserOrGreaterForeach(10, CP.lt, node => (lesserSum += node.key)); + objBST.lesserOrGreaterTraverse(node => (lesserSum += node.key), CP.lt, 10); expect(lesserSum).toBe(45); expect(node15).toBeInstanceOf(BSTNode); @@ -249,14 +252,15 @@ describe('BST operations test', () => { const node11 = objBST.get(11); expect(node11).toBeInstanceOf(BSTNode); - const dfsInorderNodes = objBST.dfs('in', 'node'); + const dfsInorderNodes = objBST.dfs(node => node, 'in'); expect(dfsInorderNodes[0].key).toBe(1); expect(dfsInorderNodes[dfsInorderNodes.length - 1].key).toBe(16); objBST.perfectlyBalance(); expect(objBST.isPerfectlyBalanced()).toBe(true); - const bfsNodesAfterBalanced = objBST.bfs('node'); + const bfsNodesAfterBalanced: BSTNode<{key: number; keyA: number}>[] = []; + objBST.bfs(node => bfsNodesAfterBalanced.push(node)); expect(bfsNodesAfterBalanced[0].key).toBe(8); expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].key).toBe(16); @@ -374,12 +378,14 @@ describe('BST operations test', () => { expect(objBST.isAVLBalanced()).toBe(false); - const bfsIDs = objBST.bfs(); + const bfsIDs: number[] = []; + objBST.bfs(node => bfsIDs.push(node.key)); expect(bfsIDs[0]).toBe(2); expect(bfsIDs[1]).toBe(12); expect(bfsIDs[2]).toBe(16); - const bfsNodes = objBST.bfs('node'); + const bfsNodes: BSTNode<{key: number; keyA: number}>[] = []; + objBST.bfs(node => bfsNodes.push(node)); expect(bfsNodes[0].key).toBe(2); expect(bfsNodes[1].key).toBe(12); expect(bfsNodes[2].key).toBe(16); @@ -396,11 +402,11 @@ describe('BST Performance test', function () { it(`Observe the time consumption of BST.dfs be good`, function () { const startDFS = performance.now(); - const dfs = bst.dfs(); + const dfs = bst.dfs(node => node); isDebug && console.log('---bfs', performance.now() - startDFS, dfs.length); }); - it('Should the time consumption of lesserOrGreaterForeach fitting O(n log n)', function () { + it('Should the time consumption of lesserOrGreaterTraverse fitting O(n log n)', function () { const nodes: number[] = []; for (let i = 0; i < inputSize; i++) { nodes.push(i); @@ -409,9 +415,33 @@ describe('BST Performance test', function () { bst.addMany(nodes); isDebug && console.log('---add', performance.now() - start); const startL = performance.now(); - bst.lesserOrGreaterForeach(inputSize / 2, CP.lt, node => { - return node.key - 1; - }); - isDebug && console.log('---lesserOrGreaterForeach', performance.now() - startL); + bst.lesserOrGreaterTraverse( + node => { + node.key - 1; + }, + CP.lt, + inputSize / 2 + ); + isDebug && console.log('---lesserOrGreaterTraverse', performance.now() - startL); + }); + + it('Should the time consumption of listLevels fitting well', function () { + const nodes: number[] = []; + for (let i = 0; i < inputSize; i++) { + nodes.push(i); + } + const start = performance.now(); + bst.addMany(nodes); + isDebug && console.log('---add', performance.now() - start); + const startL = performance.now(); + const arr: number[][] = []; + bst.bfs((node, level) => { + if (level !== undefined) { + if (!arr[level]) arr[level] = []; + arr[level].push(node.key); + } + }, true); + isDebug && console.log('---listLevels', arr); + isDebug && console.log('---listLevels', performance.now() - startL); }); }); diff --git a/test/unit/data-structures/binary-tree/overall.test.ts b/test/unit/data-structures/binary-tree/overall.test.ts index 2cdffee..5325180 100644 --- a/test/unit/data-structures/binary-tree/overall.test.ts +++ b/test/unit/data-structures/binary-tree/overall.test.ts @@ -24,7 +24,8 @@ describe('Overall BinaryTree Test', () => { expect(bst.get(6)).toBeNull(); bst.isAVLBalanced(); // true or false expect(bst.isAVLBalanced()).toBe(true); - const bfsIDs = bst.bfs(); + const bfsIDs: number[] = []; + bst.bfs(node => bfsIDs.push(node.key)); bfsIDs[0] === 11; // true expect(bfsIDs[0]).toBe(11); diff --git a/test/unit/data-structures/binary-tree/rb-tree.test.ts b/test/unit/data-structures/binary-tree/rb-tree.test.ts index 1d3a5ac..884e126 100644 --- a/test/unit/data-structures/binary-tree/rb-tree.test.ts +++ b/test/unit/data-structures/binary-tree/rb-tree.test.ts @@ -7,7 +7,7 @@ describe('Red-Black Tree Tests', () => { // tree = new RBTree>(); // }); - test('Insertion and In-order Traversal', () => { + test('Insertion and In-order Traverse', () => { // tree.add(5); // tree.add(3); // tree.add(7); @@ -16,9 +16,9 @@ describe('Red-Black Tree Tests', () => { // tree.add(6); // tree.add(8); // - // const inOrderTraversal: number[] = tree.DFS('in') + // const inOrderTraverse: number[] = tree.DFS('in') // - // expect(inOrderTraversal).toEqual([2, 3, 4, 5, 6, 7, 8]); + // expect(inOrderTraverse).toEqual([2, 3, 4, 5, 6, 7, 8]); }); test('Deletion', () => { @@ -35,9 +35,9 @@ describe('Red-Black Tree Tests', () => { // expect(tree.has(3)).toBe(false); // // // Perform in-order traversal to check if the tree is still balanced - // const inOrderTraversal: number[] = tree.DFS('in'); + // const inOrderTraverse: number[] = tree.DFS('in'); // // - // expect(inOrderTraversal).toEqual([2, 4, 5, 6, 7, 8]); + // expect(inOrderTraverse).toEqual([2, 4, 5, 6, 7, 8]); }); }); diff --git a/test/unit/data-structures/binary-tree/tree-multiset.test.ts b/test/unit/data-structures/binary-tree/tree-multiset.test.ts index 4be9ce1..4a9a101 100644 --- a/test/unit/data-structures/binary-tree/tree-multiset.test.ts +++ b/test/unit/data-structures/binary-tree/tree-multiset.test.ts @@ -16,7 +16,6 @@ describe('TreeMultiset operations test', () => { expect(treeMultiset.size).toBe(16); expect(treeMultiset.count).toBe(18); - expect(treeMultiset.bfs('key')); expect(treeMultiset.has(6)); @@ -25,13 +24,13 @@ describe('TreeMultiset operations test', () => { const nodeId10 = treeMultiset.get(10); expect(nodeId10?.key).toBe(10); - const nodeVal9 = treeMultiset.get(9, 'val'); + const nodeVal9 = treeMultiset.get(9, node => node.val); expect(nodeVal9?.key).toBe(9); - const nodesByCount1 = treeMultiset.getNodesByCount(1); + const nodesByCount1 = treeMultiset.getNodes(1, node => node.count); expect(nodesByCount1.length).toBe(14); - const nodesByCount2 = treeMultiset.getNodesByCount(2); + const nodesByCount2 = treeMultiset.getNodes(2, node => node.count); expect(nodesByCount2.length).toBe(2); const leftMost = treeMultiset.getLeftMost(); expect(leftMost?.key).toBe(1); @@ -41,29 +40,25 @@ describe('TreeMultiset operations test', () => { expect(minNodeBySpecificNode?.key).toBe(12); let subTreeSum = 0; - node15 && treeMultiset.subTreeForeach(15, (node: TreeMultisetNode) => (subTreeSum += node.key)); + node15 && treeMultiset.subTreeTraverse((node: TreeMultisetNode) => (subTreeSum += node.key), 15); expect(subTreeSum).toBe(70); let lesserSum = 0; - treeMultiset.lesserOrGreaterForeach(10, CP.lt, (node: TreeMultisetNode) => (lesserSum += node.key)); + treeMultiset.lesserOrGreaterTraverse((node: TreeMultisetNode) => (lesserSum += node.key), CP.lt, 10); expect(lesserSum).toBe(45); expect(node15 instanceof TreeMultisetNode); if (node15 instanceof TreeMultisetNode) { - const subTreeAdd = treeMultiset.subTreeForeach(15, (node: TreeMultisetNode) => (node.count += 1)); + const subTreeAdd = treeMultiset.subTreeTraverse((node: TreeMultisetNode) => (node.count += 1), 15); expect(subTreeAdd); } const node11 = treeMultiset.get(11); expect(node11 instanceof TreeMultisetNode); if (node11 instanceof TreeMultisetNode) { - const allGreaterNodesAdded = treeMultiset.lesserOrGreaterForeach( - 11, - CP.gt, - (node: TreeMultisetNode) => (node.count += 2) - ); + const allGreaterNodesAdded = treeMultiset.lesserOrGreaterTraverse(node => (node.count += 2), CP.gt, 11); expect(allGreaterNodesAdded); } - const dfsInorderNodes = treeMultiset.dfs('in', 'node'); + const dfsInorderNodes = treeMultiset.dfs(node => node, 'in'); expect(dfsInorderNodes[0].key).toBe(1); expect(dfsInorderNodes[dfsInorderNodes.length - 1].key).toBe(16); expect(treeMultiset.isPerfectlyBalanced()).toBe(false); @@ -73,7 +68,7 @@ describe('TreeMultiset operations test', () => { expect(treeMultiset.isPerfectlyBalanced()).toBe(true); expect(treeMultiset.isAVLBalanced()).toBe(true); - const bfsNodesAfterBalanced = treeMultiset.bfs('node'); + const bfsNodesAfterBalanced = treeMultiset.bfs(node => node); expect(bfsNodesAfterBalanced[0].key).toBe(8); expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].key).toBe(16); @@ -194,13 +189,13 @@ describe('TreeMultiset operations test', () => { expect(treeMultiset.isAVLBalanced()).toBe(true); - const bfsIDs = treeMultiset.bfs(); + const bfsIDs = treeMultiset.bfs(node => node.key); expect(bfsIDs[0]).toBe(12); expect(bfsIDs[1]).toBe(2); expect(bfsIDs[2]).toBe(16); - const bfsNodes = treeMultiset.bfs('node'); + const bfsNodes = treeMultiset.bfs(node => node); expect(bfsNodes[0].key).toBe(12); expect(bfsNodes[1].key).toBe(2); @@ -284,7 +279,7 @@ describe('TreeMultiset operations test', () => { // expect(allGreaterNodesAdded).toBeDefined(); // } // - // const dfsInorderNodes = objTreeMultiset.dfs('in', 'node'); + // const dfsInorderNodes = objTreeMultiset.dfs(node => node, 'in'); // expect(dfsInorderNodes[0].key).toBe(1); // expect(dfsInorderNodes[dfsInorderNodes.length - 1].key).toBe(16); // @@ -474,18 +469,18 @@ describe('TreeMultiset Performance test', function () { it(`Observe the time consumption of TreeMultiset.dfs be good`, function () { const startDFS = performance.now(); - const dfs = treeMS.dfs(); + const dfs = treeMS.dfs(node => node); isDebug && console.log('---bfs', performance.now() - startDFS, dfs.length); }); - it('Should the time consumption of lesserOrGreaterForeach fitting O(n log n)', function () { + it('Should the time consumption of lesserOrGreaterTraverse fitting O(n log n)', function () { const start = performance.now(); for (let i = 0; i < inputSize; i++) { treeMS.add(i); } isDebug && console.log('---add', performance.now() - start); const startL = performance.now(); - treeMS.lesserOrGreaterForeach(inputSize / 2, CP.lt, (node: TreeMultisetNode) => (node.count += 1)); - isDebug && console.log('---lesserOrGreaterForeach', performance.now() - startL); + treeMS.lesserOrGreaterTraverse((node: TreeMultisetNode) => (node.count += 1), CP.lt, inputSize / 2); + isDebug && console.log('---lesserOrGreaterTraverse', performance.now() - startL); }); });