Noticeably Playful Monkeys

    bstrees
    TypeScript icon, indicating that this package has built-in type declarations

    1.1.3 • Public • Published

    bstrees CI Status version Known Vulnerabilities Coverage Status code style: prettier semantic-release Conventional Commits GitHub top language node version npm downloads License

    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:ci

    Install

    npm i bstrees

    DownloadsWeekly Downloads

    21

    Version

    1.1.3

    License

    AGPL-3.0-only

    Unpacked Size

    52.1 kB

    Total Files

    14

    Last publish

    Collaborators

    • vansergen