{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CS Notes 13

# CS Notes 13 - 1*2 2 2*2 … 2 h-2*2 2 h-1*2 2 h*2 • =-2h...

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

Building a Heap out of an initial set of items with priorities 1. Just insert each item in set one at a time into a heap (starting with empty heap) o Worst case time: Log1+log2+log3+…+logn=log(1*2*3*…*n)=log(n!)=nlogn O(nlogn) 1. Use repeated sift downs o Throw the set into an array o Start from the bottom, check the max children and parent o If child>parent, sift down parent o Worst case time: Add # of comparisons for all sift downs 2 h-1 *2(h-(h-1)) S= sum of comparisons 2S= 2 h-1 *2(h-(h-1)) 2S-S = -2h+2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 *2+2 2 *2+…+2 h-2 *2+2 h-1 *2+2 h *2 • = -2h+2(2 1 +2 2 +…+2 h ) • = -2h+2(2 +2 1 +2 2 +…+2 h )-2 • = -2h+2(n)-2 • h->O(logn) • n-> O(n) • O(n) o Linear time build heap Heap Review • Insert o O(log n) • deleteMax o O(log n) • Building a heap out of a set of (item priority entries) o Repeated inserts • O(n logn) • Know/derive o Repeated sift down: O(n) • Know/Derive Updating priority of item in heap • If K is leaf, 2k+1>=n n= size of array...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online