Fast Splay Tree

3.2.3 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This example demonstrates creating a SplayTree, inserting and adding elements, retrieving keys and values, finding specific nodes, and checking the min/max keys, showcasing basic tree operations.

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);

view raw JSON →