Splaytree v3
Describe your change:
Added an implementation of the Splay Tree data structure in Python, located at
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 access time for frequently used elements.
The code includes:
- Node class with parent, left, and right pointers.
- Rotation methods (
_rotate_left,_rotate_right). - Core
_splayoperation implementing Zig, Zig-Zig, and Zig-Zag cases. - A
searchmethod that splays the found node to the root. - Example usage block for demonstration and testing.
Reference: Wikipedia - Splay Tree
- [x] Add an algorithm?
- [ ] Fix a bug or typo in an existing algorithm?
- [ ] Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
- [ ] 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, 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 that pass automated testing.
- [x] The implementation includes a link to a reference resource (Wikipedia).
- [x] If this pull request resolves one or more open issues, the description above includes the issue number(s) with a closing keyword.
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) |
Additional Notes
This code was tested manually to verify splay rotations and root updates.
It follows the standard Splay Tree algorithm used in self-adjusting BSTs and adheres to the repository’s coding standards.
The code i have given will work only with the provided example please verify it