Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ahmedgaafer/ts-data-structures
A lightweight TS data structures package
https://github.com/ahmedgaafer/ts-data-structures
data-structures graph heap javascript javascript-library stack typescript typescript-library
Last synced: 18 days ago
JSON representation
A lightweight TS data structures package
- Host: GitHub
- URL: https://github.com/ahmedgaafer/ts-data-structures
- Owner: ahmedgaafer
- License: mit
- Created: 2019-05-31T18:25:56.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-10-18T13:14:49.000Z (about 1 year ago)
- Last Synced: 2024-12-21T07:15:42.721Z (25 days ago)
- Topics: data-structures, graph, heap, javascript, javascript-library, stack, typescript, typescript-library
- Language: TypeScript
- Homepage: https://ahmedgaafer.github.io/TS-Data-Structures/
- Size: 3.39 MB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# TypeScript Data Structures
Data Structures package made by TypeScript.
## Install
```bash
$ npm i @ahmeds.gaafer/ts-data-structures
```## Data Structure list:
- [x] Singly Linked List
- [x] Doubly Linked List
- [x] Queue
- [x] Stack
- [x] Heap
- [x] Graph## Usage
### Singly Linked List:
```ts
import { SinglyLinkedList } from "@ahmeds.gaafer/ts-data-structures";// The constructor can accept an array as an input and it will transform the array to the linked list
const arr = [1, 2, 3];
const list = new SinglyLinkedList(arr);list.push(1);
list.view();
// You can chain methods that does not return a value
list.popStart().pop().view(); // vaild
list.getHead().view(); // invalid
```functions:
- **_.insert(data, pos)_** => Insert data to certian position.
- **_.pushStart(data)_** => Insert data at the head position.
- **_.push(data)_** => Insert data at tail position.
- **_.delete(pos)_** => Delete element at a certian position.
- **_.pop()_** => Delete element from the end of the tail.
- **_.popStart()_** => Delte element from the head.
- **_.getHead()_** => Returns a copy of the head node.
- **_.getTail()_** => Returns a copy of the tail node.
- **_.view()_** => Prints a visual dispaly of the linked list.
- **_.toArray()_** => Returns the linked list as an array### Doubly Linked List:
```ts
import { DoublyLinkedList } from "@ahmeds.gaafer/ts-data-structures";
// The constructor can accept an array as an input and it will transform the array to the linked list
const arr = [1, 2, 3];
const list = new DoublyLinkedList(arr);list.push(1);
list.view();
// You can chain methods that does not return a value
list.popStart().pop().view(); // vaild
list.getHead().view(); // invalid
```functions:
- **_.insert(data, pos)_** => Insert data to certian position.
- **_.pushStart(data)_** => Insert data at the head position.
- **_.push(data)_** => Insert data at tail position.
- **_.delete(pos)_** => Delete element at a certian position.
- **_.pop()_** => Delete element from the end of the tail.
- **_.popStart()_** => Delte element from the head.
- **_.getHead()_** => Returns a copy of the head node.
- **_.getTail()_** => Returns a copy of the tail node.
- **_.view()_** => Prints a visual dispaly of the linked list.
- **_.toArray()_** => Returns the linked list as an array.### Queue:
```ts
import { Queue } from "@ahmeds.gaafer/ts-data-structures";// The constructor can accept an array as an input and it will transform the array to the Queue
const arr = [1, 2, 3];
const queue = new Queue(arr);queue.enqueue(5);
queue.deqeue();// You can chain methods that does not return a value
queue.enqueue(5).dequeue().view(); //vaildqueue.peak().view(); // invalid
```functions:
- **_.enqueue(data)_** => Add data to the end of the queue.
- **_.dequeue()_** => Remove first element of the queue.
- **_.peak()_** => Returns the first element in the queue.
- **_.getSize()_** => Returns the size of the queue.
- **_.view()_** => Prints a visual dispaly of the queue.### Stack:
```ts
import { Stack } from "@ahmeds.gaafer/ts-data-structures";// The constructor can accept an array as an input and it will transform the array to the Heap
const arr = [1, 2, 3];
const stack = new Stack(arr);
stack.push(1);
// You can chain methods that does not return a value
stack.push(1).push(2).pop(); // vaild
stack.peak().push(1); // invalid
```functions:
- **_.push(data)_** => Inserts data to top of stack.
- **_.pop()_** => Removes the item on the top of the stack.
- **_.peak()_** => Returns the item on the top of the stack.
- **_.getSize()_** =>Returns the size of the stack;
- **_.view()_** => Prints a visual display of the stack.### Heap:
```ts
import { Heap } from "@ahmeds.gaafer/ts-data-structures";// The constructor can accept an array as an input and it will transform the array to the Heap
const arr = [1, 2, 3];
const maxHeapOptions = {
cmp: (a, b) => a < b;
}
const minHeapOptions = {
cmp: (a, b) => a > b;
}// Heap type is set to number by default but you can change it
//min heap
const minHeap = new Heap(arr, minHeapOptions);//max heap
const maxHeap = new Heap(arr, maxHeapOptions);/// or
const maxHeap = new Heap([], maxHeapOptions);maxHeap.push(5);
// You can chain methods that does not return a value
minHeap.push(1).push(2); // vaild
minHeap.peak().push(1); // invalid
```functions:
- **_.push(data)_** => Insert a data to the heap.
- **_.pop()_** => Removes the root of the heap and fixes the the heap.
- **_.peak()_** => Returns the value of the root of the heap.
- **_.getSize()_** => Returns the size of the heap;
- **_.view()_** => Prints the array of the heap;### Graph:
```ts
import { Graph } from "@ahmeds.gaafer/ts-data-structures";//The graph can has 2 arguments that are set to false by default.
//The first argument is "isUniDirectional" if you set it to true the graph will be a directed graph.
//The second argument is "isWeighted" if you set it to true the graph will be weighted.const g = new Graph(); // un-directed un-weighted graph
//or
const g = new Graph(true, false); // directed un-weighted graph.
//or
const g = new Graph(false, true); // un-directed weighted graph.
//or
const g = new Graph(true, true); // directed weighted graphg.addVertex(1);
g.addVertex(2);// You can chain methods that does not return a value
g.addVertex(1).addVertex(2); // valid
g.getNeighbors(1).addVertex(3); // invalid
g.removeVertex(5); // remove a non-existent vertex
// etc...
```functions:
- **_.addVertex(v)_** => Adds vertex to the graph.
- **_.addEdge(u, v, w: conditional)_** => Add edge between vertex "u" and vertex "v" . weight of the edge "w" is valid if only the graph is set to be weighted.
- **_.removeVertex(v)_** => Removes the vertex from the graph and removes all of the linked edges to it.
- **_.removeEdge(u, v)_** => Removes edge between vertex "u" and "v".
- **_.getVerticesNumbers()_** => Returns the number of vertices in the Graph.
- **_.getNeighbors(v)_** => Returns a the list of neighbors of vertex v
- **_.view()_** => Display a visual display of the graphs adjacency list.# Important Note:
> **_Any usage of the functions not mentioned in the functions above might lead to un-expected behavior and may lead to the functions throwing an error._**