algorithmlib
v2.0.4
Published
Data Structure & Algorithm library written with Typescript
Downloads
8
Maintainers
Readme
Overview
Algorithmlib is an algorithm library that supports Common. js and ES module syntax. It can be applied to Node.js projects or projects built through ES modules to reduce the time spent in handwriting data structures or algorithms in JavaScript or TypeScript projects.
Quick Start Guide
Install
npm install algorithmlib
Demo
- Import
Stack
through ES module syntax
import { Stack } from 'algorithmlib'
let a = new Stack()
a.push('element')
a.pop()
- Import
Stack
through Common.js syntax
const { Stack } = require('algorithmlib')
let a = new Stack()
a.push('element')
a.pop()
- Import
Stack
through<script>
tag
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="./node_modules/algorithmlib/dist/index.iife.js"></script>
</head>
<body>
<script>
const { Stack } = algorithmlib
let a = new Stack()
a.push('element')
a.pop()
</script>
</body>
</html>
API Reference
Stack<T>
- pop(): T | undefined
- push(element: T): Stack<T>
- isEmpty(): boolean
Queue<T>
- dequeue(): T | undefined
- enqueue(element: T): Queue<T>
- isEmpty(): boolean
LinkedList<T>
- indexOf(element: T): number
- insert(element: T, index?: number): LinkedList<T>
- remove(index: number): boolean
- isEmpty(): boolean
HashTable<K extends number | string, V>
- get(key: K): V | undefined
- set(key: K, value: V): HashTable<K, V>
- remove(key: K): boolean
- isEmpty(): boolean
BinarySearchTree<T>
- min(): BinarySearchTreeNode<T> | null
- max(): BinarySearchTreeNode<T> | null
- minNode(node: BinarySearchTreeNode<T> | null): BinarySearchTreeNode<T> | null
- maxNode(node: BinarySearchTreeNode<T> | null): BinarySearchTreeNode<T> | null
- insert(index: number, element: T): BinarySearchTree<T>
- search(index: number): boolean
- remove(index: number): boolean
- isEmpty(): boolean
Binary Tree Traversal
- preOrderTraversal: <T>(tree: BinarySearchTree<T>) => { index: number; element: T }[]
- postOrderTraversal: <T>(tree: BinarySearchTree<T>) => { index: number; element: T }[]
- inOrderTraversal: <T>(tree: BinarySearchTree<T>) => { index: number; element: T }[]
- levelOrderTraversal: <T>(tree: BinarySearchTree<T>) => { index: number; element: T }[][]
Graph<K>
- addVertex(...vertices: K[]): Graph<K>
- addEdge(v: K, edges: Array<[K, number | null]>): Graph<K>
- removeVertex(v: K): Graph<K>
- removeEdge(v: K, w: K): Graph<K>
Graph Traversal
- breadthFirstSearch: <K>(graph: Graph<K>, startVertex: K) => K[]
- depthFirstSearch: <K>(graph: Graph<K>, startVertex: K) => K[]
Sort Algorithm
- bubbleSort<T>: (array: T[], compare?: sortedCallbackFuction<T>) => T[];
- insertSort<T>: (array: T[], compare?: sortedCallbackFuction<T>) => T[];
- quickSort<T>: (array: T[], compare?: sortedCallbackFuction<T>) => T[];
- mergeSort<T>: (array: T[], compare?: sortedCallbackFuction<T>) => T[];
Search Algorithm
- binarySearch<T>: (array: T[], target: number | { [key: string]: number }) => number | boolean
License
Copyright (c) 2023-present Steve Yang