feat: Add Splay Tree implementation in Python #13870
Closed
+157
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
…ue #13844
Describe your change:
This PR introduces the Splay Tree implementation in Python, resolving issue #13844.
The Splay Tree is a self-adjusting Binary Search Tree (BST) that automatically moves frequently accessed nodes closer to the root using the splay operation (a sequence of tree rotations). This ensures that recently or frequently accessed elements can be accessed rapidly again.
Implementation Details
File Location: The implementation is added under data_structures/trees/splay_tree.py.
Structure: It includes a Node class and a SplayTree class with the core logic.
Key Methods Implemented:
_rotate(self, x): Handles single left/right rotations.
_splay(self, x): The core operation, handling zig, zig-zig, and zig-zag cases.
insert(self, key): Inserts the node and splays it to the root.
search(self, key): Searches for a key and splays the accessed node (or the last accessed node if not found) to the root.
Testing: Unit tests are provided in data_structures/trees/splay_tree_test.py to cover insertion and splay behavior.
This PR resolves the request in issue #13844.
Checklist: