bstrees
v2.0.0
Published
Binary search trees
Downloads
32
Maintainers
Readme
bstrees
A simple library to store data in binary search trees.
Installation
npm install bstrees
Usage
BSTree
A simple binary search tree
import { BSTree } from "bstrees";
interface IOrder {
id: number;
amount: number[];
}
interface IOrders {
price: number;
orders: IOrder[];
}
const orders = new BSTree<IOrders>((a, b) => a.price - b.price);
const {
data: { orders: current },
} = orders.insert({ price: 3, orders: [] });
current.push({ id: 1, amount: 2 });
console.log(orders.array);
.insert()
const tree = new BSTree<number>();
tree.insert(3);
tree.insert(5);
tree.insert(1);
tree.insert(0);
tree.insert(4);
tree.insert(6);
.find()
const tree = new BSTree<number>();
tree.insert(3);
console.log(tree.find(2)?.data);
console.log(tree.find(3)?.data);
console.log(tree.find(2, { upsert: true })?.data);
.delete()
const tree = new BSTree<number>();
tree.insert(3);
tree.insert(5);
tree.insert(1);
tree.delete(3);
.array()
const tree = new BSTree<number>();
tree.insert(3);
tree.insert(5);
tree.insert(1);
console.log(...tree.array);
.from()
const tree = BSTree.from([3, 5, 1, 4, 0, 6]);
console.log(...tree.array);
// or with custom objects
const input = [{ id: 3 }, { id: 1 }, { id: 0 }];
function Comparator(a, b) {
return a.id < b.id ? -1 : a.id > b.id ? 1 : 0;
}
const custom_tree = BSTree.from(input, Comparator);
console.log(...custom_tree.array);
AVLTree
import { AVLTree } from "bstrees";
const tree = new AVLTree<number>();
Coverage
npm run coverage