bstrees
    Preparing search index...

    Class BSTree<T>

    An unbalanced binary search tree.

    Operations take O(h) time, where h is the height of the tree: O(log n) on random input, but O(n) when values are inserted in sorted order. Many random removals mixed with insertions also make it deeper over time. Prefer AVLTree unless the input order is known to be random.

    Like Array.from, from() called on a subclass creates an instance of that subclass. TypeScript requires the subclass constructor to accept the comparator as its only argument and the values to match the subclass type.

    const tree = new BSTree<number>();
    tree.insert(3);
    tree.insert(1);
    tree.toArray(); // [1, 3]

    Type Parameters

    • T

    Hierarchy (View Summary)

    Index
    • Type Parameters

      • T

      Parameters

      • compare: Comparator<T>

        The sort order. Optional only for numbers, strings and bigints. In generic code where TypeScript cannot tell that T is one of them (T extends number), pass a comparator or create the tree with BSTree.from<T>([]).

      Returns BSTree<T>

      If compare is neither a function nor undefined.

    • Type Parameters

      • T

      Parameters

      Returns BSTree<T>

    • get height(): number

      The number of edges on the longest path from the root to a leaf: -1 for an empty tree and 0 for a tree with a single value. O(1).

      Returns number

    • get size(): number

      The number of values in the tree. O(1).

      Returns number

    • Returns the least value greater than or equal to value.

      Parameters

      • value: T

      Returns T | undefined

      If the comparator rejects the value.

    • Removes the value equal to value.

      Parameters

      • value: T

      Returns boolean

      true if a value was removed.

      If the comparator rejects the value.

    • Returns the greatest value less than or equal to value.

      Parameters

      • value: T

      Returns T | undefined

      If the comparator rejects the value.

    • Calls callback for every value in ascending order.

      Parameters

      • callback: (value: T, index: number, tree: this) => void
      • OptionalthisArg: unknown

      Returns void

    • Returns the stored value equal to value. Useful when the comparator only looks at a key: pass a probe with that key to get the stored record.

      Parameters

      • value: T

      Returns T | undefined

      const users = new AVLTree<{ id: number; name: string }>((a, b) => a.id - b.id);
      users.insert({ id: 1, name: "Alice" });
      users.get({ id: 1, name: "" })?.name; // "Alice"

      If the comparator rejects the value.

    • Returns the stored value equal to value, inserting value first if there is none.

      Parameters

      • value: T

      Returns T

      const book = new AVLTree<{ price: number; orders: number[] }>(
      (a, b) => a.price - b.price,
      );
      book.getOrInsert({ price: 10, orders: [] }).orders.push(1);
      book.getOrInsert({ price: 10, orders: [] }).orders.push(2);
      book.get({ price: 10, orders: [] })?.orders; // [1, 2]

      If the comparator rejects the value.

    • Checks whether the tree contains a value equal to value.

      Parameters

      • value: T

      Returns boolean

      If the comparator rejects the value.

    • Returns the least value strictly greater than value.

      Parameters

      • value: T

      Returns T | undefined

      If the comparator rejects the value.

    • Inserts value unless an equal value is already stored (that value is kept as is).

      Parameters

      • value: T

      Returns boolean

      true if the value was inserted.

      If the comparator rejects the value.

    • Returns the greatest value strictly less than value.

      Parameters

      • value: T

      Returns T | undefined

      If the comparator rejects the value.

    • Returns the largest value, or undefined if the tree is empty.

      Returns T | undefined

    • Returns the smallest value, or undefined if the tree is empty.

      Returns T | undefined

    • Iterates over the values within the bounds, in ascending order unless reverse is set. Modifications during the iteration follow the same rules as for SortedTree.values.

      Parameters

      Returns TreeIterator<T>

      const tree = AVLTree.from([1, 2, 3, 4, 5]);
      [...tree.range({ gte: 2, lt: 4 })]; // [2, 3]
      [...tree.range({ gt: 3, reverse: true })]; // [5, 4]

      If both gt and gte (or lt and lte) are set, or if the comparator rejects a bound. When range is called, each bound is compared with itself and, if the tree is not empty, with a stored value, so an invalid bound throws before the iteration starts.

    • Iterates over the values in ascending order.

      The tree may be modified during the iteration: removed values are skipped, values inserted ahead of the iterator are visited and values inserted behind it are not. Unlike with a Set, a value is never visited twice, even if it is deleted and inserted again.

      After the tree changes, the iterator finds its place again by comparing with the last value it returned. While you use the iterator, do not change the fields the comparator reads of that value, even after removing it from the tree.

      Returns TreeIterator<T>

    • Creates a tree from numbers, strings or bigints.

      Parameters

      • this: new (compare?: Comparator<number>) => BSTree<number>
      • values: Iterable<number>

      Returns BSTree<number>

    • Creates a tree from numbers, strings or bigints.

      Parameters

      • this: new (compare?: Comparator<string>) => BSTree<string>
      • values: Iterable<string>

      Returns BSTree<string>

    • Creates a tree from numbers, strings or bigints.

      Parameters

      • this: new (compare?: Comparator<bigint>) => BSTree<bigint>
      • values: Iterable<bigint>

      Returns BSTree<bigint>

    • Creates a tree from any values sorted by compare.

      Type Parameters

      • T

      Parameters

      Returns BSTree<T>

    • Creates a tree from bigints of an explicit type, also in generic code (BSTree.from<T>(values) with T extends bigint).

      Type Parameters

      • T extends bigint

      Parameters

      Returns BSTree<T>

    • Creates a tree from numbers of an explicit type, such as BSTree.from<1 | 2>([1, 2]), also in generic code (BSTree.from<T>(values) with T extends number).

      Type Parameters

      • T extends number

      Parameters

      Returns BSTree<T>

    • Creates a tree from strings of an explicit type, also in generic code (BSTree.from<T>(values) with T extends string).

      Type Parameters

      • T extends string

      Parameters

      Returns BSTree<T>