Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue with AVL tree del_node function causing RL rotation asser… #11530

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 28 additions & 17 deletions data_structures/binary_tree/avl_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,12 @@


def del_node(root: MyNode, data: Any) -> MyNode | None:
if root is None:
return None

left_child = root.get_left()
right_child = root.get_right()

if root.get_data() == data:
if left_child is not None and right_child is not None:
temp_data = get_left_most(right_child)
Expand All @@ -210,31 +214,38 @@
else:
return None
elif root.get_data() > data:
if left_child is None:
print("No such data")
return root
else:
root.set_left(del_node(left_child, data))
# root.get_data() < data
elif right_child is None:
return root
root.set_left(del_node(left_child, data))
else:
root.set_right(del_node(right_child, data))

# Update left and right children after potential changes
left_child = root.get_left()
right_child = root.get_right()

if get_height(right_child) - get_height(left_child) == 2:
assert right_child is not None
if get_height(right_child.get_right()) > get_height(right_child.get_left()):
if right_child and get_height(right_child.get_right()) > get_height(
right_child.get_left()
):
root = left_rotation(root)
else:
root = rl_rotation(root)
elif get_height(right_child) - get_height(left_child) == -2:
assert left_child is not None
if get_height(left_child.get_left()) > get_height(left_child.get_right()):
if right_child and right_child.get_left():

Check failure on line 231 in data_structures/binary_tree/avl_tree.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (PLR5501)

data_structures/binary_tree/avl_tree.py:230:9: PLR5501 Use `elif` instead of `else` then `if`, to reduce indentation
root = rl_rotation(root)
else:
print("Skipping RL rotation as conditions are not met")
elif get_height(left_child) - get_height(right_child) == 2:
if left_child and get_height(left_child.get_left()) > get_height(
left_child.get_right()
):
root = right_rotation(root)
else:
root = lr_rotation(root)
height = my_max(get_height(root.get_right()), get_height(root.get_left())) + 1
root.set_height(height)
if left_child and left_child.get_right():

Check failure on line 241 in data_structures/binary_tree/avl_tree.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (PLR5501)

data_structures/binary_tree/avl_tree.py:240:9: PLR5501 Use `elif` instead of `else` then `if`, to reduce indentation
root = lr_rotation(root)
else:
print("Skipping LR rotation as conditions are not met")

root.set_height(
my_max(get_height(root.get_left()), get_height(root.get_right())) + 1
)
return root


Expand Down
Loading