Fast Splay Tree
splaytree is a JavaScript library providing a fast, non-recursive implementation of a Splay tree data structure. It is designed for use in both Node.js environments (requiring Node.js >=18.20 or >=20) and modern browsers. The current stable version is 3.2.3, with recent patch releases indicating active maintenance and quick fixes for module resolution issues. Key differentiators include its simplicity (under 1000 lines of code) and high performance, offering amortized O(log n) time complexity for search, insert, and delete operations. It supports splitting, merging, key updates, bulk loading, and allows for optional duplicate keys. Its API is similar to w8r/avl, making it familiar to users of that library, and the implementation is adapted directly from Wikipedia's top-down splaying algorithm.
Common errors
-
TypeError: SplayTree is not a constructor
cause Attempting to instantiate `SplayTree` after an incorrect CommonJS `require()` or a named import when it's a default export.fixEnsure you are importing `SplayTree` correctly. For ESM, use `import SplayTree from 'splaytree';`. For CommonJS in some contexts, you may need `const SplayTree = require('splaytree').default;`. -
RangeError: Maximum call stack size exceeded
cause This error often indicates an infinite recursion, commonly stemming from an incorrectly implemented custom comparator function that does not consistently provide a strict weak ordering, leading to an unbalanced or malformed tree.fixCarefully review and test your custom comparator function. Ensure it handles all edge cases, returns 0 for equality, a negative number for 'a < b', and a positive number for 'a > b', and that its logic is consistent for `comparator(a,b)` and `comparator(b,a)`.
Warnings
- gotcha Incorrectly implemented comparator function can lead to a malformed tree or unpredictable search results and performance issues.
- breaking The package requires Node.js version 18.20 or 20 and above. Older Node.js versions are not supported.
- gotcha The `insert()` method allows duplicate keys, creating a new node for each insertion. The `add()` method, however, will not insert a new node if the key is already present in the tree.
- gotcha Recent patch versions (v3.2.1-v3.2.3) addressed broken `.cjs` and `.mjs` links in the distribution. While resolved, ensure your bundler or environment correctly handles dual CommonJS/ESM packages to avoid module resolution errors.
Install
-
npm install splaytree -
yarn add splaytree -
pnpm add splaytree
Imports
- SplayTree
import { SplayTree } from 'splaytree'; const SplayTree = require('splaytree');import SplayTree from 'splaytree';
- Node
import type { Node } from 'splaytree';
Quickstart
import SplayTree from "splaytree";
const tree = new SplayTree();
// Insert various key-value pairs
tree.insert(5, "apple");
tree.insert(-10, "banana");
tree.add(0, "cherry"); // Using add to prevent duplicates if 0 already existed
tree.insert(33, "date");
tree.insert(2, "elderberry");
tree.insert(5, "fig"); // Demonstrates duplicate key insertion with 'insert'
console.log("Keys in sorted order:", tree.keys());
console.log("Values in sorted order:", tree.values());
console.log("Node with key 0:", tree.find(0)?.data); // Finds data associated with key 0
console.log("Minimum key:", tree.min());
console.log("Maximum key:", tree.max());
console.log("Tree size:", tree.size);