@@ -196,8 +196,13 @@ def get_left_most(root: MyNode) -> Any:
196
196
197
197
198
198
def del_node (root : MyNode , data : Any ) -> MyNode | None :
199
+ if root is None :
200
+ print ("Nothing to delete" )
201
+ return None
202
+
199
203
left_child = root .get_left ()
200
204
right_child = root .get_right ()
205
+
201
206
if root .get_data () == data :
202
207
if left_child is not None and right_child is not None :
203
208
temp_data = get_left_most (right_child )
@@ -221,20 +226,23 @@ def del_node(root: MyNode, data: Any) -> MyNode | None:
221
226
else :
222
227
root .set_right (del_node (right_child , data ))
223
228
224
- if get_height (right_child ) - get_height (left_child ) == 2 :
229
+ root .set_height (my_max (get_height (root .get_right ()), get_height (root .get_left ())) + 1 )
230
+
231
+ balance_factor = get_height (root .get_left ()) - get_height (root .get_right ())
232
+
233
+ if balance_factor == 2 :
225
234
assert right_child is not None
226
235
if get_height (right_child .get_right ()) > get_height (right_child .get_left ()):
227
236
root = left_rotation (root )
228
237
else :
229
238
root = rl_rotation (root )
230
- elif get_height ( right_child ) - get_height ( left_child ) == - 2 :
239
+ elif balance_factor == - 2 :
231
240
assert left_child is not None
232
241
if get_height (left_child .get_left ()) > get_height (left_child .get_right ()):
233
242
root = right_rotation (root )
234
243
else :
235
244
root = lr_rotation (root )
236
- height = my_max (get_height (root .get_right ()), get_height (root .get_left ())) + 1
237
- root .set_height (height )
245
+
238
246
return root
239
247
240
248
0 commit comments