Python icon indicating copy to clipboard operation
Python copied to clipboard

Add Splay Tree implementation

Open Prigoistic opened this issue 2 months ago • 1 comments

Describe your change:

This PR adds a complete implementation of a Splay Tree - a self-adjusting binary search tree that provides excellent amortized performance and cache locality.

Key Features:

  • Self-adjusting binary search tree with splaying operations
  • Implements all standard BST operations (insert, delete, search, min, max)
  • Includes zig, zig-zig, and zig-zag rotation operations
  • Amortized O(log n) time complexity for all operations
  • Comprehensive doctests with 81 passing tests
  • Performance demonstration showing locality of reference
  • Includes tree visualization and traversal methods

Why Splay Tree?

  • Simpler implementation than AVL or Red-Black trees (no balance factors or colors)
  • Excellent cache performance - frequently accessed elements stay near root
  • Self-optimizing based on access patterns
  • Educational value for understanding self-balancing trees

Implementation Details:

  • Located in data_structures/binary_tree/splay_tree.py
  • Full type hints throughout
  • Detailed docstrings with examples
  • Handles edge cases (empty tree, duplicates, single node)
  • Supports any comparable data type (int, string, etc.)

Fixes #13760

  • [x] Add an algorithm?
  • [ ] Fix a bug or typo in an existing algorithm?
  • [ ] Add or change doctests?
  • [ ] Documentation change?

Checklist:

  • [x] I have read CONTRIBUTING.md.
  • [x] This pull request is all my own work -- I have not plagiarized.
  • [x] I know that pull requests will not be merged if they fail the automated tests.
  • [x] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • [x] All new Python files are placed inside an existing directory.
  • [x] All filenames are in all lowercase characters with no spaces or dashes.
  • [x] All functions and variable names follow Python naming conventions.
  • [x] All function parameters and return values are annotated with Python type hints.
  • [x] All functions have doctests that pass the automated testing.
  • [x] All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • [x] If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #13760".

Prigoistic avatar Oct 26 '25 08:10 Prigoistic

@darkstar please check the PR and merge this under hacktober fest label

Prigoistic avatar Oct 26 '25 08:10 Prigoistic