Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including data-structures-again with all npm packages installed. Try it out:

var dataStructuresAgain = require("data-structures-again")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

data-structures-again v2.1.2

A Javascript library of simple data structures

Data Structures Again

Coverage Status npm version

Light weight javascript data structures library

  • Binary Search Tree
  • Stack
  • Queue
  • Heap
  • Graph
  • Disjoint-set (New!)

Installation and Usage

npm install data-structures-again

Binary Search Tree:

const { BST } = require("data-structures-again");

const bst = new BST();
bst.insert(2);
bst.insert(1);
const node = bst.search(1); // { data: 1, left: null, right: null }
bst.delete(1);

Stack:

const { Stack } = require("data-structures-again");

const stack = new Stack();
stack.push(1);
stack.push(2);
const data = stack.pop(); // 2
const top = stack.peek(); // 1

Queue:

const { Queue } = require("data-structures-again");

const queue = new Queue();
queue.enqueue(1);
queue.enqueue(2);
const data = queue.dequeue(); // 1
const top = queue.peek(); // 2

Heap:

const { Heap } = require("data-structures-again");

const minHeap = new Heap()
minHeap.push(5)
minHeap.push(2)
minHeap.peek()  // 2


const maxHeap = new Heap((a, b) => b - a)
maxHeap.push(4)
maxHeap.push(10)
maxHeap.peek() // 10

Graph

const { Graph } = require("data-structures-again");

const graph = new Graph()

/*
    a---b
    |  /    
    | /
    c
*/

graph.addVertex('a')
graph.addVertex('b')
graph.addVertex('c')

graph.addEdge('a', 'c') // add weight using graph.addEdge('a', 'c', 10)
graph.addEdge('c', 'b')
graph.addEdge('a', 'b')

const output = []
graph.dfs('a', vertex => output.push(vertex.name)) // ['a', 'c', 'b']

Disjoint Set(Union-find)

const { DisjointSet } = require("data-structures-again");

const ds = new DisjointSet()
ds.union('a', 'b')
ds.union('b', 'c')
ds.union('d', 'c')

ds.find('d') // 'a'
ds.isConnected('a', 'd') // true

License

MIT.

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free