Created
February 18, 2013 22:32
-
-
Save sahid/4981382 to your computer and use it in GitHub Desktop.
Delete a node ins a BST
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def transplant(root, node, newone): | |
if newone: | |
newone.parent = node.parent | |
if node.parent: | |
if node.parent.left == node: | |
node.parent.left = newone | |
else: | |
node.parent.right = newone | |
else: | |
root = newone | |
return root | |
def delete(root, node): | |
if node.left is None: | |
root = transplant(root, node, node.right) | |
elif node.right is None: | |
root = transplant(root, node, node.left) | |
else: | |
# TODO(sahid): Just crazy to understand! | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment