ts-heapq
TypeScript icon, indicating that this package has built-in type declarations

1.0.6 • Public • Published

Heapq for Typescript

TypeScript Build Status Maintainability npm version

NPM

Heap queue algorithm implementation for Typescript based on heapq.py module from CPython

Installation

npm install ts-heapq

Use

Test

Simple example

import { Heapq } from "ts-heapq";

let heap: Heapq<number> = new Heapq<number>();
heap.push(3);
heap.push(1);
heap.push(2);

heap.top(); // return 1;
heap.pop(); // return 1, heap contains [2, 3];
heap.top(); // returns 2;

Implementing max heap using custom comparator

import { Heapq } from "ts-heapq";

let maxHeap: Heapq<number> = new Heapq<number>([], comparator: (a: number, b: number) => a > b);
maxHeap.push(1);
maxHeap.push(3);
maxHeap.push(2);

maxHeap.top(); // return 3;
maxHeap.pop(); // return 3, heap contains [2, 1];
maxHeap.top(); // returns 2;

Package Sidebar

Install

npm i ts-heapq

Weekly Downloads

3

Version

1.0.6

License

MIT

Unpacked Size

26.9 kB

Total Files

24

Last publish

Collaborators

  • andrewnester