我遇到了一個方法,我寫入字符串字插入二進制樹的方法。下面的代碼是有問題的方法。基本上,如果該單詞尚不在樹中(如BinaryTreeNode
),則插入該單詞,如果該單詞在樹中,則其頻率(在BinaryTreeNode
內的計數變量)將增加1。我的問題是與臨時變量searchWord
。將其定義爲String
會產生類型不匹配,並且getFrequency()
未被定義爲String
類型的語句。通用類型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;
}
頻率get/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方法在哪裏定義? – 2012-04-10 03:30:19
'BinaryTreeNode()'。 – lollercopter 2012-04-10 03:32:33