Skip to content

Instantly share code, notes, and snippets.

@aniruddha84
Last active March 10, 2016 00:11
Show Gist options
  • Save aniruddha84/47e1a662766193bf7481 to your computer and use it in GitHub Desktop.
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.
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
@senthil1216
Copy link

should you not return in line 3
return bnode.right

@aniruddha84
Copy link
Author

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment