mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-03-27 17:06:44 +00:00
Merge branch 'master' of https://github.com/ShauryaDusht/Python
This commit is contained in:
commit
bc93da6d92
@ -196,8 +196,13 @@ def get_left_most(root: MyNode) -> Any:
|
||||
|
||||
|
||||
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()
|
||||
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)
|
||||
@ -209,32 +214,41 @@ def del_node(root: MyNode, data: Any) -> MyNode | None:
|
||||
root = right_child
|
||||
else:
|
||||
return None
|
||||
elif root.get_data() > data:
|
||||
elif data < root.get_data():
|
||||
if left_child is None:
|
||||
print("No such data")
|
||||
print(f"No such data ({data}) exists in the left subtree.")
|
||||
return root
|
||||
else:
|
||||
root.set_left(del_node(left_child, data))
|
||||
# root.get_data() < data
|
||||
elif right_child is None:
|
||||
return root
|
||||
else:
|
||||
root.set_right(del_node(right_child, data))
|
||||
if right_child is None:
|
||||
print(f"No such data ({data}) exists in the right subtree.")
|
||||
return root
|
||||
else:
|
||||
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
|
||||
if 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:
|
||||
elif balance_factor == -2:
|
||||
assert left_child is not None
|
||||
if 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)
|
||||
|
||||
return root
|
||||
|
||||
|
||||
|
@ -231,4 +231,4 @@ def main() -> None:
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
main()
|
@ -39,9 +39,11 @@ def frac_knapsack(vl, wt, w, n):
|
||||
return (
|
||||
0
|
||||
if k == 0
|
||||
else sum(vl[:k]) + (w - acc[k - 1]) * (vl[k]) / (wt[k])
|
||||
if k != n
|
||||
else sum(vl[:k])
|
||||
else (
|
||||
sum(vl[:k]) + (w - acc[k - 1]) * (vl[k]) / (wt[k])
|
||||
if k != n
|
||||
else sum(vl[:k])
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
|
@ -204,9 +204,11 @@ class Matrix:
|
||||
return Matrix(
|
||||
[
|
||||
[
|
||||
self.minors().rows[row][column]
|
||||
if (row + column) % 2 == 0
|
||||
else self.minors().rows[row][column] * -1
|
||||
(
|
||||
self.minors().rows[row][column]
|
||||
if (row + column) % 2 == 0
|
||||
else self.minors().rows[row][column] * -1
|
||||
)
|
||||
for column in range(self.minors().num_columns)
|
||||
]
|
||||
for row in range(self.minors().num_rows)
|
||||
|
Loading…
x
Reference in New Issue
Block a user