0

I want to know that what class from java's standard collection can be made a parent class of Min-Heap or Max-Heap? I develop the class Heap which can convert the heap to min or max depending upon strategy and used methods like add, toString, toArray for serving the purpose of standard collection method names.I need to make a parent class for Heap. Which class or collection i can extend?

I am using Node structure of left -right child.

user2864740
  • 60,010
  • 15
  • 145
  • 220
user2801336
  • 187
  • 1
  • 2
  • 10
  • 2
    There is no standard "Heap" structure in Java, although a [PriorityQueue](http://docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html) might be usable for some situations. – user2864740 Mar 05 '14 at 21:57
  • 1
    In any case, I would *not* extend an existing [collection] class for a custom collection, but rather implement the appropriate interface(s), the most trivial being `Collection`. (I'm fairly opposed to subtype polymorphism, excepting for cases where using such greatly simplifies a a problem, which I find very infrequent.) – user2864740 Mar 05 '14 at 21:59

2 Answers2

4

Javas PriorityQueue is (usually; a JRE would be allowed to do differently) implemented as a heap.

If you want the other sort order, use a reverse comparator, e.g. Collections.reverseOrder().

There is no need for a parent class for a min or max heap; as they are the same thing, just with different sort order.

So nothing for you to do, use PriorityQueue.

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
1

There is no standard and I don't recommend implementing the data structure yourself. Luckily, Guava has an implementation of min-max heap based on the one by Atkinson et al., which only uses an array to store data.

Giovanni Botta
  • 9,626
  • 5
  • 51
  • 94