Heaps
An Introduction |
Prof. David Bernstein |
Computer Science Department |
bernstdh@jmu.edu |
A 3-Heap
siftup(i) { while (i is not the root node and i.value > i.parent.value) { swap(i, i.parent); } }
Suppose a node with value 44 is added to the end of the 3-heap above...
siftdown(i) { while (i is not a leaf node and i.value < i.maxchild.value) { swap(i, i.maxchild); } }
Suppose the value of the root node is changed to 43...
add(i) { add i to the back of the heap; siftup(i); }
remove() { swap(front, back); remove the back element; siftdown(front); }