Created
March 15, 2016 00:12
-
-
Save insipx/202535299f55c2fa0207 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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