diff --git a/package-lock.json b/package-lock.json index a705cb4..c4f24c4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "data-structure-typed", - "version": "1.38.1", + "version": "1.38.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "data-structure-typed", - "version": "1.38.1", + "version": "1.38.2", "license": "MIT", "devDependencies": { "@types/benchmark": "^2.1.3", @@ -15,17 +15,17 @@ "@typescript-eslint/eslint-plugin": "^6.7.4", "@typescript-eslint/parser": "^6.7.4", "auto-changelog": "^2.4.0", - "avl-tree-typed": "^1.38.1", + "avl-tree-typed": "^1.38.2", "benchmark": "^2.1.4", - "binary-tree-typed": "^1.38.1", - "bst-typed": "^1.38.1", + "binary-tree-typed": "^1.38.2", + "bst-typed": "^1.38.2", "dependency-cruiser": "^14.1.0", "eslint": "^8.50.0", "eslint-config-prettier": "^9.0.0", "eslint-import-resolver-alias": "^1.1.2", "eslint-import-resolver-typescript": "^3.6.1", "eslint-plugin-import": "^2.28.1", - "heap-typed": "^1.38.1", + "heap-typed": "^1.38.2", "istanbul-badges-readme": "^1.8.5", "jest": "^29.7.0", "prettier": "^3.0.3", @@ -2728,9 +2728,9 @@ } }, "node_modules/avl-tree-typed": { - "version": "1.38.1", - "resolved": "https://registry.npmjs.org/avl-tree-typed/-/avl-tree-typed-1.38.1.tgz", - "integrity": "sha512-owzl0/cOijCKDyL8RAR+8Q2ibtqA+datgKQEbs1qNpCvMgYBtaytIgwyR68jYD+u7QGTXWYdO288H6NiKI0eTA==", + "version": "1.38.2", + "resolved": "https://registry.npmjs.org/avl-tree-typed/-/avl-tree-typed-1.38.2.tgz", + "integrity": "sha512-2P1DPzdnTuvENa6bGA7vqglC+Ae8GndRSX4H5JeB4V5ZVoiWFpSDL2lO4kRJJYbceL+hemx4Ge/MriGYKgXIfQ==", "dev": true }, "node_modules/babel-jest": { @@ -2924,9 +2924,9 @@ } }, "node_modules/binary-tree-typed": { - "version": "1.38.1", - "resolved": "https://registry.npmjs.org/binary-tree-typed/-/binary-tree-typed-1.38.1.tgz", - "integrity": "sha512-h4dBfVSaqUPfpVRHI7ZWUv3BSFYF9VtzoRu5X2rIq4se89QX+1cQTirQ7bCG0mDLJ9A4GfmEVMvQvvmVhorBEQ==", + "version": "1.38.2", + "resolved": "https://registry.npmjs.org/binary-tree-typed/-/binary-tree-typed-1.38.2.tgz", + "integrity": "sha512-odemWypyURZVq3qdegpaQDFydmt3sZA3uLHbcav3bBbnPj9WuMSIbOeOs21Nrj/yJHk1GrmlFRbPjYrrDXfdaw==", "dev": true }, "node_modules/brace-expansion": { @@ -3005,9 +3005,9 @@ } }, "node_modules/bst-typed": { - "version": "1.38.1", - "resolved": "https://registry.npmjs.org/bst-typed/-/bst-typed-1.38.1.tgz", - "integrity": "sha512-jNBmdOaSAApSx5iYo5ijunp3rtATcTmNOVnRiasCqKzSYTLfCJiGqIoXkDYMIFWK7hlgbAsVJkzTLciKf4RjcQ==", + "version": "1.38.2", + "resolved": "https://registry.npmjs.org/bst-typed/-/bst-typed-1.38.2.tgz", + "integrity": "sha512-BcTSvIbi8m5kEqzlS++YYlg2IgKhuqsX1mu200BLGq4d327vsQq9hY8TneuL54N4KAr4McR2GqpkDaHXQ3eTbw==", "dev": true }, "node_modules/buffer-from": { @@ -4756,9 +4756,9 @@ } }, "node_modules/heap-typed": { - "version": "1.38.1", - "resolved": "https://registry.npmjs.org/heap-typed/-/heap-typed-1.38.1.tgz", - "integrity": "sha512-PvLlO5lSds9zOOX4GvVS2mgZfLk0f7Ev+++hYkGrlCqpcCGDsXVn+B0k/Jel0mkTXIK3zVkev2if7Cxsmk1pEQ==", + "version": "1.38.2", + "resolved": "https://registry.npmjs.org/heap-typed/-/heap-typed-1.38.2.tgz", + "integrity": "sha512-GVNHQlbQkuuZMpMBj8fvekI8ClHst3s8W1qozLragMLD/ndb+acumWCXeypR9gwgjDTneqOW6fAgpUMDDKOn9Q==", "dev": true }, "node_modules/html-escaper": { diff --git a/package.json b/package.json index 3d07be4..93cf47e 100644 --- a/package.json +++ b/package.json @@ -61,17 +61,17 @@ "@typescript-eslint/eslint-plugin": "^6.7.4", "@typescript-eslint/parser": "^6.7.4", "auto-changelog": "^2.4.0", - "avl-tree-typed": "^1.38.1", + "avl-tree-typed": "^1.38.2", "benchmark": "^2.1.4", - "binary-tree-typed": "^1.38.1", - "bst-typed": "^1.38.1", + "binary-tree-typed": "^1.38.2", + "bst-typed": "^1.38.2", "dependency-cruiser": "^14.1.0", "eslint": "^8.50.0", "eslint-config-prettier": "^9.0.0", "eslint-import-resolver-alias": "^1.1.2", "eslint-import-resolver-typescript": "^3.6.1", "eslint-plugin-import": "^2.28.1", - "heap-typed": "^1.38.1", + "heap-typed": "^1.38.2", "istanbul-badges-readme": "^1.8.5", "jest": "^29.7.0", "prettier": "^3.0.3", diff --git a/src/data-structures/binary-tree/avl-tree.ts b/src/data-structures/binary-tree/avl-tree.ts index 704e907..1b1c88d 100644 --- a/src/data-structures/binary-tree/avl-tree.ts +++ b/src/data-structures/binary-tree/avl-tree.ts @@ -152,7 +152,7 @@ export class AVLTree = AVLTreeNode> extends B // Balance Restoration: If a balance issue is discovered after inserting a node, it requires balance restoration operations. Balance restoration includes four basic cases where rotation operations need to be performed to fix the balance: switch ( this._balanceFactor(A) // second O(1) - ) { + ) { case -2: if (A && A.left) { if (this._balanceFactor(A.left) <= 0) { diff --git a/src/data-structures/binary-tree/binary-indexed-tree.ts b/src/data-structures/binary-tree/binary-indexed-tree.ts index 467e918..da9dae0 100644 --- a/src/data-structures/binary-tree/binary-indexed-tree.ts +++ b/src/data-structures/binary-tree/binary-indexed-tree.ts @@ -17,7 +17,7 @@ export class BinaryIndexedTree { * @param - - `frequency`: The default frequency value. It is optional and has a default * value of 0. */ - constructor({frequency = 0, max}: { frequency?: number; max: number }) { + constructor({frequency = 0, max}: {frequency?: number; max: number}) { this._freq = frequency; this._max = max; this._freqMap = {0: 0}; diff --git a/src/data-structures/binary-tree/binary-tree.ts b/src/data-structures/binary-tree/binary-tree.ts index 9a38bc1..7b2ea5e 100644 --- a/src/data-structures/binary-tree/binary-tree.ts +++ b/src/data-structures/binary-tree/binary-tree.ts @@ -407,7 +407,7 @@ export class BinaryTree = BinaryTreeNode> return -1; } - const stack: { node: N; depth: number }[] = [{node: beginRoot, depth: 0}]; + const stack: {node: N; depth: number}[] = [{node: beginRoot, depth: 0}]; let maxHeight = 0; while (stack.length > 0) { @@ -512,9 +512,9 @@ export class BinaryTree = BinaryTreeNode> * traverse the binary tree. It can have two possible values: * @returns The function `getNodes` returns an array of nodes (`N[]`). */ - getNodes( + getNodes = MapCallback>( nodeProperty: BinaryTreeNodeKey | N, - callback: MapCallback = this._defaultCallbackByKey, + callback: C = this._defaultCallbackByKey as C, onlyOne = false, beginRoot: N | null = this.root, iterationType = this.iterationType @@ -570,9 +570,9 @@ export class BinaryTree = BinaryTreeNode> * performed when searching for nodes in the binary tree. It can have one of the following values: * @returns a boolean value. */ - has( + has = MapCallback>( nodeProperty: BinaryTreeNodeKey | N, - callback: MapCallback = this._defaultCallbackByKey, + callback: C = this._defaultCallbackByKey as C, beginRoot = this.root, iterationType = this.iterationType ): boolean { @@ -595,9 +595,9 @@ export class BinaryTree = BinaryTreeNode> * performed when searching for a node in the binary tree. It can have one of the following values: * @returns either the found node (of type N) or null if no node is found. */ - get( + get = MapCallback>( nodeProperty: BinaryTreeNodeKey | N, - callback: MapCallback = this._defaultCallbackByKey, + callback: C = this._defaultCallbackByKey as C, beginRoot = this.root, iterationType = this.iterationType ): N | null { @@ -761,11 +761,11 @@ export class BinaryTree = BinaryTreeNode> * performed on the binary tree. It can have two possible values: * @returns The function `subTreeTraverse` returns an array of `MapCallbackReturn`. */ - subTreeTraverse( - callback: MapCallback = this._defaultCallbackByKey, + subTreeTraverse = MapCallback>( + callback: C = this._defaultCallbackByKey as C, beginRoot: N | BinaryTreeNodeKey | null = this.root, iterationType = this.iterationType - ): MapCallbackReturn[] { + ): ReturnType[] { if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot); const ans: MapCallbackReturn[] = []; @@ -808,12 +808,12 @@ export class BinaryTree = BinaryTreeNode> * iteration used in the depth-first search algorithm. It can have two possible values: * @returns The function `dfs` returns an array of `MapCallbackReturn` values. */ - dfs( - callback: MapCallback = this._defaultCallbackByKey, + dfs = MapCallback>( + callback: C = this._defaultCallbackByKey as C, pattern: DFSOrderPattern = 'in', beginRoot: N | null = this.root, iterationType: IterationType = IterationType.ITERATIVE - ): MapCallbackReturn[] { + ): ReturnType[] { if (!beginRoot) return []; const ans: MapCallbackReturn[] = []; if (iterationType === IterationType.RECURSIVE) { @@ -842,7 +842,7 @@ export class BinaryTree = BinaryTreeNode> _traverse(beginRoot); } else { // 0: visit, 1: print - const stack: { opt: 0 | 1; node: N | null | undefined }[] = [{opt: 0, node: beginRoot}]; + const stack: {opt: 0 | 1; node: N | null | undefined}[] = [{opt: 0, node: beginRoot}]; while (stack.length > 0) { const cur = stack.pop(); @@ -896,12 +896,12 @@ export class BinaryTree = BinaryTreeNode> * in the breadth-first search (BFS) algorithm. It can have two possible values: * @returns The function `bfs` returns an array of `BFSCallbackReturn[]`. */ - bfs( - callback: BFSCallback = this._defaultCallbackByKey, + bfs = BFSCallback>( + callback: C = this._defaultCallbackByKey as C, withLevel: boolean = false, beginRoot: N | null = this.root, iterationType = this.iterationType - ): BFSCallbackReturn[] { + ): ReturnType[] { if (!beginRoot) return []; const ans: BFSCallbackReturn[] = []; @@ -964,11 +964,11 @@ export class BinaryTree = BinaryTreeNode> * `beginRoot` is `null`, an empty array will be returned. * @returns The `morris` function returns an array of `MapCallbackReturn` values. */ - morris( - callback: MapCallback = this._defaultCallbackByKey, + morris = MapCallback>( + callback: C = this._defaultCallbackByKey as C, pattern: DFSOrderPattern = 'in', beginRoot: N | null = this.root - ): MapCallbackReturn[] { + ): ReturnType[] { if (beginRoot === null) return []; const ans: MapCallbackReturn[] = []; @@ -1078,7 +1078,7 @@ export class BinaryTree = BinaryTreeNode> * This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape. */ - protected _defaultCallbackByKey: MapCallback = node => node.key; + protected _defaultCallbackByKey: (node: N) => number = node => node.key; /** * The function `_addTo` adds a new node to a binary tree if there is an available position. diff --git a/src/data-structures/binary-tree/bst.ts b/src/data-structures/binary-tree/bst.ts index b16ec02..0237476 100644 --- a/src/data-structures/binary-tree/bst.ts +++ b/src/data-structures/binary-tree/bst.ts @@ -238,9 +238,9 @@ export class BST = BSTNode> extends BinaryTree * @returns either the first node that matches the given nodeProperty and callback, or null if no * matching node is found. */ - override get( + override get = MapCallback>( nodeProperty: BinaryTreeNodeKey | N, - callback: MapCallback = this._defaultCallbackByKey, + callback: C = this._defaultCallbackByKey as C, beginRoot = this.root, iterationType = this.iterationType ): N | null { @@ -289,9 +289,9 @@ export class BST = BSTNode> extends BinaryTree * traverse the binary tree. It can have one of the following values: * @returns an array of nodes (N[]). */ - override getNodes( + override getNodes = MapCallback>( nodeProperty: BinaryTreeNodeKey | N, - callback: MapCallback = this._defaultCallbackByKey, + callback: C = this._defaultCallbackByKey as C, onlyOne = false, beginRoot: N | null = this.root, iterationType = this.iterationType @@ -363,12 +363,12 @@ export class BST = BSTNode> extends BinaryTree * done recursively or iteratively. It can have two possible values: * @returns The function `lesserOrGreaterTraverse` returns an array of `MapCallbackReturn`. */ - lesserOrGreaterTraverse( - callback: MapCallback = this._defaultCallbackByKey, + lesserOrGreaterTraverse = MapCallback>( + callback: C = this._defaultCallbackByKey as C, lesserOrGreater: CP = CP.lt, targetNode: N | BinaryTreeNodeKey | null = this.root, iterationType = this.iterationType - ): MapCallbackReturn { + ): ReturnType[] { if (typeof targetNode === 'number') targetNode = this.get(targetNode); const ans: MapCallbackReturn[] = []; if (!targetNode) return ans; diff --git a/src/data-structures/binary-tree/tree-multiset.ts b/src/data-structures/binary-tree/tree-multiset.ts index 40b9ddc..a36826b 100644 --- a/src/data-structures/binary-tree/tree-multiset.ts +++ b/src/data-structures/binary-tree/tree-multiset.ts @@ -37,7 +37,8 @@ export class TreeMultisetNode< */ export class TreeMultiset = TreeMultisetNode> extends AVLTree - implements IBinaryTree { + implements IBinaryTree +{ /** * The constructor function for a TreeMultiset class in TypeScript, which extends another class and sets an option to * merge duplicated values. diff --git a/src/data-structures/graph/abstract-graph.ts b/src/data-structures/graph/abstract-graph.ts index f93a731..e820f6b 100644 --- a/src/data-structures/graph/abstract-graph.ts +++ b/src/data-structures/graph/abstract-graph.ts @@ -105,7 +105,8 @@ export abstract class AbstractEdge { export abstract class AbstractGraph< V extends AbstractVertex = AbstractVertex, E extends AbstractEdge = AbstractEdge -> implements IGraph { +> implements IGraph +{ private _vertices: Map = new Map(); get vertices(): Map { @@ -553,14 +554,14 @@ export abstract class AbstractGraph< } getMinDist && - distMap.forEach((d, v) => { - if (v !== srcVertex) { - if (d < minDist) { - minDist = d; - if (genPaths) minDest = v; + distMap.forEach((d, v) => { + if (v !== srcVertex) { + if (d < minDist) { + minDist = d; + if (genPaths) minDest = v; + } } - } - }); + }); genPaths && getPaths(minDest); @@ -622,7 +623,7 @@ export abstract class AbstractGraph< if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity); } - const heap = new PriorityQueue<{ key: number; val: V }>((a, b) => a.key - b.key); + const heap = new PriorityQueue<{key: number; val: V}>((a, b) => a.key - b.key); heap.add({key: 0, val: srcVertex}); distMap.set(srcVertex, 0); @@ -851,7 +852,7 @@ export abstract class AbstractGraph< * `predecessor` property is a 2D array of vertices (or `null`) representing the predecessor vertices in the shortest * path between vertices in the */ - floyd(): { costs: number[][]; predecessor: (V | null)[][] } { + floyd(): {costs: number[][]; predecessor: (V | null)[][]} { const idAndVertices = [...this._vertices]; const n = idAndVertices.length; diff --git a/src/data-structures/graph/directed-graph.ts b/src/data-structures/graph/directed-graph.ts index f30a841..438ea75 100644 --- a/src/data-structures/graph/directed-graph.ts +++ b/src/data-structures/graph/directed-graph.ts @@ -64,7 +64,8 @@ export class DirectedEdge extends AbstractEdge { export class DirectedGraph = DirectedVertex, E extends DirectedEdge = DirectedEdge> extends AbstractGraph - implements IGraph { + implements IGraph +{ /** * The constructor function initializes an instance of a class. */ diff --git a/src/data-structures/graph/undirected-graph.ts b/src/data-structures/graph/undirected-graph.ts index a7a81ff..b962047 100644 --- a/src/data-structures/graph/undirected-graph.ts +++ b/src/data-structures/graph/undirected-graph.ts @@ -51,11 +51,12 @@ export class UndirectedEdge extends AbstractEdge { } export class UndirectedGraph< - V extends UndirectedVertex = UndirectedVertex, - E extends UndirectedEdge = UndirectedEdge -> + V extends UndirectedVertex = UndirectedVertex, + E extends UndirectedEdge = UndirectedEdge + > extends AbstractGraph - implements IGraph { + implements IGraph +{ /** * The constructor initializes a new Map object to store edges. */ diff --git a/src/data-structures/hash/hash-map.ts b/src/data-structures/hash/hash-map.ts index 5231237..f3a5213 100644 --- a/src/data-structures/hash/hash-map.ts +++ b/src/data-structures/hash/hash-map.ts @@ -157,7 +157,7 @@ export class HashMap { } } - * entries(): IterableIterator<[K, V]> { + *entries(): IterableIterator<[K, V]> { for (const bucket of this.table) { if (bucket) { for (const [key, value] of bucket) { diff --git a/src/data-structures/hash/tree-map.ts b/src/data-structures/hash/tree-map.ts index a6d743d..fe86360 100644 --- a/src/data-structures/hash/tree-map.ts +++ b/src/data-structures/hash/tree-map.ts @@ -1,2 +1 @@ -export class TreeMap { -} +export class TreeMap {} diff --git a/src/data-structures/hash/tree-set.ts b/src/data-structures/hash/tree-set.ts index 65f14db..591aeda 100644 --- a/src/data-structures/hash/tree-set.ts +++ b/src/data-structures/hash/tree-set.ts @@ -1,2 +1 @@ -export class TreeSet { -} +export class TreeSet {} diff --git a/src/data-structures/linked-list/singly-linked-list.ts b/src/data-structures/linked-list/singly-linked-list.ts index b39925f..2eaca25 100644 --- a/src/data-structures/linked-list/singly-linked-list.ts +++ b/src/data-structures/linked-list/singly-linked-list.ts @@ -490,7 +490,7 @@ export class SinglyLinkedList { return count; } - * [Symbol.iterator]() { + *[Symbol.iterator]() { let current = this.head; while (current) { diff --git a/src/data-structures/matrix/matrix.ts b/src/data-structures/matrix/matrix.ts index 7e8ae4b..8f27617 100644 --- a/src/data-structures/matrix/matrix.ts +++ b/src/data-structures/matrix/matrix.ts @@ -14,7 +14,7 @@ export class MatrixNTI2D { * given initial value or 0 if not provided. * @param options - An object containing the following properties: */ - constructor(options: { row: number; col: number; initialVal?: V }) { + constructor(options: {row: number; col: number; initialVal?: V}) { const {row, col, initialVal} = options; this._matrix = new Array(row).fill(undefined).map(() => new Array(col).fill(initialVal || 0)); } diff --git a/src/data-structures/matrix/vector2d.ts b/src/data-structures/matrix/vector2d.ts index e8e4074..1b215ca 100644 --- a/src/data-structures/matrix/vector2d.ts +++ b/src/data-structures/matrix/vector2d.ts @@ -10,8 +10,7 @@ export class Vector2D { public x: number = 0, public y: number = 0, public w: number = 1 // needed for matrix multiplication - ) { - } + ) {} /** * The function checks if the x and y values of a point are both zero. diff --git a/src/data-structures/queue/deque.ts b/src/data-structures/queue/deque.ts index 9faabda..3290ab1 100644 --- a/src/data-structures/queue/deque.ts +++ b/src/data-structures/queue/deque.ts @@ -9,8 +9,7 @@ import {DoublyLinkedList} from '../linked-list'; // O(n) time complexity of obtaining the value // O(1) time complexity of adding at the beginning and the end -export class Deque extends DoublyLinkedList { -} +export class Deque extends DoublyLinkedList {} // O(1) time complexity of obtaining the value // O(n) time complexity of adding at the beginning and the end @@ -20,9 +19,9 @@ export class ObjectDeque { if (capacity !== undefined) this._capacity = capacity; } - private _nodes: { [key: number]: E } = {}; + private _nodes: {[key: number]: E} = {}; - get nodes(): { [p: number]: E } { + get nodes(): {[p: number]: E} { return this._nodes; } @@ -157,7 +156,7 @@ export class ObjectDeque { return this._size <= 0; } - protected _seNodes(value: { [p: number]: E }) { + protected _seNodes(value: {[p: number]: E}) { this._nodes = value; } diff --git a/src/data-structures/queue/queue.ts b/src/data-structures/queue/queue.ts index d11dd92..4549b31 100644 --- a/src/data-structures/queue/queue.ts +++ b/src/data-structures/queue/queue.ts @@ -183,7 +183,7 @@ export class Queue { return new Queue(this.nodes.slice(this.offset)); } - * [Symbol.iterator]() { + *[Symbol.iterator]() { for (const item of this.nodes) { yield item; } diff --git a/src/types/data-structures/binary-tree/binary-tree.ts b/src/types/data-structures/binary-tree/binary-tree.ts index 4d0e908..80ce0a4 100644 --- a/src/types/data-structures/binary-tree/binary-tree.ts +++ b/src/types/data-structures/binary-tree/binary-tree.ts @@ -1,4 +1,5 @@ import {BinaryTreeNode} from '../../../data-structures'; +import PropertyKeysOf = jest.PropertyKeysOf; /** * Enum representing different loop types. @@ -24,7 +25,7 @@ export enum FamilyPosition { export type BinaryTreeNodeKey = number; -export type BFSCallback = (node: N, level?: number) => any; +export type BFSCallback = (node: N, level?: number) => D; export type BFSCallbackReturn = ReturnType>; diff --git a/src/types/data-structures/matrix/navigator.ts b/src/types/data-structures/matrix/navigator.ts index 34eddd9..9d8b9a9 100644 --- a/src/types/data-structures/matrix/navigator.ts +++ b/src/types/data-structures/matrix/navigator.ts @@ -1,6 +1,6 @@ export type Direction = 'up' | 'right' | 'down' | 'left'; -export type Turning = { [key in Direction]: Direction }; +export type Turning = {[key in Direction]: Direction}; export type NavigatorParams = { matrix: T[][]; diff --git a/src/types/helpers.ts b/src/types/helpers.ts index ca84f77..077114d 100644 --- a/src/types/helpers.ts +++ b/src/types/helpers.ts @@ -2,7 +2,7 @@ export type Comparator = (a: T, b: T) => number; export type DFSOrderPattern = 'pre' | 'in' | 'post'; -export type MapCallback = (node: N) => any; +export type MapCallback = (node: N) => D; export type MapCallbackReturn = ReturnType>; diff --git a/src/types/utils/utils.ts b/src/types/utils/utils.ts index 1f3a505..f4d26c4 100644 --- a/src/types/utils/utils.ts +++ b/src/types/utils/utils.ts @@ -1,5 +1,5 @@ export type ToThunkFn = () => ReturnType; -export type Thunk = () => ReturnType & { __THUNK__: symbol }; +export type Thunk = () => ReturnType & {__THUNK__: symbol}; export type TrlFn = (...args: any[]) => any; export type TrlAsyncFn = (...args: any[]) => any; diff --git a/src/types/utils/validate-type.ts b/src/types/utils/validate-type.ts index 3ebf451..ac9ff28 100644 --- a/src/types/utils/validate-type.ts +++ b/src/types/utils/validate-type.ts @@ -1,6 +1,6 @@ -export type KeyValueObject = { [key: string]: any }; +export type KeyValueObject = {[key: string]: any}; -export type KeyValueObjectWithKey = { [key: string]: any; key: string | number | symbol }; +export type KeyValueObjectWithKey = {[key: string]: any; key: string | number | symbol}; export type NonNumberNonObjectButDefined = string | boolean | symbol | null; diff --git a/test/integration/bst.test.ts b/test/integration/bst.test.ts index 425af06..a482a37 100644 --- a/test/integration/bst.test.ts +++ b/test/integration/bst.test.ts @@ -183,7 +183,7 @@ describe('Individual package BST operations test', () => { }); it('should perform various operations on a Binary Search Tree with object values', () => { - const objBST = new BST>(); + const objBST = new BST>(); expect(objBST).toBeInstanceOf(BST); objBST.add(11, {key: 11, keyA: 11}); objBST.add(3, {key: 3, keyA: 3}); 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 aa0ebee..e0e5e18 100644 --- a/test/unit/data-structures/binary-tree/avl-tree.test.ts +++ b/test/unit/data-structures/binary-tree/avl-tree.test.ts @@ -38,8 +38,7 @@ describe('AVL Tree Test', () => { expect(dfs[dfs.length - 1].key).toBe(16); tree.perfectlyBalance(); - const bfs: AVLTreeNode[] = []; - tree.bfs(node => bfs.push(node)); + const bfs = tree.bfs(node => node); expect(tree.isPerfectlyBalanced()).toBe(true); expect(bfs[0].key).toBe(8); expect(bfs[bfs.length - 1].key).toBe(16); @@ -98,8 +97,7 @@ describe('AVL Tree Test', () => { expect(tree.getHeight()).toBe(1); expect(tree.isAVLBalanced()).toBe(true); - const lastBFSIds = new Array(); - tree.bfs(node => lastBFSIds.push(node.key)); + const lastBFSIds = tree.bfs(); expect(lastBFSIds[0]).toBe(12); expect(lastBFSIds[1]).toBe(2); expect(lastBFSIds[2]).toBe(16); @@ -111,3 +109,22 @@ describe('AVL Tree Test', () => { expect(lastBFSNodes[2].key).toBe(16); }); }); + +describe('AVLTree APIs test', () => { + const avl = new AVLTree>(); + beforeEach(() => { + avl.clear(); + }); + + it('add', () => { + avl.add(1); + const node2 = new AVLTreeNode(2); + avl.add(node2); + const node3 = new AVLTreeNode(3, {id: 3, text: 'text3'}); + avl.add(node3); + avl.add(node3, {id: 3, text: 'text33'}); + + const bfsRes = avl.bfs(node => node.key, false); + expect(bfsRes[0]).toBe(2); + }); +}); 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 21cbe99..fcb5f4e 100644 --- a/test/unit/data-structures/binary-tree/binary-tree.test.ts +++ b/test/unit/data-structures/binary-tree/binary-tree.test.ts @@ -1,4 +1,4 @@ -import {BinaryTree, BinaryTreeNode} from '../../../../src'; +import {AVLTree, AVLTreeNode, BinaryTree, BinaryTreeNode} from '../../../../src'; describe('BinaryTreeNode', () => { it('should create an instance of BinaryTreeNode', () => { @@ -194,3 +194,22 @@ describe('BinaryTree Morris Traversal', () => { expect(node1?.right).toBe(node3); }); }); + +describe('BinaryTree APIs test', () => { + const avl = new AVLTree>(); + beforeEach(() => { + avl.clear(); + }); + + it('add', () => { + avl.add(1); + const node2 = new AVLTreeNode(2); + avl.add(node2); + const node3 = new AVLTreeNode(3, {id: 3, text: 'text3'}); + avl.add(node3); + avl.add(node3, {id: 3, text: 'text33'}); + + const bfsRes = avl.bfs(node => node); + expect(bfsRes[0]?.key).toBe(2); + }); +}); diff --git a/test/unit/data-structures/binary-tree/bst.test.ts b/test/unit/data-structures/binary-tree/bst.test.ts index 6290d07..9683af9 100644 --- a/test/unit/data-structures/binary-tree/bst.test.ts +++ b/test/unit/data-structures/binary-tree/bst.test.ts @@ -189,7 +189,7 @@ describe('BST operations test', () => { }); it('should perform various operations on a Binary Search Tree with object values', () => { - const objBST = new BST>(); + const objBST = new BST>(); expect(objBST).toBeInstanceOf(BST); objBST.add(11, {key: 11, keyA: 11}); objBST.add(3, {key: 3, keyA: 3}); @@ -260,7 +260,7 @@ describe('BST operations test', () => { objBST.perfectlyBalance(); expect(objBST.isPerfectlyBalanced()).toBe(true); - const bfsNodesAfterBalanced: BSTNode<{ key: number; keyA: number }>[] = []; + 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); @@ -385,7 +385,7 @@ describe('BST operations test', () => { expect(bfsIDs[1]).toBe(12); expect(bfsIDs[2]).toBe(16); - const bfsNodes: BSTNode<{ key: number; keyA: number }>[] = []; + 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); diff --git a/test/unit/data-structures/binary-tree/overall.test.ts b/test/unit/data-structures/binary-tree/overall.test.ts index d12b9ad..5325180 100644 --- a/test/unit/data-structures/binary-tree/overall.test.ts +++ b/test/unit/data-structures/binary-tree/overall.test.ts @@ -29,7 +29,7 @@ describe('Overall BinaryTree Test', () => { bfsIDs[0] === 11; // true expect(bfsIDs[0]).toBe(11); - const objBST = new BST>(); + const objBST = new BST>(); objBST.add(11, {key: 11, keyA: 11}); objBST.add(3, {key: 3, keyA: 3}); 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 997c28d..6812143 100644 --- a/test/unit/data-structures/binary-tree/tree-multiset.test.ts +++ b/test/unit/data-structures/binary-tree/tree-multiset.test.ts @@ -206,7 +206,7 @@ describe('TreeMultiset operations test', () => { }); it('should perform various operations on a Binary Search Tree with object values', () => { - const objTreeMultiset = new TreeMultiset>(); + const objTreeMultiset = new TreeMultiset>(); expect(objTreeMultiset).toBeInstanceOf(TreeMultiset); objTreeMultiset.add(11, {key: 11, keyA: 11}); objTreeMultiset.add(3, {key: 3, keyA: 3}); diff --git a/test/unit/data-structures/heap/heap.test.ts b/test/unit/data-structures/heap/heap.test.ts index 14c54cf..8745402 100644 --- a/test/unit/data-structures/heap/heap.test.ts +++ b/test/unit/data-structures/heap/heap.test.ts @@ -22,7 +22,7 @@ describe('Heap Operation Test', () => { }); it('should object heap work well', function () { - const minHeap = new MinHeap<{ a: string; key: number }>((a, b) => a.key - b.key); + const minHeap = new MinHeap<{a: string; key: number}>((a, b) => a.key - b.key); minHeap.add({key: 1, a: 'a1'}); minHeap.add({key: 6, a: 'a6'}); minHeap.add({key: 2, a: 'a2'}); @@ -37,7 +37,7 @@ describe('Heap Operation Test', () => { i++; } - const maxHeap = new MaxHeap<{ key: number; a: string }>((a, b) => b.key - a.key); + const maxHeap = new MaxHeap<{key: number; a: string}>((a, b) => b.key - a.key); maxHeap.add({key: 1, a: 'a1'}); maxHeap.add({key: 6, a: 'a6'}); maxHeap.add({key: 5, a: 'a5'}); diff --git a/test/unit/data-structures/linked-list/doubly-linked-list.test.ts b/test/unit/data-structures/linked-list/doubly-linked-list.test.ts index 0838337..eab2ad5 100644 --- a/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +++ b/test/unit/data-structures/linked-list/doubly-linked-list.test.ts @@ -3,7 +3,7 @@ import {bigO, magnitude} from '../../../utils'; describe('DoublyLinkedList Operation Test', () => { let list: DoublyLinkedList; - let objectList: DoublyLinkedList<{ keyA: number }>; + let objectList: DoublyLinkedList<{keyA: number}>; beforeEach(() => { list = new DoublyLinkedList(); diff --git a/test/unit/data-structures/linked-list/singly-linked-list.test.ts b/test/unit/data-structures/linked-list/singly-linked-list.test.ts index 092b2e6..74a3de8 100644 --- a/test/unit/data-structures/linked-list/singly-linked-list.test.ts +++ b/test/unit/data-structures/linked-list/singly-linked-list.test.ts @@ -3,10 +3,10 @@ import {bigO, magnitude} from '../../../utils'; describe('SinglyLinkedList Operation Test', () => { let list: SinglyLinkedList; - let objectList: SinglyLinkedList<{ keyA: number }>; + let objectList: SinglyLinkedList<{keyA: number}>; beforeEach(() => { list = new SinglyLinkedList(); - objectList = new SinglyLinkedList<{ keyA: number }>(); + objectList = new SinglyLinkedList<{keyA: number}>(); }); describe('push', () => { diff --git a/test/unit/data-structures/priority-queue/max-priority-queue.test.ts b/test/unit/data-structures/priority-queue/max-priority-queue.test.ts index f389309..f8a63bc 100644 --- a/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +++ b/test/unit/data-structures/priority-queue/max-priority-queue.test.ts @@ -17,7 +17,7 @@ describe('MaxPriorityQueue Operation Test', () => { }); it('should add elements and maintain heap property in a object MaxPriorityQueue', () => { - const priorityQueue = new MaxPriorityQueue<{ keyA: number }>((a, b) => b.keyA - a.keyA); + const priorityQueue = new MaxPriorityQueue<{keyA: number}>((a, b) => b.keyA - a.keyA); priorityQueue.refill([{keyA: 5}, {keyA: 3}, {keyA: 1}]); priorityQueue.add({keyA: 7}); @@ -64,7 +64,7 @@ describe('MaxPriorityQueue Operation Test', () => { it('should correctly heapify an object array', () => { const nodes = [{keyA: 5}, {keyA: 3}, {keyA: 7}, {keyA: 1}]; - const maxPQ = MaxPriorityQueue.heapify<{ keyA: number }>(nodes, (a, b) => b.keyA - a.keyA); + const maxPQ = MaxPriorityQueue.heapify<{keyA: number}>(nodes, (a, b) => b.keyA - a.keyA); expect(maxPQ.poll()?.keyA).toBe(7); expect(maxPQ.poll()?.keyA).toBe(5); diff --git a/test/utils/big-o.ts b/test/utils/big-o.ts index cc9fc86..43d7eca 100644 --- a/test/utils/big-o.ts +++ b/test/utils/big-o.ts @@ -26,7 +26,7 @@ export const bigO = { function findPotentialN(input: any): number { let longestArray: any[] = []; - let mostProperties: { [key: string]: any } = {}; + let mostProperties: {[key: string]: any} = {}; function recurse(obj: any) { if (Array.isArray(obj)) {