This commit is contained in:
Shaurya Bisht 2024-10-14 19:08:42 +05:30
commit b3ba109e2c

View File

@ -196,8 +196,13 @@ def get_left_most(root: MyNode) -> Any:
def del_node(root: MyNode, data: Any) -> MyNode | None: def del_node(root: MyNode, data: Any) -> MyNode | None:
if root is None:
print("Node is empty, nothing to delete")
return None
left_child = root.get_left() left_child = root.get_left()
right_child = root.get_right() right_child = root.get_right()
if root.get_data() == data: if root.get_data() == data:
if left_child is not None and right_child is not None: if left_child is not None and right_child is not None:
temp_data = get_left_most(right_child) temp_data = get_left_most(right_child)
@ -209,32 +214,41 @@ def del_node(root: MyNode, data: Any) -> MyNode | None:
root = right_child root = right_child
else: else:
return None return None
elif root.get_data() > data: elif data < root.get_data():
if left_child is None: if left_child is None:
print("No such data") print(f"No such data ({data}) exists in the left subtree.")
return root return root
else: else:
root.set_left(del_node(left_child, data)) root.set_left(del_node(left_child, data))
# root.get_data() < data else:
elif right_child is None: if right_child is None:
print(f"No such data ({data}) exists in the right subtree.")
return root return root
else: else:
root.set_right(del_node(right_child, data)) root.set_right(del_node(right_child, data))
if get_height(right_child) - get_height(left_child) == 2: # Update the height of the node
root.set_height(
1 + my_max(get_height(root.get_left()), get_height(root.get_right()))
)
# Get the balance factor
balance_factor = get_height(root.get_right()) - get_height(root.get_left())
# Balance the tree
if balance_factor == 2:
assert right_child is not None assert right_child is not None
if get_height(right_child.get_right()) > get_height(right_child.get_left()): if get_height(right_child.get_right()) > get_height(right_child.get_left()):
root = left_rotation(root) root = left_rotation(root)
else: else:
root = rl_rotation(root) root = rl_rotation(root)
elif get_height(right_child) - get_height(left_child) == -2: elif balance_factor == -2:
assert left_child is not None assert left_child is not None
if get_height(left_child.get_left()) > get_height(left_child.get_right()): if get_height(left_child.get_left()) > get_height(left_child.get_right()):
root = right_rotation(root) root = right_rotation(root)
else: else:
root = lr_rotation(root) root = lr_rotation(root)
height = my_max(get_height(root.get_right()), get_height(root.get_left())) + 1
root.set_height(height)
return root return root