-
-
Notifications
You must be signed in to change notification settings - Fork 49.1k
feat: Add Splay Tree implementation to data_structures/trees/ for issue #13844 #13872
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add Splay Tree implementation to data_structures/trees/ for issue #13844 #13872
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
|
|
||
| class Node: | ||
| """A single node in the Splay Tree.""" | ||
| def __init__(self, key, parent=None, left=None, right=None): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide return type hint for the function: __init__. If the function does not return a value, please provide the type hint as: def function() -> None:
Please provide type hint for the parameter: key
Please provide type hint for the parameter: parent
Please provide type hint for the parameter: left
Please provide type hint for the parameter: right
| A self-adjusting Binary Search Tree (BST) that uses the splay operation | ||
| to move the most recently accessed node to the root of the tree. | ||
| """ | ||
| def __init__(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide return type hint for the function: __init__. If the function does not return a value, please provide the type hint as: def function() -> None:
| def __init__(self): | ||
| self.root = None | ||
|
|
||
| def _rotate(self, x: Node): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide return type hint for the function: _rotate. If the function does not return a value, please provide the type hint as: def function() -> None:
Please provide descriptive name for the parameter: x
| else: | ||
| self.root = x # x is the new root | ||
|
|
||
| def _splay(self, x: Node): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide return type hint for the function: _splay. If the function does not return a value, please provide the type hint as: def function() -> None:
Please provide descriptive name for the parameter: x
| self._rotate(x) # Rotate x first | ||
| self._rotate(x) # Then rotate x again | ||
|
|
||
| def search(self, key): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide return type hint for the function: search. If the function does not return a value, please provide the type hint as: def function() -> None:
Please provide type hint for the parameter: key
| self._splay(last) # Splay the last accessed node if key was not found | ||
| return None | ||
|
|
||
| def insert(self, key): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide return type hint for the function: insert. If the function does not return a value, please provide the type hint as: def function() -> None:
Please provide type hint for the parameter: key
for more information, see https://pre-commit.ci
…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.
Linked Issue:
This PR resolves the request in issue #13844.
Checklist: