Skip to content

Instantly share code, notes, and snippets.

@futureperfect
Last active December 25, 2015 13:09
Show Gist options
  • Save futureperfect/6981509 to your computer and use it in GitHub Desktop.
Save futureperfect/6981509 to your computer and use it in GitHub Desktop.
Find a value within a binary search tree
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);
}
}
}
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