mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-04-21 05:07:35 +00:00
Updated insert and del_node function
This commit is contained in:
parent
7654b0b9ed
commit
b867de8159
@ -150,6 +150,8 @@ def rl_rotation(node: MyNode) -> MyNode:
|
|||||||
def insert_node(node: MyNode | None, data: Any) -> MyNode | None:
|
def insert_node(node: MyNode | None, data: Any) -> MyNode | None:
|
||||||
if node is None:
|
if node is None:
|
||||||
return MyNode(data)
|
return MyNode(data)
|
||||||
|
if data == node.get_data(): # No duplicates allowed
|
||||||
|
return node
|
||||||
if data < node.get_data():
|
if data < node.get_data():
|
||||||
node.set_left(insert_node(node.get_left(), data))
|
node.set_left(insert_node(node.get_left(), data))
|
||||||
if (
|
if (
|
||||||
@ -163,7 +165,7 @@ def insert_node(node: MyNode | None, data: Any) -> MyNode | None:
|
|||||||
node = right_rotation(node)
|
node = right_rotation(node)
|
||||||
else:
|
else:
|
||||||
node = lr_rotation(node)
|
node = lr_rotation(node)
|
||||||
else:
|
elif data > node.get_data():
|
||||||
node.set_right(insert_node(node.get_right(), data))
|
node.set_right(insert_node(node.get_right(), data))
|
||||||
if get_height(node.get_right()) - get_height(node.get_left()) == 2:
|
if get_height(node.get_right()) - get_height(node.get_left()) == 2:
|
||||||
right_child = node.get_right()
|
right_child = node.get_right()
|
||||||
@ -202,6 +204,7 @@ def get_balance(node: MyNode | None) -> int:
|
|||||||
|
|
||||||
|
|
||||||
def get_min_value_node(node: MyNode) -> MyNode:
|
def get_min_value_node(node: MyNode) -> MyNode:
|
||||||
|
# Returns the node with the minimum value in the tree that is leftmost node
|
||||||
# Function get_left_most is not used here because it returns the value of the node
|
# Function get_left_most is not used here because it returns the value of the node
|
||||||
while True:
|
while True:
|
||||||
left_child = node.get_left()
|
left_child = node.get_left()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user