Python icon indicating copy to clipboard operation
Python copied to clipboard

Added Splay Tree Implementation in Python with Type Hints and Example (Fixes #13844)

Open yeshuawm999 opened this issue 2 months ago • 2 comments

Describe your change:

Added a new algorithm implementation: Splay Tree under
data_structures/binary_tree/splay_tree.py.

A Splay Tree is a self-adjusting binary search tree that moves recently accessed elements closer to the root using rotations (splaying).
This improves average access time for frequently used elements.

The implementation includes:

  • Node and SplayTree classes.
  • Core rotation methods (_rotate_left, _rotate_right).
  • Complete _splay operation (Zig, Zig-Zig, Zig-Zag cases).
  • search() method that splays the found node to the root.
  • Example usage block for demonstration and testing.
  • Type hints, PEP8 formatting, and docstrings for clarity.
  • Verified to pass all pre-commit and ruff lint checks.

Fixes #13844


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, I have opened this PR only for the Splay Tree algorithm.
  • [x] The new Python file is placed inside an existing directory: data_structures/binary_tree/.
  • [x] The filename is in all lowercase characters with no spaces or dashes (splay_tree.py).
  • [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 or example blocks that pass automated testing.
  • [x] This algorithm includes a reference URL for more information (Wikipedia link provided).
  • [x] This PR resolves an open issue using a closing keyword: Fixes #13844

Example Output:

Before search: 10 Found: True After splay, new root: 5


Complexity Analysis

Operation Average Worst
Search O(log n) O(n)
Insert O(log n) O(n)
Delete O(log n) O(n)

Reference

📘 Wikipedia - Splay Tree

yeshuawm999 avatar Nov 07 '25 17:11 yeshuawm999

Hi maintainers 👋 The PR is ready for review — all checks have passed successfully. Please take a look when you get a chance. Thank you!

yeshuawm999 avatar Nov 13 '25 16:11 yeshuawm999

Hi maintainers 👋 The PR is ready for review — all checks have passed successfully. Please take a look when you get a chance. Thank you!

yeshuawm999 avatar Nov 19 '25 16:11 yeshuawm999