Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java implementation for Min-Max Heap?

Do you know of a popular library (Apache, Google, etc, collections) which has a reliable Java implementation for a min-max heap, that is a heap which allows to peek its minimum and maximum value in O(1) and to remove an element in O(log n)?

like image 729
Yuval Adam Avatar asked Jul 08 '09 14:07

Yuval Adam


2 Answers

From Guava: MinMaxPriorityQueue.

like image 128
Louis Wasserman Avatar answered Sep 21 '22 20:09

Louis Wasserman


Instead of a max-min heap, could you use two instances of a java.util.PriorityQueue containing the same elements? The first instance would be passed a comparator which puts the maximum at the head, and the second instance would use a comparator which puts the minimum at the head.

The downside is that add, delete, etc would have to be performed on both structures, but it should satisfy your requirements.

like image 29
Il-Bhima Avatar answered Sep 23 '22 20:09

Il-Bhima