Skip to content

Conversation

@yeshuawm999
Copy link

Describe your change:

Added a new algorithm implementation: Splay Tree under
data_structures/binary_tree/splay_tree.py.

A Splay Tree is a self-adjusting binary search tree that moves recently accessed
elements closer to the root using rotations (splaying).
This improves average access time for frequently used elements.

The implementation includes:

  • Node and SplayTree classes.
  • Core rotation methods (_rotate_left, _rotate_right).
  • Complete _splay operation (Zig, Zig-Zig, Zig-Zag cases).
  • search() method that splays the found node to the root.
  • Example usage block for demonstration and testing.
  • Type hints, PEP8 formatting, and docstrings for clarity.
  • Verified to pass all pre-commit and ruff lint checks.

Fixes #13844


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, I have opened this PR only for the Splay Tree algorithm.
  • The new Python file is placed inside an existing directory: data_structures/binary_tree/.
  • The filename is in all lowercase characters with no spaces or dashes (splay_tree.py).
  • 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 or example blocks that pass automated testing.
  • This algorithm includes a reference URL for more information (Wikipedia link provided).
  • This PR resolves an open issue using a closing keyword: Fixes Add splay tree in python in the repo #13844

Example Output:

Before search: 10
Found: True
After splay, new root: 5


Complexity Analysis

Operation Average Worst
Search O(log n) O(n)
Insert O(log n) O(n)
Delete O(log n) O(n)

Reference

📘 Wikipedia - Splay Tree

@algorithms-keeper algorithms-keeper bot added documentation This PR modified documentation files require descriptive names This PR needs descriptive function and/or variable names require tests Tests [doctest/unittest/pytest] are required labels Nov 7, 2025
Copy link

@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.


# --- Basic Rotation Operations ---

def _rotate_left(self, x: Node) -> None:

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_left

Please provide descriptive name for the parameter: x

y.left = x
x.parent = y

def _rotate_right(self, x: Node) -> None:

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_right

Please provide descriptive name for the parameter: x


# --- Core Splay Operation ---

def _splay(self, x: Node) -> None:

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

Please provide descriptive name for the parameter: x


# --- Search Method (Uses splay) ---

def search(self, key: int) -> bool:

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 search

@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Nov 7, 2025
Copy link

@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.


# --- Basic Rotation Operations ---

def _rotate_left(self, x: Node) -> None:

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_left

Please provide descriptive name for the parameter: x

y.left = x
x.parent = y

def _rotate_right(self, x: Node) -> None:

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_right

Please provide descriptive name for the parameter: x


# --- Core Splay Operation ---

def _splay(self, x: Node) -> None:

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

Please provide descriptive name for the parameter: x


# --- Search Method (Uses splay) ---

def search(self, key: int) -> bool:

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 search

@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Nov 7, 2025
Copy link

@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.


# --- Basic Rotation Operations ---

def _rotate_left(self, x: Node) -> None:

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_left

Please provide descriptive name for the parameter: x

y.left = x
x.parent = y

def _rotate_right(self, x: Node) -> None:

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_right

Please provide descriptive name for the parameter: x


# --- Core Splay Operation ---

def _splay(self, x: Node) -> None:

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

Please provide descriptive name for the parameter: x


# --- Search Method ---

def search(self, key: int) -> bool:

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 search

Copy link

@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.


# --- Basic Rotation Operations ---

def _rotate_left(self, x: Node) -> None:

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_left

Please provide descriptive name for the parameter: x

y.left = x
x.parent = y

def _rotate_right(self, x: Node) -> None:

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_right

Please provide descriptive name for the parameter: x


# --- Core Splay Operation ---

def _splay(self, x: Node) -> None:

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

Please provide descriptive name for the parameter: x


# --- Search Method ---

def search(self, key: int) -> bool:

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 search

Copy link

@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.


# --- Basic Rotation Operations ---

def _rotate_left(self, x: Node) -> None:

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_left

Please provide descriptive name for the parameter: x

y.left = x
x.parent = y

def _rotate_right(self, x: Node) -> None:

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_right

Please provide descriptive name for the parameter: x


# --- Core Splay Operation ---

def _splay(self, x: Node) -> None:

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

Please provide descriptive name for the parameter: x


# --- Search Method ---

def search(self, key: int) -> bool:

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 search

Copy link

@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.


# --- Basic Rotation Operations ---

def _rotate_left(self, x: Node) -> None:

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_left

Please provide descriptive name for the parameter: x

y.left = x
x.parent = y

def _rotate_right(self, x: Node) -> None:

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_right

Please provide descriptive name for the parameter: x


# --- Core Splay Operation ---

def _splay(self, x: Node) -> None:

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

Please provide descriptive name for the parameter: x


# --- Search Method ---

def search(self, key: int) -> bool:

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 search

Copy link

@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.


# --- Basic Rotation Operations ---

def _rotate_left(self, x: Node) -> None:

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_left

Please provide descriptive name for the parameter: x

y.left = x
x.parent = y

def _rotate_right(self, x: Node) -> None:

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_right

Please provide descriptive name for the parameter: x


# --- Core Splay Operation ---

def _splay(self, x: Node) -> None:

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

Please provide descriptive name for the parameter: x


# --- Search Method ---

def search(self, key: int) -> bool:

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 search

@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Nov 7, 2025
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 documentation This PR modified documentation files require descriptive names This PR needs descriptive function and/or variable names require tests Tests [doctest/unittest/pytest] are required

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add splay tree in python in the repo

1 participant