単語とその頻度を保持するために splaytree を実装しており、単語と頻度 (キーと値) の各ペアを保持する Pair クラスを作成することにしました。つまり、splaytree の各ノードは Pair クラスのペアを保持します。Pair クラスは次のようになります。
public class SplayEntry<K, V> implements Comparable<SplayEntry<K, V>>{
public K word;
public V frequency;
public SplayEntry(K word, V frequency) {
this.word = word;
this.frequency = frequency;
}
getters, setters, hashCode, equals, compareTo etc...
スプレイツリー:
public class SplayTree<AnyType extends Comparable<? super AnyType>> {
public SplayTree( )
{
nullNode = new BinaryNode<AnyType>( null );
nullNode.left = nullNode.right = nullNode;
root = nullNode;
}
BinaryNode クラスがあります。
私が問題を抱えているのは、すべての単語と頻度のペアをツリーに入れ、ペアが既に存在するかどうかを確認し、存在する場合は頻度を1つ上げる方法です。テキスト ファイルを 1 行ずつ読み込み、各行を単語に分割してから、countWords() メソッドを実行しますが、現在は混乱しています。
public void countWords(String line) {
line = line.toLowerCase();
String[] words = line.split("\\P{L}+");
SplayEntry<String, Integer> entry = new SplayEntry<String, Integer>(null, null);
for (int i = 0, n = words.length; i < n; i++) {
Integer occurances = 0;
entry.setWord(words[i]);
entry.setFrequency(occurances);
if (tree.contains(entry.equals(entry)) && entry.getFrequency() == 0) {
occurances = 1;
} else {
int value = occurances.intValue();
occurances = new Integer(value + 1);
entry.setFrequency(occurances);
}
entry = new SplayEntry<String, Integer>(words[i], occurances);
tree.insert(entry);
}
}
私はこれが実際には機能していないことを知っています.SplayEntryクラスをインスタンス化する方法と順序を理解するのに助けが必要ですか? また、words 配列内のすべての単語について、それがツリー内にある (含む) SplayEntry に存在するかどうかをチェックし、その単語が新しい単語である場合、頻度は 1 になり、それ以外の場合、頻度は+1になります。最後に、新しい SplayEntry を Splaytree に追加し、それを適切なノードに配置します。
現在、必要以上に多くの時間を同じコードに取り組んでいて、自分自身を混乱させています。正しい方向に導くことができるいくつかの指針を非常に高く評価します!
私が自分自身を明確にしていない場合は教えてください。