Last active
March 10, 2016 00:11
-
-
Save aniruddha84/47e1a662766193bf7481 to your computer and use it in GitHub Desktop.
Find in-order successor of a node in a binary search tree. Each node has access to parent node.
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
def next_successor(bnode) | |
if bnode.right | |
curr = bnode.right | |
until curr.left.nil? | |
curr = curr.left | |
end | |
curr | |
else | |
curr = bnode | |
prev = bnode.parent | |
until prev.nil? or curr == prev.left | |
curr = prev | |
prev = prev.parent | |
end | |
prev | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍