Binoheap
Binomial heaps for ES6
Description
ES6 implementation of the binomial heap data structure with TypeScript support.
Visit the contributing guidelines to learn more on how to translate this document into more languages.
Contents
Install
Yarn
yarn add binoheap
NPM
npm install binoheap
In Depth
A binomial heap data structure, is a specific implementation of the heap data structure, comprised of collections of binomial trees that are linearly linked together, where each tree is a minimum or maximum ordered heap. Binomial heaps are similar to binary heaps but they have a more specific structure and allow for efficient O(log n)
heap merging.
A binomial heap is implemented as a set of binomial trees, compared to a binary heap that has the shape of a single binary tree, which are defined recursively as follows:
- A binomial tree of order
0
is a single node. - A binomial tree of order
k
has a root node whose children are roots of binomial trees of orders k−1
, k−2
, ..., 2
, 1
, 0
.
A binomial tree of order k
has 2^k
nodes and height k
. Because of its unique structure, a binomial tree of order k
can be constructed from two trees of order k−1
by attaching one of them as the leftmost child of the root of the other. This feature is central to the merge operation of a binomial heap, which is its major advantage over other conventional heaps.
Additionally, each tree in the binomial heap should satisfy the binomial heap properties:
- There can only be either one or zero binomial trees for each order, including zero order.
- Each binomial tree in a heap obeys the minimum-heap property or the maximum-heap property, if the heap is either minimum or maximum ordered:
- Minimum-Heap Property: the key of a node is greater than or equal to the key of its parent.
- Maximum-Heap Property: the key of a node is smaller than or equal to the key of its parent.
The first property implies that a binomial heap with n
nodes consists of at most 1 + log2 n
binomial trees. The second property ensures that the root of each binomial tree contains the smallest or the largest key in the tree, which applies to the entire heap.
Binoheap binomial heaps are implemented using doubly linear linked lists for storing nodes, thus parent nodes point directly to their children and the child nodes point back to their parent. Each node contains a parent
, sibling
& child
pointer, as well as a key
, a value
and a degree
property.
Usage
Binoheap exposes a chainable API, that can be utilized through a simple and minimal syntax, allowing you to combine methods effectively.
To create a min-ordered binomial heap, where the nodes of each binomial tree obey the min-heap property, according to which the parent node is always smaller than or equal to its children nodes, we provide as argument to the Heap
class, on instantiation, a binary comparator function compareMin(x, y)
, which returns a positive number when the node x
is greater than node y
, zero when equal and a negative number when less than.
Additionally, to create a max-ordered binomial heap, where the nodes of each binomial tree obey the max-heap property, according to which the parent node is always greater than or equal to its children nodes, we provide as argument to the Heap
class, on instantiation, a binary comparator function compareMax(x, y)
, which returns a negative number when the node x
is greater than the node y
, zero when equal and a positive number when less than.
By default, if no comparator function is provided on instantiation, a min-ordered binomial heap instance is returned, where the key
value of each node is used for maintaining the min-heap property by the comparator function.
Usage examples can be also found at the test
directory.
'use strict';
const {Heap, Node} = require('binoheap');
const heap = new Heap((x, y) => y.key - x.key);
heap.isEmpty();
heap.insert(1, 'A');
heap.isEmpty();
heap.size;
heap.head;
heap.head.toPair();
const node = new Node(1, 'A');
heap.head.key === node.key;
heap.head.value === node.value;
heap.head.degree === node.degree;
heap
.insert(2, 'B')
.insert(3, 'C')
.insert(4, 'D')
.insert(5, 'E')
.insert(6, 'F')
.insert(7, 'G');
heap.head.siblings();
heap.extremum();
heap.extremumKey();
heap.extremumValue();
heap.roots();
heap.includes(100);
heap.includes(2);
heap.search(1);
heap.search(20);
heap.removeExtremum();
heap.head;
heap.size;
const heap2 = new Heap((x, y) => y.key - x.key));
heap2
.insert(8, 'H')
.insert(9, 'I');
heap2.updateKey(8, 15);
heap.merge(heap2);
heap.head.descendants();
API
heap.head
Returns the head node of the heap. If the heap is empty then null
is returned.
const {Heap} = require('binoheap');
const heap = new Heap();
heap.insert(10, 'A');
heap.head;
heap.size
Returns the total number of nodes residing in the heap.
const {Heap} = require('binoheap');
const heap = new Heap();
heap
.insert(10, 'A')
.insert(20, 'B')
.insert(30, 'C');
heap.size;
heap.clear()
Mutates the heap by removing all residing nodes and returns it empty.
const {Heap} = require('binoheap');
const heap = new Heap();
heap
.insert(10, 'A')
.insert(20, 'B')
.insert(30, 'C');
heap.size;
heap.clear();
heap.size;
heap.extremum()
- Return Type:
Node | undefined
Returns the node corresponding to the minimum key
value, if the heap is minimum-ordered or the node corresponding to the maximum key
, if the heap is maximum-ordered. If the heap is empty then undefined
is returned.
const {Heap} = require('binoheap');
const minHeap = new Heap((x, y) => x.key - y.key);
minHeap
.insert(10, 'A')
.insert(20, 'B')
.insert(30, 'C');
minHeap.extremum();
const maxHeap = new Heap((x, y) => y.key - x.key);
maxHeap
.insert(10, 'A')
.insert(20, 'B')
.insert(30, 'C');
maxHeap.extremum();
heap.extremumKey()
- Return Type:
Number | undefined
Returns the minimum key
value, if the heap is minimum-ordered or the maximum key
value, if the heap is maximum-ordered. If the heap is empty then undefined
is returned.
const {Heap} = require('binoheap');
const minHeap = new Heap((x, y) => x.key - y.key);
minHeap
.insert(10, 'A')
.insert(20, 'B')
.insert(30, 'C');
minHeap.extremumKey();
const maxHeap = new Heap((x, y) => y.key - x.key);
maxHeap
.insert(10, 'A')
.insert(20, 'B')
.insert(30, 'C');
maxHeap.extremumKey();
heap.extremumValue()
- Return Type:
Any | undefined
Returns the value
corresponding to the node with the minimum key
in the heap, if the heap is minimum-ordered or the value
corresponding to the node with the maximum key
, if the heap is maximum-ordered. If the heap is empty then undefined
is returned.
const {Heap} = require('binoheap');
const minHeap = new Heap((x, y) => x.key - y.key);
minHeap
.insert(10, 'A')
.insert(20, 'B')
.insert(30, 'C');
minHeap.extremumValue();
const maxHeap = new Heap((x, y) => y.key - x.key);
maxHeap
.insert(10, 'A')
.insert(20, 'B')
.insert(30, 'C');
maxHeap.extremumKey();
heap.heapTrees()
Returns the number of binomial trees that the heap is comprised of.
const {Heap} = require('binoheap');
const heap = new Heap();
heap
.insert(10, 'A')
.insert(20, 'B')
.insert(30, 'C');
heap.heapTrees();
heap.includes(key)
Traverses the nodes in the binomial heap level by level, from left to right & from top to bottom, and determines whether the heap includes a node with the given key
value, returning true
or false
as appropriate.
key
Node key
to search for.
const {Heap} = require('binoheap');
const heap = new Heap();
heap
.insert(10, 'A')
.insert(20, 'B')
.insert(30, 'C');
heap.includes(20);
heap.includes(5);
heap.insert(key, value)
Mutates the heap by inserting a new node at the appropriate location. Returns the heap itself.
key
Can be any number that will correspond to the key
of the created node.
value
Can be any value that will stored in the created node.
const {Heap} = require('binoheap');
const heap = new Heap();
heap.insert(10, 'A');
heap
.insert(20, 'B')
.insert(30, 'C');
heap.isEmpty()
Determines whether the heap is empty, returning true
or false
as appropriate.
const {Heap} = require('binoheap');
const heap = new Heap();
heap.isEmpty();
heap.insert(10, 'A');
heap.isEmpty();
heap.merge(heap)
Mutates the Heap
instance by merging it with the given binomial heap. Returns the resulting merged heap.
heap
Heap to merge with the Heap
instance.
const {Heap} = require('binoheap');
const heap1 = new Heap();
heap1
.insert(10, 'A')
.insert(20, 'B')
.insert(30, 'C');
const heap2 = new Heap();
heap2
.insert(40, 'D')
.insert(50, 'E')
.insert(60, 'F');
heap1.merge(heap2);
heap.removeExtremum()
- Return Type:
Node | undefined
Mutates the heap by removing the node corresponding to the minimum key
value, if the heap is minimum-ordered or the node corresponding to the maximum key
if the heap is maximum-ordered.
The removed node is returned by the method if the heap is not empty. If the heap is empty then undefined
is returned instead.
key
Can be any number that corresponds to the key
of an existing node.
const {Heap} = require('binoheap');
const minHeap = new Heap((x, y) => x.key - y.key);
minHeap
.insert(10, 'A')
.insert(20, 'B')
.insert(30, 'C');
minHeap.removeExtremum();
minHeap;
const maxHeap = new Heap((x, y) => y.key - x.key);
maxHeap
.insert(10, 'A')
.insert(20, 'B')
.insert(30, 'C');
maxHeap.removeExtremum();
maxHeap;
heap.roots()
Traverses the root nodes of the binomial trees residing in the heap and stores of each traversed root node in an array. At the end of the traversal, the method returns the array which will contain all tree root nodes in ascending degree
order.
const {Heap} = require('binoheap');
const heap = new Heap();
heap
.insert(10, 'A')
.insert(20, 'B')
.insert(30, 'C');
heap.roots();
heap.search(key)
- Return Type:
Node | undefined
Traverses the nodes in the binomial heap level by level, from left to right & from top to bottom, and determines whether the heap includes a node with the given key
value, returning node itself, without mutating the heap, or undefined
as appropriate.
key
Node key
to search for.
const {Heap} = require('binoheap');
const heap = new Heap();
heap
.insert(10, 'A')
.insert(20, 'B')
.insert(30, 'C');
heap.search(10);
heap.includes(5);
heap.updateKey(key, newKey)
Traverses the nodes in the binomial heap level by level, from left to right & from top to bottom, and determines whether the heap includes a node with the given key
value. If the node is found then, its key
value is mutated by replacing it with the new newKey
one. Returns the heap itself.
The method can be used to only decrease or increase the key value of targeted nodes if the binomial heap is either minimum or maximum ordered.
key
Can be any number that corresponds to the key
of an existing node.
newKey
New number value to be used as node key.
const {Heap} = require('binoheap');
const minHeap = new Heap((x, y) => x.key - y.key);
minHeap
.insert(10, 'A')
.insert(20, 'B');
minHeap.head;
minHeap.head.child;
minHeap.updateKey(20, 5);
minHeap.head;
minHeap.head.child;
const maxHeap = new Heap((x, y) => y.key - x.key);
maxHeap
.insert(10, 'A')
.insert(20, 'B');
maxHeap.head;
maxHeap.head.child;
maxHeap.update(10, 25);
maxHeap.head;
maxHeap.head.child;
Available, along with the Heap
exposed class, is the Node
class, mainly useful for testing purposes, since it can be utilized to compare heap nodes. The class has a binary constructor method, with a key
and a value
parameter, corresponding to the key and the value stored in the created instance, respectively.
node.key
The key
corresponding to the node instance.
const {Node} = require('binoheap');
const node = new Node(10, 'A');
node.key;
node.value
The value that the node contains.
const {Node} = require('binoheap');
const node = new Node(10, 'A');
node.value;
node.value = 'B'
node.value;
node.degree
Degree of the node.
const {Heap} = require('binoheap');
const heap = new Heap();
heap.insert(10, 'A').head;
heap.head.degree;
heap.insert(20, 'B').head;
heap.head.degree;
node.parent
The parent node of the node instance.
const {Node} = require('binoheap');
const node1 = new Node(10, 'A');
node1.parent;
const node2 = new Node(20, 'B');
node1.parent = node2;
node.child
The child node of the node instance.
const {Node} = require('binoheap');
const node1 = new Node(10, 'A');
node1.child;
const node2 = new Node(20, 'B');
node1.child = node2;
node.child
The sibling node of the node instance.
const {Node} = require('binoheap');
const node1 = new Node(10, 'A');
node1.sibling;
const node2 = new Node(20, 'B');
node1.sibling = node2;
node.siblings()
Traverses all sibling nodes of the Node
instance and stores them in an array. The array is returned at the end of the traversal.
const {Heap} = require('binoheap');
const heap = new Heap();
heap
.insert(10, 'A')
.insert(20, 'B')
.insert(30, 'C')
.insert(40, 'D')
.insert(50, 'E')
.insert(60, 'F')
.insert(70, 'G');
heap.head.siblings();
node.descendants()
Traverses all the descendant nodes of the Node
instance, through the Node#child
pointer, and stores each one of them in an array. The array is returned at the end of the traversal.
const {Heap} = require('binoheap');
const heap = new Heap();
heap
.insert(10, 'A')
.insert(20, 'B')
.insert(30, 'C')
.insert(40, 'D');
heap.head.descendants();
node.toPair()
- Return Type:
[Number, Any]
Returns an ordered-pair/2-tuple, where the first element is a number corresponding to the key
of the node, and the last one is a value, that can be of any type, corresponding to the value
stored in the node.
const {Heap, Node} = require('binoheap');
const heap = new Heap();
const node = new Node(5, 'B');
node.toPair();
heap.insert(10, 'A').head.toPair();
Development
For more info on how to contribute to the project, please read the contributing guidelines.
- Fork the repository and clone it to your machine
- Navigate to your local fork:
cd binoheap
- Install the project dependencies:
npm install
or yarn install
- Lint the code and run the tests:
npm test
or yarn test
Related
- avlbinstree - AVL self-balancing binary search trees for ES6
- binstree - Binary search trees for ES6
- doublie - Doubly circular & linear linked lists for ES6
- dsforest - Disjoint-set forests for ES6
- kiu - FIFO Queues for ES6
- mheap - Binary min & max heaps for ES6
- prioqueue - Priority queues for ES6
- singlie - Singly circular & linear linked lists for ES6
Team
License
MIT