From 6c45f53f72216609fa3b11cfde580ae9909d8c06 Mon Sep 17 00:00:00 2001 From: Revone Date: Sat, 28 Oct 2023 17:37:51 +0800 Subject: [PATCH] [heap, priority-queue, binary-indexed-tree] Heap and PriorityQueue constructors use a single object as a parameter for uniformity with the design of other data structures. Binary Indexed Tree includes the getPrefixSum method. --- CHANGELOG.md | 2 +- package-lock.json | 50 +++++++++---------- package.json | 10 ++-- .../binary-tree/binary-indexed-tree.ts | 20 ++++++++ src/data-structures/graph/abstract-graph.ts | 2 +- src/data-structures/heap/heap.ts | 24 ++++----- src/data-structures/heap/max-heap.ts | 14 +++--- src/data-structures/heap/min-heap.ts | 14 +++--- .../priority-queue/max-priority-queue.ts | 14 +++--- .../priority-queue/min-priority-queue.ts | 14 +++--- .../priority-queue/priority-queue.ts | 6 +-- test/integration/bst.test.ts | 2 +- .../binary-tree/avl-tree.test.ts | 2 +- .../binary-tree/binary-index-tree.test.ts | 35 +++++++++++++ .../binary-tree/binary-tree.test.ts | 2 +- .../data-structures/binary-tree/bst.test.ts | 6 +-- .../binary-tree/overall.test.ts | 2 +- .../binary-tree/tree-multiset.test.ts | 2 +- test/unit/data-structures/heap/heap.test.ts | 4 +- .../data-structures/heap/max-heap.test.ts | 2 +- .../data-structures/heap/min-heap.test.ts | 2 +- .../linked-list/doubly-linked-list.test.ts | 2 +- .../linked-list/singly-linked-list.test.ts | 4 +- .../priority-queue/max-priority-queue.test.ts | 6 +-- .../priority-queue/priority-queue.test.ts | 20 ++++---- test/utils/big-o.ts | 2 +- 26 files changed, 163 insertions(+), 100 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index faa6716..43c4c33 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ All notable changes to this project will be documented in this file. - [Semantic Versioning](https://semver.org/spec/v2.0.0.html) - [`auto-changelog`](https://github.com/CookPete/auto-changelog) -## [v1.38.9](https://github.com/zrwusa/data-structure-typed/compare/v1.35.0...main) (upcoming) +## [v1.39.0](https://github.com/zrwusa/data-structure-typed/compare/v1.35.0...main) (upcoming) ### Changes diff --git a/package-lock.json b/package-lock.json index fe7c2c2..036d8c4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "data-structure-typed", - "version": "1.38.9", + "version": "1.39.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "data-structure-typed", - "version": "1.38.9", + "version": "1.39.0", "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.8", + "avl-tree-typed": "^1.38.9", "benchmark": "^2.1.4", - "binary-tree-typed": "^1.38.8", - "bst-typed": "^1.38.8", + "binary-tree-typed": "^1.38.9", + "bst-typed": "^1.38.9", "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.8", + "heap-typed": "^1.38.9", "istanbul-badges-readme": "^1.8.5", "jest": "^29.7.0", "prettier": "^3.0.3", @@ -2728,12 +2728,12 @@ } }, "node_modules/avl-tree-typed": { - "version": "1.38.8", - "resolved": "https://registry.npmjs.org/avl-tree-typed/-/avl-tree-typed-1.38.8.tgz", - "integrity": "sha512-TMR5vZgH2fGUWDHqG7LNnGmWximwzg4tGthKNT3l9bL7zGnAg2C406j/6Hp/J29mvb5DV+j2NCCeBXmhu/B+AA==", + "version": "1.38.9", + "resolved": "https://registry.npmjs.org/avl-tree-typed/-/avl-tree-typed-1.38.9.tgz", + "integrity": "sha512-1Qy7d5G8JXa5Z2KKmlZI1WOmJkIyW6l2zRGHCxXItEfIg+BirZm6BK4IOb03OJjA2qg2N5AxgZDKuMDAzbRXNw==", "dev": true, "dependencies": { - "data-structure-typed": "^1.38.8" + "data-structure-typed": "^1.38.9" } }, "node_modules/babel-jest": { @@ -2927,12 +2927,12 @@ } }, "node_modules/binary-tree-typed": { - "version": "1.38.8", - "resolved": "https://registry.npmjs.org/binary-tree-typed/-/binary-tree-typed-1.38.8.tgz", - "integrity": "sha512-sS1Efx5XDFY8d1r+pLrt17sZH2bDQbhzJm27OFPO6p/Qkz7qSeJZhYTrpnbr2IFjk3eh0ljkIa3e9+s7AYfhow==", + "version": "1.38.9", + "resolved": "https://registry.npmjs.org/binary-tree-typed/-/binary-tree-typed-1.38.9.tgz", + "integrity": "sha512-XGp+ADfVMhhpEqLV+Bi27FkJcT1P6ExPpGuhzhKzyfoWmsnjp94tuuvrUDCqPjYoioGwzbG7yUnC2Fbi4CbsAA==", "dev": true, "dependencies": { - "data-structure-typed": "^1.38.8" + "data-structure-typed": "^1.38.9" } }, "node_modules/brace-expansion": { @@ -3011,12 +3011,12 @@ } }, "node_modules/bst-typed": { - "version": "1.38.8", - "resolved": "https://registry.npmjs.org/bst-typed/-/bst-typed-1.38.8.tgz", - "integrity": "sha512-gv3w5u3YIpnmQltcxWbLtpFqom3/z9b/NPdtc57m6rPaRAAWQy4pzyXAETgQX29p8RrCkVn/2cZLC6i6YfEjWw==", + "version": "1.38.9", + "resolved": "https://registry.npmjs.org/bst-typed/-/bst-typed-1.38.9.tgz", + "integrity": "sha512-jnHwqyTjRCDitmzXydhcP1YmRsCfBYxwhDXD8lm1pQPWOXkcAIU7HqTg7WCPrReo1GaWfQJeBhAIsguluEQ6Lw==", "dev": true, "dependencies": { - "data-structure-typed": "^1.38.8" + "data-structure-typed": "^1.38.9" } }, "node_modules/buffer-from": { @@ -3413,9 +3413,9 @@ } }, "node_modules/data-structure-typed": { - "version": "1.38.8", - "resolved": "https://registry.npmjs.org/data-structure-typed/-/data-structure-typed-1.38.8.tgz", - "integrity": "sha512-HU+9+HqDLN9f9ipANtibwkK3d65f0FzpvuKavrHSbBMiJvufwchZUxNkfmwGLqgdX8MUGw/Rrk15GpEloYhKNw==", + "version": "1.38.9", + "resolved": "https://registry.npmjs.org/data-structure-typed/-/data-structure-typed-1.38.9.tgz", + "integrity": "sha512-iHOVtsBY7ZVI28I0ikedDFmBeu3rGu1gSnmemg11PT/KIuflNe9zdwlNm+yZ4M56LbQp0aqY09YoNnNX43UvRw==", "dev": true }, "node_modules/debug": { @@ -4771,12 +4771,12 @@ } }, "node_modules/heap-typed": { - "version": "1.38.8", - "resolved": "https://registry.npmjs.org/heap-typed/-/heap-typed-1.38.8.tgz", - "integrity": "sha512-jckb42KnKLidVpfbAChCkrYKFU1rVvTXiH/2G6ABsipzhOIvpM5ItIiR20hT2QP6wkUsEg/CG/K7h3HDQIsXXg==", + "version": "1.38.9", + "resolved": "https://registry.npmjs.org/heap-typed/-/heap-typed-1.38.9.tgz", + "integrity": "sha512-50RxpJOqMDMbpGa3NZny3YO8NArmwVeIipNDHkOvHXH+P93BvbaFz06rOyuJ1cUxDwTPsv34VfJgrz576/BZtg==", "dev": true, "dependencies": { - "data-structure-typed": "^1.38.8" + "data-structure-typed": "^1.38.9" } }, "node_modules/html-escaper": { diff --git a/package.json b/package.json index af9208a..8ee3af3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "data-structure-typed", - "version": "1.38.9", + "version": "1.39.0", "description": "Data Structures of Javascript & TypeScript. Binary Tree, BST, Graph, Heap, Priority Queue, Linked List, Queue, Deque, Stack, AVL Tree, Tree Multiset, Trie, Directed Graph, Undirected Graph, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue.", "main": "dist/cjs/index.js", "module": "dist/mjs/index.js", @@ -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.8", + "avl-tree-typed": "^1.38.9", "benchmark": "^2.1.4", - "binary-tree-typed": "^1.38.8", - "bst-typed": "^1.38.8", + "binary-tree-typed": "^1.38.9", + "bst-typed": "^1.38.9", "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.8", + "heap-typed": "^1.38.9", "istanbul-badges-readme": "^1.8.5", "jest": "^29.7.0", "prettier": "^3.0.3", diff --git a/src/data-structures/binary-tree/binary-indexed-tree.ts b/src/data-structures/binary-tree/binary-indexed-tree.ts index 467e918..4b4e8dd 100644 --- a/src/data-structures/binary-tree/binary-indexed-tree.ts +++ b/src/data-structures/binary-tree/binary-indexed-tree.ts @@ -143,6 +143,26 @@ export class BinaryIndexedTree { return this._binarySearch(sum, (x, y) => x <= y); } + /** + * The function calculates the prefix sum of an array using a binary indexed tree. + * @param {number} i - The parameter "i" in the function "getPrefixSum" represents the index of the element in the + * array for which we want to calculate the prefix sum. + * @returns The function `getPrefixSum` returns the prefix sum of the elements in the binary indexed tree up to index + * `i`. + */ + getPrefixSum(i: number): number { + this._checkIndex(i); + i++; // Convert to 1-based index + + let sum = 0; + while (i > 0) { + sum += this._getFrequency(i); + i -= i & -i; + } + + return sum; + } + /** * The function returns the value of a specific index in a freqMap data structure, or a default value if * the index is not found. diff --git a/src/data-structures/graph/abstract-graph.ts b/src/data-structures/graph/abstract-graph.ts index 49ecc28..adc52db 100644 --- a/src/data-structures/graph/abstract-graph.ts +++ b/src/data-structures/graph/abstract-graph.ts @@ -622,7 +622,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 }>({comparator: (a, b) => a.key - b.key}); heap.add({key: 0, val: srcVertex}); distMap.set(srcVertex, 0); diff --git a/src/data-structures/heap/heap.ts b/src/data-structures/heap/heap.ts index 177b1e7..b4a197b 100644 --- a/src/data-structures/heap/heap.ts +++ b/src/data-structures/heap/heap.ts @@ -7,12 +7,16 @@ import type {Comparator, DFSOrderPattern} from '../../types'; -export class Heap { +export class Heap { protected nodes: E[] = []; protected readonly comparator: Comparator; - constructor(comparator: Comparator) { - this.comparator = comparator; + constructor(options: { comparator: Comparator; nodes?: E[] }) { + this.comparator = options.comparator; + if (options.nodes && options.nodes.length > 0) { + this.nodes = options.nodes; + this.fix(); + } } /** @@ -32,15 +36,11 @@ export class Heap { /** * Static method that creates a binary heap from an array of nodes and a comparison function. - * @param nodes - * @param comparator - Comparison function. * @returns A new Heap instance. + * @param options */ - static heapify(nodes: E[], comparator: Comparator): Heap { - const binaryHeap = new Heap(comparator); - binaryHeap.nodes = [...nodes]; - binaryHeap.fix(); // Fix heap properties - return binaryHeap; + static heapify(options: { nodes: E[]; comparator: Comparator }): Heap { + return new Heap(options); } /** @@ -180,7 +180,7 @@ export class Heap { * @returns A new Heap instance containing the same elements. */ clone(): Heap { - const clonedHeap = new Heap(this.comparator); + const clonedHeap = new Heap({comparator: this.comparator}); clonedHeap.nodes = [...this.nodes]; return clonedHeap; } @@ -269,7 +269,7 @@ export class FibonacciHeapNode { export class FibonacciHeap { root?: FibonacciHeapNode; - size: number = 0; + size = 0; protected min?: FibonacciHeapNode; protected readonly comparator: Comparator; diff --git a/src/data-structures/heap/max-heap.ts b/src/data-structures/heap/max-heap.ts index 55ace08..139ef64 100644 --- a/src/data-structures/heap/max-heap.ts +++ b/src/data-structures/heap/max-heap.ts @@ -11,14 +11,16 @@ import type {Comparator} from '../../types'; export class MaxHeap extends Heap { constructor( - comparator: Comparator = (a: E, b: E) => { - if (!(typeof a === 'number' && typeof b === 'number')) { - throw new Error('The a, b params of compare function must be number'); - } else { - return b - a; + options: { comparator: Comparator; nodes?: E[] } = { + comparator: (a: E, b: E) => { + if (!(typeof a === 'number' && typeof b === 'number')) { + throw new Error('The a, b params of compare function must be number'); + } else { + return b - a; + } } } ) { - super(comparator); + super(options); } } diff --git a/src/data-structures/heap/min-heap.ts b/src/data-structures/heap/min-heap.ts index fcc809b..5057017 100644 --- a/src/data-structures/heap/min-heap.ts +++ b/src/data-structures/heap/min-heap.ts @@ -11,14 +11,16 @@ import type {Comparator} from '../../types'; export class MinHeap extends Heap { constructor( - comparator: Comparator = (a: E, b: E) => { - if (!(typeof a === 'number' && typeof b === 'number')) { - throw new Error('The a, b params of compare function must be number'); - } else { - return a - b; + options: { comparator: Comparator; nodes?: E[] } = { + comparator: (a: E, b: E) => { + if (!(typeof a === 'number' && typeof b === 'number')) { + throw new Error('The a, b params of compare function must be number'); + } else { + return a - b; + } } } ) { - super(comparator); + super(options); } } diff --git a/src/data-structures/priority-queue/max-priority-queue.ts b/src/data-structures/priority-queue/max-priority-queue.ts index 49f016e..409c99f 100644 --- a/src/data-structures/priority-queue/max-priority-queue.ts +++ b/src/data-structures/priority-queue/max-priority-queue.ts @@ -10,14 +10,16 @@ import type {Comparator} from '../../types'; export class MaxPriorityQueue extends PriorityQueue { constructor( - compare: Comparator = (a: E, b: E) => { - if (!(typeof a === 'number' && typeof b === 'number')) { - throw new Error('The a, b params of compare function must be number'); - } else { - return b - a; + options: { comparator: Comparator; nodes?: E[] } = { + comparator: (a: E, b: E) => { + if (!(typeof a === 'number' && typeof b === 'number')) { + throw new Error('The a, b params of compare function must be number'); + } else { + return b - a; + } } } ) { - super(compare); + super(options); } } diff --git a/src/data-structures/priority-queue/min-priority-queue.ts b/src/data-structures/priority-queue/min-priority-queue.ts index 0e1b3e8..da8ab64 100644 --- a/src/data-structures/priority-queue/min-priority-queue.ts +++ b/src/data-structures/priority-queue/min-priority-queue.ts @@ -10,14 +10,16 @@ import type {Comparator} from '../../types'; export class MinPriorityQueue extends PriorityQueue { constructor( - compare: Comparator = (a: E, b: E) => { - if (!(typeof a === 'number' && typeof b === 'number')) { - throw new Error('The a, b params of compare function must be number'); - } else { - return a - b; + options: { comparator: Comparator; nodes?: E[] } = { + comparator: (a: E, b: E) => { + if (!(typeof a === 'number' && typeof b === 'number')) { + throw new Error('The a, b params of compare function must be number'); + } else { + return a - b; + } } } ) { - super(compare); + super(options); } } diff --git a/src/data-structures/priority-queue/priority-queue.ts b/src/data-structures/priority-queue/priority-queue.ts index 2bc2769..60deb98 100644 --- a/src/data-structures/priority-queue/priority-queue.ts +++ b/src/data-structures/priority-queue/priority-queue.ts @@ -9,8 +9,8 @@ import {Heap} from '../heap'; import {Comparator} from '../../types'; -export class PriorityQueue extends Heap { - constructor(comparator: Comparator) { - super(comparator); +export class PriorityQueue extends Heap { + constructor(options: { comparator: Comparator; nodes?: E[] }) { + super(options); } } diff --git a/test/integration/bst.test.ts b/test/integration/bst.test.ts index 0a33db7..35a6061 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<{key: number; keyA: number}>(); + const objBST = new BST<{ key: number; keyA: number }>(); 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 cfd74ea..5c99c38 100644 --- a/test/unit/data-structures/binary-tree/avl-tree.test.ts +++ b/test/unit/data-structures/binary-tree/avl-tree.test.ts @@ -110,7 +110,7 @@ describe('AVL Tree Test', () => { }); describe('AVLTree APIs test', () => { - const avl = new AVLTree<{id: number; text: string}>(); + const avl = new AVLTree<{ id: number; text: string }>(); beforeEach(() => { avl.clear(); }); diff --git a/test/unit/data-structures/binary-tree/binary-index-tree.test.ts b/test/unit/data-structures/binary-tree/binary-index-tree.test.ts index dbbf71e..db4e912 100644 --- a/test/unit/data-structures/binary-tree/binary-index-tree.test.ts +++ b/test/unit/data-structures/binary-tree/binary-index-tree.test.ts @@ -283,3 +283,38 @@ function loopLowerBoundTests(bit: BinaryIndexedTree, values: number[]) { } } } + +describe('', () => { + class NumArrayDC { + private _tree: BinaryIndexedTree; + private readonly _nums: number[]; + + constructor(nums: number[]) { + this._nums = nums; + this._tree = new BinaryIndexedTree({max: nums.length + 1}); + for (let i = 0; i < nums.length; i++) { + this._tree.update(i + 1, nums[i]); + } + } + + update(index: number, val: number): void { + this._tree.update(index + 1, val - this._nums[index]); + this._nums[index] = val; + } + + sumRange(left: number, right: number): number { + return this._tree.getPrefixSum(right + 1) - this._tree.getPrefixSum(left); + } + } + + it('', () => { + const numArray = new NumArrayDC([1, 3, 5, 8, 2, 9, 4, 5, 8, 1, 3, 2]); + expect(numArray.sumRange(0, 8)).toBe(45); + expect(numArray.sumRange(0, 2)).toBe(9); + numArray.update(1, 2); + expect(numArray.sumRange(0, 2)).toBe(8); + expect(numArray.sumRange(3, 4)).toBe(10); + numArray.update(3, 2); + expect(numArray.sumRange(3, 4)).toBe(4); + }); +}); 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 97e3482..3398166 100644 --- a/test/unit/data-structures/binary-tree/binary-tree.test.ts +++ b/test/unit/data-structures/binary-tree/binary-tree.test.ts @@ -200,7 +200,7 @@ describe('BinaryTree Morris Traversal', () => { }); describe('BinaryTree APIs test', () => { - const avl = new AVLTree<{id: number; text: string}>(); + const avl = new AVLTree<{ id: number; text: string }>(); beforeEach(() => { avl.clear(); }); diff --git a/test/unit/data-structures/binary-tree/bst.test.ts b/test/unit/data-structures/binary-tree/bst.test.ts index 7f8e374..1991ad0 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<{key: number; keyA: number}>(); + const objBST = new BST<{ key: number; keyA: number }>(); 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 520c2bc..8c1921c 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<{key: number; keyA: number}>(); + const objBST = new BST<{ key: number; keyA: number }>(); 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 8de9ddd..361b90d 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<{key: number; keyA: number}>(); + const objTreeMultiset = new TreeMultiset<{ key: number; keyA: number }>(); 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 8745402..d827b91 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 }>({comparator: (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 }>({comparator: (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/heap/max-heap.test.ts b/test/unit/data-structures/heap/max-heap.test.ts index b6dd044..67ec6f8 100644 --- a/test/unit/data-structures/heap/max-heap.test.ts +++ b/test/unit/data-structures/heap/max-heap.test.ts @@ -5,7 +5,7 @@ describe('MaxHeap', () => { let maxHeap: MaxHeap; beforeEach(() => { - maxHeap = new MaxHeap(numberComparator); + maxHeap = new MaxHeap({comparator: numberComparator}); }); test('add and poll elements in descending order', () => { diff --git a/test/unit/data-structures/heap/min-heap.test.ts b/test/unit/data-structures/heap/min-heap.test.ts index be8d548..d462c9b 100644 --- a/test/unit/data-structures/heap/min-heap.test.ts +++ b/test/unit/data-structures/heap/min-heap.test.ts @@ -5,7 +5,7 @@ describe('MinHeap', () => { let minHeap: MinHeap; beforeEach(() => { - minHeap = new MinHeap(numberComparator); + minHeap = new MinHeap({comparator: numberComparator}); }); test('add and poll elements in ascending order', () => { 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 eab2ad5..0838337 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 74a3de8..092b2e6 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 f8a63bc..8b2f287 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 }>({comparator: (a, b) => b.keyA - a.keyA}); priorityQueue.refill([{keyA: 5}, {keyA: 3}, {keyA: 1}]); priorityQueue.add({keyA: 7}); @@ -53,7 +53,7 @@ describe('MaxPriorityQueue Operation Test', () => { it('should correctly heapify an array', () => { const array = [5, 3, 7, 1]; - const heap = MaxPriorityQueue.heapify(array, (a, b) => b - a); + const heap = MaxPriorityQueue.heapify({nodes: array, comparator: (a, b) => b - a}); heap.refill(array); expect(heap.poll()).toBe(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: nodes, comparator: (a, b) => b.keyA - a.keyA}); expect(maxPQ.poll()?.keyA).toBe(7); expect(maxPQ.poll()?.keyA).toBe(5); 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 29bcd87..dd4fba0 100644 --- a/test/unit/data-structures/priority-queue/priority-queue.test.ts +++ b/test/unit/data-structures/priority-queue/priority-queue.test.ts @@ -3,7 +3,7 @@ import {getRandomInt} from '../../../utils'; describe('PriorityQueue Operation Test', () => { it('should PriorityQueue poll, pee, heapify, toArray work well', function () { - const minPQ = new PriorityQueue((a, b) => a - b); + const minPQ = new PriorityQueue({comparator: (a, b) => a - b}); minPQ.refill([5, 2, 3, 4, 6, 1]); expect(minPQ.toArray()).toEqual([1, 2, 3, 4, 6, 5]); minPQ.poll(); @@ -11,13 +11,13 @@ describe('PriorityQueue Operation Test', () => { minPQ.poll(); expect(minPQ.toArray()).toEqual([4, 5, 6]); expect(minPQ.peek()).toBe(4); - expect(PriorityQueue.heapify([3, 2, 1, 5, 6, 7, 8, 9, 10], (a, b) => a - b).toArray()).toEqual([ - 1, 2, 3, 5, 6, 7, 8, 9, 10 - ]); + expect(PriorityQueue.heapify({nodes: [3, 2, 1, 5, 6, 7, 8, 9, 10], comparator: (a, b) => a - b}).toArray()).toEqual( + [1, 2, 3, 5, 6, 7, 8, 9, 10] + ); }); it('should Max PriorityQueue poll, peek, heapify, toArray work well', function () { - const maxPriorityQueue = new PriorityQueue((a, b) => b - a); + const maxPriorityQueue = new PriorityQueue({comparator: (a, b) => b - a}); maxPriorityQueue.refill([5, 2, 3, 4, 6, 1]); expect(maxPriorityQueue.toArray()).toEqual([6, 5, 3, 4, 2, 1]); maxPriorityQueue.poll(); @@ -25,13 +25,13 @@ describe('PriorityQueue Operation Test', () => { maxPriorityQueue.poll(); expect(maxPriorityQueue.toArray()).toEqual([3, 2, 1]); expect(maxPriorityQueue.peek()).toBe(3); - expect(PriorityQueue.heapify([3, 2, 1, 5, 6, 7, 8, 9, 10], (a, b) => a - b).toArray()).toEqual([ - 1, 2, 3, 5, 6, 7, 8, 9, 10 - ]); + expect(PriorityQueue.heapify({nodes: [3, 2, 1, 5, 6, 7, 8, 9, 10], comparator: (a, b) => a - b}).toArray()).toEqual( + [1, 2, 3, 5, 6, 7, 8, 9, 10] + ); }); it('should PriorityQueue clone, sort, getNodes, dfs work well', function () { - const minPQ1 = new PriorityQueue((a, b) => a - b); + const minPQ1 = new PriorityQueue({comparator: (a, b) => a - b}); minPQ1.refill([2, 5, 8, 3, 1, 6, 7, 4]); const clonedPriorityQueue = minPQ1.clone(); expect(clonedPriorityQueue.getNodes()).toEqual(minPQ1.getNodes()); @@ -45,7 +45,7 @@ describe('PriorityQueue Operation Test', () => { describe('Priority Queue Performance Test', () => { it('should numeric heap work well', function () { const values = Array.from(new Array(10000), () => getRandomInt(1, 10000000)); - const minPriorityQueue = new PriorityQueue((a, b) => a - b); + const minPriorityQueue = new PriorityQueue({comparator: (a, b) => a - b}); minPriorityQueue.refill(values); const sorted = minPriorityQueue.sort(); expect(sorted).toEqual(values.sort((a, b) => a - b)); diff --git a/test/utils/big-o.ts b/test/utils/big-o.ts index 43d7eca..cc9fc86 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)) {