Add sort method in heap.Ready to publish v1.20.0

This commit is contained in:
Revone 2023-09-01 12:43:33 +08:00
parent d93895dfff
commit 6f86f71cdd
6 changed files with 39 additions and 33 deletions

23
package-lock.json generated
View file

@ -1,12 +1,12 @@
{
"name": "data-structure-typed",
"version": "1.19.7",
"version": "1.19.9",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "data-structure-typed",
"version": "1.19.7",
"version": "1.19.9",
"license": "MIT",
"dependencies": {
"zod": "^3.22.2"
@ -2175,24 +2175,11 @@
}
},
"node_modules/data-structure-typed": {
"version": "1.19.7",
"resolved": "https://registry.npmjs.org/data-structure-typed/-/data-structure-typed-1.19.7.tgz",
"integrity": "sha512-01z8P6DDXy044GitWYxds7sQzAYS1USxVAQEl7v8gSSa4YG7nzg0ZhL/xyU3QjzFjn+XsJWCOaiNG8eMFr54HQ==",
"version": "1.19.9",
"resolved": "https://registry.npmjs.org/data-structure-typed/-/data-structure-typed-1.19.9.tgz",
"integrity": "sha512-HiAE7UEDuRWRflZuvL93juVZ7o5IARYcHfkXcif4GPFd5gqjJkFGOnu2H3zISbTBYoWo/BEBBv9ciP2dhFtJaA==",
"dev": true,
"dependencies": {
"avl-tree-typed": "^1.19.6",
"bst-typed": "^1.19.6",
"heap-typed": "^1.19.6",
"zod": "^3.22.2"
}
},
"node_modules/data-structure-typed/node_modules/bst-typed": {
"version": "1.19.45",
"resolved": "https://registry.npmjs.org/bst-typed/-/bst-typed-1.19.45.tgz",
"integrity": "sha512-x/vLIaFDdgv4zcTVgpJmTwW6BanpPPM9eE/P8v72VE9eFgBEspDGOSJsd4Fel7fO8DF+MUqwksMreCln0ECy3g==",
"dev": true,
"dependencies": {
"data-structure-typed": "^1.19.4",
"zod": "^3.22.2"
}
},

View file

@ -1,6 +1,6 @@
{
"name": "data-structure-typed",
"version": "1.19.8",
"version": "1.20.0",
"description": "Javascript & TypeScript Data Structure Library, meticulously crafted to empower developers with a versatile set of essential data structures. Our library includes a wide range of data structures, such as Binary Tree, AVL Tree, Binary Search Tree (BST), Tree Multiset, Segment Tree, Binary Indexed Tree, Graph, Directed Graph, Undirected Graph, Singly Linked List, Hash, CoordinateSet, CoordinateMap, Heap, Doubly Linked List, Priority Queue, Max Priority Queue, Min Priority Queue, Queue, ObjectDeque, ArrayDeque, Stack, and Trie. Each data structure is thoughtfully designed and implemented using TypeScript to provide efficient, reliable, and easy-to-use solutions for your programming needs. Whether you're optimizing algorithms, managing data, or enhancing performance, our TypeScript Data Structure Library is your go-to resource. Elevate your coding experience with these fundamental building blocks for software development.",
"main": "dist/index.js",
"scripts": {

View file

@ -174,6 +174,23 @@ export abstract class Heap<T = number> {
return isItem ? itemArray : itemArray.map(item => item.val);
}
sort(isItem?: undefined): (T | undefined)[];
sort(isItem: false): (T | undefined)[];
sort(isItem: true): (HeapItem<T> | null)[];
/**
* The function sorts the elements in the priority queue and returns either the sorted items or their values depending
* on the value of the isItem parameter.
* @param {boolean} [isItem] - The `isItem` parameter is a boolean flag that indicates whether the sorted result should
* be an array of `HeapItem<T>` objects or an array of the values (`T`) of those objects. If `isItem` is `true`, the
* sorted result will be an array of `HeapItem
* @returns an array of either `HeapItem<T>`, `null`, `T`, or `undefined` values.
*/
sort(isItem?: boolean): (HeapItem<T> | null | T | undefined)[] {
isItem = isItem ?? false;
const sorted = this._pq.sort();
return isItem ? sorted : sorted.map(item => item.val);
}
/**
* The clear function clears the priority queue.
*/

View file

@ -4,9 +4,10 @@ describe('Individual package BST operations test', () => {
it('should perform various operations on a Binary Search Tree with numeric values', () => {
const bst = new BST();
expect(bst).toBeInstanceOf(BST);
bst.add(11);
bst.add(3);
bst.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
bst.add(11, 11);
bst.add(3, 3);
const idsOrVals = [15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
bst.addMany(idsOrVals, idsOrVals);
expect(bst.root).toBeInstanceOf(BSTNode);
if (bst.root) expect(bst.root.id).toBe(11);
@ -193,7 +194,7 @@ describe('Individual package BST operations test', () => {
{id: 14, keyA: 14}, {id: 4, keyA: 4}, {id: 7, keyA: 7},
{id: 10, keyA: 10}, {id: 5, keyA: 5}];
objBST.addMany(values);
objBST.addMany(values.map(item => item.id), values);
expect(objBST.root).toBeInstanceOf(BSTNode);

View file

@ -34,9 +34,6 @@ describe('AVL Tree Test', () => {
// node15 has type problem. After the uniform design, the generics of containers (DirectedGraph, BST) are based on the type of value. However, this design has a drawback: when I attempt to inherit from the Vertex or BSTNode classes, the types of the results obtained by all methods are those of the parent class.
expect(node15?.val).toBe(15);
const node11 = tree.get(11);
const dfs = tree.DFS('in', 'node');
expect(dfs[0].id).toBe(1);
expect(dfs[dfs.length - 1].id).toBe(16);

View file

@ -4,16 +4,20 @@ describe('Heap Operation Test', () => {
it('should numeric heap work well', function () {
const minNumHeap = new MinHeap<number>();
minNumHeap.add(1).add(6).add(2).add(0).add(5).add(9);
expect(minNumHeap.has(1)).toBe(true);
expect(minNumHeap.has(2)).toBe(true);
expect(minNumHeap.poll()).toBe(0);
expect(minNumHeap.poll()).toBe(1);
expect(minNumHeap.peek()).toBe(2);
expect(minNumHeap.toArray().length).toBe(4);
expect(minNumHeap.toArray()[0]).toBe(2);
expect(minNumHeap.toArray()[1]).toBe(5);
expect(minNumHeap.toArray()[2]).toBe(9);
expect(minNumHeap.toArray()[3]).toBe(6);
expect(!minNumHeap.has(1));
expect(minNumHeap.has(2));
const arrFromHeap = minNumHeap.toArray();
expect(arrFromHeap.length).toBe(4);
expect(arrFromHeap[0]).toBe(2);
expect(arrFromHeap[1]).toBe(5);
expect(arrFromHeap[2]).toBe(9);
expect(arrFromHeap[3]).toBe(6);
expect(minNumHeap.sort()).toEqual([2, 5, 6, 9]);
});
it('should object heap work well', function () {