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

45 lines (44 loc) 1.24 kB
import { INodeManager } from "./interfaces/INodeManager"; /** * Resources used to write this: * * https://www.youtube.com/playlist?list=PL9xmBV_5YoZNqDI8qfOZgzbqahCUmUEin * * https://www.youtube.com/watch?v=YCo2-H2CL6Q * * https://www.youtube.com/watch?v=eO3GzpCCUSg */ export declare class Tree<K, V> { private nodeManager; constructor(nodeManager: INodeManager<K, V>); /** * Add nodes to a tree one by one (for incremental updates) * * @param key * @param value Value to be associated with a key */ addNode(key: K, value: V): void; /** * Remove a node from the tree * * @param key */ removeNode(key: K): void; /** * Get the node value by target key * * @param targetKey * * @return */ getNodeValue(targetKey: K): 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): [K, V] | null; private addNodeInternal; private removeNodeInternal; private getClosestNodeInternal; private pickClosestNode; }