Heaps
Pseudo code for adding/removing from a heap:
push(v){
// 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 max 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(max heap property is violated by curr, left, right){
// Determine which child has larger value
// Swap curr with this child
// Update curr, left, right
}
// Remove A[A.length-1] from array
// Return this element back
}