# HW6A - child percolate down one level if H>Elements...

This preview shows pages 1–3. Sign up to view the full content.

p.212 6.2 a. Show the result of inserting 10, 12, 1, 14, 6, 5, 8, 15, 3, 9, 7, 4, 11, 13, and 2, one at a time, into an initially empty binary heap. b. Show the result of using the linear-time algorithm to build a binary heap using the same input. a. 10 12 1 14 6 5 8 15 3 9 7 4 11 13 2 b. 10 12 1 14 6 5 8 15 3 9 7 4 11 13 2 p.212 6.3 Show the result of performing three DeleteMin operations in the heap of the previous exercise. a. 10 12 14 6 5 8 15 9 7 4 11 13 b.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
10 12 14 6 5 8 15 9 7 4 11 13 p.212 6.4 Write the routines to do a "percolate up" and a "percolate down" in a binary heap. void PercolateUp( int p, PriorityQueue H ) { ElementType Tmp = H->Elements[ p ]; /* save this key */ for ( ; H->Elements[ p / 2 ] > Tmp; p /= 2 ) /* as long as parent is larger */ H->Elements[ p ] = H->Elements[ p / 2 ]; /* percolate up */ H->Elements[ p ] = Tmp; /* save this key at the proper position */ } void PercolateDown( int p, PriorityQueue H ) { int child; ElementType Tmp = H->Elements[ p ]; /* save this key */ for ( ; p * 2 <= H->Size; p = child ) { /* Find smaller child */ child = p * 2; if ( child != H->Size && H->Elements[ child + 1 ] < H->Elements[ child ] )
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: child++; /* percolate down one level */ if ( H->Elements[ child ] < Tmp ) H->Elements[ p ] = H->Elements[ child ]; else break ; } H->Elements[ p ] = Tmp; /* save this key at the proper position */ } p.213 6.13 If a d-heap is stored as an array, for an entry located in position i, where are the parents and children? Answer ... .. .. ... ... ... ... ... ... ... m +1 nodes Level n-1 p Level n i } l nodes Assume k n to be the number of nodes in a full d-heap of height n . Then 1 1 ... 1 1 2 − − = + + + = + d d d d d k n n n That is, 1 1 1 − − = − d d k n n md k i n . For any node on level n , there exist integers m and l such that l + + = − 1 . Since 1 1 1 1 1 2 − − − − = ⇒ + + = − − d d p m m k p n n , we obtain l d d d p d d i n n + − − − − + − − = − ) 1 1 1 ( 1 1 1 That is, , where l d p i + + − = 1 ) 1 ( d l = if % = d i , or otherwise. d i l % =...
View Full Document

## This note was uploaded on 02/16/2011 for the course CS 136 taught by Professor Yuechen during the Winter '08 term at Zhejiang University.

### Page1 / 3

HW6A - child percolate down one level if H>Elements...

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

View Full Document
Ask a homework question - tutors are online