バイナリ検索ツリーでキーの深さを計算しようとしていますが、スタックオーバーフローエラーが発生しています。ここに私の現在のコードです。スタックオーバーフローバイナリ検索ツリーの計算深さ
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;
}
、これは私のアルゴリズム
//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
である私は、Javaに新しいですので、私は、スタックオーバーフローエラーを取得していますし、私はありません質問
パラメータ「x」を使用していないことに気付いていませんか? –
コンパレータを見せることができますか? – stinepike
指定されたキーが存在しない場合、アルゴリズムは-1を返さない –