Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add Splay Tree implementation (#13760) [hacktoberfest]
This PR adds a Splay Tree (Self Adjusting BST) implementation in Python under the `data_structures/binary_tree/` directory. ## Details: - Implements the Splay Tree data structure with all core operations: insertion, search, splay rotation, and inorder traversal. - Includes class-based design (`Node`, `SplayTree`) and follows Python community style. - Provides an example usage and test in the `__main__` block. ## Why Splay Tree? A Splay Tree is a self-adjusting binary search tree that provides fast access to recently used elements by moving them closer to the root through 'splaying' operations. Frequently accessed nodes move towards the top, which is useful for caching and memory management systems. ## Issue Reference This solves [Issue #13760](#13760). ## Hacktoberfest Marking this contribution for Hacktoberfest 2025. **For Maintainers:** Please add the `hacktoberfest-accepted` label to count this PR towards Hacktoberfest. ## Checklist - [x] Follows contribution guidelines - [x] Code is documented with docstrings and inline comments - [x] Includes an example/test case - [x] New file in correct directory --- Feel free to use or modify the text above for your commit and pull request! If you want it in Hinglish or need a quick summary, let me know.
  • Loading branch information
gunjan-creates authored Oct 31, 2025
commit 50c2b34d4d4baaaf30ed533bb20b1dd5716d458d
66 changes: 42 additions & 24 deletions data_structures/binary_tree/splay_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@
self.parent = None
self.key = key

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

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__

return f"Node({self.key})"


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

self.root = None

def _right_rotate(self, x):

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

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 _right_rotate

Please provide type hint for the parameter: x

Please provide descriptive name for the parameter: x

y = x.left
if not y:
return
x.left = y.right
if y.right:
y.right.parent = x
Expand All @@ -34,6 +39,8 @@

def _left_rotate(self, x):

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

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 _left_rotate

Please provide type hint for the parameter: x

Please provide descriptive name for the parameter: x

y = x.right
if not y:
return
x.right = y.left
if y.left:
y.left.parent = x
Expand All @@ -48,35 +55,40 @@
x.parent = y

def _splay(self, x):

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:

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 type hint for the parameter: x

Please provide descriptive name for the parameter: x

while x.parent:
if not x.parent.parent:
if x.parent.left == x:
self._right_rotate(x.parent)
while x and x.parent:
p = x.parent
g = p.parent
# Zig step (p is root)
if not g:
if p.left == x:
self._right_rotate(p)
else:
self._left_rotate(x.parent)
self._left_rotate(p)
else:
if x.parent.left == x and x.parent.parent.left == x.parent:
self._right_rotate(x.parent.parent)
self._right_rotate(x.parent)
elif x.parent.right == x and x.parent.parent.right == x.parent:
self._left_rotate(x.parent.parent)
self._left_rotate(x.parent)
elif x.parent.left == x and x.parent.parent.right == x.parent:
self._right_rotate(x.parent)
self._left_rotate(x.parent)
else:
self._left_rotate(x.parent)
self._right_rotate(x.parent)
# Zig-Zig
if p.left == x and g.left == p:

Check failure on line 69 in data_structures/binary_tree/splay_tree.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (PLR5501)

data_structures/binary_tree/splay_tree.py:67:13: PLR5501 Use `elif` instead of `else` then `if`, to reduce indentation
self._right_rotate(g)
self._right_rotate(p)
elif p.right == x and g.right == p:
self._left_rotate(g)
self._left_rotate(p)
# Zig-Zag
elif p.left == x and g.right == p:
self._right_rotate(p)
self._left_rotate(g)
else: # p.right == x and g.left == p
self._left_rotate(p)
self._right_rotate(g)

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:

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 insert

Please provide type hint for the parameter: key

z = self.root
p = None
while z:
p = z
if key < z.key:
z = z.left
else:
z = z.right

Check failure on line 91 in data_structures/binary_tree/splay_tree.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (SIM108)

data_structures/binary_tree/splay_tree.py:88:13: SIM108 Use ternary operator `z = z.left if key < z.key else z.right` instead of `if`-`else`-block
z = Node(key)
z.parent = p
if not p:
Expand All @@ -89,26 +101,29 @@

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:

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 type hint for the parameter: key

z = self.root
last = None
while z:
last = z
if key == z.key:
self._splay(z)
return z
elif key < z.key:
if not z.left:
self._splay(z)
return None
z = z.left
else:
if not z.right:
self._splay(z)
return None
z = z.right
# splay the last accessed node (closest) if present
if last:
self._splay(last)
return None

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

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 inorder

Please provide type hint for the parameter: node

Please provide type hint for the parameter: result

if result is None:
result = []
node = node or self.root
# if node is explicitly passed as None and tree is empty, return empty result
if node is None:
node = self.root
if node is None:
return result
if node.left:
self.inorder(node.left, result)
result.append(node.key)
Expand All @@ -120,6 +135,9 @@
# Example Usage / Test
if __name__ == "__main__":
tree = SplayTree()
# empty tree -> inorder should return []
print(tree.inorder()) # []

for key in [10, 20, 30, 40, 50, 25]:
tree.insert(key)
print(tree.inorder()) # Output should be the inorder traversal of tree
Expand Down
Loading