-
-
Notifications
You must be signed in to change notification settings - Fork 49.2k
Added Splay Tree Implementation in Python with Type Hints and Example (Fixes #13844) #13883
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 8 commits
b713a49
4ba2d14
3be2619
0f53a16
9cbc0ed
59596c3
23eeffe
d41a6ea
d469992
31ef935
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| """ | ||
| Splay Tree implementation in Python. | ||
|
|
||
| A Splay Tree is a self-adjusting binary search tree where recently accessed | ||
| elements are moved closer to the root using rotations (splaying). | ||
| This improves access times for frequently used elements. | ||
|
|
||
| Reference: https://en.wikipedia.org/wiki/Splay_tree | ||
| Author: yeshuawm999 | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
| class Node: | ||
| """A node in the Splay Tree.""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| key: int, | ||
| parent: Node | None = None, | ||
| left: Node | None = None, | ||
| right: Node | None = None, | ||
| ) -> None: | ||
| self.key = key | ||
| self.parent = parent | ||
| self.left = left | ||
| self.right = right | ||
|
|
||
|
|
||
| class SplayTree: | ||
| """A self-adjusting Binary Search Tree (Splay Tree).""" | ||
|
|
||
| def __init__(self) -> None: | ||
| self.root: Node | None = None | ||
|
|
||
| # --- Basic Rotation Operations --- | ||
|
|
||
| def _rotate_left(self, x: Node) -> 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. As there is no test file in this pull request nor any test function or class in the file Please provide descriptive name for the parameter: 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. As there is no test file in this pull request nor any test function or class in the file Please provide descriptive name for the parameter: 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. As there is no test file in this pull request nor any test function or class in the file Please provide descriptive name for the parameter: 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. As there is no test file in this pull request nor any test function or class in the file Please provide descriptive name for the parameter: 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. As there is no test file in this pull request nor any test function or class in the file Please provide descriptive name for the parameter: 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. As there is no test file in this pull request nor any test function or class in the file Please provide descriptive name for the parameter: |
||
| """Perform a left rotation around node x.""" | ||
| y = x.right | ||
| if not y: | ||
| return | ||
| x.right = y.left | ||
| if y.left: | ||
| y.left.parent = x | ||
|
|
||
| y.parent = x.parent | ||
| if not x.parent: | ||
| self.root = y | ||
| elif x == x.parent.left: | ||
| x.parent.left = y | ||
| else: | ||
| x.parent.right = y | ||
|
|
||
| y.left = x | ||
| x.parent = y | ||
|
|
||
| def _rotate_right(self, x: Node) -> 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. As there is no test file in this pull request nor any test function or class in the file Please provide descriptive name for the parameter: 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. As there is no test file in this pull request nor any test function or class in the file Please provide descriptive name for the parameter: 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. As there is no test file in this pull request nor any test function or class in the file Please provide descriptive name for the parameter: 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. As there is no test file in this pull request nor any test function or class in the file Please provide descriptive name for the parameter: 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. As there is no test file in this pull request nor any test function or class in the file Please provide descriptive name for the parameter: 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. As there is no test file in this pull request nor any test function or class in the file Please provide descriptive name for the parameter: 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. As there is no test file in this pull request nor any test function or class in the file Please provide descriptive name for the parameter: |
||
| """Perform a right rotation around node x.""" | ||
| y = x.left | ||
| if not y: | ||
| return | ||
| x.left = y.right | ||
| if y.right: | ||
| y.right.parent = x | ||
|
|
||
| y.parent = x.parent | ||
| if not x.parent: | ||
| self.root = y | ||
| elif x == x.parent.right: | ||
| x.parent.right = y | ||
| else: | ||
| x.parent.left = y | ||
|
|
||
| y.right = x | ||
| x.parent = y | ||
|
|
||
| # --- Core Splay Operation --- | ||
|
|
||
| def _splay(self, x: Node) -> 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. As there is no test file in this pull request nor any test function or class in the file Please provide descriptive name for the parameter: 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. As there is no test file in this pull request nor any test function or class in the file Please provide descriptive name for the parameter: 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. As there is no test file in this pull request nor any test function or class in the file Please provide descriptive name for the parameter: 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. As there is no test file in this pull request nor any test function or class in the file Please provide descriptive name for the parameter: 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. As there is no test file in this pull request nor any test function or class in the file Please provide descriptive name for the parameter: 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. As there is no test file in this pull request nor any test function or class in the file Please provide descriptive name for the parameter: 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. As there is no test file in this pull request nor any test function or class in the file Please provide descriptive name for the parameter: |
||
| """Moves node x to the root of the tree using rotations.""" | ||
| while x.parent: | ||
| parent = x.parent | ||
| grandparent = parent.parent | ||
|
|
||
| if not grandparent: | ||
| # Zig Case (x is a child of the root) | ||
| # One single rotation: | ||
| # Right if x is a left child, Left if x is a right child | ||
| if x == parent.left: | ||
| self._rotate_right(parent) | ||
| else: | ||
| self._rotate_left(parent) | ||
| elif x == parent.left and parent == grandparent.left: | ||
| # Zig-Zig (both left) | ||
| self._rotate_right(grandparent) | ||
| self._rotate_right(parent) | ||
| elif x == parent.right and parent == grandparent.right: | ||
| # Zig-Zig (both right) | ||
| self._rotate_left(grandparent) | ||
| self._rotate_left(parent) | ||
| elif x == parent.left and parent == grandparent.right: | ||
| # Zig-Zag (left-right) | ||
| self._rotate_right(parent) | ||
| self._rotate_left(grandparent) | ||
| else: | ||
| # Zig-Zag (right-left) | ||
| self._rotate_left(parent) | ||
| self._rotate_right(grandparent) | ||
|
|
||
| # --- Search Method --- | ||
|
|
||
| def search(self, key: int) -> bool: | ||
|
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. As there is no test file in this pull request nor any test function or class in the file 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. As there is no test file in this pull request nor any test function or class in the file 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. As there is no test file in this pull request nor any test function or class in the file 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. As there is no test file in this pull request nor any test function or class in the file 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. As there is no test file in this pull request nor any test function or class in the file 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. As there is no test file in this pull request nor any test function or class in the file 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. As there is no test file in this pull request nor any test function or class in the file |
||
| """Search for a key. If found, splay it to the root.""" | ||
| current = self.root | ||
| found_node = None | ||
| while current: | ||
| if key == current.key: | ||
| found_node = current | ||
| break | ||
| current = current.left if key < current.key else current.right | ||
|
|
||
| if found_node: | ||
| self._splay(found_node) | ||
| return True | ||
| return False | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| """ | ||
| Example run: | ||
| >>> tree = SplayTree() | ||
| >>> tree.root = Node(10) | ||
| >>> tree.root.left = Node(5, parent=tree.root) | ||
| >>> tree.root.right = Node(15, parent=tree.root) | ||
| >>> print("Before search:", tree.root.key) | ||
| >>> found = tree.search(5) | ||
| >>> print("Found:", found) | ||
| >>> print("After splay, new root:", tree.root.key) | ||
| """ | ||
|
|
||
| tree = SplayTree() | ||
| tree.root = Node(10) | ||
| tree.root.left = Node(5, parent=tree.root) | ||
| tree.root.right = Node(15, parent=tree.root) | ||
|
|
||
| print("Before search:", tree.root.key) | ||
| found = tree.search(5) | ||
| print("Found:", found) | ||
| print("After splay, new root:", tree.root.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.
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_rotate_leftPlease provide descriptive name for the parameter:
x