forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete_a_node_in_bst.py
35 lines (27 loc) · 932 Bytes
/
delete_a_node_in_bst.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from inorder_successor import inorder_successor
# The above line imports the inorder_successor function from the inorder_successor.py file
def delete_node(root,val):
""" This function deletes a node with value val from the BST"""
# search in the left subtree
if root.data < val:
root.right = delete_node(root.right,val)
# search in the right subtree
elif root.data>val:
root.left=delete_node(root.left,val)
# node to be deleted is found
else:
# case 1: no child leaf node
if root.left is None and root.right is None:
return None
# case 2: one child
if root.left is None:
return root.right
# case 2: one child
elif root.right is None:
return root.left
# case 3: two children
# find the inorder successor
IS=inorder_successor(root.right)
root.data=IS.data
root.right=delete_node(root.right,IS.data)
return root