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

@gatzxr/data-structures

v1.1.2

Published

A set of the most common data-structures

Downloads

6

Readme

@gatzxr/data-structures

This repository contains a set of the most popular data structures written in TypeScript

  • Singly Linked List
  • Doubly Linked List
  • Stack
  • Queue
  • Binary Search Tree
  • Max Binary Heap
  • Priority Queue
  • Hash Table
  • Unweighted Graph
  • Weighted Graph

Installation

$ npm i @gatzxr/data-structures

Usage

import {
    SinglyLinkedList,
    DoublyLinkedList,
    Stack,
    Queue,
    BinarySearchTree,
    PriorityQueue,
    MaxBinaryHeap,
    HashTable,
    UnweightedGraph,
    WeightedGraph
} from '@gatzxr/data-structures';

const singlyLinkedList = new SinglyLinkedList();

Singly Linked List

A linked list is an array like object used for storing data. Unlike the array, elements in a linked list can be easily inserted or removed without reallocating or reorganizing the entire structure. A singly linked list consists of nodes where each node has a value and a pointer to the next node.

Methods:

  • push(value: any) : SinglyLinkedList
  • pop() : ListNode
  • shift() : ListNode
  • unshift(value: any) : SinglyLinkedList
  • get(index: number) : ListNode
  • set(index: number, value: any) : boolean
  • insert(index: number, value: any) : boolean
  • remove(index: number) : ListNode
  • reverse() : SinglyLinkedList

Time Complexity

  • Insertion: O(1)
  • Deletion: O(1) when deleting first item or O(n) when deleting last item
  • Searching: O(n)
  • Access: O(n)

A visual representation of the singly linked list can be found here.

Doubly Linked List

A doubly linked list is very similar to singly linked list, except every node has a second pointer, to the previous node.

Methods:

  • push(value: any) : DoublyLinkedList
  • pop() : ListNode
  • shift() : ListNode
  • unshift(value: any) : DoublyLinkedList
  • get(index: number) : ListNode
  • set(index: number, value: any) : boolean
  • insert(index: number, value: any) : boolean
  • remove(index: number) : ListNode

Time Complexity

  • Insertion: O(1)
  • Deletion: O(1)
  • Searching: O(n)
  • Access: O(n)

A visual representation of the doubly linked list can be found here (dll).

NOTE: Although doubly linked list are faster, they occupy more space due to the extra node.

Stack

Stack is an abstract data structure which follows LIFO principle. The last element added to the stack will be the first element removed from the stack.

Methods:

  • push(value: any): number
  • pop(): any

Time Complexity

  • Insertion: O(1)
  • Deletion: O(1)
  • Searching: O(n)
  • Access: O(n)

A visual representation of a stack can be found here (stack).

Queue

Very similar to stacks, queues are an abstract data structure which follow FIFO principle. The fist element added to the queue will be the first element removed from the queue.

Methods:

  • enqueue(value: any): number
  • dequeue(): QueueNode

Time Complexity

  • Insertion: O(1)
  • Deletion: O(1)
  • Searching: O(n)
  • Access: O(n)

A visual representation of a queue can be found here (queue).

Binary Search Tree

A tree is a data structure that consists of nodes in a parent/child relationship. In a binary tree, each node can have at most two children. A binary search tree is a more specific binary tree where the value of each node is greater than all the values in left subtree and smaller than all the values in the right subtree.

Methods

  • insert(value: number): BinarySearchTree
  • find(value: number): number
  • breadthFirstTraversal(): number[]
  • depthFirstTraversalPreOrder(): number[]
  • depthFirstTraversalPostOrder(): number[]
  • depthFirstTraversalInOrder(): number[]

Time Complexity

  • Insertion: Average: O(logn), Worst: O(n)
  • Deletion: Average: O(logn), Worst: O(n)
  • Searching: Average: O(logn), Worst: O(n)
  • Access: Average: O(logn), Worst: O(n)

A visual representation of a binary search tree can be found here.

Max Binary Heap

A binary heap is very similar to binary search trees, but with some different rules. In a MaxBinaryHeap, parent nodes are always larger than child nodes.

Methods

  • insert(value: number): MaxBinaryHeap
  • extractMax(): number

Time Complexity

  • Insertion: O(logn)
  • Deletion: O(logn)
  • Searching: O(n)

A visual representation of a binary heap can be found here.

Priority Queue

A priority queue is a data structure where each element has a priority. Elements with higher priorities are served before elements with lower priorities.

Methods

  • enqueue(value: any, priority: number): PriorityQueue
  • dequeue(): any

Hash Table

A hash table is a data structure that stores key-value pairs. A hash table uses a hash function to compute an index into an array of slots, from which the desired value can be found. Hash tables are fast for finding, adding and removing values.

Methods

  • set(key: string, value: any): void
  • get(key: string): any
  • values(): any[]
  • keys(): string[]

Time Complexity

  • Insertion: Average: O(1), Worst: O(n)
  • Deletion: Average: O(1), Worst: O(n)
  • Searching: Average: O(1), Worst: O(n)

Hash table time complexity heavily depends on the hash function.

Graph

A graph is a data structure which consists of a finite set of vertices (nodes) together with a set of unordered (unweighted) pairs of these vertices.

Unweighted Graph Methods

  • addVertex(vertex: string): void
  • addEdge(vertex1: string, vertex2: string): void
  • removeEdge(vertex1: string, vertex2: string): void
  • removeVertex(vertex: string): void
  • depthFirstTraversalRecursive(start)
  • depthFirstTraversalIterative(start)
  • breadthFirstTraversal(start)

Weighted Graph Methods

  • addVertex(vertex: string): void
  • addEdge(vertex1: any, vertex2: any, weight: number)
  • dijkstra(start, finish)

Time Complexity (adjacency list implementation)

  • Add Vertex: O(1)
  • Add Edge: O(1)
  • Remove Vertex: O(|V| + |E|) where |V| is number of vertices and |E| number of edges
  • Remove Edge: O(|E|) where |E| is number of edges