lecture-15

Trickle down new subtree root cpsc 223 fall 2010 27

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: // number of items in the heap 10 we may have empty slots at the end … 9 8 size = 6 items = 10 9 5 2 4 0 1 8 5 2 4 2 3 4 5 6 CPSC 223  ­ ­ Fall 2010 25 Array-Based Heap Implementation Entry Heap::findMax() { if(!isEmpty()) return items[0]; else … handle empty case … } What is the worst case Lme complexity of findMax? 10 we may have empty slots at the end … 9 8 size = 6 items = 10 9 5 2 4 0 1 CPSC 223  ­ ­ Fall 2010 8 5 2 4 2 3 4 5 6 26 13 10/21/10 Heap Implementation Deleting the max key 1.  Remove root 2.  Promote last item to root (becomes a “semiheap”) 3.  “Trickle down” the new root 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 Heap Implementation Deleting the max key Delete 10 (root) 10 9 5 8 2 Promote last item to root 5 4 4 5 8 2 4 9 Trickle down 4 again 9 8 2 Trickle down 4 9 5 4 CPSC 223  ­ ­ Fall 2010 8 2 28 14 10/21/10 Array-Based Heap Implementation void Heap::deleteMax() { if(!isEmpty()) { size ­ ­; items[0] = items[size]; heapRebuild(0); } } size = 6 10 items = 10 9 9 8 8 5 2 4 2 4 1 2 3 4 5 items = 4 9 8 5 2 4 0 5 0 1 2 3 4 5 size = 5 CPSC 223  ­ ­ Fall 2010 6 6 29 Array-Based Heap Implementation void Heap::heapRebuild(int root) { int child = 2*root + 1;...
View Full Document

This document was uploaded on 03/18/2014 for the course CPSC 223 at Gonzaga.

Ask a homework question - tutors are online