Skip to content

Commit 4ba2d14

Browse files
committed
Added example block to Splay Tree implementation
1 parent b713a49 commit 4ba2d14

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

data_structures/binary_tree/splay_tree.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
"""
2+
Splay Tree implementation in Python.
3+
4+
A Splay Tree is a self-adjusting binary search tree where recently accessed
5+
elements are moved closer to the root through rotations (splaying).
6+
This improves access times for frequently used elements.
7+
8+
Author:yeshuawm999
9+
Repository: https://github.com/TheAlgorithms/Python
10+
"""
11+
112
#class node
213
class Node:
314
"""A node in the Splay Tree."""
@@ -8,7 +19,7 @@ def __init__(self, key, parent=None, left=None, right=None):
819
self.right = right # Pointer to the right child
920

1021

11-
#Spary tree class
22+
#Spary Tree class
1223
class SplayTree:
1324
"""A self-adjusting Binary Search Tree."""
1425
def __init__(self):
@@ -116,4 +127,15 @@ def search(self, key):
116127
self._splay(found_node) # Node is brought to the root
117128
return True
118129
return False
119-
130+
131+
if __name__ == "__main__":
132+
tree = SplayTree()
133+
# Manually create nodes to demonstrate splay
134+
tree.root = Node(10)
135+
tree.root.left = Node(5, parent=tree.root)
136+
tree.root.right = Node(15, parent=tree.root)
137+
138+
print("Before search:", tree.root.key)
139+
found = tree.search(5)
140+
print("Found:", found)
141+
print("After splay, new root:", tree.root.key)

0 commit comments

Comments
 (0)