Skip to content

Conversation

@Vedanth-Rach
Copy link

…ue #13844

Describe your change:

This PR introduces the Splay Tree implementation in Python, resolving issue #13844.
The Splay Tree is a self-adjusting Binary Search Tree (BST) that automatically moves frequently accessed nodes closer to the root using the splay operation (a sequence of tree rotations). This ensures that recently or frequently accessed elements can be accessed rapidly again.

Implementation Details
File Location: The implementation is added under data_structures/trees/splay_tree.py.

Structure: It includes a Node class and a SplayTree class with the core logic.

Key Methods Implemented:

_rotate(self, x): Handles single left/right rotations.

_splay(self, x): The core operation, handling zig, zig-zig, and zig-zag cases.

insert(self, key): Inserts the node and splays it to the root.

search(self, key): Searches for a key and splays the accessed node (or the last accessed node if not found) to the root.

Testing: Unit tests are provided in data_structures/trees/splay_tree_test.py to cover insertion and splay behavior.

Linked Issue:
This PR resolves the request in issue #13844.

  • [x ] 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 type hints https://docs.python.org/3/library/typing.html labels Nov 5, 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 single 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

A self-adjusting Binary Search Tree (BST) that uses the splay operation
to move the most recently accessed node to the root of the 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:

def __init__(self):
self.root = None

def _rotate(self, x: Node):

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: _rotate. 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

else:
self.root = x # x is the new root

def _splay(self, x: Node):

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: _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

self._rotate(x) # Rotate x first
self._rotate(x) # Then rotate x again

def search(self, key):

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: 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

self._splay(last) # Splay the last accessed node if key was not found
return None

def insert(self, key):

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: insert. 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 5, 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 require descriptive names This PR needs descriptive function and/or variable names require type hints https://docs.python.org/3/library/typing.html

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant