Skip to content

Conversation

@yeshuawm999
Copy link

Describe your change:

Added an implementation of the Splay Tree data structure in Python, located at
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 access time for frequently used elements.

The code includes:

  • Node class with parent, left, and right pointers.
  • Rotation methods (_rotate_left, _rotate_right).
  • Core _splay operation implementing Zig, Zig-Zig, and Zig-Zag cases.
  • A search method that splays the found node to the root.
  • Example usage block for demonstration and testing.

Reference: Wikipedia - Splay Tree


  • 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, 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 that pass automated testing.
  • The implementation includes a link to a reference resource (Wikipedia).
  • If this pull request resolves one or more open issues, the description above includes the issue number(s) with a closing keyword.

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)

Additional Notes

This code was tested manually to verify splay rotations and root updates.
It follows the standard Splay Tree algorithm used in self-adjusting BSTs and adheres to the repository’s coding standards.

@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Oct 31, 2025
@yeshuawm999
Copy link
Author

The code i have given will work only with the provided example
please verify it

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

#class node
class Node:
"""A node in the Splay Tree."""
def __init__(self, key, parent=None, left=None, right=None):

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:

Please provide type hint for the parameter: key

Please provide type hint for the parameter: parent

Please provide type hint for the parameter: left

Please provide type hint for the parameter: right

#Spary Tree class
class SplayTree:
"""A self-adjusting Binary Search Tree."""
def __init__(self):

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:


# --- Basic Rotation Operations ---

def _rotate_left(self, x):

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 return type hint for the function: _rotate_left. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide descriptive name for the parameter: x

Please provide type hint for the parameter: x

y.left = x
x.parent = y

def _rotate_right(self, x):

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 return type hint for the function: _rotate_right. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide descriptive name for the parameter: x

Please provide type hint for the parameter: x


# --- Core Splay Operation ---

def _splay(self, x):

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 return type hint for the function: _splay. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide descriptive name for the parameter: x

Please provide type hint for the parameter: x


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

def search(self, key):

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

Please provide return type hint for the function: search. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: key

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

class Node:
"""A node in the Splay Tree."""

def __init__(self, key, parent=None, left=None, right=None):

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:

Please provide type hint for the parameter: key

Please provide type hint for the parameter: parent

Please provide type hint for the parameter: left

Please provide type hint for the parameter: right

class SplayTree:
"""A self-adjusting Binary Search Tree."""

def __init__(self):

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:


# --- Basic Rotation Operations ---

def _rotate_left(self, x):

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 return type hint for the function: _rotate_left. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide descriptive name for the parameter: x

Please provide type hint for the parameter: x

y.left = x
x.parent = y

def _rotate_right(self, x):

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 return type hint for the function: _rotate_right. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide descriptive name for the parameter: x

Please provide type hint for the parameter: x


# --- Core Splay Operation ---

def _splay(self, x):

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 return type hint for the function: _splay. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide descriptive name for the parameter: x

Please provide type hint for the parameter: x


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

def search(self, key):

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

Please provide return type hint for the function: search. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: key

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.

#class node
class Node:
"""A node in the Splay Tree."""
def __init__(self, key, parent=None, left=None, right=None):

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:

Please provide type hint for the parameter: key

Please provide type hint for the parameter: parent

Please provide type hint for the parameter: left

Please provide type hint for the parameter: right

#Spary Tree class
class SplayTree:
"""A self-adjusting Binary Search Tree."""
def __init__(self):

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:


# --- Basic Rotation Operations ---

def _rotate_left(self, x):

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 return type hint for the function: _rotate_left. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide descriptive name for the parameter: x

Please provide type hint for the parameter: x

y.left = x
x.parent = y

def _rotate_right(self, x):

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 return type hint for the function: _rotate_right. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide descriptive name for the parameter: x

Please provide type hint for the parameter: x


# --- Core Splay Operation ---

def _splay(self, x):

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 return type hint for the function: _splay. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide descriptive name for the parameter: x

Please provide type hint for the parameter: x


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

def search(self, key):

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

Please provide return type hint for the function: search. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: key

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.

#class node
class Node:
"""A node in the Splay Tree."""
def __init__(self, key, parent=None, left=None, right=None):

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:

Please provide type hint for the parameter: key

Please provide type hint for the parameter: parent

Please provide type hint for the parameter: left

Please provide type hint for the parameter: right

#Spary Tree class
class SplayTree:
"""A self-adjusting Binary Search Tree."""
def __init__(self):

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:


# --- Basic Rotation Operations ---

def _rotate_left(self, x):

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 return type hint for the function: _rotate_left. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide descriptive name for the parameter: x

Please provide type hint for the parameter: x

y.left = x
x.parent = y

def _rotate_right(self, x):

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 return type hint for the function: _rotate_right. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide descriptive name for the parameter: x

Please provide type hint for the parameter: x


# --- Core Splay Operation ---

def _splay(self, x):

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 return type hint for the function: _splay. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide descriptive name for the parameter: x

Please provide type hint for the parameter: x


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

def search(self, key):

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

Please provide return type hint for the function: search. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: key

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.

class Node:
"""A node in the Splay Tree."""

def __init__(self, key, parent=None, left=None, right=None):

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:

Please provide type hint for the parameter: key

Please provide type hint for the parameter: parent

Please provide type hint for the parameter: left

Please provide type hint for the parameter: right

class SplayTree:
"""A self-adjusting Binary Search Tree."""

def __init__(self):

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:


# --- Basic Rotation Operations ---

def _rotate_left(self, x):

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 return type hint for the function: _rotate_left. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide descriptive name for the parameter: x

Please provide type hint for the parameter: x

y.left = x
x.parent = y

def _rotate_right(self, x):

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 return type hint for the function: _rotate_right. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide descriptive name for the parameter: x

Please provide type hint for the parameter: x


# --- Core Splay Operation ---

def _splay(self, x):

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 return type hint for the function: _splay. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide descriptive name for the parameter: x

Please provide type hint for the parameter: x


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

def search(self, key):

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

Please provide return type hint for the function: search. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: key

@yeshuawm999 yeshuawm999 closed this Nov 7, 2025
@yeshuawm999 yeshuawm999 deleted the splaytree-v3 branch November 7, 2025 17:03
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.

1 participant