1+ #class node
2+ class Node :
3+ """A node in the Splay Tree."""
4+ def __init__ (self , key , parent = None , left = None , right = None ):
5+ self .key = key # The value stored in the node
6+ self .parent = parent # Pointer to the parent node
7+ self .left = left # Pointer to the left child
8+ self .right = right # Pointer to the right child
9+
10+
11+ #Spary tree class
12+ class SplayTree :
13+ """A self-adjusting Binary Search Tree."""
14+ def __init__ (self ):
15+ self .root = None # The root of the tree
16+
17+ # --- Basic Rotation Operations ---
18+
19+ def _rotate_left (self , x ):
20+ """Perform a left rotation around node x (moving x down and right)."""
21+ y = x .right
22+ # 1. Update x's right child to be y's left child
23+ x .right = y .left
24+ if y .left :
25+ y .left .parent = x
26+
27+ # 2. Update y's parent to be x's parent
28+ y .parent = x .parent
29+ if not x .parent :
30+ self .root = y # y becomes the new root
31+ elif x == x .parent .left :
32+ x .parent .left = y
33+ else :
34+ x .parent .right = y
35+
36+ # 3. Update y's left child to be x
37+ y .left = x
38+ x .parent = y
39+
40+ def _rotate_right (self , x ):
41+ """Perform a right rotation around node x (moving x down and left)."""
42+ y = x .left
43+ # 1. Update x's left child to be y's right child
44+ x .left = y .right
45+ if y .right :
46+ y .right .parent = x
47+
48+ # 2. Update y's parent to be x's parent
49+ y .parent = x .parent
50+ if not x .parent :
51+ self .root = y # y becomes the new new root
52+ elif x == x .parent .right :
53+ x .parent .right = y
54+ else :
55+ x .parent .left = y
56+
57+ # 3. Update y's right child to be x
58+ y .right = x
59+ x .parent = y
60+
61+ # --- Core Splay Operation ---
62+
63+ def _splay (self , x ):
64+ """Moves node x to the root of the tree using a sequence of rotations."""
65+ while x .parent :
66+ parent = x .parent
67+ grandparent = parent .parent
68+
69+ if not grandparent :
70+ # Zig Case (x is a child of the root)
71+ # One single rotation (Right if x is left child, Left if x is right child)
72+ if x == parent .left :
73+ self ._rotate_right (parent )
74+ else :
75+ self ._rotate_left (parent )
76+
77+ else :
78+ # Two rotations are performed: Zig-Zig or Zig-Zag
79+
80+ # Case 1: Zig-Zig (x, parent, and grandparent are all on one side)
81+ if (x == parent .left and parent == grandparent .left ):
82+ # x and parent are both left children (Left-Left)
83+ self ._rotate_right (grandparent ) # Rotate grandparent down
84+ self ._rotate_right (parent ) # Rotate parent down
85+ elif (x == parent .right and parent == grandparent .right ):
86+ # x and parent are both right children (Right-Right)
87+ self ._rotate_left (grandparent ) # Rotate grandparent down
88+ self ._rotate_left (parent ) # Rotate parent down
89+
90+ # Case 2: Zig-Zag (x is on one side, parent is on the other)
91+ elif (x == parent .left and parent == grandparent .right ):
92+ # x is left child, parent is right child
93+ self ._rotate_right (parent ) # Rotate parent first
94+ self ._rotate_left (grandparent ) # Rotate grandparent next
95+ else : # x == parent.right and parent == grandparent.left
96+ # x is right child, parent is left child
97+ self ._rotate_left (parent ) # Rotate parent first
98+ self ._rotate_right (grandparent ) # Rotate grandparent next
99+
100+ # --- Example Search Method (Uses splay) ---
101+
102+ def search (self , key ):
103+ """Searches for a key. If found, splays it to the root."""
104+ current = self .root
105+ found_node = None
106+ while current :
107+ if key == current .key :
108+ found_node = current
109+ break
110+ elif key < current .key :
111+ current = current .left
112+ else :
113+ current = current .right
114+
115+ if found_node :
116+ self ._splay (found_node ) # Node is brought to the root
117+ return True
118+ return False
119+
0 commit comments