updated avl_tree.py

This commit is contained in:
Shaurya Bisht 2024-10-13 08:48:22 +00:00
parent ca0ee11eb5
commit 20aed9e68d
2 changed files with 57 additions and 45 deletions

11
.vscode/settings.json vendored
View File

@ -1,5 +1,14 @@
{ {
"githubPullRequests.ignoredPullRequestBranches": [ "githubPullRequests.ignoredPullRequestBranches": [
"master" "master"
] ],
"python.testing.unittestArgs": [
"-v",
"-s",
".",
"-p",
"*test.py"
],
"python.testing.pytestEnabled": false,
"python.testing.unittestEnabled": true
} }

View File

@ -194,60 +194,63 @@ def get_left_most(root: MyNode) -> Any:
root = left_child root = left_child
return root.get_data() return root.get_data()
# Function to get balance factor
def get_balance(node: MyNode) -> int:
if node is None:
return 0
return get_height(node.get_left()) - get_height(node.get_right())
def get_min_value_node(node: MyNode) -> MyNode:
current = node
while current.get_left() is not None:
current = current.get_left()
return current
def del_node(root: MyNode, data: Any) -> MyNode | None: def del_node(root: MyNode, data: Any) -> MyNode | None:
if root is None: if root is None:
print("Nothing to delete") print("Nothing to delete")
return None return None
left_child = root.get_left() if root.get_data() > data:
right_child = root.get_right() root.set_left(del_node(root.get_left(), data))
elif root.get_data() < data:
if root.get_data() == data: root.set_right(del_node(root.get_right(), data))
if left_child is not None and right_child is not None:
temp_data = get_left_most(right_child)
root.set_data(temp_data)
root.set_right(del_node(right_child, temp_data))
elif left_child is not None:
root = left_child
elif right_child is not None:
root = right_child
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
else: else:
root.set_right(del_node(right_child, data)) # Node to delete found
if root.get_left() is None:
root.set_height( return root.get_right()
my_max(get_height(root.get_right()), get_height(root.get_left())) + 1 elif root.get_right() is None:
) return root.get_left()
balance_factor = get_height(root.get_left()) - get_height(root.get_right())
if balance_factor == 2:
assert right_child is not None
if get_height(right_child.get_right()) > get_height(right_child.get_left()):
root = left_rotation(root)
else: else:
root = rl_rotation(root) # Node with two children
elif balance_factor == -2: temp = get_min_value_node(root.get_right())
assert left_child is not None root.set_data(temp.get_data())
if get_height(left_child.get_left()) > get_height(left_child.get_right()): root.set_right(del_node(root.get_right(), temp.get_data()))
root = right_rotation(root)
else: root.set_height(1 + my_max(get_height(root.get_left()), get_height(root.get_right())))
root = lr_rotation(root)
balance = get_balance(root)
# Left Left
if balance > 1 and get_balance(root.get_left()) >= 0:
return right_rotation(root)
# Left Right
if balance > 1 and get_balance(root.get_left()) < 0:
root.set_left(left_rotation(root.get_left()))
return right_rotation(root)
# Right Right
if balance < -1 and get_balance(root.get_right()) <= 0:
return left_rotation(root)
# Right Left
if balance < -1 and get_balance(root.get_right()) > 0:
root.set_right(right_rotation(root.get_right()))
return left_rotation(root)
return root return root
class AVLtree: class AVLtree:
""" """
An AVL tree doctest An AVL tree doctest