
Security News
Official Go SDK for MCP in Development, Stable Release Expected in August
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.
@andrewrubin/min-heap
Advanced tools
A Min Heap data structure for use in JavaScript
Install it:
npm install @andrewrubin/min-heap
Import it:
import MinHeap from "@andrewrubin/min-heap";
Use it:
const minHeap = new MinHeap();
minHeap.insert(22);
minHeap.insert(6);
minHeap.insert(-2);
minHeap.peek(); // -2
minHeap.pull(); // removes and returns -2
minHeap.isEmpty(); // false
MinHeap's default behavior is to directly compare the values of nodes (consider the usage example above, where the inserted values are integers). However, if your data takes on a different form, MinHeap allows you to pass a custom comparison callback to its constructor.
The comparison callback receives two arguments: each is a node to compare within the heap. The comparison function is expected to return true
if the second node passed is greater than the first, and false otherwise (technically "greater than" could mean anything — how you prioritize your data is up to you!).
"Nodes" in this implementation of a min heap are always objects (specifically, instances of the HeapNode
class defined in the source code) which MinHeap creates upon calling insert(value)
. You can access a node's value for comparison via node.value
.
In the example below, we need the priority of nodes to be based on their values' birth year.
import MinHeap from "@andrewrubin/min-heap";
// First, some sample data…
const john = {
firstName: "John",
lastName: "Lennon",
birthDate: {
month: 10,
day: 9,
year: 1940,
},
};
const paul = {
firstName: "Paul",
lastName: "McCartney",
birthDate: {
month: 6,
day: 18,
year: 1942,
},
};
const george = {
firstName: "George",
lastName: "Harrison",
birthDate: {
month: 2,
day: 25,
year: 1943,
},
};
// Create our custom compare function, based on our known data shape:
const comparisonFunction = (nodeA, nodeB) => {
const yearA = nodeA.value.birthDate.year;
const yearB = nodeB.value.birthDate.year;
return yearB > yearA;
};
// Instantiate the min heap, passing our compare function as the sole argument:
const minHeap = new MinHeap(comparisonFunction);
minHeap.insert(paul);
minHeap.insert(john);
minHeap.insert(george);
minHeap.peek();
// => returns the John object, as it is at the root of the min heap.
FAQs
Min Heap data structure in JavaScript
The npm package @andrewrubin/min-heap receives a total of 13 weekly downloads. As such, @andrewrubin/min-heap popularity was classified as not popular.
We found that @andrewrubin/min-heap demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.
Security News
New research reveals that LLMs often fake understanding, passing benchmarks but failing to apply concepts or stay internally consistent.
Security News
Django has updated its security policies to reject AI-generated vulnerability reports that include fabricated or unverifiable content.