You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
There are neither a lot of resources on internet nor any book which guides and dictates best practices in the implementation of popular Data Structures using Javascript. The purpose of this library is to provide cooked implementation of popular data structures in javascript.
Installation
npm -npm install @js-labs/data-structures
Getting hands dirty
Clone the repogit clone https://github.com/linux-nerd/data-structures.js.git
bst.lookup(10);// returns an object with keys hasVal, currentNode and parentNode
Height of the binary search tree or a node
bst.height();//gives height of the BST 1bst.height(bst.lookup(10).currentNode);// gives the height of the node - 0
Traverse the BST and return a List
bst.traverse("inOrder");// traverse method expects a parameter - inOrder|preOrder|postOrder| levelOrder
Delete elements from binary search tree
bst.delete(10);bst.delete(20);
Graph
Import Graph class and instantiate it and create an object of adjacency list implementation of Graph. To create a directed graph pass the string argument 'directed'. If the Graph class is called without a parameter then by default its undirected graph.
import{Graph}from"@js-labs/data-structures/lib/ds";constgraph=newGraph();// this will create an undirected Graphconstgraph=newGraph("directed");// this will create a directed graph or diGraphconstadjList=graph.createGraph("adjList");// create Adjacency List implementation of graph
Add and remove a node to the graph
// add a nodeadjList.addNode("A");adjList.addNode("B");// remove a nodeadjList.removeNode("A");adjList.removeNode("B");
Add and remove an edge between two nodes to the graph. iF a node is not added, then it first adds the node and then create an edge.
// add an edgeadjList.addEdge("A","B",200);// it will add an edge between A and B of weight 200adjList.addEdge("B","C");// it will first add node C and then create an edge b/w B and C// remove an edgeadjList.removeEdge("A","B");adjList.removeEdge("B","C");
// inserts item at the end of the listlist.insert("firstVal");list.insert("SecondVal");list.insertAfter("Mid","firstVal");// insert Mid after firstVallist.insertBefore("xyz","secondVal");// insert xyz before secondVallist.remove("firstVal");// removes firstVal from the list.
Get size and search for an item and check if the list is empty