mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
Create bst_delete_node.py
This commit is contained in:
parent
e1a2904cfe
commit
0c05504ae1
46
trees/bst_delete_node.py
Normal file
46
trees/bst_delete_node.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# author: bt3gl
|
||||||
|
|
||||||
|
|
||||||
|
def successor(root):
|
||||||
|
|
||||||
|
root = root.right
|
||||||
|
while root.left:
|
||||||
|
root = root.left
|
||||||
|
return root.val
|
||||||
|
|
||||||
|
|
||||||
|
def predecessor(root):
|
||||||
|
|
||||||
|
root = root.left
|
||||||
|
while root.right:
|
||||||
|
root = root.right
|
||||||
|
return root.val
|
||||||
|
|
||||||
|
|
||||||
|
def delete_node(root, key):
|
||||||
|
|
||||||
|
if not root:
|
||||||
|
return root
|
||||||
|
|
||||||
|
if key > root.val:
|
||||||
|
root.right = deleteNode(root.right, key)
|
||||||
|
|
||||||
|
elif key < root.val:
|
||||||
|
root.left = deleteNode(root.left, key)
|
||||||
|
|
||||||
|
else:
|
||||||
|
if not (root.left or root.right):
|
||||||
|
root = None
|
||||||
|
|
||||||
|
elif root.right:
|
||||||
|
root.val = successor(root)
|
||||||
|
root.right = deleteNode(root.right, root.val)
|
||||||
|
|
||||||
|
else:
|
||||||
|
root.val = predecessor(root)
|
||||||
|
root.left = deleteNode(root.left, root.val)
|
||||||
|
|
||||||
|
return root
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user