バイナリツリーに文字列を挿入する方法に問題があります。以下のコードは問題のメソッドです。基本的には、単語がまだツリーにない場合(BinaryTreeNode
)、ツリー内にある場合はその頻度(count変数はBinaryTreeNode
)が1つ増えます。私の問題は一時変数searchWord
です。 String
と定義するとタイプの不一致が発生し、タイプString
にはgetFrequency()
というステートメントが定義されていません。ジェネリック型T
は、プレースホルダとしてのみ存在し、動作しません。したがって、それを何と定義すべきですか?バイナリツリー変数型の問題
buildBinaryTree方法:
public static void buildBinaryTree(String word) {
//if word is already in tree
if(wordTree.contains(word)) {
//find existing word node
T searchWord = wordTree.find(word); //problem here
//increment frequency by 1
searchWord.setFrequency(searchWord.getFrequency() + 1);
} else {
//add word to tree
System.out.println(word);
wordTree.addElement(word);
}
}
BinaryTreeNodeコンストラクタ:
/**
* Creates a new tree node with the specified data.
* @param obj the element that will become a part of the new tree node
*/
BinaryTreeNode(T obj) {
element = obj;
left = null;
right = null;
frequency = 1;
}
周波数を取得/ setメソッド:
/**
* Gets the frequency.
* @return the frequency
*/
public int getFrequency() {
return frequency;
}
/**
* Sets the frequency.
* @param frequency the frequency to set
*/
public void setFrequency(int frequency) {
this.frequency = frequency;
}
getFrequencyメソッドはどこで定義されていますか? –
'BinaryTreeNode()' – lollercopter