Skip to content

Add splay tree (self-adjusting BST) - #15266

Merged
cclauss merged 2 commits into
TheAlgorithms:masterfrom
priya-sundaram-dev:add-splay-tree
Sep 10, 2026
Merged

Add splay tree (self-adjusting BST)#15266
cclauss merged 2 commits into
TheAlgorithms:masterfrom
priya-sundaram-dev:add-splay-tree

Conversation

@priya-sundaram-dev

Copy link
Copy Markdown
Contributor

Describe your change

Adds data_structures/binary_tree/splay_tree.py, implementing a splay tree — a self-adjusting binary search tree that moves every accessed node to the root via rotations ("splaying"), giving amortized O(log n) operations and excellent performance under locality of reference.

Requested by @cclauss in #13760.

The implementation covers:

  • insert, search, delete — each splays the accessed node to the root
  • Zig / zig-zig / zig-zag rotation cases in a single recursive _splay
  • In-order iteration (__iter__) yielding keys in ascending order
  • Full type hints and doctests for every public method

Verified locally: doctests pass, ruff check/ruff format clean, ty check clean, and a 200-trial randomized stress test confirms the tree stays consistent with a reference set across interleaved insert/search/delete.

Checklist

  • Add an algorithm?
  • 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: Closes #13760.

Closes #13760

Implements a splay tree with insert, search and delete, each of which
splays the accessed node to the root for amortized O(log n) access and
locality of reference. Includes full type hints and doctests.

Closes TheAlgorithms#13760
@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed require tests Tests [doctest/unittest/pytest] are required labels Sep 10, 2026

@algorithms-keeper algorithms-keeper Bot left a comment

Copy link
Copy Markdown

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.

self.left: Node | None = None
self.right: Node | None = None

def __repr__(self) -> str:

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 __repr__

from collections.abc import Iterator


class Node:

@cclauss cclauss Sep 10, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please simplify by making Node a dataclass.

@algorithms-keeper algorithms-keeper Bot removed the require tests Tests [doctest/unittest/pytest] are required label Sep 10, 2026
@priya-sundaram-dev

Copy link
Copy Markdown
Contributor Author

Done — pushed 4d86ab2b:

  • Node is now a dataclass (key, left, right), which drops the hand-written __init__ and __repr__. The left/right children use field(repr=False) so a node still prints compactly as Node(key=10) rather than recursively dumping the whole subtree.
  • Added a class docstring doctest for the repr (>>> Node(10)Node(key=10)), which also addresses algorithms-keeper's note about __repr__.

All 30 doctests pass locally and ruff is clean. Thanks for the review!

@algorithms-keeper algorithms-keeper Bot removed the awaiting reviews This PR is ready to be reviewed label Sep 10, 2026
@cclauss
cclauss merged commit faa6b36 into TheAlgorithms:master Sep 10, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Splay Trees implantation ( Self Adjusting BST)

2 participants