val maxHeap = scala.collection.mutable.PriorityQueue[Int] //Gives MaxHeap使用排序将PriorityQueue转换为minHeap的最简洁、最有效的方法是什么?
发布于 2014-11-25 06:22:24
您必须定义自己的Ordering:
scala> object MinOrder extends Ordering[Int] {
def compare(x:Int, y:Int) = y compare x
}
defined object MinOrder然后在创建堆时使用它:
scala> val minHeap = scala.collection.mutable.PriorityQueue.empty(MinOrder)
minHeap: scala.collection.mutable.PriorityQueue[Int] = PriorityQueue()
scala> minHeap.ord
res1: Ordering[Int] = MinOrder$@158ac84e发布于 2016-08-14 02:24:08
2016年8月更新:您可以考虑chrisokasaki/scads/scala/heapTraits.scala来自)的建议。
这个提议说明了Heap的“不那么容易”的部分。
类型库与合并操作的概念证明。 在这里,"typesafe“意味着接口将永远不允许在同一个堆中混合不同的顺序。 特别地,
见它的设计。
val h1 = LeftistHeap.Min.empty[Int] // an empty min-heap of integershttps://stackoverflow.com/questions/27119557
复制相似问题