Ich habe Probleme mit einer Methode, die ich geschrieben habe, um String-Wörter in einen Binärbaum einzufügen. Der folgende Code ist die fragliche Methode. Grundsätzlich wird das Wort eingefügt, wenn es noch nicht im Baum ist (als BinaryTreeNode
), und wenn es im Baum ist, wird seine Häufigkeit (Zählvariable innerhalb von BinaryTreeNode
) um Eins erhöht. Mein Problem ist mit der temporären Variable searchWord
. Wenn Sie es als String
definieren, wird ein Typenkonflikt erzeugt, und für den Typ String
wird keine Anweisung mit der Angabe getFrequency()
definiert. Der generische Typ T
ist nur als Platzhalter da - auch das funktioniert nicht. Wie sollte es definiert werden?Binary Tree Variablentyp Ausgabe
buildBinaryTree Methode:
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 Konstruktor:
/**
* 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;
}
Frequency get/set-Methoden:
/**
* 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;
}
Wo ist die getFrequency-Methode definiert? –
'BinaryTreeNode()'. – lollercopter