Created
June 27, 2012 21:50
-
-
Save harit-sunrun/3007089 to your computer and use it in GitHub Desktop.
Find inOrder successor and inOrder predecessor in binary search tree (BST)
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
Node predecessor(Node node) { | |
if ((node.left == null) && (node.right==null)) { | |
return node; | |
} | |
if (node.right != null) { | |
return predecessor(node.right); | |
} | |
if (node.left != null) { | |
return predecessor(node.left); | |
} | |
} |
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
Node successor(Node node) { | |
if ((node.left == null) && (node.right==null)) { | |
return node; | |
} | |
if (node.left != null) { | |
return successor(node.left); | |
} | |
if (node.right != null) { | |
return successor(node.right); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
is this correct? if yes can you please explain how does this work?