Skip to content

Instantly share code, notes, and snippets.

@RafaelBroseghini
Created November 20, 2018 00:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RafaelBroseghini/59abbfdc10fb93b1442d4be908ec26fe to your computer and use it in GitHub Desktop.
Save RafaelBroseghini/59abbfdc10fb93b1442d4be908ec26fe to your computer and use it in GitHub Desktop.
Find the successor to a node in a Binary Search Tree before deleting from the tree
"""
Assume we have defined a BST class:
class BST(object):
class __Node(object):
def __init__(self, data, right=None, left=None):
self.data = data
self.right = right
self.left = left
def __init__(self, root=None):
self.root = root
This function finds the successor that will replace the
node passed as a parameter in the Binary Search Tree.
"""
def find_successor(node):
if node.right != None:
current = node.right
while current.left != None:
current = current.left
else:
current = current.left
return current
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment