Applications of Finger Tree. This project also demostrates usage of Finger Tree in implementing other data structures.
Using npm:
npm install fingertree-based
Random access sequence, supports length
, push
, get
and set
Create a new Sequence
import { Sequence } from 'fingertree-based';
let seq = new Sequence;
Get the length of a sequence
let len = sequence.length;
Push a new element of any type into sequence
sequence.push('a');
sequence.push(1);
Get element positioned by index, zero-indexed. Minus value is also allowed. It will throw an error if index
out of range.
sequence.get(0); // get the first element
sequence.get(-1); // get the last element
sequence.get(1000000); // throws an error if `index` is out of range
Set element at index
. Similar rules with index
work like get
here.
sequence.set(0, 'hello'); // set the first element
sequence.set(-1, 1); // set the last element
sequence.set(10000, 'error'); // throws an error if `index` is out of range
PriorityQueue, supports push
and pop
.
Create a new priority queue.
import { PriorityQueue } from 'fingertree-based';
let queue = new PriorityQueue;
Check if the queue is empty.
let empty = queue.empty; // true or false
Push an element of any type with priority
. priority
should be a non-negative integer or an error will be throwed.
queue.push('a', 1);
queue.push(123, 0);
queue.push('error', -1); // throws an error if `priority` is negative
Pop the element with largest priority. Throws an error if queue is already empty.
let element = queue.pop();