mirror of
https://github.com/zrwusa/data-structure-typed.git
synced 2024-11-23 12:54:04 +00:00
[core] In order to adhere to best practices, rename the DFS method and the BFS method as dfs and bfs.
This commit is contained in:
parent
ed1ecf541f
commit
f08819eb83
|
@ -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<BSTNode<{id: number, keyA: number}>>();
|
||||
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);
|
||||
|
||||
|
|
|
@ -859,40 +859,40 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|||
}
|
||||
|
||||
/**
|
||||
* Performs a breadth-first search (BFS) on a binary tree, accumulating properties of each node based on their 'key' property.
|
||||
* 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[];
|
||||
bfs(): BinaryTreeNodeKey[];
|
||||
|
||||
/**
|
||||
* Performs a breadth-first search (BFS) on a binary tree, accumulating properties of each node based on the specified property name.
|
||||
* 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[];
|
||||
bfs(nodeOrPropertyName: 'key'): BinaryTreeNodeKey[];
|
||||
|
||||
/**
|
||||
* Performs a breadth-first search (BFS) on a binary tree, accumulating the 'val' property of each node.
|
||||
* 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'][];
|
||||
bfs(nodeOrPropertyName: 'val'): N['val'][];
|
||||
|
||||
/**
|
||||
* Performs a breadth-first search (BFS) on a binary tree, accumulating nodes themselves.
|
||||
* 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[];
|
||||
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.
|
||||
* 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.
|
||||
* 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 `AbstractBinaryTreeNodeProperties` class with generic type `N`.
|
||||
*/
|
||||
BFS(nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N> {
|
||||
bfs(nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N> {
|
||||
nodeOrPropertyName = nodeOrPropertyName ?? 'key';
|
||||
this._clearResults();
|
||||
const queue: Array<N | null | undefined> = [this.root];
|
||||
|
@ -910,43 +910,43 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|||
}
|
||||
|
||||
/**
|
||||
* Performs a depth-first search (DFS) traversal on a binary tree and accumulates properties of each node based on their 'key' property.
|
||||
* 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[];
|
||||
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.
|
||||
* 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.
|
||||
* @returns An array of values corresponding to the specified property.
|
||||
*/
|
||||
DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'key'): BinaryTreeNodeKey[];
|
||||
dfs(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'key'): BinaryTreeNodeKey[];
|
||||
|
||||
/**
|
||||
* Performs a depth-first search (DFS) traversal on a binary tree and accumulates the 'val' property of each node.
|
||||
* 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.
|
||||
* @returns An array of 'val' properties from each node.
|
||||
*/
|
||||
DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'val'): N[];
|
||||
dfs(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'val'): N[];
|
||||
|
||||
/**
|
||||
* Performs a depth-first search (DFS) traversal on a binary tree and accumulates nodes themselves.
|
||||
* 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.
|
||||
* @returns An array of binary tree nodes.
|
||||
*/
|
||||
DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'node'): N[];
|
||||
dfs(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'node'): N[];
|
||||
|
||||
/**
|
||||
* The DFS function performs a depth-first search traversal on a binary tree and returns the accumulated properties of
|
||||
* 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 {'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'`.
|
||||
* @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?: 'in' | 'pre' | 'post', nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N> {
|
||||
dfs(pattern?: 'in' | 'pre' | 'post', nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N> {
|
||||
pattern = pattern ?? 'in';
|
||||
nodeOrPropertyName = nodeOrPropertyName ?? 'key';
|
||||
this._clearResults();
|
||||
|
@ -977,13 +977,13 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|||
// --- start additional methods ---
|
||||
|
||||
/**
|
||||
* Performs an iterative depth-first search (DFS) traversal on a binary tree and accumulates properties of each node based on their 'key' property.
|
||||
* Performs an iterative 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.
|
||||
*/
|
||||
DFSIterative(): BinaryTreeNodeKey[];
|
||||
|
||||
/**
|
||||
* Performs an iterative depth-first search (DFS) traversal on a binary tree and accumulates properties of each node based on the specified property name.
|
||||
* Performs an iterative 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.
|
||||
* @returns An array of values corresponding to the specified property.
|
||||
|
@ -991,7 +991,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|||
DFSIterative(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'key'): BinaryTreeNodeKey[];
|
||||
|
||||
/**
|
||||
* Performs an iterative depth-first search (DFS) traversal on a binary tree and accumulates the 'val' property of each node.
|
||||
* Performs an iterative 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.
|
||||
* @returns An array of 'val' properties from each node.
|
||||
|
@ -999,7 +999,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|||
DFSIterative(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'val'): N[];
|
||||
|
||||
/**
|
||||
* Performs an iterative depth-first search (DFS) traversal on a binary tree and accumulates nodes themselves.
|
||||
* Performs an iterative 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.
|
||||
* @returns An array of binary tree nodes.
|
||||
|
@ -1233,7 +1233,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|||
|
||||
/**
|
||||
* Time complexity is O(n)
|
||||
* Space complexity of Iterative DFS equals to recursive DFS which is O(n) because of the stack
|
||||
* Space complexity of Iterative dfs equals to recursive dfs which is O(n) because of the stack
|
||||
*/
|
||||
|
||||
/**
|
||||
|
|
|
@ -440,7 +440,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|||
* @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();
|
||||
|
||||
|
|
|
@ -22,7 +22,7 @@ export class TreeMultisetNode<V = any, NEIGHBOR extends TreeMultisetNode<V, NEIG
|
|||
* tree node. If no value is provided, it will be `undefined`.
|
||||
* @param {number} [count=1] - The `count` parameter is a number that represents the number of times a particular value
|
||||
* occurs in a binary tree node. It has a default value of 1, which means that if no value is provided for the `count`
|
||||
* parameter when creating a new instance of the `BinaryTreeNode` class,
|
||||
* parameter when creating a new instance of the `BinaryTreeNode` class.
|
||||
*/
|
||||
constructor(key: BinaryTreeNodeKey, val?: V, count = 1) {
|
||||
super(key, val);
|
||||
|
@ -248,7 +248,7 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = 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<N extends TreeMultisetNode<N['val'], N> = 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<N extends TreeMultisetNode<N['val'], N> = 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<N extends TreeMultisetNode<N['val'], N> = 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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -195,13 +195,13 @@ export class PriorityQueue<E = any> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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) => {
|
||||
|
|
|
@ -98,29 +98,29 @@ export interface IAbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val'],
|
|||
|
||||
subTreeAdd(subTreeRoot: N, delta: number, propertyName?: BinaryTreeNodePropertyName): boolean;
|
||||
|
||||
BFS(): BinaryTreeNodeKey[];
|
||||
bfs(): BinaryTreeNodeKey[];
|
||||
|
||||
BFS(nodeOrPropertyName: 'key'): BinaryTreeNodeKey[];
|
||||
bfs(nodeOrPropertyName: 'key'): BinaryTreeNodeKey[];
|
||||
|
||||
BFS(nodeOrPropertyName: 'val'): N['val'][];
|
||||
bfs(nodeOrPropertyName: 'val'): N['val'][];
|
||||
|
||||
BFS(nodeOrPropertyName: 'node'): N[];
|
||||
bfs(nodeOrPropertyName: 'node'): N[];
|
||||
|
||||
BFS(nodeOrPropertyName: 'count'): number[];
|
||||
bfs(nodeOrPropertyName: 'count'): number[];
|
||||
|
||||
BFS(nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N>;
|
||||
bfs(nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N>;
|
||||
|
||||
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<N>;
|
||||
dfs(pattern?: 'in' | 'pre' | 'post', nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N>;
|
||||
|
||||
DFSIterative(): BinaryTreeNodeKey[];
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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]);
|
||||
});
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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<number>({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]);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in a new issue