-
- Notifications
You must be signed in to change notification settings - Fork 49.2k
Add Splay Tree implementation #13760 [hacktoberfest] #13817
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
This PR adds a Splay Tree (Self Adjusting BST) implementation in Python under the `data_structures/binary_tree/` directory. ## Details: - Implements the Splay Tree data structure with all core operations: insertion, search, splay rotation, and inorder traversal. - Includes class-based design (`Node`, `SplayTree`) and follows Python community style. - Provides an example usage and test in the `__main__` block. ## Why Splay Tree? A Splay Tree is a self-adjusting binary search tree that provides fast access to recently used elements by moving them closer to the root through 'splaying' operations. Frequently accessed nodes move towards the top, which is useful for caching and memory management systems. ## Issue Reference This solves [Issue #13760](#13760). ## Hacktoberfest Marking this contribution for Hacktoberfest 2025. **For Maintainers:** Please add the `hacktoberfest-accepted` label to count this PR towards Hacktoberfest. ## Checklist - [x] Follows contribution guidelines - [x] Code is documented with docstrings and inline comments - [x] Includes an example/test case - [x] New file in correct directory --- Feel free to use or modify the text above for your commit and pull request! If you want it in Hinglish or need a quick summary, let me know.
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -12,13 +12,18 @@ | |
| self.parent = None | ||
| self.key = key | ||
| | ||
| def __repr__(self): | ||
| return f"Node({self.key})" | ||
| | ||
| | ||
| class SplayTree: | ||
| def __init__(self): | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: | ||
| self.root = None | ||
| | ||
| def _right_rotate(self, x): | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: Please provide descriptive name for the parameter: | ||
| y = x.left | ||
| if not y: | ||
| return | ||
| x.left = y.right | ||
| if y.right: | ||
| y.right.parent = x | ||
| | @@ -34,6 +39,8 @@ | |
| | ||
| def _left_rotate(self, x): | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: Please provide descriptive name for the parameter: | ||
| y = x.right | ||
| if not y: | ||
| return | ||
| x.right = y.left | ||
| if y.left: | ||
| y.left.parent = x | ||
| | @@ -48,35 +55,40 @@ | |
| x.parent = y | ||
| | ||
| def _splay(self, x): | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: Please provide descriptive name for the parameter: | ||
| while x.parent: | ||
| if not x.parent.parent: | ||
| if x.parent.left == x: | ||
| self._right_rotate(x.parent) | ||
| while x and x.parent: | ||
| p = x.parent | ||
| g = p.parent | ||
| # Zig step (p is root) | ||
| if not g: | ||
| if p.left == x: | ||
| self._right_rotate(p) | ||
| else: | ||
| self._left_rotate(x.parent) | ||
| self._left_rotate(p) | ||
| else: | ||
| if x.parent.left == x and x.parent.parent.left == x.parent: | ||
| self._right_rotate(x.parent.parent) | ||
| self._right_rotate(x.parent) | ||
| elif x.parent.right == x and x.parent.parent.right == x.parent: | ||
| self._left_rotate(x.parent.parent) | ||
| self._left_rotate(x.parent) | ||
| elif x.parent.left == x and x.parent.parent.right == x.parent: | ||
| self._right_rotate(x.parent) | ||
| self._left_rotate(x.parent) | ||
| else: | ||
| self._left_rotate(x.parent) | ||
| self._right_rotate(x.parent) | ||
| # Zig-Zig | ||
| if p.left == x and g.left == p: | ||
| self._right_rotate(g) | ||
| self._right_rotate(p) | ||
| elif p.right == x and g.right == p: | ||
| self._left_rotate(g) | ||
| self._left_rotate(p) | ||
| # Zig-Zag | ||
| elif p.left == x and g.right == p: | ||
| self._right_rotate(p) | ||
| self._left_rotate(g) | ||
| else: # p.right == x and g.left == p | ||
| self._left_rotate(p) | ||
| self._right_rotate(g) | ||
| | ||
| def insert(self, key): | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: | ||
| z = self.root | ||
| p = None | ||
| while z: | ||
| p = z | ||
| if key < z.key: | ||
| z = z.left | ||
| else: | ||
| z = z.right | ||
| z = Node(key) | ||
| z.parent = p | ||
| if not p: | ||
| | @@ -89,26 +101,29 @@ | |
| | ||
| def search(self, key): | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: | ||
| z = self.root | ||
| last = None | ||
| while z: | ||
| last = z | ||
| if key == z.key: | ||
| self._splay(z) | ||
| return z | ||
| elif key < z.key: | ||
| if not z.left: | ||
| self._splay(z) | ||
| return None | ||
| z = z.left | ||
| else: | ||
| if not z.right: | ||
| self._splay(z) | ||
| return None | ||
| z = z.right | ||
| # splay the last accessed node (closest) if present | ||
| if last: | ||
| self._splay(last) | ||
| return None | ||
| | ||
| def inorder(self, node=None, result=None): | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: Please provide type hint for the parameter: | ||
| if result is None: | ||
| result = [] | ||
| node = node or self.root | ||
| # if node is explicitly passed as None and tree is empty, return empty result | ||
| if node is None: | ||
| node = self.root | ||
| if node is None: | ||
| return result | ||
| if node.left: | ||
| self.inorder(node.left, result) | ||
| result.append(node.key) | ||
| | @@ -120,6 +135,9 @@ | |
| # Example Usage / Test | ||
| if __name__ == "__main__": | ||
| tree = SplayTree() | ||
| # empty tree -> inorder should return [] | ||
| print(tree.inorder()) # [] | ||
| | ||
| for key in [10, 20, 30, 40, 50, 25]: | ||
| tree.insert(key) | ||
| print(tree.inorder()) # Output should be the inorder traversal of tree | ||
| | ||
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:
__repr__. If the function does not return a value, please provide the type hint as:def function() -> None:As there is no test file in this pull request nor any test function or class in the file
data_structures/binary_tree/splay_tree.py, please provide doctest for the function__repr__