Binary search trees
npm install bstreesA simple library to store data in binary search trees.
``bash`
npm install bstrees
A simple binary search tree
`typescript
import { BSTree } from "bstrees";
interface IOrder {
id: number;
amount: number[];
}
interface IOrders {
price: number;
orders: IOrder[];
}
const orders = new BSTree
const {
data: { orders: current },
} = orders.insert({ price: 3, orders: [] });
current.push({ id: 1, amount: 2 });
console.log(orders.array);
`
- .insert()
`typescript`
const tree = new BSTree
tree.insert(3);
tree.insert(5);
tree.insert(1);
tree.insert(0);
tree.insert(4);
tree.insert(6);
- .find()
`typescript`
const tree = new BSTree
tree.insert(3);
console.log(tree.find(2)?.data);
console.log(tree.find(3)?.data);
console.log(tree.find(2, { upsert: true })?.data);
- .delete()
`typescript`
const tree = new BSTree
tree.insert(3);
tree.insert(5);
tree.insert(1);
tree.delete(3);
- .array()
`typescript`
const tree = new BSTree
tree.insert(3);
tree.insert(5);
tree.insert(1);
console.log(...tree.array);
- .from()
`typescript`
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);
`typescript`
import { AVLTree } from "bstrees";
const tree = new AVLTree
`bash``
npm run coverage