0

You will implement a heap from the following interface:

public interface Heap<V extends Comparable<V>> {
  public void add(V value);
  public V[] toArray(V[] array);
  public V remove();
  public void fromArray(V[] array);
  public V[] getSortedContents(V[] array);
}

However, the Heap should be implemented as a Node Heap, that is, the internal implementation should be a tree instead of an array. The getSortedContents method should, internally, transform the heap into its array representation, and the perform Heap-Sort on it, then return the array as the result.

This prompt is kinda confusing to me. My question is what exactly is this asking me to do? I am having trouble understanding the prompt. Does it want me to just create 1 class called NodeHeap.java that implements Heap?

UPDATE: So would my NodeHeap class look something like this? or am I completely wrong

public class NodeHeap<V> implements Heap<V> {

@Override
public void add(V value) {


}

@Override
public V[] toArray(V[] array) {

    return null;
}

@Override
public V remove() {

    return null;
}

@Override
public void fromArray(V[] array) {


}

@Override
public V[] getSortedContents(V[] array) {

    return null;
}

}'

1 Answers1

0

The main concern of the question seems to be that the data is stored internally in a tree (and a heap tree I assume), instead of implemented with an array/list. In fact I take the Node Heap as "heap implemented by node", but it works ok as a class name too.

While you can implement it in one class, given the fact that Java only has one tree model in the Swing package, it is pretty common to implement your own tree, which certainly involves more classes (can be inner or anonymous). The question doesn't seem to mind that.

UPDATE: The code looks like a good start, assuming you are not going to actually return null when you are done.

Community
  • 1
  • 1
Sheepy
  • 17,324
  • 4
  • 45
  • 69