120310COMP250

# 120310COMP250 - 18:37 HEAPS cont’d When you are pushing...

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
This is the end of the preview. Sign up to access the rest of the 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 h­l positions…(swap h­l 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

{[ snackBarMessage ]}

### Page1 / 3

120310COMP250 - 18:37 HEAPS cont’d When you are pushing...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online