我遇到了一个方法,我写入字符串字插入二进制树的方法。下面的代码是有问题的方法。基本上,如果该单词尚不在树中(如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