Skip to content

Instantly share code, notes, and snippets.

@itsfarseen
Last active April 12, 2019 05:23
Show Gist options
  • Save itsfarseen/03bef13f037fc88b94e8676b1dfbd7b7 to your computer and use it in GitHub Desktop.
Save itsfarseen/03bef13f037fc88b94e8676b1dfbd7b7 to your computer and use it in GitHub Desktop.
Inorder successor, recursive
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