简体   繁体   English

std::make_heap 相当于 Java?

[英]std::make_heap equivalent in Java?

STL has this really useful function. Is there any equivalent to this in Java? STL 有这个非常有用的 function。在 Java 中有没有等价物? I could write my custom implementation but it would be useful not to reinvent the wheel.我可以编写我的自定义实现,但不要重新发明轮子会很有用。

std::make_heap makes a binary heap, which is used as a priority-queue and is called PriorityQueue in Java. std::make_heap一个二进制堆,该堆用作优先级队列,在Java中称为PriorityQueue

Correction : there isn't a standard binary-heap class in Java, but I suggested two in the comment below. 更正 :Java中没有标准的二进制堆类,但是我在下面的注释中建议了两个。

PriorityQueue has a special constructor that can "heapify" an exisisting ArrayList (or any Collection). PriorityQueue有一个特殊的构造函数,可以“堆化”现有的 ArrayList(或任何集合)。

Example:例子:

ArrayList<Integer> x = new ArrayList<>();

PriorityQueue<Integer> pq = new PriorityQueue<Integer>(x);  // O(N)

This operation copies input whereas make_heap just reorders the array in-place.此操作复制输入,而make_heap只是就地重新排序数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM