简体   繁体   English

Java 和 Scala 中的最小和最大堆

[英]Min and Max heap in Java and Scala

After doing some digging here is what I understood-在这里做了一些挖掘之后,我明白了-

Java default Priority Queue is a min heap but Scala default Priority Queue is a max heap. Java 默认优先级队列是最小堆,但 Scala 默认优先级队列是最大堆。 Is this correct?这个对吗? Why is the implementation different in scala, it just causes more confusion in my opinion.为什么 scala 中的实现不同,我认为这只会引起更多的混乱。

The Java PriorityQueue (see docs ) defines the highest-priority item to be the one with the lowest sorting order, so a min-heap is the logical choice of data structure. Java PriorityQueue (参见文档)将最高优先级项定义为具有最低排序顺序的项,因此最小堆是数据结构的逻辑选择。

The Scala PriorityQueue (see docs ) apparently defines the highest-priority item to be the highest-sorting , so a max-heap is the logical choice of data structure. Scala PriorityQueue (参见文档)显然将最高优先级项定义为最高排序,因此最大堆是数据结构的逻辑选择。

If you're asking why it is that highest priority means lowest-sorting in Java and highest-sorting is Scala, only the developers of the respective language libraries can answer that.如果您问为什么最高优先级意味着 Java 中的最低排序和最高排序是 Scala,那么只有相应语言库的开发人员才能回答这个问题。

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

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