An extensive standalone data structure library for JavaScript.
MIT License
Needle is a standalone extensive data structure library in JavaScript.
Install as an npm package or download the minified file of the latest version.
You can install Needle as a node package with npm, and it's as easy as:
$ npm install --save node-needle
If you prefer to use Needle on the client side, just download the minified file of the latest version and include it in your webpage:
<!-- The complete Needle library -->
<script src="path/to/needle.min.js"></script>
When you have Needle installed, you can use it on the server in Node like so:
const Needle = require('node-needle');
// Example: Create a new instance of a hashmap
var map = Needle.Hashmap();
or you can just use it on the client side like so:
// Needle gets pushed onto the global scope under the alias "Needle"
var bst = new Needle.BinarySearchTree();
Needle has a variety of different data structures at its disposal. Here is a reference to all of the currently available data structures that Needle supports. Some data structures have not been added yet and they will appear unavailable.
If you feel that there should be additional data structures added to this library, send me a message and let me know what you had in mind.
Note: In the API, *
refers to any type. This is commonly used when specifying the type of data; since all types of data are supported when inserting custom data into a data structure.
root - Node - The root node of the binary tree. compare - function - Compares two elements to each other to determine the ordering of the heap.
Note: The default compare
function is defined as
function defaultCompare(a, b){
return (a < b);
}
compare
.Node
has a right component.Node
has a left component.Node
is a leaf.Node
.Node
.Node
.Node
.data
. The Node
argument will determine which subtree to attempt to insert the node at. Inserting at the root subtree is selected by default if this parameter is left blank (recommended).data
in the binary search tree. The Node
argument will determine which subtree to attempt to search for the node. Searching at the root subtree is selected by default if this parameter is left blank (recommended).heap - Array - The array based heap acting as a binary heap. compare - function - Compares two elements to each other to determine the ordering of the heap.
Note: The default compare
function is defined as
function defaultCompare(a, b){
return (a < b);
}
compare
.data
into the heap and adjusts the heap accordingly.data - Array - The array of bit sequences.
size
if argument is given.index
.size
.bitarray
.bitarray
.bitarray
.head - Node - The first node in the linked list. tail - Node - The last node in the linked list. size - number - The number of nodes in the linked list.
data
is given.data
and inserts at the front of the list.data
and insert in the location of the linked list specified by index
.data
and insert after the node which has the data specified by targetData
and returns true
if the element was successfully added to the linked list.data
and inserts at the back of the list.data
and returns true
if the element was successfully found and removed from the linked list.index
.data
and returns that Node
if the element was successfully found but returns false
if the node was not found.index
and returns that Node
.state - number - Holds the current hash of the internal window.
key
to its respective value
.key
.key
, returns true
if deletion was successful and false
if the entry was not found.Node
to the first entry and returns the unhashed key.Node
and returns an the unhashed key.state - number - The internal hash value of the current window.
base
of the rolling hash.arg
in the relative base.old
segment and appending on the new
segment, then returns the newly updates state
of the internal window.old
segment from the internal window.new
segment onto the internal window.string
, number
(assumed in the relative base
), or Array
of elements in the relative base, and returns the hash of the argument.head - Node - The first node in the linked list. size - number - The number of nodes in the linked list.
data
is given.data
and inserts at the front of the list.data
and insert in the location of the linked list specified by index
.data
and insert after the node which has the data specified by targetData
and returns true
if the element was successfully added to the linked list.data
and inserts at the back of the list.data
and returns true
if the element was successfully found and removed from the linked list.index
.data
and returns that Node
if the element was successfully found but returns false
if the node was not found.index
and returns that Node
.top - Node - The top node in the stack. size - number - The number of nodes in the stack.
data
, is created and inserted.Node
of the stack.data
, to the top of the stack.Node
that was previously the top, and just deleted.front - Node - The first node in the queue. back - Node - The last node in the queue. size - number - The number of nodes in the queue.
data
, is created and inserted.data
, to the queue.Here are an assortment of examples using various data structures provided by Needle. If you wish there to be examples of a data structure in particular, feel free to let me know what you have in mind.
// Priority Queue implementation using a binary heap
const Needle = require('node-needle');
var Level = function(key, value){
this.key = key;
this.value = value;
}
var priorityQueue = new Needle.BinaryHeap(function(a, b){
return a.key < b.key;
});
priorityQueue.insert(new Level(3, "Level 3"));
priorityQueue.insert(new Level(1, "Level 1"));
priorityQueue.insert(new Level(2, "Level 2"));
priorityQueue.peek(); // => {1, "Level 1"}
priorityQueue.size(); // => 3
priorityQueue.delete();
priorityQueue.peek(); // => {2, "Level 2"}
priorityQueue.size(); // => 2
// Iterating through a Hashmap
const Needle = require('node-needle');
var map = new Needle.Hashmap();
map.put(1, "Level 1");
map.put("2", "Level 2");
map.put({key: "three"}, "Level 3");
// Insertion order is kept, despite key value
for(var it = map.iterator(); it !== null; it = map.next()){
console.log(it); // 1 -> "2" -> {key: "three"}
console.log(map.get(it)); // "Level 1" -> "Level 2" -> "Level 3"
}
Copyright (c) 2015 Nick Zuber