Last active
April 12, 2019 05:23
-
-
Save itsfarseen/03bef13f037fc88b94e8676b1dfbd7b7 to your computer and use it in GitHub Desktop.
Inorder successor, recursive
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
class Node: | |
def __init__(self, value, left=None, right=None): | |
self.value = value | |
self.left = left | |
self.right = right | |
def successor(node, x): | |
""" Return smallest node greater than x, if it exists, | |
in the subtree rooted at given node """ | |
if node is None: | |
return None | |
if node.value > x: | |
s = successor(node.left, x) | |
if s is None: | |
s = node | |
return s | |
else: | |
return successor(node.right, x) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment