{"id":26107293,"url":"https://github.com/biggyspender/ts-bin-heap","last_synced_at":"2026-04-29T12:38:26.437Z","repository":{"id":35152870,"uuid":"213673859","full_name":"biggyspender/ts-bin-heap","owner":"biggyspender","description":"A flexible binary heap. Can be used as a priority queue.","archived":false,"fork":false,"pushed_at":"2022-12-10T05:12:05.000Z","size":1180,"stargazers_count":0,"open_issues_count":25,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-08T00:42:44.720Z","etag":null,"topics":["binary-heap","javascript","javascript-library","npm-package","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/biggyspender.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-10-08T14:56:32.000Z","updated_at":"2020-03-27T01:55:15.000Z","dependencies_parsed_at":"2023-01-15T15:00:36.696Z","dependency_job_id":null,"html_url":"https://github.com/biggyspender/ts-bin-heap","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/biggyspender%2Fts-bin-heap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/biggyspender%2Fts-bin-heap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/biggyspender%2Fts-bin-heap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/biggyspender%2Fts-bin-heap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/biggyspender","download_url":"https://codeload.github.com/biggyspender/ts-bin-heap/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242762738,"owners_count":20181267,"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":["binary-heap","javascript","javascript-library","npm-package","typescript"],"created_at":"2025-03-09T22:53:04.088Z","updated_at":"2026-04-29T12:38:21.401Z","avatar_url":"https://github.com/biggyspender.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ts-bin-heap\n\nA flexible [binary-heap](https://en.wikipedia.org/wiki/Binary_heap). This data-structure is optimized for the retrieval of either the maximum or the minimum\n\nThis package contains functions for creating binary-heaps conforming to the [`BinaryHeap\u003cT\u003e` interface](./src/interfaces/BinaryHeap.ts)\n\nThe main methods of `BinaryHeap\u003cT\u003e` are simply `push` and `pop`. You can push items into the heap, and you can pop items from it. **The item that is popped will always be either the maximum or the minimum item in the collection.**\n\n[![npm version](https://img.shields.io/npm/v/ts-bin-heap.svg?style=flat)](https://npmjs.org/package/ts-bin-heap 'View this project on npm')\n[![Build Status](https://travis-ci.org/biggyspender/ts-bin-heap.svg?branch=master)](https://travis-ci.org/biggyspender/ts-bin-heap)\n\n[Auto-generated documentation](https://biggyspender.github.io/ts-bin-heap/)\n\n## Usage\n\n```typescript\nimport { createBinaryHeap } from 'ts-bin-heap'\n```\n\n### `createBinaryHeap`\n\nThe `createBinaryHeap` function should be all you need to get started.\n\nPlay with [this example on codesandbox.io](https://codesandbox.io/s/binaryheap-8tffl?expanddevtools=1\u0026fontsize=14\u0026module=%2Fsrc%2Findex.ts).\n\nSay we have a collection of people:\n\n```typescript\nconst people: Person[] = [\n  {\n    name: 'will',\n    priority: 10\n  },\n  {\n    name: 'jack',\n    priority: 7\n  },\n  {\n    name: 'nicole',\n    priority: 8\n  },\n  {\n    name: 'poppy',\n    priority: 44\n  }\n]\n```\n\nNow we can make a binary heap to hold these people, using the `rankSelector` parameter to pass in a function that calculates the order ranking of the items that are inserted.\n\n```typescript\nconst heap: BinaryHeap\u003cPerson\u003e = createBinaryHeap\u003cPerson\u003e(person =\u003e person.priority)\npeople.forEach(p =\u003e heap.push(p))\nconst lowestPriorityUser = heap.pop() // returns {name: 'jack', priority: 7}\n```\n\nThe full definition of `createBinaryHeap` is as follows:\n\n```typescript\ncreateBinaryHeap\u003cT\u003e(\n  rankSelector: (item: T) =\u003e number,\n  type: 'min' | 'max' = 'min',\n  stable: boolean = true\n): BinaryHeap\u003cT\u003e\n```\n\n#### `rankSelector`\n\n**Required** : A method that takes an item and selects from it an order ranking.\n\n#### `type`\n\nOne of `\"min\"` or `\"max\"`. Changes the behaviour of `pop` to return either the minimum or the maximum item in the collection. Defaults to `\"min\"`\n\n#### `stable`\n\nBinary heaps are inherently [unstable](https://en.wikipedia.org/wiki/Sorting_algorithm#Stability). This means that items that are inserted with equal order ranking will be popped in an indeterminate order. It is possible to make the heap stable by tagging each entry with an \"order-of-insertion\" field, and using this as a secondary comparison when selecting the maximum/minimum item. **This option is switched on by default.** Supplying `false` here will revert to the faster, unstable version.\n\n### acknowledgements\n\nCreated using the wonderful [https://github.com/alexjoverm/typescript-library-starter](https://github.com/alexjoverm/typescript-library-starter).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbiggyspender%2Fts-bin-heap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbiggyspender%2Fts-bin-heap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbiggyspender%2Fts-bin-heap/lists"}