Last active
December 25, 2015 13:09
-
-
Save futureperfect/6981509 to your computer and use it in GitHub Desktop.
Find a value within a binary search tree
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
public class FindInBST { | |
public static Node<T> findValueInBST(T value, Node<T> root) { | |
checkNotNull(value); | |
//terminating condition | |
if (root == null) { | |
return null; | |
} | |
if(value.compareTo(root.value) == 0) { | |
return root; | |
} else if(value.compareTo(root.value) > 0) { | |
return findValueInBST(value, root.leftChild); | |
} else { | |
return findValueInBST(value, root.rightChild); | |
} | |
} | |
} |
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
private class Node <T implements Comparable> { | |
T value; | |
Node<T> leftChild; | |
Node<T> rightChild; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment