









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










CPSC 223 -‐-‐ Fall 2010 2
CPSC 223 -‐-‐ Fall 2010 3
CPSC 223 -‐-‐ Fall 2010 4
Remove B
CPSC 223 -‐-‐ Fall 2010 19
Entry Heap:: findMax () { if(! isEmpty ()) return items[0]; else … handle empty case … } CPSC 223 -‐-‐ Fall 2010 26 10 9 8 5 2 4 10 9 8 5 2 4 0 1 2 3 4 5 6 we may have empty slots at the end … items = size = 6
a). If root’s search key smaller than largest child b). Then exchange (swap) them c). Trickle down new subtree root CPSC 223 -‐-‐ Fall 2010 27
CPSC 223 -‐-‐ Fall 2010 28 10 9 8 5 2 4 Delete 10 (root) 4 9 8 5 2 Promote last item to root 9 4 8 5 2 Trickle down 4 9 5 8 4 2 Trickle down 4 again
CPSC 223 -‐-‐ Fall 2010 31
a). If item’s search key is greater than root’s search key b). Then exchange (swap) them c). Trickle up new root CPSC 223 -‐-‐ Fall 2010 32 9 6 7 5 2 4 Insert 10 10 9 6 10 5 2 4 Trickle up 7 10 6 9 5 2 4 Trickle up 7
void Heap:: insert (const Entry& newItem) { if(size < MAXITEMS – 1) { int loc = size; int parent = (loc – 1)/2; items[loc] = newItem; while(parent >= 0 && items[parent] < newItem) { swap (items[parent], items[loc]); loc = parent; parent = (loc – 1)/ } size++; } } CPSC 223 -‐-‐ Fall 2010 33