I'm trying to calculate the depth of a key in a binary search tree and I am getting a stack overflow error and I'm not sure why. Here is my current code.
private int calcDepth( Tree<K, V> x, K keyIn, int currentLevel){
//BASE CASE
if (this.key.compareTo(keyIn) == 0) return currentLevel;
if (this.key.compareTo(keyIn) < 0){
return calcDepth(this.left, keyIn, currentLevel+1);
}
if (this.key.compareTo(keyIn) > 0){
return calcDepth(this.right, keyIn, currentLevel + 1);
}
return -1;
}
and this is my algorithm
//ALGORITHIM
//1. if the current key is equal to the parameter key
// return the currentLevel
//2. if the current key is less than the parameter key
// go left and increment the level
//3. if the current key is greater than the paramete key
// go right and increment the level
//4. if none of these cases are met (key is not in tree
// return -1
I am new to java so forgive the beginner level of the question