Need a fast and efficient heap/priority queue in your JavaScript/TypeScript projects? Look no further!
This package offers a simple, flexible, and performant implementation of the Heap data structure, ready to tackle your most demanding tasks.
Push, top and change higher priority comparator method.
const heap = new Heap<number>();
heap.push(404);
heap.push(200);
heap.push(500);
console.log(heap.top()); // 500
heap.changeHigherPriorityComparator((a, b) => a < b);
console.log(heap.top()); // 200
Default custom comparator and toSortedArray method.
const heap = new Heap<Profile>({
comparator: (a, b) => a.points > b.points
// if a.points is greater than b.points, then 'a' has higher priority
});
heap.push({ name: "Avi", points: 500 });
heap.push({ name: "Foo", points: 900 });
heap.push({ name: "Bar", points: 700 });
console.log(heap.top()); // { name: "Foo", points: 900 }
console.log(heap.toSortedArray(profile => profile.name)); // ["Foo", "Bar", "Avi"]
For better examples and details about the methods and good practices, please see the following documentation.
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'feat: add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the MIT License. See LICENSE.txt
for more information.
Aleiva - @aleiva17 - aleiva1700@gmail.com