public class PriorityQueue <Type extends Comparable>{
private int size = 0;
private int currentPos = 0;
private Type array[] = null;
public PriorityQueue(int size) {
this.array = (Type[]) new Object[size];
this.size = size;
}
public PriorityQueue() {
this(50); // default size
}
private void bubbleUp(int pos) {
int parent = pos/2;
if(array[pos].compareTo(array[parent]) == -1) {
// parintele e mai mare decat fiul
// le schimbam pozitia
Type aux = array[parent];
array[parent] = array[pos];
array[pos] = aux;
this.bubbleUp(parent);
}
}
public void addElements(Type newElm) throws Exception {
if(this.currentPos == this.size) {
throw new Exception();
}
else {
this.array[++this.currentPos] = newElm;
this.bubbleUp(this.currentPos);
}
}
}
Comments