This project is part of the @thi.ng/umbrella monorepo.
Various heap implementations for arbitrary values and with customizable ordering.
Type agnostic binary heap & d-ary heap implementations with customizable
ordering and fanout / tree arity (in case of DHeap
). Both Heap
and
DHeap
have an identical base API, however the former provides several
additional operations.
STABLE - used in production
Search or submit any issues for this package
yarn add @thi.ng/heaps
// ES module
<script type="module" src="https://unpkg.com/@thi.ng/heaps?module" crossorigin></script>
// UMD
<script src="https://unpkg.com/@thi.ng/heaps/lib/index.umd.js" crossorigin></script>
Package sizes (gzipped, pre-treeshake): ESM: 1.53 KB / CJS: 1.59 KB / UMD: 1.69 KB
import { Heap, DHeap } from "@thi.ng/heaps";
// with initial values, custom comparator and heap arity
const h = new DHeap<number>(
[5, 2, 10, 15, 18, 23, 22, -1],
{
// custom comparator
compare: (a, b) => b - a,
// branch size (DHeap only)
d: 4
}
);
h.pop();
// 23
h.pop();
// 22
// insert new value unless it's a new root
// else pop and return current root
h.pushPop(16)
// 18
h.push(24);
Karsten Schmidt
If this project contributes to an academic publication, please cite it as:
@misc{thing-heaps,
title = "@thi.ng/heaps",
author = "Karsten Schmidt",
note = "https://thi.ng/heaps",
year = 2017
}
© 2017 - 2021 Karsten Schmidt // Apache Software License 2.0