Heaps
Pseudo code for adding/removing with a min-heap:
push(v){
// If using fixed-size array, ensure the array A is big enough
// Add v to last slot in array A
curr = A.length - 1
p = parent(curr)
while(A[curr] and A[p] violate min heap property){
// Swap values A[curr] and A[p]
// Update curr and p
}
}
pop(){
// Swap the values A[0] and A[A.length-1]
curr = 0
left = getLeft(curr)
right = getRight(curr)
while(min heap property is violated by curr, left, right){
// Determine which child has smaller value
// Swap curr with this child
// Update curr, left, right
}
// Remove A[A.length-1] from array
// Return this element back
}