splaytree-ts is a TypeScript library that provides efficient implementations of Splay Tree data structures, specifically `SplayTreeMap` and `SplayTreeSet`. Splay trees are self-balancing binary search trees with the distinct characteristic that recently accessed elements are more quickly accessible again, offering O(log(n)) amortized time complexity for fundamental operations like insertion, lookup, and removal. The current stable version is 1.0.2. Its key differentiation lies in the splaying heuristic, which optimizes for temporal locality of reference, making it particularly performant for workloads with skewed access patterns. The library supports custom comparison functions and key validation predicates for flexible use with various data types, enhancing its utility beyond simple primitive comparisons.
npm install splaytree-tsVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic usage of SplayTreeSet for number operations and SplayTreeMap with a custom comparison function for object keys.
Ensure your custom `compare(a, b)` function returns a negative number if `a < b`, a positive number if `a > b`, and zero if `a == b`, and maintains consistency across all comparisons.
Provide a `compare` function to the constructor, e.g., `new SplayTreeMap((a, b) => a.id - b.id)` if `id` is the key. For deep comparison, a more complex function might be required.
Supply a `isValidKey` function to the constructor that explicitly checks for valid key types: `new SplayTreeMap(compareFn, (val) => typeof val === 'object' && val !== null)`.
Avoid modifying the tree directly within a `forEach` callback. If modifications are necessary, collect items to be modified and perform changes after the iteration completes, or use an alternative iteration strategy.
Provide a custom `compare` function to the `SplayTreeMap` or `SplayTreeSet` constructor that knows how to compare your specific key objects.
Review and correct your custom `compare` function to ensure it always returns a consistent negative, positive, or zero value based on the relative order of `a` and `b`.
Implement and supply an `isValidKey` predicate function to the `SplayTreeMap`/`SplayTreeSet` constructor to explicitly validate input key types before comparison, preventing operations with unsupported values.
No dependency data recorded yet.