From f08819eb838b67cd0788d3e4529ae0816ff4aefc Mon Sep 17 00:00:00 2001 From: Revone Date: Tue, 10 Oct 2023 22:15:01 +0800 Subject: [PATCH] [core] In order to adhere to best practices, rename the DFS method and the BFS method as dfs and bfs. --- README.md | 4 +- .../binary-tree/abstract-binary-tree.ts | 54 +++++++++---------- src/data-structures/binary-tree/bst.ts | 2 +- .../binary-tree/tree-multiset.ts | 18 +++---- src/data-structures/graph/abstract-graph.ts | 10 ++-- .../priority-queue/priority-queue.ts | 6 +-- src/interfaces/abstract-binary-tree.ts | 24 ++++----- .../binary-tree/avl-tree.test.ts | 8 +-- .../binary-tree/binary-tree.test.ts | 2 +- .../data-structures/binary-tree/bst.test.ts | 16 +++--- .../binary-tree/overall.test.ts | 2 +- .../binary-tree/tree-multiset.test.ts | 18 +++---- .../priority-queue/priority-queue.test.ts | 8 +-- 13 files changed, 86 insertions(+), 86 deletions(-) diff --git a/README.md b/README.md index a36b5a5..3f70858 100644 --- a/README.md +++ b/README.md @@ -109,7 +109,7 @@ bst.getLeftMost()?.id === 1; // true bst.remove(6); bst.get(6); // null bst.isAVLBalanced(); // true -bst.BFS()[0] === 11; // true +bst.bfs()[0] === 11; // true const objBST = new BST>(); objBST.add(11, {id: 11, keyA: 11}); @@ -145,7 +145,7 @@ expect(leftMost?.id).toBe(1); bst.remove(6); bst.get(6); // null bst.isAVLBalanced(); // true or false -const bfsIDs = bst.BFS(); +const bfsIDs = bst.bfs(); bfsIDs[0] === 11; // true expect(bfsIDs[0]).toBe(11); diff --git a/src/data-structures/binary-tree/abstract-binary-tree.ts b/src/data-structures/binary-tree/abstract-binary-tree.ts index b94c8a5..3b7a771 100644 --- a/src/data-structures/binary-tree/abstract-binary-tree.ts +++ b/src/data-structures/binary-tree/abstract-binary-tree.ts @@ -859,40 +859,40 @@ export abstract class AbstractBinaryTree { + bfs(nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties { nodeOrPropertyName = nodeOrPropertyName ?? 'key'; this._clearResults(); const queue: Array = [this.root]; @@ -910,43 +910,43 @@ export abstract class AbstractBinaryTree { + dfs(pattern?: 'in' | 'pre' | 'post', nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties { pattern = pattern ?? 'in'; nodeOrPropertyName = nodeOrPropertyName ?? 'key'; this._clearResults(); @@ -977,13 +977,13 @@ export abstract class AbstractBinaryTree = BSTNode> extends BinaryTree * @returns The function `perfectlyBalance()` returns a boolean value. */ perfectlyBalance(): boolean { - const sorted = this.DFS('in', 'node'), + const sorted = this.dfs('in', 'node'), 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 8735a20..2eabdd6 100644 --- a/src/data-structures/binary-tree/tree-multiset.ts +++ b/src/data-structures/binary-tree/tree-multiset.ts @@ -22,7 +22,7 @@ export class TreeMultisetNode = TreeMultiset * @returns The function `perfectlyBalance()` returns a boolean value. */ override perfectlyBalance(): boolean { - const sorted = this.DFS('in', 'node'), + const sorted = this.dfs('in', 'node'), n = sorted.length; if (sorted.length < 1) return false; @@ -514,10 +514,10 @@ export class TreeMultiset = TreeMultiset /** * 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. + * bfs traversal. */ BFSCount(): number[] { - const nodes = super.BFS('node'); + const nodes = super.bfs('node'); return nodes.map(node => node.count); } @@ -551,9 +551,9 @@ export class TreeMultiset = TreeMultiset * The function DFSIterativeCount 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'. + * 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 - * in the DFS traversal. + * in the dfs traversal. */ DFSIterativeCount(pattern?: 'in' | 'pre' | 'post'): number[] { pattern = pattern ?? 'in'; @@ -564,13 +564,13 @@ export class TreeMultiset = TreeMultiset /** * 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 + * 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 * traversal. */ DFSCount(pattern?: DFSOrderPattern): number[] { pattern = pattern ?? 'in'; - const nodes = super.DFS(pattern, 'node'); + const nodes = super.dfs(pattern, 'node'); return nodes.map(node => node.count); } diff --git a/src/data-structures/graph/abstract-graph.ts b/src/data-structures/graph/abstract-graph.ts index 23f690f..82f00b5 100644 --- a/src/data-structures/graph/abstract-graph.ts +++ b/src/data-structures/graph/abstract-graph.ts @@ -889,7 +889,7 @@ export abstract class AbstractGraph< } /** - * Tarjan is an algorithm based on DFS,which is used to solve the connectivity problem of graphs. + * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs. * Tarjan can find cycles in directed or undirected graph * Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time, * Tarjan solve the bi-connected components of undirected graphs; @@ -897,7 +897,7 @@ export abstract class AbstractGraph< * / /** - * Tarjan is an algorithm based on DFS,which is used to solve the connectivity problem of graphs. + * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs. * Tarjan can find cycles in directed or undirected graph * Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time, * Tarjan solve the bi-connected components of undirected graphs; @@ -918,8 +918,8 @@ export abstract class AbstractGraph< * @returns The function `tarjan` returns an object with the following properties: */ tarjan(needArticulationPoints?: boolean, needBridges?: boolean, needSCCs?: boolean, needCycles?: boolean) { - // !! in undirected graph we will not let child visit parent when DFS - // !! articulation point(in DFS search tree not in graph): (cur !== root && cur.has(child)) && (low(child) >= dfn(cur)) || (cur === root && cur.children() >= 2) + // !! in undirected graph we will not let child visit parent when dfs + // !! articulation point(in dfs search tree not in graph): (cur !== root && cur.has(child)) && (low(child) >= dfn(cur)) || (cur === root && cur.children() >= 2) // !! bridge: low(child) > dfn(cur) const defaultConfig = false; @@ -947,7 +947,7 @@ export abstract class AbstractGraph< lowMap.set(cur, dfn); const neighbors = this.getNeighbors(cur); - let childCount = 0; // child in DFS tree not child in graph + let childCount = 0; // child in dfs tree not child in graph for (const neighbor of neighbors) { if (neighbor !== parent) { if (dfnMap.get(neighbor) === -1) { diff --git a/src/data-structures/priority-queue/priority-queue.ts b/src/data-structures/priority-queue/priority-queue.ts index 4ecbbd0..bebc648 100644 --- a/src/data-structures/priority-queue/priority-queue.ts +++ b/src/data-structures/priority-queue/priority-queue.ts @@ -195,13 +195,13 @@ export class PriorityQueue { } /** - * The DFS function performs a depth-first search traversal on a binary tree and returns an array of visited nodes + * The dfs function performs a depth-first search traversal on a binary tree and returns an array of visited nodes * based on the specified traversal order. * @param {PriorityQueueDFSOrderPattern} dfsMode - The dfsMode parameter is a string that specifies the order in which - * the nodes should be visited during the Depth-First Search (DFS) traversal. It can have one of the following values: + * the nodes should be visited during the Depth-First Search (dfs) traversal. It can have one of the following values: * @returns an array of type `(E | null)[]`. */ - DFS(dfsMode: PriorityQueueDFSOrderPattern): (E | null)[] { + dfs(dfsMode: PriorityQueueDFSOrderPattern): (E | null)[] { const visitedNode: (E | null)[] = []; const traverse = (cur: number) => { diff --git a/src/interfaces/abstract-binary-tree.ts b/src/interfaces/abstract-binary-tree.ts index f75caa8..df0941d 100644 --- a/src/interfaces/abstract-binary-tree.ts +++ b/src/interfaces/abstract-binary-tree.ts @@ -98,29 +98,29 @@ export interface IAbstractBinaryTree; + bfs(nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties; - DFS(): BinaryTreeNodeKey[]; + dfs(): BinaryTreeNodeKey[]; - DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'key'): BinaryTreeNodeKey[]; + dfs(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'key'): BinaryTreeNodeKey[]; - DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'val'): N[]; + dfs(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'val'): N[]; - DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'node'): N[]; + dfs(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'node'): N[]; - DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'count'): number[]; + dfs(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'count'): number[]; - DFS(pattern?: 'in' | 'pre' | 'post', nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties; + dfs(pattern?: 'in' | 'pre' | 'post', nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties; DFSIterative(): BinaryTreeNodeKey[]; 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 20f412c..a5eb88c 100644 --- a/test/unit/data-structures/binary-tree/avl-tree.test.ts +++ b/test/unit/data-structures/binary-tree/avl-tree.test.ts @@ -31,12 +31,12 @@ describe('AVL Tree Test', () => { // 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('in', 'node'); expect(dfs[0].key).toBe(1); expect(dfs[dfs.length - 1].key).toBe(16); tree.perfectlyBalance(); - const bfs = tree.BFS('node'); + const bfs = tree.bfs('node'); expect(tree.isPerfectlyBalanced()).toBe(true); expect(bfs[0].key).toBe(8); expect(bfs[bfs.length - 1].key).toBe(16); @@ -95,12 +95,12 @@ describe('AVL Tree Test', () => { expect(tree.getHeight()).toBe(1); expect(tree.isAVLBalanced()).toBe(true); - const lastBFSIds = tree.BFS(); + const lastBFSIds = tree.bfs(); expect(lastBFSIds[0]).toBe(12); expect(lastBFSIds[1]).toBe(2); expect(lastBFSIds[2]).toBe(16); - const lastBFSNodes = tree.BFS('node'); + const lastBFSNodes = tree.bfs('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 ef211b7..abef0e9 100644 --- a/test/unit/data-structures/binary-tree/binary-tree.test.ts +++ b/test/unit/data-structures/binary-tree/binary-tree.test.ts @@ -114,7 +114,7 @@ describe('BinaryTree', () => { binaryTree.add(5); binaryTree.add(7); - const inOrder = binaryTree.DFS('in'); + const inOrder = binaryTree.dfs('in'); 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 8c4f785..84ac377 100644 --- a/test/unit/data-structures/binary-tree/bst.test.ts +++ b/test/unit/data-structures/binary-tree/bst.test.ts @@ -44,14 +44,14 @@ describe('BST operations test', () => { const node11 = bst.get(11); expect(node11).toBeInstanceOf(BSTNode); - const dfsInorderNodes = bst.DFS('in', 'node'); + const dfsInorderNodes = bst.dfs('in', 'node'); 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 = bst.bfs('node'); expect(bfsNodesAfterBalanced[0].key).toBe(8); expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].key).toBe(16); @@ -169,12 +169,12 @@ describe('BST operations test', () => { expect(bst.isAVLBalanced()).toBe(false); - const bfsIDs = bst.BFS(); + const bfsIDs = bst.bfs(); expect(bfsIDs[0]).toBe(2); expect(bfsIDs[1]).toBe(12); expect(bfsIDs[2]).toBe(16); - const bfsNodes = bst.BFS('node'); + const bfsNodes = bst.bfs('node'); expect(bfsNodes[0].key).toBe(2); expect(bfsNodes[1].key).toBe(12); expect(bfsNodes[2].key).toBe(16); @@ -242,14 +242,14 @@ describe('BST operations test', () => { const node11 = objBST.get(11); expect(node11).toBeInstanceOf(BSTNode); - const dfsInorderNodes = objBST.DFS('in', 'node'); + const dfsInorderNodes = objBST.dfs('in', 'node'); 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 = objBST.bfs('node'); expect(bfsNodesAfterBalanced[0].key).toBe(8); expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].key).toBe(16); @@ -367,12 +367,12 @@ describe('BST operations test', () => { expect(objBST.isAVLBalanced()).toBe(false); - const bfsIDs = objBST.BFS(); + const bfsIDs = objBST.bfs(); expect(bfsIDs[0]).toBe(2); expect(bfsIDs[1]).toBe(12); expect(bfsIDs[2]).toBe(16); - const bfsNodes = objBST.BFS('node'); + const bfsNodes = objBST.bfs('node'); expect(bfsNodes[0].key).toBe(2); expect(bfsNodes[1].key).toBe(12); expect(bfsNodes[2].key).toBe(16); diff --git a/test/unit/data-structures/binary-tree/overall.test.ts b/test/unit/data-structures/binary-tree/overall.test.ts index b802d47..206a071 100644 --- a/test/unit/data-structures/binary-tree/overall.test.ts +++ b/test/unit/data-structures/binary-tree/overall.test.ts @@ -24,7 +24,7 @@ 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 = bst.bfs(); bfsIDs[0] === 11; // true expect(bfsIDs[0]).toBe(11); 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 a4a2e2f..8b926c3 100644 --- a/test/unit/data-structures/binary-tree/tree-multiset.test.ts +++ b/test/unit/data-structures/binary-tree/tree-multiset.test.ts @@ -15,7 +15,7 @@ describe('TreeMultiset operations test', () => { expect(treeMultiset.size).toBe(16); expect(treeMultiset.count).toBe(18); - expect(treeMultiset.BFS('key')); + expect(treeMultiset.bfs('key')); expect(treeMultiset.has(6)); @@ -56,7 +56,7 @@ describe('TreeMultiset operations test', () => { expect(allGreaterNodesAdded); } - const dfsInorderNodes = treeMultiset.DFS('in', 'node'); + const dfsInorderNodes = treeMultiset.dfs('in', 'node'); expect(dfsInorderNodes[0].key).toBe(1); expect(dfsInorderNodes[dfsInorderNodes.length - 1].key).toBe(16); expect(treeMultiset.isPerfectlyBalanced()).toBe(false); @@ -66,7 +66,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'); expect(bfsNodesAfterBalanced[0].key).toBe(8); expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].key).toBe(16); @@ -187,13 +187,13 @@ describe('TreeMultiset operations test', () => { expect(treeMultiset.isAVLBalanced()).toBe(true); - const bfsIDs = treeMultiset.BFS(); + const bfsIDs = treeMultiset.bfs(); 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'); expect(bfsNodes[0].key).toBe(12); expect(bfsNodes[1].key).toBe(2); @@ -277,14 +277,14 @@ describe('TreeMultiset operations test', () => { // expect(allGreaterNodesAdded).toBeDefined(); // } // - // const dfsInorderNodes = objTreeMultiset.DFS('in', 'node'); + // const dfsInorderNodes = objTreeMultiset.dfs('in', 'node'); // expect(dfsInorderNodes[0].key).toBe(1); // expect(dfsInorderNodes[dfsInorderNodes.length - 1].key).toBe(16); // // objTreeMultiset.perfectlyBalance(); // expect(objTreeMultiset.isPerfectlyBalanced()).toBe(true); // - // const bfsNodesAfterBalanced = objTreeMultiset.BFS('node'); + // const bfsNodesAfterBalanced = objTreeMultiset.bfs('node'); // expect(bfsNodesAfterBalanced[0].key).toBe(8); // expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].key).toBe(16); // @@ -403,12 +403,12 @@ describe('TreeMultiset operations test', () => { // // expect(objTreeMultiset.isAVLBalanced()).toBe(false); // - // const bfsIDs = objTreeMultiset.BFS(); + // const bfsIDs = objTreeMultiset.bfs(); // expect(bfsIDs[0]).toBe(2); // expect(bfsIDs[1]).toBe(12); // expect(bfsIDs[2]).toBe(16); // - // const bfsNodes = objTreeMultiset.BFS('node'); + // const bfsNodes = objTreeMultiset.bfs('node'); // expect(bfsNodes[0].key).toBe(2); // expect(bfsNodes[1].key).toBe(12); // expect(bfsNodes[2].key).toBe(16); diff --git a/test/unit/data-structures/priority-queue/priority-queue.test.ts b/test/unit/data-structures/priority-queue/priority-queue.test.ts index 8679862..e25e208 100644 --- a/test/unit/data-structures/priority-queue/priority-queue.test.ts +++ b/test/unit/data-structures/priority-queue/priority-queue.test.ts @@ -48,14 +48,14 @@ describe('PriorityQueue Operation Test', () => { ).toEqual([1, 2, 3, 5, 6, 7, 8, 9, 10]); }); - it('should PriorityQueue clone, sort, getNodes, DFS work well', function () { + it('should PriorityQueue clone, sort, getNodes, dfs work well', function () { const minPQ1 = new PriorityQueue({nodes: [2, 5, 8, 3, 1, 6, 7, 4], comparator: (a, b) => a - b}); const clonedPriorityQueue = minPQ1.clone(); expect(clonedPriorityQueue.getNodes()).toEqual(minPQ1.getNodes()); expect(clonedPriorityQueue.sort()).toEqual([1, 2, 3, 4, 5, 6, 7, 8]); - expect(minPQ1.DFS('in')).toEqual([4, 3, 2, 5, 1, 8, 6, 7]); - expect(minPQ1.DFS('post')).toEqual([4, 3, 5, 2, 8, 7, 6, 1]); - expect(minPQ1.DFS('pre')).toEqual([1, 2, 3, 4, 5, 6, 8, 7]); + expect(minPQ1.dfs('in')).toEqual([4, 3, 2, 5, 1, 8, 6, 7]); + expect(minPQ1.dfs('post')).toEqual([4, 3, 5, 2, 8, 7, 6, 1]); + expect(minPQ1.dfs('pre')).toEqual([1, 2, 3, 4, 5, 6, 8, 7]); }); });