diff --git a/CHANGELOG.md b/CHANGELOG.md index 17f7c20..ec5c230 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.45.2](https://github.com/zrwusa/data-structure-typed/compare/v1.35.0...main) (upcoming) +## [v1.45.3](https://github.com/zrwusa/data-structure-typed/compare/v1.35.0...main) (upcoming) ### Changes diff --git a/README.md b/README.md index 5458473..e49afe2 100644 --- a/README.md +++ b/README.md @@ -71,46 +71,96 @@ const { } = dataStructureTyped; ``` -#### Binary Tree +## Software Engineering Design Standards + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
PrincipleDescription
PracticalityFollows ES6 and ESNext standards, offering unified and considerate optional parameters, and simplifies method names.
ExtensibilityAdheres to OOP (Object-Oriented Programming) principles, allowing inheritance for all data structures.
ModularizationIncludes data structure modularization and independent NPM packages.
EfficiencyAll methods provide time and space complexity, comparable to native JS performance.
MaintainabilityFollows open-source community development standards, complete documentation, continuous integration, and adheres to TDD (Test-Driven Development) patterns.
TestabilityAutomated and customized unit testing, performance testing, and integration testing.
PortabilityPlans for porting to Java, Python, and C++, currently achieved to 80%.
ReusabilityFully decoupled, minimized side effects, and adheres to OOP.
SecurityCarefully designed security for member variables and methods. Data structure software does not need to consider other security aspects.
ScalabilityData structure software does not involve load issues.
+ +## Vivid Examples + +### Binary Tree [Try it out](https://vivid-algorithm.vercel.app/), or you can execute your own code using our [visual tool](https://github.com/zrwusa/vivid-algorithm) ![](https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/examples/videos/webp_output/binary-tree-array-to-binary-tree.webp) -#### Binary Tree DFS +### Binary Tree DFS [Try it out](https://vivid-algorithm.vercel.app/), or you can execute your own code using our [visual tool](https://github.com/zrwusa/vivid-algorithm) ![](https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/examples/videos/webp_output/binary-tree-dfs-in-order.webp) -#### AVL Tree +### AVL Tree [Try it out](https://vivid-algorithm.vercel.app/), or you can execute your own code using our [visual tool](https://github.com/zrwusa/vivid-algorithm) ![](https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/examples/videos/webp_output/avl-tree-test.webp) -#### Tree Multi Map +### Tree Multi Map [Try it out](https://vivid-algorithm.vercel.app/), or you can execute your own code using our [visual tool](https://github.com/zrwusa/vivid-algorithm) ![](https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/examples/videos/webp_output/tree-multiset-test.webp) -#### Matrix +### Matrix [Try it out](https://vivid-algorithm.vercel.app/algorithm/graph/), or you can execute your own code using our [visual tool](https://github.com/zrwusa/vivid-algorithm) ![](https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/examples/videos/webp_output/matrix-cut-off-tree-for-golf.webp) -#### Directed Graph +### Directed Graph [Try it out](https://vivid-algorithm.vercel.app/algorithm/graph/), or you can execute your own code using our [visual tool](https://github.com/zrwusa/vivid-algorithm) ![](https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/examples/videos/webp_output/directed-graph-test.webp) -#### Map Graph +### Map Graph [Try it out](https://vivid-algorithm.vercel.app/algorithm/graph/), or you can execute your own code using our [visual tool](https://github.com/zrwusa/vivid-algorithm) ![](https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/examples/videos/webp_output/map-graph-test.webp) @@ -568,7 +618,7 @@ Array.from(dijkstraResult?.seen ?? []).map(vertex => vertex.key) // ['A', 'B', ' -### Standard library data structure comparison +## Standard library data structure comparison @@ -753,9 +803,20 @@ Array.from(dijkstraResult?.seen ?? []).map(vertex => vertex.key) // ['A', 'B', '
-## Code design +## Benchmark -### Adhere to ES6 standard naming conventions for APIs. +[//]: # (No deletion!!! Start of Replace Section) +
+
priority-queue
+
test nametime taken (ms)executions per secsample deviation
100,000 add & pop24.0041.664.11e-4
100,000 CPT add & pop28.2335.438.86e-4
+
+ +[//]: # (No deletion!!! End of Replace Section) + + +## Codebase design + +### Adhere to ES6 and ESNext standard naming conventions for APIs. Standardize API conventions by using 'add' and 'delete' for element manipulation methods in all data structures. @@ -766,58 +827,3 @@ Opt for concise and clear method names, avoiding excessive length while ensuring By strictly adhering to object-oriented design (BinaryTree -> BST -> AVLTree -> TreeMultimap), you can seamlessly inherit the existing data structures to implement the customized ones you need. Object-oriented design stands as the optimal approach to data structure design. - -## Benchmark - -[//]: # (No deletion!!! Start of Replace Section) -
-
comparation
-
test nametime taken (ms)executions per secsample deviation
SRC 10,000 add0.591701.783.28e-5
CJS 10,000 add0.611648.706.98e-5
MJS 10,000 add0.591691.862.44e-5
SRC PQ 10,000 add & pop4.97201.191.37e-4
CJS PQ 10,000 add & pop4.93202.705.60e-5
MJS PQ 10,000 add & pop4.98200.744.39e-4
-
-
avl-tree
-
test nametime taken (ms)executions per secsample deviation
10,000 add randomly32.2730.990.00
10,000 add & delete randomly73.6713.570.00
10,000 addMany41.8123.920.00
10,000 get29.2134.240.00
-
-
binary-tree
-
test nametime taken (ms)executions per secsample deviation
1,000 add randomly13.8272.380.00
1,000 add & delete randomly16.0162.455.80e-4
1,000 addMany12.3081.330.01
1,000 get19.7550.630.01
1,000 dfs157.126.360.00
1,000 bfs56.7217.634.27e-4
1,000 morris334.972.990.03
-
-
bst
-
test nametime taken (ms)executions per secsample deviation
10,000 add randomly28.3035.340.00
10,000 add & delete randomly67.4714.820.00
10,000 addMany29.2534.180.00
10,000 get30.5332.750.01
-
-
rb-tree
-
test nametime taken (ms)executions per secsample deviation
100,000 add96.6710.340.01
100,000 add & delete randomly224.854.450.01
100,000 getNode40.8324.492.73e-4
-
-
directed-graph
-
test nametime taken (ms)executions per secsample deviation
1,000 addVertex0.119364.631.25e-5
1,000 addEdge6.18161.781.26e-4
1,000 getVertex0.052.12e+44.19e-6
1,000 getEdge25.9838.500.01
tarjan240.234.160.02
tarjan all227.364.400.00
topologicalSort185.855.380.00
-
-
hash-map
-
test nametime taken (ms)executions per secsample deviation
10,000 set1.001001.311.82e-5
10,000 set & get1.54650.144.87e-5
-
-
heap
-
test nametime taken (ms)executions per secsample deviation
10,000 add & pop6.97143.495.27e-4
10,000 fib add & pop378.402.640.05
-
-
doubly-linked-list
-
test nametime taken (ms)executions per secsample deviation
1,000,000 unshift224.554.450.07
1,000,000 unshift & shift182.175.490.06
1,000,000 insertBefore342.632.920.08
-
-
singly-linked-list
-
test nametime taken (ms)executions per secsample deviation
10,000 push & pop222.424.500.01
10,000 insertBefore248.394.030.00
-
-
max-priority-queue
-
test nametime taken (ms)executions per secsample deviation
10,000 refill & poll11.8384.551.45e-4
-
-
priority-queue
-
test nametime taken (ms)executions per secsample deviation
10,000 add & pop10.7992.722.01e-4
-
-
deque
-
test nametime taken (ms)executions per secsample deviation
1,000,000 push231.334.320.06
1,000,000 shift27.6436.170.01
-
-
queue
-
test nametime taken (ms)executions per secsample deviation
1,000,000 push46.1621.660.01
1,000,000 push & shift82.1812.170.00
-
-
stack
-
test nametime taken (ms)executions per secsample deviation
1,000,000 push45.4522.000.01
1,000,000 push & pop50.1019.960.01
-
-
trie
-
test nametime taken (ms)executions per secsample deviation
100,000 push46.2521.620.00
100,000 getWords65.1715.340.00
-
- -[//]: # (No deletion!!! End of Replace Section) \ No newline at end of file diff --git a/package.json b/package.json index d49f2b0..3a2c32e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "data-structure-typed", - "version": "1.45.2", + "version": "1.45.3", "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", diff --git a/src/data-structures/heap/heap.ts b/src/data-structures/heap/heap.ts index b7b8287..6978fda 100644 --- a/src/data-structures/heap/heap.ts +++ b/src/data-structures/heap/heap.ts @@ -8,18 +8,18 @@ import type { Comparator, DFSOrderPattern } from '../../types'; export class Heap { - constructor(options: { comparator: Comparator; nodes?: E[] }) { + constructor(options: { comparator: Comparator; elements?: E[] }) { this._comparator = options.comparator; - if (options.nodes && options.nodes.length > 0) { - this._nodes = options.nodes; + if (options.elements && options.elements.length > 0) { + this._elements = options.elements; this.fix(); } } - protected _nodes: E[] = []; + protected _elements: E[] = []; - get nodes(): E[] { - return this._nodes; + get elements(): E[] { + return this._elements; } protected _comparator: Comparator; @@ -32,7 +32,7 @@ export class Heap { * Get the size (number of elements) of the heap. */ get size(): number { - return this.nodes.length; + return this.elements.length; } /** @@ -40,25 +40,25 @@ export class Heap { * @returns The last element or undefined if the heap is empty. */ get leaf(): E | undefined { - return this.nodes[this.size - 1] ?? undefined; + return this.elements[this.size - 1] ?? undefined; } /** - * Static method that creates a binary heap from an array of nodes and a comparison function. + * Static method that creates a binary heap from an array of elements and a comparison function. * @returns A new Heap instance. * @param options */ - static heapify(options: { nodes: E[]; comparator: Comparator }): Heap { + static heapify(options: { elements: E[]; comparator: Comparator }): Heap { return new Heap(options); } /** - * Time Complexity: O(log n), where n is the number of nodes in the heap. + * Time Complexity: O(log n), where n is the number of elements in the heap. * Space Complexity: O(1) */ /** - * Time Complexity: O(log n), where n is the number of nodes in the heap. + * Time Complexity: O(log n), where n is the number of elements in the heap. * Space Complexity: O(1) * * Insert an element into the heap and maintain the heap properties. @@ -69,56 +69,53 @@ export class Heap { } /** - * Time Complexity: O(log n), where n is the number of nodes in the heap. + * Time Complexity: O(log n), where n is the number of elements in the heap. * Space Complexity: O(1) */ /** - * Time Complexity: O(log n), where n is the number of nodes in the heap. + * Time Complexity: O(log n), where n is the number of elements in the heap. * Space Complexity: O(1) * * Insert an element into the heap and maintain the heap properties. * @param element - The element to be inserted. */ push(element: E): Heap { - this.nodes.push(element); - this.bubbleUp(this.nodes.length - 1); + this._elements.push(element); + this._bubbleUp(this.elements.length - 1); return this; } /** - * Time Complexity: O(log n), where n is the number of nodes in the heap. + * Time Complexity: O(log n), where n is the number of elements in the heap. * Space Complexity: O(1) */ /** - * Time Complexity: O(log n), where n is the number of nodes in the heap. + * Time Complexity: O(log n), where n is the number of elements in the heap. * Space Complexity: O(1) * * Remove and return the top element (smallest or largest element) from the heap. * @returns The top element or undefined if the heap is empty. */ poll(): E | undefined { - if (this.nodes.length === 0) { - return undefined; + if (this.elements.length === 0) return; + const value = this.elements[0]; + const last = this.elements.pop()!; + if (this.elements.length) { + this.elements[0] = last; + this._sinkDown(0, this.elements.length >> 1); } - if (this.nodes.length === 1) { - return this.nodes.pop() as E; - } - - const topValue = this.nodes[0]; - this.nodes[0] = this.nodes.pop() as E; - this.sinkDown(0); - return topValue; + return value; } /** - * Time Complexity: O(log n), where n is the number of nodes in the heap. + * Time Complexity: O(log n), where n is the number of elements in the heap. * Space Complexity: O(1) */ /** - * Time Complexity: O(log n), where n is the number of nodes in the heap. + * Time Complexity: O(log n), where n is the number of elements in the heap. * Space Complexity: O(1) * * Remove and return the top element (smallest or largest element) from the heap. @@ -133,10 +130,7 @@ export class Heap { * @returns The top element or undefined if the heap is empty. */ peek(): E | undefined { - if (this.nodes.length === 0) { - return undefined; - } - return this.nodes[0]; + return this.elements[0]; } /** @@ -148,36 +142,36 @@ export class Heap { } /** - * Reset the nodes of the heap. Make the nodes empty. + * Reset the elements of the heap. Make the elements empty. */ clear() { - this._nodes = []; + this._elements = []; } /** - * Time Complexity: O(n), where n is the number of elements in the nodes array. + * Time Complexity: O(n), where n is the number of elements in the elements array. * Space Complexity: O(n) */ /** - * Time Complexity: O(n), where n is the number of elements in the nodes array. + * Time Complexity: O(n), where n is the number of elements in the elements array. * Space Complexity: O(n) * - * Clear and add nodes of the heap - * @param nodes + * Clear and add elements of the heap + * @param elements */ - refill(nodes: E[]) { - this._nodes = nodes; + refill(elements: E[]) { + this._elements = elements; this.fix(); } /** - * Time Complexity: O(n), where n is the number of nodes in the heap. + * Time Complexity: O(n), where n is the number of elements in the heap. * Space Complexity: O(1) */ /** - * Time Complexity: O(n), where n is the number of nodes in the heap. + * Time Complexity: O(n), where n is the number of elements in the heap. * Space Complexity: O(1) * * Use a comparison function to check whether a binary heap contains a specific element. @@ -185,16 +179,47 @@ export class Heap { * @returns Returns true if the specified element is contained; otherwise, returns false. */ has(element: E): boolean { - return this.nodes.includes(element); + return this.elements.includes(element); } /** - * Time Complexity: O(n), where n is the number of nodes in the heap. + * Time Complexity: O(n). The worst-case O(n), where n is the number of elements in the heap. This is because, in the worst case, the element to be deleted is located at the end of the heap (not the root), and after deletion, we may need to reorganize the elements by performing a sinkDown operation. + * Space Complexity: O(1) + */ + + /** + * Time Complexity: O(n). The worst-case O(n), where n is the number of elements in the heap. This is because, in the worst case, the element to be deleted is located at the end of the heap (not the root), and after deletion, we may need to reorganize the elements by performing a sinkDown operation. + * Space Complexity: O(1) + * + * The `delete` function removes an element from an array-like data structure, maintaining the order + * and structure of the remaining elements. + * @param {E} element - The `element` parameter represents the element that you want to delete from + * the array `this.elements`. + * @returns The `delete` function is returning a boolean value. It returns `true` if the element was + * successfully deleted from the array, and `false` if the element was not found in the array. + */ + delete(element: E) { + const index = this.elements.indexOf(element); + if (index < 0) return false; + if (index === 0) { + this.pop(); + } else if (index === this.elements.length - 1) { + this.elements.pop(); + } else { + this.elements.splice(index, 1, this.elements.pop()!); + this._bubbleUp(index); + this._sinkDown(index, this.elements.length >> 1); + } + return true; + } + + /** + * Time Complexity: O(n), where n is the number of elements in the heap. * Space Complexity: O(h), where h is the height of the heap. */ /** - * Time Complexity: O(n), where n is the number of nodes in the heap. + * Time Complexity: O(n), where n is the number of elements in the heap. * Space Complexity: O(h), where h is the height of the heap. * * Depth-first search (DFS) method, different traversal orders can be selected。 @@ -209,16 +234,16 @@ export class Heap { if (index < this.size) { if (order === 'in') { dfsHelper(2 * index + 1); - result.push(this.nodes[index]); + result.push(this.elements[index]); dfsHelper(2 * index + 2); } else if (order === 'pre') { - result.push(this.nodes[index]); + result.push(this.elements[index]); dfsHelper(2 * index + 1); dfsHelper(2 * index + 2); } else if (order === 'post') { dfsHelper(2 * index + 1); dfsHelper(2 * index + 2); - result.push(this.nodes[index]); + result.push(this.elements[index]); } } }; @@ -241,15 +266,7 @@ export class Heap { * @returns An array containing the elements of the heap. */ toArray(): E[] { - return [...this.nodes]; - } - - /** - * Time Complexity: O(1) - * Space Complexity: O(1) - */ - getNodes(): E[] { - return this.nodes; + return [...this.elements]; } /** @@ -266,7 +283,7 @@ export class Heap { */ clone(): Heap { const clonedHeap = new Heap({ comparator: this.comparator }); - clonedHeap._nodes = [...this.nodes]; + clonedHeap._elements = [...this.elements]; return clonedHeap; } @@ -292,6 +309,21 @@ export class Heap { return visitedNode; } + /** + * Time Complexity: O(log n) + * Space Complexity: O(1) + */ + + /** + * Time Complexity: O(n) + * Space Complexity: O(1) + * + * Fix the entire heap to maintain heap properties. + */ + fix() { + for (let i = Math.floor(this.size / 2); i >= 0; i--) this._sinkDown(i, this.elements.length >> 1); + } + /** * Time Complexity: O(log n) * Space Complexity: O(1) @@ -304,33 +336,20 @@ export class Heap { * Float operation to maintain heap properties after adding an element. * @param index - The index of the newly added element. */ - protected bubbleUp(index: number): void { - // const element = this.nodes[index]; - // while (index > 0) { - // const parentIndex = (index - 1) >> 1; - // const parent = this.nodes[parentIndex]; - // if (this.comparator(element, parent) < 0) { - // this.nodes[index] = parent; - // this.nodes[parentIndex] = element; - // index = parentIndex; - // } else { - // break; - // } - // } - - const item = this.nodes[index]; + protected _bubbleUp(index: number) { + const element = this.elements[index]; while (index > 0) { const parent = (index - 1) >> 1; - const parentItem = this.nodes[parent]; - if (this.comparator(parentItem, item) <= 0) break; - this.nodes[index] = parentItem; + const parentItem = this.elements[parent]; + if (this._comparator(parentItem, element) <= 0) break; + this.elements[index] = parentItem; index = parent; } - this.nodes[index] = item; + this.elements[index] = element; } /** - * Time Complexity: O(log n) + * Time Complexity: O(n) * Space Complexity: O(1) */ @@ -340,41 +359,26 @@ export class Heap { * * Sinking operation to maintain heap properties after removing the top element. * @param index - The index from which to start sinking. + * @param halfLength */ - protected sinkDown(index: number): void { - const leftChildIndex = index << 1 | 1; - const rightChildIndex = leftChildIndex + 1; - const length = this.nodes.length; - let targetIndex = index; - - if (leftChildIndex < length && this.comparator(this.nodes[leftChildIndex], this.nodes[targetIndex]) < 0) { - targetIndex = leftChildIndex; + protected _sinkDown(index: number, halfLength: number) { + const element = this.elements[index]; + while (index < halfLength) { + let left = index << 1 | 1; + const right = left + 1; + let minItem = this.elements[left]; + if ( + right < this.elements.length && + this._comparator(minItem, this.elements[right]) > 0 + ) { + left = right; + minItem = this.elements[right]; + } + if (this._comparator(minItem, element) >= 0) break; + this.elements[index] = minItem; + index = left; } - if (rightChildIndex < length && this.comparator(this.nodes[rightChildIndex], this.nodes[targetIndex]) < 0) { - targetIndex = rightChildIndex; - } - - if (targetIndex !== index) { - const temp = this.nodes[index]; - this.nodes[index] = this.nodes[targetIndex]; - this.nodes[targetIndex] = temp; - this.sinkDown(targetIndex); - } - } - - /** - * Time Complexity: O(n) - * Space Complexity: O(1) - */ - - /** - * Time Complexity: O(n) - * Space Complexity: O(1) - * - * Fix the entire heap to maintain heap properties. - */ - protected fix() { - for (let i = Math.floor(this.size / 2); i >= 0; i--) this.sinkDown(i); + this.elements[index] = element; } } @@ -500,22 +504,22 @@ export class FibonacciHeap { } /** - * Time Complexity: O(n), where n is the number of nodes in the linked list. + * Time Complexity: O(n), where n is the number of elements in the linked list. * Space Complexity: O(1) */ /** - * Time Complexity: O(n), where n is the number of nodes in the linked list. + * Time Complexity: O(n), where n is the number of elements in the linked list. * Space Complexity: O(1) * * Get the size (number of elements) of the heap. * @param {FibonacciHeapNode} head - The head of the linked list. * @protected - * @returns FibonacciHeapNode[] - An array containing the nodes of the linked list. + * @returns FibonacciHeapNode[] - An array containing the elements of the linked list. */ consumeLinkedList(head?: FibonacciHeapNode): FibonacciHeapNode[] { - const nodes: FibonacciHeapNode[] = []; - if (!head) return nodes; + const elements: FibonacciHeapNode[] = []; + if (!head) return elements; let node: FibonacciHeapNode | undefined = head; let flag = false; @@ -525,12 +529,12 @@ export class FibonacciHeap { else if (node === head) flag = true; if (node) { - nodes.push(node); + elements.push(node); node = node.right; } } - return nodes; + return elements; } /** @@ -552,12 +556,12 @@ export class FibonacciHeap { } /** - * Time Complexity: O(log n), where n is the number of nodes in the heap. + * Time Complexity: O(log n), where n is the number of elements in the heap. * Space Complexity: O(1) */ /** - * Time Complexity: O(log n), where n is the number of nodes in the heap. + * Time Complexity: O(log n), where n is the number of elements in the heap. * Space Complexity: O(1) * * Remove and return the top element (smallest or largest element) from the heap. @@ -568,12 +572,12 @@ export class FibonacciHeap { } /** - * Time Complexity: O(log n), where n is the number of nodes in the heap. + * Time Complexity: O(log n), where n is the number of elements in the heap. * Space Complexity: O(1) */ /** - * Time Complexity: O(log n), where n is the number of nodes in the heap. + * Time Complexity: O(log n), where n is the number of elements in the heap. * Space Complexity: O(1) * * Remove and return the top element (smallest or largest element) from the heap. @@ -584,8 +588,8 @@ export class FibonacciHeap { const z = this.min!; if (z.child) { - const nodes = this.consumeLinkedList(z.child); - for (const node of nodes) { + const elements = this.consumeLinkedList(z.child); + for (const node of elements) { this.mergeWithRoot(node); node.parent = undefined; } @@ -737,12 +741,12 @@ export class FibonacciHeap { } /** - * Time Complexity: O(n log n), where n is the number of nodes in the heap. + * Time Complexity: O(n log n), where n is the number of elements in the heap. * Space Complexity: O(n) */ /** - * Time Complexity: O(n log n), where n is the number of nodes in the heap. + * Time Complexity: O(n log n), where n is the number of elements in the heap. * Space Complexity: O(n) * * Remove and return the top element (smallest or largest element) from the heap. @@ -750,13 +754,13 @@ export class FibonacciHeap { */ protected consolidate(): void { const A: (FibonacciHeapNode | undefined)[] = new Array(this.size); - const nodes = this.consumeLinkedList(this.root); + const elements = this.consumeLinkedList(this.root); let x: FibonacciHeapNode | undefined, y: FibonacciHeapNode | undefined, d: number, t: FibonacciHeapNode | undefined; - for (const node of nodes) { + for (const node of elements) { x = node; d = x.degree; diff --git a/test/performance/data-structures/comparation.test.ts b/test/performance/data-structures/comparison.test.ts similarity index 100% rename from test/performance/data-structures/comparation.test.ts rename to test/performance/data-structures/comparison.test.ts diff --git a/test/performance/data-structures/priority-queue/priority-queue.test.ts b/test/performance/data-structures/priority-queue/priority-queue.test.ts index 84c58b5..d6f8d63 100644 --- a/test/performance/data-structures/priority-queue/priority-queue.test.ts +++ b/test/performance/data-structures/priority-queue/priority-queue.test.ts @@ -5,28 +5,28 @@ import { magnitude } from '../../../utils'; import { isCompetitor } from '../../../config'; const suite = new Benchmark.Suite(); -const { TEN_THOUSAND } = magnitude; +const { HUNDRED_THOUSAND } = magnitude; -suite.add(`${TEN_THOUSAND.toLocaleString()} add & pop`, () => { +suite.add(`${HUNDRED_THOUSAND.toLocaleString()} add & pop`, () => { const pq = new PriorityQueue({ comparator: (a, b) => b - a }); - for (let i = 0; i < TEN_THOUSAND; i++) { + for (let i = 0; i < HUNDRED_THOUSAND; i++) { pq.add(i); } - for (let i = 0; i < TEN_THOUSAND; i++) { + for (let i = 0; i < HUNDRED_THOUSAND; i++) { pq.pop(); } }); if (isCompetitor) { - suite.add(`${TEN_THOUSAND.toLocaleString()} CPT add & pop`, () => { + suite.add(`${HUNDRED_THOUSAND.toLocaleString()} CPT add & pop`, () => { const pq = new CPriorityQueue(); - for (let i = 0; i < TEN_THOUSAND; i++) { + for (let i = 0; i < HUNDRED_THOUSAND; i++) { pq.push(i); } - for (let i = 0; i < TEN_THOUSAND; i++) { + for (let i = 0; i < HUNDRED_THOUSAND; i++) { pq.pop(); } }); diff --git a/test/unit/data-structures/heap/heap.test.ts b/test/unit/data-structures/heap/heap.test.ts index 081d4e0..d5f0499 100644 --- a/test/unit/data-structures/heap/heap.test.ts +++ b/test/unit/data-structures/heap/heap.test.ts @@ -1,6 +1,7 @@ import { FibonacciHeap, MaxHeap, MinHeap } from '../../../../src'; import { logBigOMetricsWrap } from '../../../utils'; + describe('Heap Operation Test', () => { it('should numeric heap work well', function () { const minNumHeap = new MinHeap(); @@ -262,7 +263,7 @@ describe('FibonacciHeap Stress Test', () => { // const minHeap = new MinHeap(); // const cHeap = new CHeap(); // const cPQ = new PriorityQueue(undefined, (a, b) => a - b); -// const n = 10000; +// const n = 100000; // // it('should add performance well', () => { // const heapCost = calcRunTime(() => { 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 d9ee64e..9e5f7ec 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 @@ -52,7 +52,7 @@ describe('MaxPriorityQueue Operation Test', () => { it('should correctly heapify an array', () => { const array = [5, 3, 7, 1]; - const heap = MaxPriorityQueue.heapify({ nodes: array, comparator: (a, b) => b - a }); + const heap = MaxPriorityQueue.heapify({ elements: array, comparator: (a, b) => b - a }); heap.refill(array); expect(heap.poll()).toBe(7); @@ -62,8 +62,8 @@ 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: nodes, comparator: (a, b) => b.keyA - a.keyA }); + const elements = [{ keyA: 5 }, { keyA: 3 }, { keyA: 7 }, { keyA: 1 }]; + const maxPQ = MaxPriorityQueue.heapify<{ keyA: number }>({ elements, 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 c4ea32d..1b9aed4 100644 --- a/test/unit/data-structures/priority-queue/priority-queue.test.ts +++ b/test/unit/data-structures/priority-queue/priority-queue.test.ts @@ -15,7 +15,7 @@ describe('PriorityQueue Operation Test', () => { expect(minPQ.peek()).toBe(4); expect( PriorityQueue.heapify({ - nodes: [3, 2, 1, 5, 6, 7, 8, 9, 10], + elements: [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]); @@ -32,7 +32,7 @@ describe('PriorityQueue Operation Test', () => { expect(maxPriorityQueue.peek()).toBe(3); expect( PriorityQueue.heapify({ - nodes: [3, 2, 1, 5, 6, 7, 8, 9, 10], + elements: [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]); @@ -42,7 +42,7 @@ describe('PriorityQueue Operation Test', () => { 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()); + expect(clonedPriorityQueue.elements).toEqual(minPQ1.elements); 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]);