ノードの優先順位が頻度である、ノードの優先順位キューを作成したいと考えています。しかし、出力には最初の要素が正しい位置に含まれていません。残りはすべて正しい位置にあります。
import java.util.*;
class node implements Comparable<node>{
char key;
int freq;
node(){}
node(char k,int f){
key=k;
freq=f;
}
public int compareTo(node n){
if(freq>n.freq)return 1;
return 0;
}
}
public class test{
public static void main(String[] args){
node x=new node('x',4);
node a=new node('a',2);
node b=new node('b',1);
node c=new node('c',7);
PriorityQueue<node> q = new PriorityQueue<node>();
q.offer(a);
q.offer(b);
q.offer(c);
q.offer(x);
while(!q.isEmpty()){
node d=q.poll();
System.out.println(d.key+" "+d.freq);
}
}
}
出力:
a 2
b 1
x 4
c 7
順序が b 、 a、x、c であってはなりません ありがとう。