val maxHeap = scala.collection.mutable.PriorityQueue[Int] //Gives MaxHeap
What is the most concise and efficient way to use Ordering to turn PriorityQueue into minHeap?
You will need to define your own Ordering :
Ordering
scala> object MinOrder extends Ordering[Int] { def compare(x:Int, y:Int) = y compare x } defined object MinOrder
Then use this when creating the heap:
scala> val minHeap = scala.collection.mutable.PriorityQueue.empty(MinOrder) minHeap: scala.collection.mutable.PriorityQueue[Int] = PriorityQueue() scala> minHeap.ord res1: Ordering[Int] = MinOrder$@158ac84e
August 2016 update: you can consider the offer of chrisokasaki/scads/scala/heapTraits.scala from Chris Okasaki ( chrisokasaki ) .
chrisokasaki/scads/scala/heapTraits.scala
chrisokasaki
This sentence illustrates the "not-so-light" part of Heap :
Heap
Proof of concept for cluster types with merge operation.Here, "typafe" means that the interface will never allow you to mix different orderings on the same heap. In particular,when adding an item to an existing heap, this insert cannot contain an order different from the one used to create the existing heap, andwhen merging two existing heaps, heaps are guaranteed to be created with the same order.
Proof of concept for cluster types with merge operation.Here, "typafe" means that the interface will never allow you to mix different orderings on the same heap. In particular,
See its design .
val h1 = LeftistHeap.Min.empty[Int] // an empty min-heap of integers