javascript-algorithms icon indicating copy to clipboard operation
javascript-algorithms copied to clipboard

[feat] New Data Structure: Splay Tree Implementation

Open SamXop123 opened this issue 2 months ago • 0 comments

Splay Tree Implementation

Description

This PR introduces a comprehensive implementation of a Splay Tree, a self-adjusting binary search tree with excellent performance characteristics for many use cases. The implementation includes all standard operations with proper splaying behavior.

Features

  • Core Operations: Insert, find, contains, remove
  • Tree Traversals: In-order, pre-order, post-order
  • Utility Methods: findMin, findMax, getHeight, isEmpty
  • Splaying Operations: zig, zig-zig, zig-zag rotations
  • Custom Comparators: Support for custom comparison functions
  • Comprehensive Test Coverage: 91.77% statement coverage, 85.63% branch coverage

Implementation Details

  • SplayTreeNode.js: Node class with rotation and splaying logic
  • SplayTree.js: Main tree class with public API
  • Test Files: 45+ test cases covering all functionality

Performance

  • Average Case: O(log n) for search, insert, and delete
  • Amortized Analysis: O(log n) per operation

Example Usage

const tree = new SplayTree();
tree.insert(10);
tree.insert(5);
tree.insert(15);
console.log(tree.contains(5)); // true
console.log(tree.findMin());  // 5
console.log(tree.findMax());  // 15

Checklist

  • [x] Implement core functionality
  • [x] Add comprehensive test cases
  • [x] Add JSDoc comments
  • [x] Update README with usage and documentation
  • [x] Ensure proper error handling

Related Issues

Closes #271

SamXop123 avatar Dec 03 '25 09:12 SamXop123