Question

This is what I have so far. I have to create an insert and delete method...

This is what I have so far. I have to create an insert and delete method for the heap.

public class HeapClass<E> {

private int size;

private E [] heap;

public HeapClass(){

this.heap = (E[]) new Object[10];

}

public HeapClass(E root) {

this.heap = (E[]) new Object[10];

this.heap[0] = root;

}

public HeapClass(E[] heap) {

this.heap = heap;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class HeapClass <E extends Comparable<E>>{
private static final int CAPACITY = 2;
private int size;
private E[] heap;
  
public HeapClass()
{
this.size = 0;
this.heap = (E[]) new Comparable[CAPACITY];
}
  
public HeapClass(E[] heap)
{
this.size = heap.length;
this.heap = (E[]) new Comparable[heap.length+1];
}
  
public HeapClass(E root)
{
this.heap = (E[]) new Comparable[10];
this.heap[0] = root;
}
  
private void doubleSize()
{
E [] old = this.heap;
this.heap = (E []) new Comparable[this.heap.length * 2];
System.arraycopy(old, 1, this.heap, 1, this.size);
}
  
public void insert(E x)
{
if(this.size == this.heap.length - 1) doubleSize();

//Insert a new item to the end of the array
int pos = ++this.size;

//Percolate up
for(; pos > 1 && x.compareTo(this.heap[pos/2]) < 0; pos = pos/2 )
this.heap[pos] = this.heap[pos/2];

this.heap[pos] = x;
}
  
private void percolatingDown(int k)
{
E tmp = this.heap[k];
int child;

for(; 2*k <= this.size; k = child)
{
child = 2*k;

if(child != this.size && this.heap[child].compareTo(this.heap[child + 1]) > 0) child++;

if(tmp.compareTo(this.heap[child]) > 0)
this.heap[k] = this.heap[child];
else
break;
}
this.heap[k] = tmp;
}
  
public E deleteFromTop() throws RuntimeException
{
if (size == 0) throw new RuntimeException();
E min = heap[1];
heap[1] = heap[size--];
percolatingDown(1);
return min;
}
  
public void printHeap()
{
System.out.print("Heap: ");
for(int i = 0; i < this.heap.length; i++)
{
if(this.heap[i] != null)
{
System.out.print(this.heap[i] + " ");
}
}
System.out.println();
}
  
public static void main(String[]args)
{
HeapClass<String> heap = new HeapClass<>();
heap.insert("c");
heap.insert("h");
heap.insert("e");
heap.insert("g");
heap.insert("g");
heap.insert("i");
heap.insert("n");
heap.insert("g");
heap.printHeap();
  
heap.deleteFromTop();
heap.printHeap();
}
}

******************************************************************** SCREENSHOT *******************************************************

Add a comment
Know the answer?
Add Answer to:
This is what I have so far. I have to create an insert and delete method...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT