Addressable Binary Heaps
A versatile TypeScript library for addressable binary heaps, delivering optimized and scalable min-heap and max-heap implementations, seamlessly supporting both object-oriented and functional paradigms.
Key Features
-
🗃️ Addressable Heaps: Implements min-heap and max-heap structures with an addressable architecture, allowing direct access to elements for modifications and extensions.
-
🚀 High Performance: Utilizes an array-based implementation for fast and efficient heap operations, ensuring optimal time complexity for insertions, deletions, and updates.
-
🛠️ Versatile API: Provides both class-based (MaxHeap
, MinHeap
) and functional (maxHeap
, minHeap
) APIs, catering to different programming styles.
-
🧩 Comprehensive Operations: Supports all essential heap functions (add
, remove
, peek
, pop
, clear
) along with efficient key modification methods (increase
, decrease
).
Table of Contents
System Requirements
Node.js | ≥ 18.0.0 |
npm | ≥ 8.0.0 |
Installation
Install via npm
npm install addressable-binary-heaps
Install via yarn
yarn add addressable-binary-heaps
Install via pnpm
pnpm install addressable-binary-heaps
Getting Started
Here's a quick guide to help you get started with the library.
Using Class-Based Implementation
import { MaxHeap } from 'addressable-binary-heaps';
const element_1 = { key: 4 };
const element_2 = { key: 2 };
const element_3 = { key: 6 };
const initialElements = [element_1, element_2];
const heap = new MaxHeap(initialElements);
console.log(heap.size);
console.log(heap.peek());
for (let elem of heap) {
console.log(elem);
}
heap.add(element_3);
console.log(heap.size);
console.log(heap.peek());
heap.forEach((elem) => {
console.log(elem);
});
console.log(heap.increase(element_2, 5));
for (const entry of heap.entries()) {
console.log(entry);
}
console.log(heap.decrease(element_2, 10));
for (const key of heap.keys()) {
console.log(key);
}
console.log(heap.pop());
console.log(heap.size);
console.log(heap.peek());
console.log(heap.remove(element_1));
console.log(heap.size);
console.log(heap.peek());
heap.clear();
console.log(heap.size);
console.log(heap.peek());
Using Functional Implementation
import { maxHeap } from 'addressable-binary-heaps';
const element_1 = { key: 4 };
const element_2 = { key: 2 };
const element_3 = { key: 6 };
const initialElements = [element_1, element_2];
const heap = maxHeap.create(initialElements);
console.log(heap.length);
console.log(maxHeap.peek(heap));
for (let elem of heap) {
console.log(elem);
}
maxHeap.add(heap, element_3);
console.log(heap.length);
console.log(maxHeap.peek(heap));
heap.forEach((elem) => {
console.log(elem);
});
console.log(maxHeap.increase(heap, element_2, 5));
for (const entry of maxHeap.entries(heap)) {
console.log(entry);
}
console.log(maxHeap.decrease(heap, element_2, 10));
for (const key of maxHeap.keys(heap)) {
console.log(key);
}
console.log(maxHeap.pop(heap));
console.log(heap.length);
console.log(maxHeap.peek(heap));
console.log(maxHeap.remove(heap, element_1));
console.log(heap.length);
console.log(maxHeap.peek(heap));
maxHeap.clear(heap);
console.log(heap.length);
console.log(maxHeap.peek(heap));
Importing Modules
The library offers flexible import options to suit different development needs. You can import everything at once, or select individual functions as needed.
Importing the Entire Package
To access all classes, interfaces, and functional APIs:
import {
MaxHeap,
MinHeap,
AbstractHeap,
IHeapArray,
IHeapNode,
maxHeap,
minHeap,
} from 'addressable-binary-heaps';
This approach is convenient when you need a broad range of functionalities from the library.
Importing Individual Heap Functions
For maximum control and minimal footprint, import individual functions or operations.
For Max Heap:
import {
add,
clear,
create,
decrease,
entries,
increase,
keys,
peek,
pop,
remove,
size,
} from 'addressable-binary-heaps/max-heap';
For Min Heap:
import {
add,
clear,
create,
decrease,
entries,
increase,
keys,
peek,
pop,
remove,
size,
} from 'addressable-binary-heaps/min-heap';
This method helps keep your bundle size small by only including necessary modules.
Code documentation
The complete API reference of the library is available at the code documentation site.
Issues and Support
If you encounter any issues or have questions, please open an issue.
License
This project is licensed under the MIT License.