Added Splay Tree Implementation in Python with Type Hints and Example (Fixes #13844)
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:
-
NodeandSplayTreeclasses. - Core rotation methods (
_rotate_left,_rotate_right). - Complete
_splayoperation (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-commitandrufflint 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
Hi maintainers 👋 The PR is ready for review — all checks have passed successfully. Please take a look when you get a chance. Thank you!
Hi maintainers 👋 The PR is ready for review — all checks have passed successfully. Please take a look when you get a chance. Thank you!