UNPKG

@subspace/red-black-tree

Version:

This library provides advanced implementation of Red-black tree, which is a kind of self-balancing binary search tree for JavaScript

39 lines (38 loc) 1.17 kB
import { INodeManagerAsync } from "./interfaces/INodeManagerAsync"; export declare class TreeAsync<K, V> { private nodeManager; constructor(nodeManager: INodeManagerAsync<K, V>); /** * Add nodes to a tree one by one (for incremental updates) * * @param key A key to be indexed, e.g. a 32 byte piece id * @param value Value to be associated with a key */ addNode(key: K, value: V): Promise<void>; /** * Remove a node from the tree * * @param key A key to be removed, e.g. a 32 byte piece id */ removeNode(key: K): Promise<void>; /** * Get the node value by target key * * @param targetKey * * @return */ getNodeValue(targetKey: K): Promise<V | null>; /** * Get the closest node key/value in a tree to a given target key * * @param targetKey * * @return The closest key and its value to the challenge or `null` if no nodes are available */ getClosestNode(targetKey: K): Promise<[K, V] | null>; private addNodeInternal; private removeNodeInternal; private getClosestNodeInternal; private pickClosestNode; }