Skip to content

Instantly share code, notes, and snippets.

@insipx
Created March 15, 2016 00:12
Show Gist options
  • Save insipx/202535299f55c2fa0207 to your computer and use it in GitHub Desktop.
Save insipx/202535299f55c2fa0207 to your computer and use it in GitHub Desktop.
public void insert(int val){
//root case
if(this.size == 1){
heap[ROOT] = val;
this.size++;
}else{
if(this.size >= this.maxSize){
//multiplying by two ensures a full (no null array out of bounds) next level of the heap
int increment = maxSize * 2;
int[] tmp = new int[maxSize + increment];
copyArr(heap, tmp);
heap = tmp;
}
heap[size] = val;
int parent = findParent(size);
if(heap[size] > heap[parent]) {
int curr = size;
swap(heap, curr, parent);
while(parent != ROOT){
curr = parent;
parent = findParent(parent);
if(heap[curr] > heap[parent]) {
swap(heap, curr, parent);
}
}
}
this.size++;
//this.HP++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment