Skip to content

Instantly share code, notes, and snippets.

@sahid
Created February 18, 2013 22:32
Show Gist options
  • Save sahid/4981382 to your computer and use it in GitHub Desktop.
Save sahid/4981382 to your computer and use it in GitHub Desktop.
Delete a node ins a BST
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