Skip to content

feat: Add Splay Tree implementation#14726

Open
JKjk555 wants to merge 1 commit into
TheAlgorithms:masterfrom
JKjk555:add-splay-tree
Open

feat: Add Splay Tree implementation#14726
JKjk555 wants to merge 1 commit into
TheAlgorithms:masterfrom
JKjk555:add-splay-tree

Conversation

@JKjk555
Copy link
Copy Markdown

@JKjk555 JKjk555 commented May 27, 2026

This PR adds a complete implementation of Splay Tree, including:

  • Core splay operations (zig, zig-zig, zig-zag rotations)
  • Standard BST operations: insert, delete, search, find min/max
  • In-order traversal, size/height calculation, tree visualization
  • Test cases demonstrating all functionalities

Closes #13760

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

@algorithms-keeper algorithms-keeper Bot added require descriptive names This PR needs descriptive function and/or variable names require tests Tests [doctest/unittest/pytest] are required require type hints https://docs.python.org/3/library/typing.html labels May 27, 2026
Copy link
Copy Markdown

@algorithms-keeper algorithms-keeper Bot left a 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 review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to 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):
Copy link
Copy Markdown

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:

伸展树(Splay Tree)实现
特性:每次访问节点后,将该节点旋转到根位置,优化局部性访问性能
"""
def __init__(self):
Copy link
Copy Markdown

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: Node | None = None

def _right_rotate(self, x: Node) -> Node:
Copy link
Copy Markdown

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 _right_rotate

Please provide descriptive name for the parameter: x

y.right = x
return y

def _left_rotate(self, x: Node) -> Node:
Copy link
Copy Markdown

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 _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:
Copy link
Copy Markdown

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 _splay

self.root = self._splay(self.root, current.key)
return current.key

def find_max(self) -> int | None:
Copy link
Copy Markdown

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 find_max

self.root = self._splay(self.root, current.key)
return current.key

def inorder_traversal(self, root: Node | None, result: list[int]) -> None:
Copy link
Copy Markdown

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 inorder_traversal

result.append(root.key)
self.inorder_traversal(root.right, result)

def get_size(self, root: Node | None) -> int:
Copy link
Copy Markdown

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 get_size

return 0
return 1 + self.get_size(root.left) + self.get_size(root.right)

def get_height(self, root: Node | None) -> int:
Copy link
Copy Markdown

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 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:
Copy link
Copy Markdown

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 print_tree

@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed tests are failing Do not merge until tests pass labels May 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed require descriptive names This PR needs descriptive function and/or variable names require tests Tests [doctest/unittest/pytest] are required require type hints https://docs.python.org/3/library/typing.html tests are failing Do not merge until tests pass

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Splay Trees implantation ( Self Adjusting BST)

1 participant