gis-tools-ts
Version:
A collection of geospatial tools primarily designed for WGS84, Web Mercator, and S2.
97 lines • 3.29 kB
TypeScript
import type { PriorityCompare } from './priorityQueue.js';
/** A basic node for a splay tree */
export declare class SplayTreeNode<T> {
readonly key: T;
left?: SplayTreeNode<T>;
right?: SplayTreeNode<T>;
/** @param key - the element to store */
constructor(key: T);
}
/**
* # Splay Tree Set
*
* ## Description
* A splay tree set is a self-balancing binary search tree that does not allow duplicate elements.
* The value of a splay tree is in it's amortized O(log n) for all insert, delete min/max,
* and find min/max operations.
*
* ## Usage
*
* ```ts
* import { SplayTreeSet } from 'gis-tools-ts';
*
* const tree = new SplayTreeSet<number>([], (a, b) => a - b);
*
* // If the element already exists, the existing element will be returned otherwise
* // the new element will be both added and returned
* let element = tree.add(1);
* tree.add(2);
*
* console.log(tree.length); // 2
* // Get first and last elements
* let firstElement = tree.first(); // 1
* let lastElement = tree.last(); // 2
* // check if a value exists
* console.log(tree.has(1)); // true
* // look for a value right before one provided
* console.log(tree.lastBefore(2)); // 1
* // look for a value right after one provided
* console.log(tree.firstAfter(1)); // 2
* ```
*
* ## Links
* - https://en.wikipedia.org/wiki/Splay_tree
* - [Splay Tree Visualizer](http://slmoore.github.io/SplayTreeVisualizer/)
* - [Visualizer Source Code](https://github.com/slmoore/SplayTreeVisualizer)
*/
export declare class SplayTreeSet<T> {
#private;
private compare;
/** @param compare - compare function */
constructor(compare?: PriorityCompare<T>);
/** @returns - the number of elements in the tree */
get length(): number;
/**
* Add an element
* @param element - the element to add
* @returns - the added element OR if the element already exists, the existing element
*/
add(element: T): T;
/**
* Check if an element exists
* @param key - the element
* @returns - true if the element exists
*/
has(key: T): boolean;
/**
* Delete an element. Return the deleted element if it exists otherwise undefined
* @param key - the element
* @returns - the deleted element
*/
delete(key: T): SplayTreeNode<T> | undefined;
/**
* Get the first element
* @returns - the first element, undefined if the queue is empty
*/
first(): T | undefined;
/**
* Get the last element
* @returns - the last element, undefined if the queue is empty
*/
last(): T | undefined;
/**
* Get the last element in the set that is strictly smaller than element.
* Returns undefined if no element was not found.
* @param element - the element to compare against
* @returns - the last element before the comparison element provided
*/
lastBefore(element: T): T | undefined;
/**
* Get the first element in the set that is strictly larger than element.
* Returns undefined if no element was not found.
* @param element - the element to compare against
* @returns - the first element after the comparison element provided
*/
firstAfter(element: T): T | undefined;
}
//# sourceMappingURL=splayTree.d.ts.map