我正在开发一个涉及数据结构的项目,其中包括哈希表,堆和不相交的集合林。我试图通过将其相似性表示为浮点数来传递从找到2个单词之间的相似性的方法输出的数据。数据存储在哈希表中,但我试图将其插入堆中。我的堆方法插入元素作为参数接收一个节点,该节点包含2个用于单词对的字符串和一个包含相似性值的双变量。我怎样才能将数据作为节点传递,以便我可以将其插入堆中。我不知道要传递给main方法中的heap.insert();
的参数。
HeapNode类:
public class HeapNode {
public String word0; // Strores first word
public String word1; // Stores second word
public double similarity; // Stores similarity value
public HeapNode(String word0, String word1, double similarity) {
super();
this.word0 = word0;
this.word1 = word1;
this.similarity = similarity;
}
堆类:
public class Heap {
public HeapNode[] H; // Stores array of nodes into heap
public int size; // Size of heap
public Heap(int capacity) {
H = new HeapNode[capacity + 1]; //We need to add 1 since we’re ’wasting’ H[0] this.size = 0;
}
public void insert (HeapNode node){// Insert nodes of similar words to heap
size++;
int i = size;
while ((i>1) && H[i/2].similarity*-1 > node.similarity * -1 ){
H[i] = H[i/2];
i = i/2;
}
H[i] = node;
}
主要方法
public class Word_Test {
public static void main (String [] args) throws FileNotFoundException{
Word_Clustering hash = new Word_Clustering(929);//hash table
Heap heap = new Heap(300); // Create heap
hash.insert(); // Insert words from text file
hash.wordSimilarities(); // Call method that finds similarity
heap.insert(); // Call insertion method.
System.out.println( );
}