-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbintree node deletion function 2.py
More file actions
23 lines (23 loc) · 1.28 KB
/
bintree node deletion function 2.py
File metadata and controls
23 lines (23 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def deleteNode(tree, value):
if(tree == None): #checks if tree is empty
return tree
elif(value < tree.value): #checks if the value is less than the value of root
tree.left = deleteNode(tree.left, value) #recursive function that will delete the value on the left side of the tree
elif(value > tree.value): #checks if value is greater than value of root
tree.right = deleteNode(tree.right, value) #recursive function that will delete the value on the right side of the tree
else:
if(tree.left == None and tree.right == None): #checks if there is no child
del(tree) #deletes node
tree = None
elif(tree.left == None): #checks if there is only one child
tree.temp = tree #stores address of current node to be deleted in temporary pointer
tree = tree.right #moves root to right child
del tree.temp #deletes the node being pointed to
elif(tree.right == None):
tree.temp = tree
tree = tree.left
del tree.temp
else:
tree.temp = min(tree.right) #if there is two children find minimum value in the right subtree
tree.value = tree.temp.value
tree.right = deleteNode(tree.right, tree.temp.value)