[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.
This commit is contained in:
Revone 2023-10-28 17:37:51 +08:00
parent c11cc4527b
commit 6c45f53f72
26 changed files with 163 additions and 100 deletions

View file

@ -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

50
package-lock.json generated
View file

@ -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": {

View file

@ -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",

View file

@ -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.

View file

@ -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);

View file

@ -7,12 +7,16 @@
import type {Comparator, DFSOrderPattern} from '../../types';
export class Heap<E> {
export class Heap<E = any> {
protected nodes: E[] = [];
protected readonly comparator: Comparator<E>;
constructor(comparator: Comparator<E>) {
this.comparator = comparator;
constructor(options: { comparator: Comparator<E>; 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<E> {
/**
* 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<E>(nodes: E[], comparator: Comparator<E>): Heap<E> {
const binaryHeap = new Heap<E>(comparator);
binaryHeap.nodes = [...nodes];
binaryHeap.fix(); // Fix heap properties
return binaryHeap;
static heapify<E>(options: { nodes: E[]; comparator: Comparator<E> }): Heap<E> {
return new Heap<E>(options);
}
/**
@ -180,7 +180,7 @@ export class Heap<E> {
* @returns A new Heap instance containing the same elements.
*/
clone(): Heap<E> {
const clonedHeap = new Heap<E>(this.comparator);
const clonedHeap = new Heap<E>({comparator: this.comparator});
clonedHeap.nodes = [...this.nodes];
return clonedHeap;
}
@ -269,7 +269,7 @@ export class FibonacciHeapNode<E> {
export class FibonacciHeap<E> {
root?: FibonacciHeapNode<E>;
size: number = 0;
size = 0;
protected min?: FibonacciHeapNode<E>;
protected readonly comparator: Comparator<E>;

View file

@ -11,14 +11,16 @@ import type {Comparator} from '../../types';
export class MaxHeap<E = any> extends Heap<E> {
constructor(
comparator: Comparator<E> = (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<E>; 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);
}
}

View file

@ -11,14 +11,16 @@ import type {Comparator} from '../../types';
export class MinHeap<E = any> extends Heap<E> {
constructor(
comparator: Comparator<E> = (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<E>; 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);
}
}

View file

@ -10,14 +10,16 @@ import type {Comparator} from '../../types';
export class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
constructor(
compare: Comparator<E> = (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<E>; 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);
}
}

View file

@ -10,14 +10,16 @@ import type {Comparator} from '../../types';
export class MinPriorityQueue<E = any> extends PriorityQueue<E> {
constructor(
compare: Comparator<E> = (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<E>; 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);
}
}

View file

@ -9,8 +9,8 @@
import {Heap} from '../heap';
import {Comparator} from '../../types';
export class PriorityQueue<E> extends Heap<E> {
constructor(comparator: Comparator<E>) {
super(comparator);
export class PriorityQueue<E = any> extends Heap<E> {
constructor(options: { comparator: Comparator<E>; nodes?: E[] }) {
super(options);
}
}

View file

@ -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});

View file

@ -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();
});

View file

@ -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);
});
});

View file

@ -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();
});

View file

@ -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);

View file

@ -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});

View file

@ -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});

View file

@ -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'});

View file

@ -5,7 +5,7 @@ describe('MaxHeap', () => {
let maxHeap: MaxHeap<number>;
beforeEach(() => {
maxHeap = new MaxHeap(numberComparator);
maxHeap = new MaxHeap({comparator: numberComparator});
});
test('add and poll elements in descending order', () => {

View file

@ -5,7 +5,7 @@ describe('MinHeap', () => {
let minHeap: MinHeap<number>;
beforeEach(() => {
minHeap = new MinHeap(numberComparator);
minHeap = new MinHeap({comparator: numberComparator});
});
test('add and poll elements in ascending order', () => {

View file

@ -3,7 +3,7 @@ import {bigO, magnitude} from '../../../utils';
describe('DoublyLinkedList Operation Test', () => {
let list: DoublyLinkedList<number>;
let objectList: DoublyLinkedList<{keyA: number}>;
let objectList: DoublyLinkedList<{ keyA: number }>;
beforeEach(() => {
list = new DoublyLinkedList();

View file

@ -3,10 +3,10 @@ import {bigO, magnitude} from '../../../utils';
describe('SinglyLinkedList Operation Test', () => {
let list: SinglyLinkedList<number>;
let objectList: SinglyLinkedList<{keyA: number}>;
let objectList: SinglyLinkedList<{ keyA: number }>;
beforeEach(() => {
list = new SinglyLinkedList<number>();
objectList = new SinglyLinkedList<{keyA: number}>();
objectList = new SinglyLinkedList<{ keyA: number }>();
});
describe('push', () => {

View file

@ -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<number>(array, (a, b) => b - a);
const heap = MaxPriorityQueue.heapify<number>({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);

View file

@ -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<number>((a, b) => a - b);
const minPQ = new PriorityQueue<number>({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<number>((a, b) => b - a);
const maxPriorityQueue = new PriorityQueue<number>({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<number>((a, b) => a - b);
const minPQ1 = new PriorityQueue<number>({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<number>((a, b) => a - b);
const minPriorityQueue = new PriorityQueue<number>({comparator: (a, b) => a - b});
minPriorityQueue.refill(values);
const sorted = minPriorityQueue.sort();
expect(sorted).toEqual(values.sort((a, b) => a - b));

View file

@ -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)) {