data-structures-typescript
A collection of commonly used data structures written in TypeScript.
Installation
To install, simply add as an npm dependency:
npm install @mpaauw/data-structures-typescript
Usage
This library offers a variety of data structures out-of-the-box. Currently supported structures include: Hash Table, Singly-Linked List, Queue, Stack, and Binary Search Tree. See the sections below for usage instructions.
HashTable
import { HashTable } from '@mpaauw/data-structures-typescript';
const hashTable = new HashTable<string, object>(100);
hashTable.put('cat', {
name: 'Ms. Kitty',
type: Animal.Cat,
weightInLbs: 4.20
});
const cat = hashTable.get('cat');
const isEmpty = hashTable.isEmpty();
const containsCat = hashTable.contains('cat');
hashTable.remove('cat');
singly-Linked List
import { SinglyLinkedList } from '@mpaauw/data-structures-typescript';
const singlyLinkedList = new SinglyLinkedList<string>();
singlyLinkedList.insertAtHead('red');
singlyLinkedList.insertAtTail('blue');
singlyLinkedList.insertAt(1, 'yellow');
const valueFound = singlyLinkedList.find('green');
const valueFoundAt = singlyLinkedList.findAt(2);
const head = singlyLinkedList.getHead();
const tail = singlyLinkedList.getTail();
const isEmpty = singlyLinkedList.isEmpty();
singlyLinkedList.removeAtHead();
singlyLinkedList.removeAtTail();
singlyLinkedList.removeAt(2);
Queue
import { Queue } from '@mpaauw/data-structures-typescript';
const queue = new Queue<string>();
queue.enqueue('dog');
const peekedValue = queue.peek();
const dequeuedValue = queue.dequeue();
const isEmpty = queue.isEmpty();
Stack
import { Stack } from '@mpaauw/data-structures-typescript';
const stack = new Stack<string>();
stack.push('fish');
const peekedValue = stack.peek();
const poppedValue = stack.pop();
const isEmpty = stack.isEmpty();
Binary Search Tree
import { BinarySearchTree } from '@mpaauw/data-structures-typescript';
const binarySearchTree = new BinarySearchTree<number>();
binarySearchTree.insertIteratively(5);
binarySearchTree.insertRecursively(10);
const foundNode = binarySearchTree.find(9001);
const minNode = binarySearchTree.findMinimumNode(foundNode);
const maxNode = binarySearchTree.findMaximumNode(minNode);
binarySearchTree.removeIteratively(1337);
binarySearchTree.removeRecursively(420);
const isEmpty = binarySearchTree.isEmpty();
const isBST = binarySearchTree.validateRecursively();