@@ -107,12 +107,11 @@ def insert(self, label: int) -> RedBlackTree:
107107 else :
108108 self .left = RedBlackTree (label , 1 , self )
109109 self .left ._insert_repair ()
110+ elif self .right :
111+ self .right .insert (label )
110112 else :
111- if self .right :
112- self .right .insert (label )
113- else :
114- self .right = RedBlackTree (label , 1 , self )
115- self .right ._insert_repair ()
113+ self .right = RedBlackTree (label , 1 , self )
114+ self .right ._insert_repair ()
116115 return self .parent or self
117116
118117 def _insert_repair (self ) -> None :
@@ -178,36 +177,34 @@ def remove(self, label: int) -> RedBlackTree: # noqa: PLR0912
178177 self .parent .left = None
179178 else :
180179 self .parent .right = None
181- else :
182- # The node is black
183- if child is None :
184- # This node and its child are black
185- if self .parent is None :
186- # The tree is now empty
187- return RedBlackTree (None )
188- else :
189- self ._remove_repair ()
190- if self .is_left ():
191- self .parent .left = None
192- else :
193- self .parent .right = None
194- self .parent = None
180+ # The node is black
181+ elif child is None :
182+ # This node and its child are black
183+ if self .parent is None :
184+ # The tree is now empty
185+ return RedBlackTree (None )
195186 else :
196- # This node is black and its child is red
197- # Move the child node here and make it black
198- self .label = child .label
199- self .left = child .left
200- self .right = child .right
201- if self .left :
202- self .left .parent = self
203- if self .right :
204- self .right .parent = self
187+ self ._remove_repair ()
188+ if self .is_left ():
189+ self .parent .left = None
190+ else :
191+ self .parent .right = None
192+ self .parent = None
193+ else :
194+ # This node is black and its child is red
195+ # Move the child node here and make it black
196+ self .label = child .label
197+ self .left = child .left
198+ self .right = child .right
199+ if self .left :
200+ self .left .parent = self
201+ if self .right :
202+ self .right .parent = self
205203 elif self .label is not None and self .label > label :
206204 if self .left :
207205 self .left .remove (label )
208- else :
209- if self .right :
210- self .right .remove (label )
206+ elif self .right :
207+ self .right .remove (label )
211208 return self .parent or self
212209
213210 def _remove_repair (self ) -> None :
@@ -369,11 +366,10 @@ def search(self, label: int) -> RedBlackTree | None:
369366 return None
370367 else :
371368 return self .right .search (label )
369+ elif self .left is None :
370+ return None
372371 else :
373- if self .left is None :
374- return None
375- else :
376- return self .left .search (label )
372+ return self .left .search (label )
377373
378374 def floor (self , label : int ) -> int | None :
379375 """Returns the largest element in this tree which is at most label.
0 commit comments