0

I am learning data structures and algorithms in my uni and I came across the Huffman code in my textbook. I tried running the code on visual studio code but I am getting an error message for the heap data structure. The message is:

Heap cannot be resolved to a type

The code is as follow:

Heap<Tree> heap = new Heap<>(); // Defined in Listing 24.10      

I also tried changing the heap to a priority queue but then the getSize() method shows an error.

Brian61354270
  • 8,690
  • 4
  • 21
  • 43
  • 3
    The code you're showing here references "Listing 24.10." Which textbook is this? What is Listing 24.10 in that book? – templatetypedef Jul 27 '21 at 20:42
  • Look up [`java.util.PriorityQueue`](https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/PriorityQueue.html) – pjs Jul 27 '21 at 20:42
  • 2
    Does this answer your question? https://stackoverflow.com/questions/14165325/is-there-a-heap-in-java – ᴓᴓᴓ Jul 27 '21 at 20:44
  • 1
    Note: "Heap" is the name of an _implementation._ "Priority Queue" is a _behavior._ A "Priority Queue" is a collection that sorts its members, and provides means to access and remove the member at one extreme end of the sort. "Heap" is the name of an efficient algorithm for doing that. – Solomon Slow Jul 27 '21 at 20:52
  • @SolomonSlow 'A "Priority Queue" is a collection that sorts its members...' Not necessarily. A binary heap implementation is only partially ordered, not sorted. – pjs Jul 27 '21 at 20:56
  • "I...tried changing the heap to a priority queue but then the getSize() method shows an error." That's a new question. You should post it as such, and your posting should include the full text of the error message and, at minimum, source code showing some context around the source line on which the error occurred. – Solomon Slow Jul 27 '21 at 20:56
  • @pjs, If you can only access the "head" then the collection effectively is sorted. The fact that some of the sorting action happens when you _remove_ an element instead of all of the sorting happening when you add an element is hidden from the caller. – Solomon Slow Jul 27 '21 at 20:57
  • Take your textbook (Intro to Java Programming) and look up the listing 24.10. The text in that listing refers you to the chapter 23 where the `Heap` class is shown as an example. Note: this is inferred from the publicly available information, notably the table of contents and the provided example source code download. – Thomas Kläger Jul 27 '21 at 21:31
  • @SolomonSlow Java's `PriorityQueue` doesn't restrict you to only the head, see [toArray()](https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/PriorityQueue.html#toArray()) which explicitly says "Returns an array containing all of the elements in this queue. **The elements are in no particular order.**" – pjs Jul 27 '21 at 22:00
  • @pjs, I don't understand what point you are trying to make. If I only want to access the members of some collection "in no particular order," then there is no reason why I should prefer to use a `PriorityQueue`. Accessing its members in a particular order is almost the entire point of that class. (the other point being, that you can add new members at any time, and they still come out sorted.) – Solomon Slow Jul 28 '21 at 14:26

3 Answers3

9

Yeah theres java.util.PriorityQueue Example:

Queue<T> heap = new PriorityQueue()

That will be min heap by default, If you wanna max heap try

Queue<T> heap = new PriorityQueue(Collections.reverseOrder())
varaprasadh
  • 488
  • 5
  • 13
1

While there isn't a Heap data structure in Java, you can use a Priority Queue, as you mentioned, to implement a heap.

To create a min heap:

PriorityQueue<Integer> min = new PriorityQueue<Integer>();

Conversely, for a max heap use Comparator.reverseOrder():

PriorityQueue<Integer> max = new PriorityQueue<Integer>(Comparator.reverseOrder());
Aniketh Malyala
  • 2,650
  • 1
  • 5
  • 14
0

Searching the Java SE documentation, there isn't a class built into Java specifically called Heap.

image of Heap search

Check your lesson and look for a library that's being used which includes a class called Heap.

Otherwise, look to other answers that provide examples of alternative heap data structures. Or other questions with the same topic. Is there a Heap in java?

Java API documentation: https://docs.oracle.com/en/java/javase/16/docs/api/index.html