Common algorithms and data structures.
Written in TypeScript, tested with Jest.
Documentation
Documentation app: raikuxq-algorithms.netlify.app/guide
Usage as package
Install by using any of these commands:
yarn add @raikuxq/alg-ds
npm install @raikuxq/alg-ds --save
Usage as repository
Clone this repository and install dependencies by using yarn
command.
yarn test
- run all tests via jest
yarn dev
- run in dev mode via nodemon (src/index.ts is an entrypoint)
yarn build
- compile ts sources into js files
yarn start
- build and run in production mode
yarn lint
- lint check via eslint
yarn lint:fix
- fix source files via eslint
Navigation
Algorithms
Utils
memoize ā Memoization util function.
Searching
binary-search [ tests ] ā Binary searching algorithm. Time: O(log(n)).
Math
factorial [ tests ] ā Recursive O(n!) and memoized O(n) factorial implementation.
fibonacci [ tests ] ā Recursive O(n!) and memoized O(n) factorial implementation.
transpose-matrix [ tests ] ā Transpose N*N matrix util function.
Sorting algorithms
bubble-sort [ tests ] ā Time: O(n^2) worst, O(n^2) avg, O(n) best. Memory: O(1) worst.
selection-sort [ tests ] ā Time: O(n^2) worst, O(n^2) avg, O(n^2) best. Memory: O(1) worst.
insertion-sort [ tests ] ā Time: O(n^2) worst, O(n^2) avg, O(n) best. Memory: O(1) worst.
merge-sort [ tests ] ā Time: O(nlog(n)) worst, O(nlog(n)) avg, O(n*log(n)) best. Memory: O(n) worst.
quick-sort [ tests ] ā Time: O(n^2) worst, O(nlog(n)) avg, O(nlog(n)) best. Memory: O(1) worst.
Linear data structures
Interfaces
ILinearStorage ā Contains common methods of linear data structures.
ILinearStorageRA ā Allows random access (from end, from start, by index).
Extends ILinearStorage interface.
IConvertableToArray ā Contain methods for converting from/into array.
Linked List
Interfaces
ILinkedList ā Contains basic linked lists operations.
Extends ILinearStorageRA and IConvertableToArray interface.
Implementation
AbstractLinkedList ā Common logic for both single and double linked lists.
Implements ILinearStorageRA interface.
SingleLinkedList
[ tests ]
ā Extends abstract linked list with implementation of one-way linking.
DoubleLinkedList
[ tests ]
ā Extends abstract linked list with implementation of two-way linking.
IterableSingleLinkedList
[ tests ]
ā Extends single linked list with iterator implementation.
Implements IIterable interface.
IterableDoubleLinkedList
[ tests ]
ā Extends double linked list with implementation of two-way linking.
Implements IBiDirectIterable interface.
Looped Array
Interfaces
IArrayFacade ā Contains basic array operations.
Extends ILinearStorageRA interface.
Extends IConvertableToArray interface.
Implementation
LoopedArray [ tests ]
ā Overwrites data on capacity overflow.
Stack
Implementation
Stack [ tests ]
ā LIFO data structure. Implements ILinearStorage interface.
Queue
Implementation
Queue [ tests ]
ā FIFO data structure. Implements ILinearStorage interface.
Non linear data structures
HASH Table
Interfaces
IKeyValueStorage ā Contains basic key-value storages operations.
Implementation
HashTableNode ā Contains key, data and isDeleted properties.
HashTable [ tests ] ā Implementation of open addressing hash table using quadratic probing
Graph
Interfaces
IGraph ā Contains basic graph operations.
IGraphIterator ā Extends default iterator with init and getPath methods.
IGraphIterationStrategy ā Iteration strategies which are used in shortest-path, has-path.
Implementation
GraphEdge ā Contains from/to links and edge weight.
AbstractGraph ā Common logic for both directed and undirected graphs.
DirectedGraph [ tests ]
ā In case of directed graph A->B and B->A edges are not the same.
UndirectedGraph [ tests ]
ā In case of undirected graph A->B and B->A are equal.
Graph Iterators
BreadthFirstSearchIterator
ā Traversal method for unweighted graphs, built on queue.
DepthFirstSearchIterator
ā Traversal method for unweighted graphs, built on stack.
DijkstraMethodIterator
ā Traversal method for weighted graphs, built on finding the minimal cost.
Graph Presenter
presenter-adjacency-lists [ tests ]
ā Representation of graph as an adjacency list (using Map).
presenter-adjacency-matrix [ tests ]
ā Representation of graph as an adjacency matrix (using Array N*N).
Graph Searching
has-path (BFS/DFS) [ tests ]
ā Search for the existence of a path between two vertices.
shortest-path (BFS/Dijkstra) [ tests ]
ā Search for one of several shortest paths between two vertices.
Graph Creators
create-graph-from-matrix [ tests ]
ā Convert a matrix N*N into a graph instance.
Graph Transposing
transpose-directed-graph [ tests ]
ā Transpose a directed graph (undirected graphs are symmetrical already).
Binary trees
IBinaryTree ā Contains basic binary tree operations.
Implementation
AbstractBinaryNode ā Contains left/right/parent links and node data.
AbstractBinaryTree ā Common logic for all types of binary trees.
BinarySearchNode ā Same as abstract binary node.
BinarySearchTree ā Implementation of unbalanced binary search tree.
Each node in left subtree is smaller and each node in right subtree is larger than the node data.
Extends AbstractSearchTree.
RandBinarySearchNode ā Have a rank attribute.
Extends BinarySearchNode.
RandBinarySearchTree
ā Implementation of randomized binary search tree, which gives expected log(N) height.
INSERTION have a 1/N+1 probability of inserting into root.
Extends BinarySearchTree.