将节点插入优先级队列java

将节点插入优先级队列java,java,priority-queue,Java,Priority Queue,嗨,我想把我创建的所有节点都放到pq中,这样我就可以根据权重对它们进行排序,然后删除前两项(最小的),这样我就可以构建一个哈夫曼树,这是一个二叉树的特殊版本,最好的方法是什么?谢谢 public class Main { public void main(String[] args) throws IOException { long start = System.currentTimeMillis(); String inputFileName =

嗨,我想把我创建的所有节点都放到pq中,这样我就可以根据权重对它们进行排序,然后删除前两项(最小的),这样我就可以构建一个哈夫曼树,这是一个二叉树的特殊版本,最好的方法是什么?谢谢

public class Main {

    public void main(String[] args) throws IOException {

        long start = System.currentTimeMillis();
        String inputFileName = args[0];
        FileReader reader = new FileReader(inputFileName);
        Scanner in = new Scanner(reader);

        // read in the data and do the work here
        // read a line at a time to enable newlines to be detected and allowed for

        while(in.hasNext()){
            CharacterMap<Character, Integer> hashMap = new CharacterMap<Character, Integer>();
            char[] chars = scanner.nextLine().toLowerCase().toCharArray();
            int c_count = 0;
            for (Character c : chars) {
                c_count += 1;
                if (hashMap.containsKey(c)) {
                    hashMap.put(c, hashMap.get(c) + 1);
                } else {
                    hashMap.put(c, 1);
                }
        }

            PriorityQueue<Node> pq = new PriorityQueue<Node>(new Comparator<Node>() {

            for (Map.Entry<Character, Integer> entry : hashMap.entrySet()){

                Node n = new Node();

                int f = entry.getValue();
                String c = entry.getKey();
                n.setWeight(f);
                n.setCharacter(c);

                n.setLeftChild(null);
                n.setRightChild(null);
                pq.add(n);
            }

        reader.close();

        String outputFileName = args[1];
        FileWriter writer = new FileWriter(outputFileName);
        writer.write("Input file " + inputFileName + "  Huffman algorithm\n\n");

        // write out the results here

        long end = System.currentTimeMillis();
        writer.write("\nElapsed time: " + (end - start) + " milliseconds");
        writer.close();
    }

}
公共类主{
public void main(字符串[]args)引发IOException{
长启动=System.currentTimeMillis();
字符串inputFileName=args[0];
FileReader=新的FileReader(inputFileName);
扫描仪输入=新扫描仪(读卡器);
//读入数据并在此处完成工作
//一次读取一行,以检测并允许换行
while(在.hasNext()中){
CharacterMap hashMap=新CharacterMap();
char[]chars=scanner.nextLine().toLowerCase().toCharray();
int c_计数=0;
for(字符c:字符){
c_计数+=1;
if(hashMap.containsKey(c)){
hashMap.put(c,hashMap.get(c)+1);
}否则{
hashMap.put(c,1);
}
}
PriorityQueue pq=新的PriorityQueue(新的比较器(){
for(Map.Entry:hashMap.entrySet()){
节点n=新节点();
int f=entry.getValue();
字符串c=entry.getKey();
n、 设定重量(f);
n、 设置字符(c);
n、 setLeftChild(null);
n、 setRightChild(空);
pq.添加(n);
}
reader.close();
字符串outputFileName=args[1];
FileWriter writer=新的FileWriter(outputFileName);
write(“输入文件”+inputFileName+“哈夫曼算法”\n\n”);
//把结果写在这里
long end=System.currentTimeMillis();
write(“\n失效时间:”+(结束-开始)+“毫秒”);
writer.close();
}
}

如果您想让优先级队列的Java实现按权重排序节点,您必须让节点类实现“comparable”接口。这意味着在节点类中放置一个“compareTo”方法,并定义按权重排序的比较,例如:

private static class Node implements Comparable<Node>{
    public int compareTo(Node n) {
        if(this.weight < n.weight){
            return -1;
        }else if(this.weight > n.weight){
            return 1;
        }else
            return 0;
    }
}
私有静态类节点实现可比较{
公共整数比较(节点n){
如果(此重量n重量){
返回1;
}否则
返回0;
}
}

在快速扫描代码时,您似乎已经这样做了。问题是什么?一旦我添加了它们,我不知道如何让队列按权重排序其中一个节点实例变量。当我执行pq时,节点是否会自动按权重顺序插入。添加(n)?这样我就可以执行pq.remove()要获得最小的权重?是的,没错。Java实现的优先级队列实际上是一个。因此,当添加一个项时,它会与它的父/子项进行比较,并“冒泡”到正确的位置。