99Author: yeshuawm999
1010"""
1111
12- < << << << HEAD
13-
14- # class node
15- class Node :
16- """A node in the Splay Tree."""
17-
18- def __init__ (self , key , parent = None , left = None , right = None ):
19- self .key = key # The value stored in the node
20- self .parent = parent # Pointer to the parent node
21- self .left = left # Pointer to the left child
22- self .right = right # Pointer to the right child
23-
24-
25- # Spary Tree class
26- class SplayTree :
27- """A self-adjusting Binary Search Tree."""
28-
29- def __init__ (self ):
30- self .root = None # The root of the tree
31-
32- # --- Basic Rotation Operations ---
33-
34- def _rotate_left (self , x ):
35- == == == =
3612from __future__ import annotations
3713from typing import Optional
3814
@@ -43,9 +19,9 @@ class Node:
4319 def __init__ (
4420 self ,
4521 key : int ,
46- parent : Optional [Node ] = None ,
47- left : Optional [Node ] = None ,
48- right : Optional [Node ] = None ,
22+ parent : Optional [" Node" ] = None ,
23+ left : Optional [" Node" ] = None ,
24+ right : Optional [" Node" ] = None ,
4925 ) -> None :
5026 self .key = key
5127 self .parent = parent
@@ -62,117 +38,61 @@ def __init__(self) -> None:
6238 # --- Basic Rotation Operations ---
6339
6440 def _rotate_left (self , x : Node ) -> None :
65- > >> >> >> 5 d5b0f04 (Fixed lint issues , added type hints , and cleaned Splay Tree implementation )
66- """Perform a left rotation around node x (moving x down and right)."""
41+ """Perform a left rotation around node x."""
6742 y = x .right
6843 if not y :
6944 return
7045 x .right = y .left
7146 if y .left :
7247 y .left .parent = x
7348
74- < << << << HEAD
75- # 2. Update y's parent to be x's parent
76- y .parent = x .parent
77- if not x .parent :
78- self .root = y # y becomes the new root
79- == == == =
8049 y .parent = x .parent
8150 if not x .parent :
8251 self .root = y
83- > >> >> >> 5 d5b0f04 (Fixed lint issues , added type hints , and cleaned Splay Tree implementation )
8452 elif x == x .parent .left :
8553 x .parent .left = y
8654 else :
8755 x .parent .right = y
8856
89- << << << < HEAD
90- # 3. Update y's left child to be x
91- == == == =
92- >> >> >> > 5 d5b0f04 (Fixed lint issues , added type hints , and cleaned Splay Tree implementation )
9357 y .left = x
9458 x .parent = y
9559
9660 def _rotate_right (self , x : Node ) -> None :
97- """Perform a right rotation around node x (moving x down and left) ."""
61+ """Perform a right rotation around node x."""
9862 y = x .left
9963 if not y :
10064 return
10165 x .left = y .right
10266 if y .right :
10367 y .right .parent = x
10468
105- < << << << HEAD
106- # 2. Update y's parent to be x's parent
107- y .parent = x .parent
108- if not x .parent :
109- self .root = y # y becomes the new new root
110- == == == =
11169 y .parent = x .parent
11270 if not x .parent :
11371 self .root = y
114- > >> >> >> 5 d5b0f04 (Fixed lint issues , added type hints , and cleaned Splay Tree implementation )
11572 elif x == x .parent .right :
11673 x .parent .right = y
11774 else :
11875 x .parent .left = y
11976
120- << << << < HEAD
121- # 3. Update y's right child to be x
122- == == == =
123- >> >> >> > 5 d5b0f04 (Fixed lint issues , added type hints , and cleaned Splay Tree implementation )
12477 y .right = x
12578 x .parent = y
12679
12780 # --- Core Splay Operation ---
12881
129- < << << << HEAD
130- def _splay (self , x ):
131- == == == =
13282 def _splay (self , x : Node ) -> None :
133- >> >> >> > 5 d5b0f04 (Fixed lint issues , added type hints , and cleaned Splay Tree implementation )
13483 """Moves node x to the root of the tree using a sequence of rotations."""
13584 while x .parent :
13685 parent = x .parent
13786 grandparent = parent .parent
13887
13988 if not grandparent :
14089 # Zig Case (x is a child of the root)
141- # One single rotation (Right if left child, Left if right child)
90+ # One single rotation:
91+ # Right if x is a left child, Left if x is a right child
14292 if x == parent .left :
14393 self ._rotate_right (parent )
14494 else :
14595 self ._rotate_left (parent )
146- < << << << HEAD
147-
148- else :
149- # Two rotations are performed: Zig-Zig or Zig-Zag
150-
151- # Case 1: Zig-Zig (x, parent, and grandparent are all on one side)
152- if x == parent .left and parent == grandparent .left :
153- # x and parent are both left children (Left-Left)
154- self ._rotate_right (grandparent ) # Rotate grandparent down
155- self ._rotate_right (parent ) # Rotate parent down
156- elif x == parent .right and parent == grandparent .right :
157- # x and parent are both right children (Right-Right)
158- self ._rotate_left (grandparent ) # Rotate grandparent down
159- self ._rotate_left (parent ) # Rotate parent down
160-
161- # Case 2: Zig-Zag (x is on one side, parent is on the other)
162- elif x == parent .left and parent == grandparent .right :
163- # x is left child, parent is right child
164- self ._rotate_right (parent ) # Rotate parent first
165- self ._rotate_left (grandparent ) # Rotate grandparent next
166- else : # x == parent.right and parent == grandparent.left
167- # x is right child, parent is left child
168- self ._rotate_left (parent ) # Rotate parent first
169- self ._rotate_right (grandparent ) # Rotate grandparent next
170-
171- # --- Example Search Method (Uses splay) ---
172-
173- def search (self , key ):
174- """Searches for a key. If found, splays it to the root."""
175- == == == =
17696
17797 # Two rotations are performed: Zig-Zig or Zig-Zag
17898 elif x == parent .left and parent == grandparent .left :
@@ -187,7 +107,7 @@ def search(self, key):
187107 # Case 2: Zig-Zag (x is left child, parent is right child)
188108 self ._rotate_right (parent )
189109 self ._rotate_left (grandparent )
190- else :
110+ elif x == parent . right and parent == grandparent . left :
191111 # Case 2: Zig-Zag (x is right child, parent is left child)
192112 self ._rotate_left (parent )
193113 self ._rotate_right (grandparent )
@@ -196,7 +116,6 @@ def search(self, key):
196116
197117 def search (self , key : int ) -> bool :
198118 """Search for a key. If found, splay it to the root."""
199- >> >> >> > 5 d5b0f04 (Fixed lint issues , added type hints , and cleaned Splay Tree implementation )
200119 current = self .root
201120 found_node = None
202121 while current :
@@ -209,22 +128,15 @@ def search(self, key: int) -> bool:
209128 current = current .right
210129
211130 if found_node :
212- < << << << HEAD
213- self ._splay (found_node ) # Node is brought to the root
214- == == == =
215131 self ._splay (found_node )
216- >> >> >> > 5 d5b0f04 (Fixed lint issues , added type hints , and cleaned Splay Tree implementation )
217132 return True
218133 return False
219134
220135
221- < << << << HEAD
222- == == == =
223- # --- Example Usage (for TheAlgorithms CI testing) ---
224- >> >> >> > 5 d5b0f04 (Fixed lint issues , added type hints , and cleaned Splay Tree implementation )
136+ # --- Example Usage ---
225137if __name__ == "__main__" :
226138 """
227- Example:
139+ Example run :
228140 >>> tree = SplayTree()
229141 >>> tree.root = Node(10)
230142 >>> tree.root.left = Node(5, parent=tree.root)
@@ -234,6 +146,7 @@ def search(self, key: int) -> bool:
234146 >>> print("Found:", found)
235147 >>> print("After splay, new root:", tree.root.key)
236148 """
149+
237150 tree = SplayTree ()
238151 tree .root = Node (10 )
239152 tree .root .left = Node (5 , parent = tree .root )
0 commit comments