set10_sol

# set10_sol - CS112 Spring 2011 Problem Set 10 Heap 1 Given...

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

CS112 Spring 2011: Problem Set 10 Heap ------------------------------------------------------------------------ 1. Given the following sequence of integers: 12, 19, 10, 4, 23, 7, 45, 8, 15 1. Build a heap by inserting the above set, one integer at a time, in the given sequence. Show the heap after every insertion. How many comparisons in all did it take to build the heap? 2. Perform successive /delete/ operations on the heap constructed in the previous step, until the heap is empty. Show the heap after every deletion. How many comparisons in all did it take to perform these deletions? *SOLUTION* 1. The heaps built one at a time are shown in the following table. The first column is the item that is inserted, the second column is the heap after this item is inserted (indicated by its level order traversal), and the third column is the number of item-to-item comparisons made by the sift up process. Insert Heap after insertion Number of comparisons -------------------------------------------------------------------- 12 12 0 19 19 12 1 10 19 12 10 1 4 19 12 10 4 1 23 23 19 10 4 12 2 7 23 19 10 4 12 7 1 45 45 19 23 4 12 7 10 2 8 45 19 23 8 12 7 10 4 2 15 45 19 23 15 12 7 10 4 8 2

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.

{[ snackBarMessage ]}

### Page1 / 3

set10_sol - CS112 Spring 2011 Problem Set 10 Heap 1 Given...

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

View Full Document
Ask a homework question - tutors are online