{"id":15910518,"url":"https://github.com/davidnguyen11/p-queue-ts","last_synced_at":"2025-03-22T08:30:47.408Z","repository":{"id":42882468,"uuid":"255492849","full_name":"davidnguyen11/p-queue-ts","owner":"davidnguyen11","description":"Priority Queue in Typescript \u0026 Javascript","archived":false,"fork":false,"pushed_at":"2023-05-13T12:52:04.000Z","size":471,"stargazers_count":15,"open_issues_count":12,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-13T12:39:11.344Z","etag":null,"topics":["algorithm","array-heap","binary-heap","binary-trees","data-structures","javascript","nodejs","priority-queue","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/davidnguyen11.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"ko_fi":"davidnguyen179"}},"created_at":"2020-04-14T02:41:23.000Z","updated_at":"2024-09-24T05:30:20.000Z","dependencies_parsed_at":"2024-10-28T12:45:33.220Z","dependency_job_id":"6e91ca90-bce2-4bb3-9f5a-53577f4c2ef9","html_url":"https://github.com/davidnguyen11/p-queue-ts","commit_stats":{"total_commits":32,"total_committers":3,"mean_commits":"10.666666666666666","dds":0.09375,"last_synced_commit":"1925963a85f4c5b664803f9bc0473b367736467c"},"previous_names":["davidnguyen179/p-queue-ts"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidnguyen11%2Fp-queue-ts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidnguyen11%2Fp-queue-ts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidnguyen11%2Fp-queue-ts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidnguyen11%2Fp-queue-ts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/davidnguyen11","download_url":"https://codeload.github.com/davidnguyen11/p-queue-ts/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244931355,"owners_count":20534005,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithm","array-heap","binary-heap","binary-trees","data-structures","javascript","nodejs","priority-queue","typescript"],"created_at":"2024-10-06T15:07:51.534Z","updated_at":"2025-03-22T08:30:47.110Z","avatar_url":"https://github.com/davidnguyen11.png","language":"TypeScript","funding_links":["https://ko-fi.com/davidnguyen179"],"categories":[],"sub_categories":[],"readme":"# Priority Queue\n\nA priority queue is a collection in which items can be added at any time, but the only item that can be removed is the one with the highest priority.\n\nFor more information, please check [wiki](https://en.wikipedia.org/wiki/Priority_queue).\n\n[![codecov](https://codecov.io/gh/davidnguyen179/p-queue-ts/branch/master/graph/badge.svg)](https://codecov.io/gh/davidnguyen179/p-queue-ts) [![Build Status](https://travis-ci.org/davidnguyen179/p-queue-ts.svg?branch=master)](https://travis-ci.org/davidnguyen179/p-queue-ts) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://github.com/davidnguyen179/p-queue-ts/pulls) [![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/davidnguyen179/p-queue-ts/blob/master/LICENSE)\n\n## Motivation\n\nDuring practising some challenges in `leetcode`. I found this [problem](https://leetcode.com/problems/last-stone-weight/) requires `priority queue`. So I decided to research some documentations online and try to implement by myself this lib. The result beats 97% in `leetcode`.\n\n## Installation\n\n_npm_\n\n```bash\nnpm i p-queue-ts\n```\n\n_yarn_\n\n```bash\nyarn add p-queue-ts\n```\n\n## Usage\n\nThe `priority queue` lib uses max heap as a default way to build a queue.\n\n```ts\nimport { PriorityQueue } from 'p-queue-ts';\n```\n\nor\n\n```ts\nconst { PriorityQueue } = require('p-queue-ts');\n```\n\n**Max priority queue**\n\n_with array of numbers_\n\n```ts\nconst p = new PriorityQueue();\np.push(2);\np.push(7);\np.push(4);\np.push(1);\np.push(8);\np.push(1);\n\n// The queue: [8, 7, 4, 1, 2, 1]\n```\n\n_with array of objects_\n\n```ts\nconst p = new PriorityQueue(function (a, b) {\n  return a.value \u003c b.value;\n});\n\np.push({ text: 'a', value: 2 });\np.push({ text: 'b', value: 7 });\np.push({ text: 'c', value: 4 });\np.push({ text: 'd', value: 1 });\np.push({ text: 'e', value: 8 });\np.push({ text: 'f', value: 1 });\n\n/** The queue\n[\n  { text: 'e', value: 8 },\n  { text: 'b', value: 7 },\n  { text: 'c', value: 4 },\n  { text: 'd', value: 1 },\n  { text: 'a', value: 2 },\n  { text: 'f', value: 1 },\n]\n */\n```\n\nIf you want to support min `priority queue`. The lib allows providing the customized `comparator`.\n\n**Min priority queue**\n\n_with array of numbers_\n\n```ts\nconst p = new PriorityQueue(function (a, b) {\n  return a \u003e b;\n});\n\np.push(2);\np.push(7);\np.push(4);\np.push(1);\np.push(8);\np.push(1);\n\n// The queue: [1, 2, 1, 7, 8, 4]\n```\n\n_with array of objects_\n\n```ts\nconst p = new PriorityQueue(function (a, b) {\n  return a.value \u003e b.value;\n});\n\np.push({ text: 'a', value: 2 });\np.push({ text: 'b', value: 7 });\np.push({ text: 'c', value: 4 });\np.push({ text: 'd', value: 1 });\np.push({ text: 'e', value: 8 });\np.push({ text: 'f', value: 1 });\n\n/** The queue\n[\n  { text: 'd', value: 1 },\n  { text: 'a', value: 2 },\n  { text: 'f', value: 1 },\n  { text: 'b', value: 7 },\n  { text: 'e', value: 8 },\n  { text: 'c', value: 4 }\n]\n */\n```\n\n## API\n\n### push(value: T)\n\nAdd elements to queue\n\n```ts\nconst p = new PriorityQueue();\np.push(1); // adding \"1\" to queue\np.push(2); // adding \"2\" to queue\np.push(3); // adding \"3\" to queue\n\n// The queue: [3, 1, 2]\n```\n\n### pop()\n\nExtract the largest or smallest element from the queue\n\n```ts\nconst p = new PriorityQueue();\np.push(1); // adding \"1\" to queue\np.push(2); // adding \"2\" to queue\np.push(3); // adding \"3\" to queue\n\nconst elmenet = p.pop(); // Output: 3\n```\n\nThe queue looks like this `[2, 1]`\n\n### top()\n\nPeek the element (get the largest or smallest element without removing it from queue)\n\n```ts\nconst p = new PriorityQueue();\np.push(1); // adding \"1\" to queue\np.push(2); // adding \"2\" to queue\np.push(3); // adding \"3\" to queue\n\nconst elmenet = p.pop(); // Output: 3\n\n// The queue is remained the same\n```\n\n### size()\n\nGet the size of the queue\n\n```ts\nconst p = new PriorityQueue();\np.push(1); // adding \"1\" to queue\np.push(2); // adding \"2\" to queue\np.push(3); // adding \"3\" to queue\np.push(4); // adding \"4\" to queue\n\nconst length = p.size(); // Output: 4\n```\n\n### empty()\n\nCheck whether the queue is empty or not.\n\n- true: if the queue is empty\n- false: if the queue has data\n\n### toArray()\n\nExtract queue to array\n\n```ts\nconst p = new PriorityQueue();\np.push(1); // adding \"1\" to queue\np.push(2); // adding \"2\" to queue\np.push(3); // adding \"3\" to queue\n\nconst array = p.toArray(); // Output: [3, 1, 2]\n```\n\n### clear()\n\nRemoves all of the elements from this priority queue.\n\n### contains(value: T, comparator?: (item: T) =\u003e boolean)\n\nReturns true if this queue contains the specified element.\n\n- true: if the element exists in queue\n- false: if the element does not exist in queue\n\n_with array of numbers_\n\n```ts\nconst p = new PriorityQueue();\np.push(2);\np.push(7);\np.push(4);\np.push(1);\np.push(8);\np.push(1);\n\np.contains(8); // true\np.contains(100); // false\n```\n\n_with array of objects_\n\n```ts\nconst p = new PriorityQueue(function (a, b) {\n  return a.value \u003e b.value;\n});\n\np.push({ text: 'a', value: 2 });\np.push({ text: 'b', value: 7 });\np.push({ text: 'c', value: 4 });\np.push({ text: 'd', value: 1 });\np.push({ text: 'e', value: 8 });\np.push({ text: 'f', value: 1 });\n\nfunction callback(item: any) {\n  return item.value === element.value;\n}\n\np.contains(8, callback); // true\np.contains(100, callback); // false\n```\n\n## Running time\n\nYou can check the performance of the package here: [https://jsperf.com/p-queue-ts](https://jsperf.com/p-queue-ts)\n\n| Operation | Binary heap |\n| --------- | ----------- |\n| push      | O(lg n)     |\n| top       | O(1)        |\n| pop       | O(lg n)     |\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidnguyen11%2Fp-queue-ts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdavidnguyen11%2Fp-queue-ts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidnguyen11%2Fp-queue-ts/lists"}