ESDS
ES Javascript Data Structures (Priority Queue, Binary Search Tree (BST), Graph, Trie, Bloom Filter, Queue, Stack, Linked List)
Explore the docs »
Report Bug 🐞
·
Request Feature 🗳
Table of Contents
-
Getting Started 🚦
- Usage 📚
- Roadmap 📍
- Contributing 💪
- License 📝
- Contact 📧
Getting Started
To get a local copy up and running follow these simple steps.
Installation
- Install NPM package
npm i esds
Examples
Priority Queue
import { PriorityQueue } from "esds";
const minPQ = new PriorityQueue("min");
const maxPQ = new PriorityQueue("max");
const arr = [20, 40, 30, 50, 15, 10, 5];
arr.forEach((element) => {
minPQ.add(element);
maxPQ.add(element);
});
console.log(minPQ.peek);
console.log(maxPQ.peek);
while (!minPQ.isEmpty) {
console.log(minPQ.poll());
}
while (!maxPQ.isEmpty) {
console.log(maxPQ.poll());
}
const comparator = (a, b) => a.age - b.age;
const customPQ = new PriorityQueue("custom", comparator);
const Person = function (name, age, role) {
this.name = name;
this.age = age;
this.role = role;
};
customPQ.add(new Person("Jane Doe", 26, "Software Engineer"));
customPQ.add(new Person("John Doe", 28, "Cloud Engineer"));
customPQ.add(new Person("Ordinary Joe", 42, "QA"));
customPQ.add(new Person("Janie Doe", 20, "Support Engineer"));
customPQ.add(new Person("Fred Bloggs", 19, "Intern"));
console.log(customPQ.contains(new Person("Janie Doe", 20, "Support Engineer")));
while (!customPQ.isEmpty) {
console.log(customPQ.poll());
}
Binary Search Tree
import { BST, List} from "esds";
const bst = new BST();
bst.add(10, "Ten");
bst.add(1, "One");
bst.add(5, "Five");
bst.add(7, "Seven");
bst.add(2, "Two");
bst.add(0, "Zero");
bst.add(8, "Eight");
console.log(bst.toArray("in-order"));
console.log(bst.toArray("pre-order"));
console.log(bst.toArray("post-order"));
console.log(bst.toArray("reverse-in-order"));
console.log(bst.get(7));
bst.update(8, "はち");
console.log(bst.get(8).value);
console.log(bst.has(5));
bst.remove(5);
console.log(bst.has(5));
if (bst.has(7)) {
const list = new List();
list.add(bst.get(7).value);
list.add(["Siete", "しち", "七", "Sieben"]);
bst.update(7, list);
}
console.log(bst.get(7).value.toArray());
function* iterator(arr) {
let i = 0;
while (true) {
yield arr[i++];
if (i === arr.length) i = 0;
}
}
const removeType = iterator(["predecessor", "successor"]);
[10, 1, 0, 5, 7].forEach((value) => bst.remove(value, removeType.next().value));
console.log(bst.toArray());
Graph
Undirected
import { Graph } from "esds";
const graph = new Graph();
graph.addNode(1, "Randall");
graph.addNode(2, "Mellisa");
graph.addNode(3, "Cecelia");
graph.addNode(4, "Velda");
graph.addNode(5, "Rossie");
graph.addEdge(1, 2);
graph.addEdge(2, 5);
graph.addEdge(3, 4);
graph.addEdge(4, 1);
graph.addEdge(5, 1);
console.log(graph.nodesConnected(2, 3));
console.log(graph.nodesConnected(1, 5));
console.log(graph.getWeight(2, 3));
console.log(graph.getWeight(1, 5));
console.log(graph.getPath(2, 3).map((value) => graph.getNode(value.node)));
console.log(graph.getPath(1, 5).map((value) => graph.getNode(value.node)));
Directed
import { Graph } from "esds";
const graph = new Graph(true);
graph.addNode(1, "City Α");
graph.addNode(2, "City β");
graph.addNode(3, "City Γ");
graph.addNode(4, "City Δ");
graph.addNode(5, "City ε");
graph.addEdge(1, 3, 75);
graph.addEdge(2, 5, 325);
graph.addEdge(3, 1, 125);
graph.addEdge(4, 2, 100);
graph.addEdge(5, 1, 415);
graph.addEdge(5, 3, 550);
console.log(graph.nodesConnected(2, 3));
console.log(graph.nodesConnected(5, 1));
let routeADistance = 0;
const routeA = graph.getPath(2, 3).map((value) => {
routeADistance += value.weight;
return graph.getNode(value.node);
});
console.log(routeA, routeADistance);
let routeB = graph.getPathWeighted(2, 3);
let routeBDistance = routeB.distance;
routeB = routeB.nodes.map((value) => graph.getNode(value));
console.log(routeB, routeBDistance);
Use cases: Social graphs, recommendation engines, navigation, supply chain, etc.
Trie
import { Trie } from "esds";
const countries = [
"United States of America",
"Canada",
"Argentina",
"Japan",
"Italy",
"Germany",
"Brazil",
"Armenia",
"Aruba",
];
const trie = new Trie();
countries.forEach((element) => trie.add(element));
console.log(trie.contains("Argentina"));
console.log(trie.contains("Spain"));
trie.update("Argentina", "🇦🇷");
trie.update("United States of America", "🇺🇸");
console.log(trie.get("Argentina"));
console.log(trie.get("United States of America"));
const prefix = "Ar";
const result = trie.find(prefix);
const dfs = (word, node) => {
if (node.child?.size > 0) {
node.child.forEach((value) => {
dfs(`${word}${value.key}`, value);
});
}
if (node.end) console.log(`${word}`);
};
dfs(prefix, result);
console.log(trie.toArray());
Use cases: Auto-complete, string search, word suggestion, prefix, IP routing, etc.
Bloom Filter
A Bloom filter is a space-efficient probabilistic data structure that is used to test whether an element is a member of a set. More Info: https://bit.ly/3D9RmH8
import { BloomFilter } from "esds";
const BF = new BloomFilter();
BF.add("john_89@email.com");
BF.add("jane2020@email.com");
BF.add("Joe@email.com");
BF.add("Doe.Jane@email.com");
BF.add("Jim.1990@email.com");
console.log(BF.has("Doe.Jane@email.com"));
console.log(BF.has("Jimmy@email.com"));
const BF2 = new BloomFilter(2048, 4);
const users = [
"almost",
"dopey",
"eritrean",
"struggle",
"hospitable",
"factor",
"quail",
];
BF2.add(users);
console.log(BF2.has("struggle"));
console.log(BF2.has("house"));
console.log(
BF2.has(["hospitable", "monitor", "dopey", "factor", "fan", "quail"])
);
Bloom Filters use cases:
- Value lookup when false positives are acceptable
- Avoid expensive lookup operations against DB
- Content filtering
Queue-Stack
import {Queue, Stack} from 'esds';
const queue = new Queue();
const stack = new Stack();
queue.add([1,2,3]);
queue.add(4);
queue.add(5);
stack.push([1,2]);
stack.add(3);
stack.add([4,5]);
while (!queue.isEmpty){
console.log(queue.poll());
}
while (!stack.isEmpty){
console.log(stack.pop());
}
Linked List
import {List} from 'esds';
const a = new List();
a.add([1, 2, 3, 4]);
console.log(a.toArray());
a.add(5);
a.add("Six");
a.add({value: 7, str: "Seven"});
console.log(a.get(6).val);
let head = a.get();
while(head){
console.log(head.val);
head = head.next;
}
let b = a.subList(2, 5);
console.log(b.toArray());
Roadmap
See the open issues for a list of proposed features (and known issues).
Contributing
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
License
Distributed under the MIT License. See LICENSE
for more information.
Contact
Project Link: https://github.com/jayalbo/ESDS