What is the easiest and most effective way to make a bunch of minutes in Scala?

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?

+7
scala data-structures priority-queue
source share
2 answers

You will need to define your own 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 
+12
source share

August 2016 update: you can consider the offer of chrisokasaki/scads/scala/heapTraits.scala from Chris Okasaki ( chrisokasaki ) .

This sentence illustrates the "not-so-light" part of 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, and
  • when merging two existing heaps, heaps are guaranteed to be created with the same order.

See its design .

 val h1 = LeftistHeap.Min.empty[Int] // an empty min-heap of integers 
0
source share

All Articles