Skip to content

Commit b867de8

Browse files
committed
Updated insert and del_node function
1 parent 7654b0b commit b867de8

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

data_structures/binary_tree/avl_tree.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ def rl_rotation(node: MyNode) -> MyNode:
150150
def insert_node(node: MyNode | None, data: Any) -> MyNode | None:
151151
if node is None:
152152
return MyNode(data)
153+
if data == node.get_data(): # No duplicates allowed
154+
return node
153155
if data < node.get_data():
154156
node.set_left(insert_node(node.get_left(), data))
155157
if (
@@ -163,7 +165,7 @@ def insert_node(node: MyNode | None, data: Any) -> MyNode | None:
163165
node = right_rotation(node)
164166
else:
165167
node = lr_rotation(node)
166-
else:
168+
elif data > node.get_data():
167169
node.set_right(insert_node(node.get_right(), data))
168170
if get_height(node.get_right()) - get_height(node.get_left()) == 2:
169171
right_child = node.get_right()
@@ -202,6 +204,7 @@ def get_balance(node: MyNode | None) -> int:
202204

203205

204206
def get_min_value_node(node: MyNode) -> MyNode:
207+
# Returns the node with the minimum value in the tree that is leftmost node
205208
# Function get_left_most is not used here because it returns the value of the node
206209
while True:
207210
left_child = node.get_left()

0 commit comments

Comments
 (0)