Created
November 20, 2018 00:33
-
-
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
This file contains hidden or 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
""" | |
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