feat: Add Splay Tree implementation#14726
Conversation
There was a problem hiding this comment.
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: | ||
| """Splay树节点类""" | ||
| def __init__(self, key: int): |
There was a problem hiding this comment.
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:
| 伸展树(Splay Tree)实现 | ||
| 特性:每次访问节点后,将该节点旋转到根位置,优化局部性访问性能 | ||
| """ | ||
| def __init__(self): |
There was a problem hiding this comment.
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: Node | None = None | ||
|
|
||
| def _right_rotate(self, x: Node) -> Node: |
There was a problem hiding this comment.
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 _right_rotate
Please provide descriptive name for the parameter: x
| y.right = x | ||
| return y | ||
|
|
||
| def _left_rotate(self, x: Node) -> Node: |
There was a problem hiding this comment.
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 _left_rotate
Please provide descriptive name for the parameter: x
| y.left = x | ||
| return y | ||
|
|
||
| def _splay(self, root: Node | None, key: int) -> Node | None: |
There was a problem hiding this comment.
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 _splay
| self.root = self._splay(self.root, current.key) | ||
| return current.key | ||
|
|
||
| def find_max(self) -> int | None: |
There was a problem hiding this comment.
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 find_max
| self.root = self._splay(self.root, current.key) | ||
| return current.key | ||
|
|
||
| def inorder_traversal(self, root: Node | None, result: list[int]) -> None: |
There was a problem hiding this comment.
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 inorder_traversal
| result.append(root.key) | ||
| self.inorder_traversal(root.right, result) | ||
|
|
||
| def get_size(self, root: Node | None) -> int: |
There was a problem hiding this comment.
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 get_size
| return 0 | ||
| return 1 + self.get_size(root.left) + self.get_size(root.right) | ||
|
|
||
| def get_height(self, root: Node | None) -> int: |
There was a problem hiding this comment.
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 get_height
| return -1 | ||
| return 1 + max(self.get_height(root.left), self.get_height(root.right)) | ||
|
|
||
| def print_tree(self, root: Node | None, indent: str = "", last: bool = True) -> None: |
There was a problem hiding this comment.
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 print_tree
This PR adds a complete implementation of Splay Tree, including:
Closes #13760
Describe your change:
Checklist: