mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-04-07 22:35:54 +00:00
improved del_node function(14)
This commit is contained in:
parent
e3603abe8d
commit
6cd0cd31fe
@ -201,13 +201,14 @@ def get_balance(node: MyNode | None) -> int:
|
|||||||
return get_height(node.get_left()) - get_height(node.get_right())
|
return get_height(node.get_left()) - get_height(node.get_right())
|
||||||
|
|
||||||
|
|
||||||
def get_min_value_node(node: MyNode | None) -> MyNode | None:
|
def get_min_value_node(node: MyNode) -> MyNode:
|
||||||
if node is None:
|
# Function get_left_most is not used here because it returns the value of the node
|
||||||
return None
|
while True:
|
||||||
current_node = node
|
left_child = node.get_left()
|
||||||
while current_node.get_left() is not None:
|
if left_child is None:
|
||||||
current_node = current_node.get_left()
|
break
|
||||||
return current_node
|
node = left_child
|
||||||
|
return node
|
||||||
|
|
||||||
|
|
||||||
def del_node(root: MyNode | None, data: Any) -> MyNode | None:
|
def del_node(root: MyNode | None, data: Any) -> MyNode | None:
|
||||||
@ -216,19 +217,20 @@ def del_node(root: MyNode | None, data: Any) -> MyNode | None:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
if root.get_data() > data:
|
if root.get_data() > data:
|
||||||
root.set_left(del_node(root.get_left(), data))
|
left_child = del_node(root.get_left(), data)
|
||||||
|
root.set_left(left_child)
|
||||||
elif root.get_data() < data:
|
elif root.get_data() < data:
|
||||||
root.set_right(del_node(root.get_right(), data))
|
right_child = del_node(root.get_right(), data)
|
||||||
|
root.set_right(right_child)
|
||||||
else:
|
else:
|
||||||
if root.get_left() is None:
|
if root.get_left() is None:
|
||||||
return root.get_right()
|
return root.get_right()
|
||||||
elif root.get_right() is None:
|
elif root.get_right() is None:
|
||||||
return root.get_left()
|
return root.get_left()
|
||||||
right_child = root.get_right()
|
right_child = root.get_right()
|
||||||
assert right_child is not None
|
assert right_child is not None
|
||||||
temp = get_min_value_node(right_child)
|
temp = get_min_value_node(right_child)
|
||||||
if temp is not None:
|
root.set_data(temp.get_data())
|
||||||
root.set_data(temp.get_data())
|
|
||||||
root.set_right(del_node(root.get_right(), temp.get_data()))
|
root.set_right(del_node(root.get_right(), temp.get_data()))
|
||||||
|
|
||||||
root.set_height(
|
root.set_height(
|
||||||
@ -242,30 +244,19 @@ def del_node(root: MyNode | None, data: Any) -> MyNode | None:
|
|||||||
assert left_child is not None
|
assert left_child is not None
|
||||||
if get_balance(left_child) >= 0:
|
if get_balance(left_child) >= 0:
|
||||||
return right_rotation(root)
|
return right_rotation(root)
|
||||||
|
root.set_left(left_rotation(left_child))
|
||||||
if balance > 1:
|
return right_rotation(root)
|
||||||
left_child = root.get_left()
|
|
||||||
assert left_child is not None
|
|
||||||
if get_balance(left_child) < 0:
|
|
||||||
root.set_left(left_rotation(left_child))
|
|
||||||
return right_rotation(root)
|
|
||||||
|
|
||||||
if balance < -1:
|
if balance < -1:
|
||||||
right_child = root.get_right()
|
right_child = root.get_right()
|
||||||
assert right_child is not None
|
assert right_child is not None
|
||||||
if get_balance(right_child) <= 0:
|
if get_balance(right_child) <= 0:
|
||||||
return left_rotation(root)
|
return left_rotation(root)
|
||||||
|
root.set_right(right_rotation(right_child))
|
||||||
if balance < -1:
|
return left_rotation(root)
|
||||||
right_child = root.get_right()
|
|
||||||
assert right_child is not None
|
|
||||||
if get_balance(right_child) > 0:
|
|
||||||
root.set_right(right_rotation(right_child))
|
|
||||||
return left_rotation(root)
|
|
||||||
|
|
||||||
return root
|
return root
|
||||||
|
|
||||||
|
|
||||||
class AVLtree:
|
class AVLtree:
|
||||||
"""
|
"""
|
||||||
An AVL tree doctest
|
An AVL tree doctest
|
||||||
@ -370,4 +361,4 @@ if __name__ == "__main__":
|
|||||||
random.shuffle(lst)
|
random.shuffle(lst)
|
||||||
for i in lst:
|
for i in lst:
|
||||||
t.del_node(i)
|
t.del_node(i)
|
||||||
print(str(t))
|
print(str(t))
|
Loading…
x
Reference in New Issue
Block a user