AVL Binary search trees
An AVL Binary search tree implementation in typescript.
![MIT](https://badgen.net/badge/license/MIT/green)
This is initially foremost a research exercise, but I'm working towards a stable production-ready
implementation.
Quick links
Installation
Install the package with npm:
npm install --save avl-bst
Usage
I will add more elaborate documentation here shortly. For now, you can visit the API documentation, and the
public API page in particular for further directions on how to use this package.
Below is a brief overview of the available methods on tree instances.
You can import the package and instantiate a new tree as follows:
import AVLTree from 'avl-bst'
interface Foo {
id: number;
name: string;
}
const tree = AVLTree.create<number, Foo>((foo) => foo.id)
const scalarTree = AVLTree.scalar<number>()
Inserting values
O (log n)
tree.insert({ id: 8, name: 'a' })
tree.insert({ id: 3, name: 'b' })
tree.insert({ id: 19, name: 'c' })
tree.insert({ id: 2, name: 'd' })
tree.insert({ id: 11, name: 'e' })
tree.insert({ id: 5, name: 'f' })
tree.insert({ id: 7, name: 'g' })
tree.insert({ id: 14, name: 'h' })
tree.insert({ id: 18, name: 'i' })
tree.insert({ id: 9, name: 'j' })
tree.insert({ id: 15, name: 'k' })
tree.insert({ id: 10, name: 'l' })
tree.insert({ id: 10, name: 'm' })
Checking the size of the tree
O (1)
tree.size()
tree.isEmpty()
Searching values
O (log n)
tree.search(14)
tree.search(100)
To get the min and max values (by key):
O (log n)
tree.minValue()
tree.maxValue()
Get lists of keys or values
O (n)
tree.keys()
tree.values()
To iterate over all values, in-order:
O (n)
tree.forEach((value: Foo) => doSomething(value))
To fold / reduce the tree:
O (n)
tree.foldLeft((acc, curr) => acc + '-' + curr.name , 'NAMES')
tree.foldRight((acc, curr) => acc + '-' + curr.name , 'REVERSED-NAMES')
To delete values:
O (log n)
tree.delete(10)
tree.delete(10)
Development / build
To locally build the package, clone the repository and then run the following command.
This will build ES modules in esm/
,
CommonJS modules in cjs/
, and minified + non-minified UMD bundles in umd/
.
$ npx yarn && npx yarn build
License
The MIT license (MIT). See the license file for more information.