Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Popular Data Structures in JavaScript

License

NotificationsYou must be signed in to change notification settings

linux-nerd/data-structures.js

Repository files navigation

Data Structures Logo

Data Structures in Javascript

Build StatusCoverage Status

Background

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

Install the dependenciesnpm install

Run dev build

  • For linux and darwinnpm run dev

  • For windowsnpm run dev:win32

To execute unit test cases

  • For linux and darwinnpm test

  • For windowsnpm test:win32

Trigger production build

  • For linux and darwinnpm run build

  • For windowsnpm run build:win32

List of Data Structures

Data structures covered so far -

Contribution

Your contribution is highly appreciated. You can contribute in several ways -

  • Opening an issue in the tracker
  • Updating and adding documentation
  • Adding new features
  • Adding demo

Vision

Once development is complete. This library will work in -

  • All supported Browsers
  • Node

and can be written in -

  • es5
  • es6
  • typescript

and will be published in

  • npm
  • bower

Binary Search Tree

Import BST class and instantiate it

import{BST}from"@js-labs/data-structures/lib/ds";constbst=newBST();

Insert values in binary search Tree

bst.insert(5);bst.insert(20);bst.insert(10);

Find size of the binary search tree

bst.len;// 3

Find an item in the binary search tree

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");

Find size of the graph.

adjList.size;// 3

Find weight of the edge in weighted graph

adjList.getEdgeWeight("A","B");

Queue

Import Queue class and create a queue object.

import{Queue}from"@js-labs/data-structures/lib/ds";constqueue=newQueue();

Add and remove elements to and from the created queue respectively

// add elements to the queuequeue.enqueue("A");queue.enqueue(123);// remove elements from the queuequeue.dequeue();queue.dequeue();

Get size and top element in the queue

queue.size();// 2queue.top();// A

Clear the entire queue at once

queue.clear();// this will empty the queue

Linked List

Import LinkedList data structure and create a list object.

import{LinkedList}from"@js-labs/data-structures/lib/ds";constlist=newLinkedList();

Insert and Remove Items from the list

// 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

list.size();// 3list.search("xyz");// truelist.search("abc");// falselist.isEmpty();// false

Iterate over list using for...of loop

for(constitemoflist){console.log(item);// 'firstVal', 'Mid', 'xyz', 'secondVal'}

Usage as spread operator

constitems=[...list];// ['firstVal', 'Mid', 'xyz', 'secondVal']

Stack (Linked List Implementation)

Import Stack data structure and create a list object.

import{Stack}from"@js-labs/data-structures/lib/ds";// const { Stack } = require('@js-labs/data-structures/lib/ds')conststack=newStack();

Get size of the stack and check if the stack is empty

stack.size();//0stack.isEmpty();//true

Push items in the stack

stack.push("item1");stack.size();//1stack.push("item2");stack.size();//2

Pop items from the stack

stack.pop();//item2stack.pop();//item1

Packages

No packages published

Contributors3

  •  
  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp