I have a normal binary search tree implemented with a string value for data, and right and left nodes. The tree works fine but I am having trouble with my rankOf function. I use recursion to find the nodes and the method is successful when the element exists, but when a value not present is does not work and I can't figure out how to set a boolean within to help with this. Here is the code:
private int rankOf(String s, Node n){ if (n != null){ //check root if (s.compareTo(n.value) == 0){ if (n.left != null){ return size(n.left); } return 0; } // only worry about left tree, easy if (s.compareTo(n.value) < 0){ return rankOf(s, n.left); } else { // must count entire left tree plus root node return rankOf(s, n.right) + size(n.left) + 1; } } //null or not found return 0; } When root is equal to the value I know the element is in the tree so something should go in there but unsure how to handle this.
sizemethod return ? Also what should your method return in case of success/failure ?