【问题标题】:Maxheap vs priorityqueue confusionMaxheap vs priorityqueue 混淆
【发布时间】:2016-07-21 04:09:54
【问题描述】:

假设我们要根据值对 hashmap 进行排序。我们实现了一个带有比较器的 priorityQueue 来做到这一点。结果,生成的 pq 是从索引 0 到结尾从最大到最小排序。

代码如下:

PriorityQueue<Map.Entry<Integer, Integer>> pq = new PriorityQueue<Map.Entry<Integer, Integer>>(
                new Comparator<Map.Entry<Integer, Integer>>() {
                    @Override
                    public int compare(Entry<Integer, Integer> o1, Entry<Integer, Integer> o2) {
                        return o2.getValue() - o1.getValue();
                    }
                });

但是,有人说它是 maxheap,我知道 heap 只是父值大于子值,但我不明白为什么它是 maxheap?它只是在priorityQueue中实现比较器?这与堆有何关系?

【问题讨论】:

    标签: java heap priority-queue max-heap minmax-heap


    【解决方案1】:

    java中优先级队列的内部结构是堆。
    此比较器将用于与优先级队列中的父值进行比较。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-26
      • 1970-01-01
      • 2016-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-08
      • 2016-09-11
      相关资源
      最近更新 更多