Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

heap-js

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

heap-js

Efficient Binary heap (priority queue, binary tree) data structure for JavaScript / TypeScript. Includes JavaScript methods, Python's heapq module methods, and Java's PriorityQueue methods.

  • 2.1.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
854K
decreased by-37.9%
Maintainers
1
Weekly downloads
 
Created

What is heap-js?

heap-js is a JavaScript implementation of a binary heap data structure. It provides efficient methods for priority queue operations, such as inserting elements, finding the minimum or maximum element, and removing elements. This package is useful for algorithms that require priority queues, such as Dijkstra's algorithm for shortest paths, Huffman coding, and more.

What are heap-js's main functionalities?

MinHeap

This feature allows you to create a MinHeap, where the smallest element is always at the root. The code sample demonstrates how to insert elements into the MinHeap, peek at the smallest element, and remove elements in ascending order.

const { MinHeap } = require('heap-js');
const minHeap = new MinHeap();
minHeap.push(3);
minHeap.push(1);
minHeap.push(2);
console.log(minHeap.peek()); // Output: 1
console.log(minHeap.pop()); // Output: 1
console.log(minHeap.pop()); // Output: 2

MaxHeap

This feature allows you to create a MaxHeap, where the largest element is always at the root. The code sample demonstrates how to insert elements into the MaxHeap, peek at the largest element, and remove elements in descending order.

const { MaxHeap } = require('heap-js');
const maxHeap = new MaxHeap();
maxHeap.push(3);
maxHeap.push(1);
maxHeap.push(2);
console.log(maxHeap.peek()); // Output: 3
console.log(maxHeap.pop()); // Output: 3
console.log(maxHeap.pop()); // Output: 2

Custom Comparator

This feature allows you to create a heap with a custom comparator function. The code sample demonstrates how to create a MinHeap for objects with a 'priority' property, and how to insert and remove elements based on their priority.

const { MinHeap } = require('heap-js');
const customHeap = new MinHeap({
  compare: (a, b) => a.priority - b.priority
});
customHeap.push({ value: 'task1', priority: 2 });
customHeap.push({ value: 'task2', priority: 1 });
console.log(customHeap.pop().value); // Output: 'task2'

Other packages similar to heap-js

Keywords

FAQs

Package last updated on 01 Sep 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc