This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 18:37 HEAPS cont’d When you are pushing down a heap, you have a lot of nodes near the bottom which only have to be pushed down a little. results in a faster than nlogn runtime downHeap <3,14> assume left and right children of I are heaps; make heap from the subtree rooted at i do this for all children if you start at the last row when everything is a leaf, every node is a heap how many leaves are there?
Picture 1 buildHeap(fast) for i = n/2 to 1 downHeap(a, i, n) start at the parent of the node, and work our way back; everytime you reach a node, you push it down so that it becomes the root of a heap. (check ) downHeap(a, I, n) child = 2*i 12/03/10 COMP250 Lecture 23 if child < n o if a[child+1] < a[child] child = child + 1 if child <= n if a[child] < a[i] { //if child is smaller, swap swap(a[child], a[i]) downHeap(a, child, n) } } How many nodes do you have at l? For each of those nodes at lvl l, need to push down hl positions…(swap hl times) • means the total number of swaps is: 12/03/10 COMP250 Lecture 23 If at level h, have to do zero swaps since h –h = 0 (level = h) ...
View
Full
Document
 Winter '08
 BLANCHETTE
 Computer Science

Click to edit the document details