I've read a little about binary heaps/priority queues and decided to try my hand at making my own implementation of one. I'm not the most experienced programmer and I would be really grateful if you guys could take a look at my heap class and tell me if it's a good implementation.
What can I improve here? Any feedback is welcome.
public class Heap<T extends Comparable<? super T>> {
private T[] array = (T[])new Comparable[10];
private int size = 0;
public void insert(T data) {
if(size+1 > array.length) expandArray();
array[size++] = data;
int pos = size-1;
T temp;
while(pos != 0 && array[pos].compareTo(array[pos/2]) < 0) {
temp = array[pos/2];
array[pos/2] = array[pos];
array[pos] = temp;
pos /= 2;
}
}
public T deleteMin() {
T min = array[0];
array[0] = array[size-1];
array[size-1] = null;
size--;
int pos = 0;
T temp;
boolean done = false;
while(pos*2+1 < size && !done) {
int minChild = pos*2+1;
if(array[minChild].compareTo(array[pos*2+2]) > 0) minChild = pos*2+2;
if(array[pos].compareTo(array[minChild]) > 0) {
temp = array[minChild];
array[minChild] = array[pos];
array[pos] = temp;
pos = minChild;
}
else done = true;
}
return min;
}
private void expandArray() {
T[] newArray = (T[])new Comparable[array.length*2];
for(int i = 0; i < array.length; i++)
newArray[i] = array[i];
array = newArray;
}
}