Skip to content

Commit 69677d5

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 4ff6dee commit 69677d5

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

data_structures/trees/splay_tree.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
# the_algorithms/trees/splay_tree.py
22

3+
34
class Node:
45
"""A single node in the Splay Tree."""
6+
57
def __init__(self, key, parent=None, left=None, right=None):
68
self.key = key
79
self.parent = parent
810
self.left = left
911
self.right = right
1012

13+
1114
class SplayTree:
1215
"""
1316
A self-adjusting Binary Search Tree (BST) that uses the splay operation
1417
to move the most recently accessed node to the root of the tree.
1518
"""
19+
1620
def __init__(self):
1721
self.root = None
1822

@@ -43,7 +47,7 @@ def _rotate(self, x: Node):
4347
else:
4448
g.right = x
4549
else:
46-
self.root = x # x is the new root
50+
self.root = x # x is the new root
4751

4852
def _splay(self, x: Node):
4953
"""Moves node x to the root of the tree using zig, zig-zig, or zig-zag operations."""
@@ -70,7 +74,7 @@ def search(self, key):
7074
Returns the node if found, otherwise None.
7175
"""
7276
curr = self.root
73-
last = None # Keeps track of the last node accessed
77+
last = None # Keeps track of the last node accessed
7478

7579
while curr:
7680
last = curr
@@ -83,7 +87,7 @@ def search(self, key):
8387
curr = curr.right
8488

8589
if last:
86-
self._splay(last) # Splay the last accessed node if key was not found
90+
self._splay(last) # Splay the last accessed node if key was not found
8791
return None
8892

8993
def insert(self, key):
@@ -101,7 +105,7 @@ def insert(self, key):
101105
curr = curr.left
102106
elif key > curr.key:
103107
curr = curr.right
104-
else: # Key already exists, splay it and return (or update value)
108+
else: # Key already exists, splay it and return (or update value)
105109
self._splay(curr)
106110
return
107111

@@ -111,4 +115,4 @@ def insert(self, key):
111115
else:
112116
parent.right = new_node
113117

114-
self._splay(new_node)
118+
self._splay(new_node)

data_structures/trees/splay_tree_test.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
import unittest
44
from splay_tree import SplayTree
55

6+
67
class TestSplayTree(unittest.TestCase):
78
def test_insert_and_root(self):
89
"""Test basic insertion and verify the splayed node becomes the root."""
910
tree = SplayTree()
1011
keys = [50, 30, 70, 20, 40]
1112
for key in keys:
1213
tree.insert(key)
13-
self.assertEqual(tree.root.key, key, f"Expected {key} to be the root after insertion.")
14+
self.assertEqual(
15+
tree.root.key, key, f"Expected {key} to be the root after insertion."
16+
)
1417

1518
def test_search_and_splay(self):
1619
"""Test searching for an existing key and verify it is splayed to the root."""
@@ -28,16 +31,19 @@ def test_search_and_splay(self):
2831
# Search for a key that doesn't exist (99). The last accessed node (e.g., 80) should be splayed.
2932
_ = tree.search(99)
3033
# The exact last accessed node depends on the tree structure, but it should not be the original root (50)
31-
self.assertNotEqual(tree.root.key, 50, "Root should change after unsuccessful search.")
34+
self.assertNotEqual(
35+
tree.root.key, 50, "Root should change after unsuccessful search."
36+
)
3237

3338
def test_empty_tree(self):
3439
"""Test operations on an empty tree."""
3540
tree = SplayTree()
3641
self.assertIsNone(tree.search(10))
3742
self.assertIsNone(tree.root)
38-
43+
3944
tree.insert(10)
4045
self.assertEqual(tree.root.key, 10)
4146

42-
if __name__ == '__main__':
47+
48+
if __name__ == "__main__":
4349
unittest.main()

0 commit comments

Comments
 (0)