npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@datastructures-es6/core

v2.1.3

Published

Implementation of the most common data structures in Javascript

Downloads

7

Readme

Data Structures ES6

Implementation of the most common data structures in Javascript (ES6+), including linear and non-linear ones, such as Array, Linked List, Queue, Stack and Binary Tree.

Futhermore, you will find here implementation of some interesting algorithms like Quick Sort for sorting arrays and Breadth First Search or Depth First Search (inOrder, preOrder, postOrder) for binary trees.

Instalation

npm i @datastructures-es6/core

How to use this lib

  1. After install the package on your personal project, you may import the code in your files:
import {
    Array,
    LinkedList,
    Queue,
    Stack,
    BinaryTree
} from '@datastructures-es6/core/src/index.js';
  1. Creating and using data structures (examples).

Array (code implementation)

const array = new Array();
array.push(10);
array.push(6);
array.push(2);

array.sort();

console.log(array.toString());

Linked List (code implementation)

const linkedList = new LinkedList();
linkedList.prepend(444);
linkedList.append(3);
linkedList.insert(1, 22);
linkedList.delete(0);

console.log(linkedList.get());

Queue (code implementation)

const queue = new Queue();
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
queue.enqueue(4);
queue.enqueue(5);

queue.dequeue();
queue.dequeue();

let currentQueueItem = queue.peek();
while (currentQueueItem) {
    console.log(currentQueueItem.value);
    currentQueueItem = currentQueueItem.next;
}

Stack (code implementation)

const stack = new Stack();
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);

stack.pop();
stack.pop();

let currentStackItem = stack.peek();
while (currentStackItem) {
    console.log(currentStackItem.value);
    currentStackItem = currentStackItem.next;
}

Binary Tree (code implementation)

const binaryTree = new BinaryTree();
binaryTree.insert(5);
binaryTree.insert(9);
binaryTree.insert(6);
binaryTree.insert(2);
binaryTree.insert(4);
binaryTree.insert(3);
binaryTree.insert(8);

console.log(binaryTree.breadthFirstSearch());

Available methods

ARRAY

  • get(index?: number): returns the element value for a provided index or returns the entire array in case you do not provide an index.
  • push(value: any): pushes the provided value in the current array.
  • length(): returns the length of the current array.
  • pop(): removes the last element from the current array and returns it.
  • update(index: number, value: any): updates the value for a new provided one in the provided index.
  • shift(): removes the first element from the current array and returns the resulting array.
  • unshift(value: any): inserts the provided value in the beginning of the current array.
  • find(value: any): if the current array has the provided value, it returns the finded index, else it returns -1.
  • sort(): returns the sorted array, using Quick sort algorithm.
  • toString(): returns the entire array in a string format.
  • join(term: string): join all values of the current array into a string, separated by the provided term.
  • concat(term: Array | any): concat the current array with a provided term (it can be any value or other Array).
  • splice(startIndex: number, quantityToDelete: number, items: any): returns a new array with the provided value inserted in the provided index, shifting the remaining elements to right. However, if you provide a n value greater than zero in the quantity to delete argument, it will delete the n elements after the provided index and replace the value instead of them.
  • slice(startIndex: number, endIndex?: number): returns a slice of the current array, based on the start/end indexes provided (if you do not provide the end index, it will consider the length of the current array).

LINKED LIST

  • prepend(value: any): inserts the provided element in the beginning of the linked list and this element becomes the head.
  • append(value: any): inserts the provided element in the end of the linked list and this element becomes the tail.
  • lookup(value: any): finds the provided element in the linked list.
  • insert(position: number, value: any): inserts the provided element in the provided position.
  • delete(position: number): removes an element from the provided position.
  • get(): returns a string containing the entire linked list.

QUEUE

  • enqueue(value: any): inserts the selected value in the end of the queue.
  • dequeue(): removes the first element of the queue.
  • lookup(value: any): finds the selected element in the queue and return it.
  • peek(): return the first element of the queue.

STACK

  • push(value: any): inserts the provided value in the top of the stack.
  • pop(): removes the element in the top of the stack.
  • lookup(value: any): finds the provided element in the stack.
  • peek(): return the element in the top of the stack.

BINARY TREE

  • insert(value: any): inserts the provided value in the correct position on the current binary tree.
  • delete(): removes the provided value from the binary tree.
  • lookup(value: any): finds the provided element on the current binary tree.
  • breadthFirstSearch(): return current binary tree after parse through the Breadth First Search algorithm.
  • depthFirstSearch(type: string): return current binary tree after parse through the Depth First Search algorithm using the approach provided by the type argument (it can be inOrder, preOrder or postOrder).

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT