Skip to content

Conversation

@Prigoistic
Copy link

Describe your change:

This PR adds a complete implementation of a Splay Tree - a self-adjusting binary search tree that provides excellent amortized performance and cache locality.

Key Features:

  • Self-adjusting binary search tree with splaying operations
  • Implements all standard BST operations (insert, delete, search, min, max)
  • Includes zig, zig-zig, and zig-zag rotation operations
  • Amortized O(log n) time complexity for all operations
  • Comprehensive doctests with 81 passing tests
  • Performance demonstration showing locality of reference
  • Includes tree visualization and traversal methods

Why Splay Tree?

  • Simpler implementation than AVL or Red-Black trees (no balance factors or colors)
  • Excellent cache performance - frequently accessed elements stay near root
  • Self-optimizing based on access patterns
  • Educational value for understanding self-balancing trees

Implementation Details:

  • Located in data_structures/binary_tree/splay_tree.py
  • Full type hints throughout
  • Detailed docstrings with examples
  • Handles edge cases (empty tree, duplicates, single node)
  • Supports any comparable data type (int, string, etc.)

Fixes #13760

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests?
  • 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 Add Splay Trees implantation ( Self Adjusting BST) #13760".

Prigoistic and others added 2 commits October 26, 2025 13:48
- Self-adjusting binary search tree with splaying operations
- Implements all standard BST operations (insert, delete, search, min, max)
- Includes zig, zig-zig, and zig-zag rotation operations
- Amortized O(log n) time complexity for all operations
- Comprehensive doctests with 81 passing tests
- Performance demonstration showing locality of reference
- Includes tree visualization and traversal methods
@Prigoistic
Copy link
Author

@darkstar please check the PR and merge this under hacktober fest label

@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Oct 26, 2025
Replace if-else block with ternary operator at line 302
to comply with Ruff linting rules
- Add explicit type annotations for current and parent variables
- Add type annotation for result lists in traversal methods
- Add null check for parent before accessing its key
- Fixes all 6 MyPy errors reported by pre-commit hooks
@algorithms-keeper algorithms-keeper bot added the require tests Tests [doctest/unittest/pytest] are required label Oct 26, 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.

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

def __repr__(self) -> str:

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__

)


def performance_comparison() -> 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 performance_comparison

@algorithms-keeper algorithms-keeper bot added awaiting reviews This PR is ready to be reviewed and removed tests are failing Do not merge until tests pass labels Oct 26, 2025
- Add 14 comprehensive doctests to performance_comparison function
- Fix MyPy union-attr error at line 259 by adding proper null check
- Total doctests increased from 81 to 95
- All tests verify splay tree behavior and BST property maintenance
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.

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

def __repr__(self) -> str:

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__

Remove redundant 'import random' on line 683 that was already
imported on line 657 in the performance_comparison function
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.

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

def __repr__(self) -> str:

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__

- Add 11 comprehensive doctests for __repr__ method
- Test various data types: int, string, negative numbers, float, None
- Test both repr() and str() functions
- Total doctests increased from 95 to 106
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 tests Tests [doctest/unittest/pytest] are required

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Splay Trees implantation ( Self Adjusting BST)

1 participant