This preview shows pages 1–4. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Second Midterm Exam, CMPSC 465, Spring 2009 Practice problems • Midterm will be on Tuesday, March 31, 8:15 PM, in 60 and 61 Willard. • This will be open book exam, you can also have notes (several sheets or one notebook). The use of communication equipment and calculators is not permitted the exam. • When you describe an algorithm, you should also justify its correctness and your estimate of the running time. • In problems where you show the working of an algorithm on diagrams each diagram is repeated to provide you with a “spare one”. Indicate which diagram contains your solution. 1 . Balanced trees, including Btrees Given a list of numbers and priorities, draw the corresponding treap, a binary search tree in which parent cannot have a smaller priority (thus the number with the largest priority is on the root). Example: (number, priority): (0,8), (1,28), (2,3), (3,25), (4,1), (5,17), (6,0), (7,9), (8,12), (9,6), (10,19), (11,17). 3 1 2 10 11 5 4 8 9 7 6 8 28 3 25 1 17 9 12 6 19 17 1 Page 222, exercise 7a (unstable/illegal nodes are in red) C C O M C O M C O P M C O P U M C O P T U M C I O P T U M C I N O P T U M C G I N O P T U G M P C I N O T U M G C I P N O T U Page 222, exercise 10 (pseudocode). We can simply insert the list elements to an initially empty 23 tree. We can do it faster if the list is already sorted. I will put a recursive code later (Saturday night). 2 2 . Heaps and Heapsort Page 229, exercises 1, 3, 6. Exercise 3. a. Finding the minimum number of keys in a heap of height h : we have h + 1 levels which we can number from 0 to h ; all levels except the last will have sizes in the sequence 1, 2, 4, etc., and the smallest possible last level has one key. Thus we have size 1 + 2 + . . . 2 h 1 + 1 = 2 h . b. If ⌊ log 2 n ⌋ = h then 2 h ≤ n < 2 h +1 , so by the previous observation, the heap of n keys must have height at least h and below h + 1, hence, exactly h . Exercise 6. To sort in increasing order, we use heap with the maximum at the root, so when we extract the value from the root we can send it to the end of the array fragment occupied by the heap. a. On the midterm, I expect to see diagrams for heaps, but here I save space (I will add diagrams on Saturday night): Making heap of 1, 2, 3, 4, 5: we get 5, 4, 3, 1, 2 (we checked positions 2, 1 and moved the values lower as needed). Maximum swapped with the end: 2, 4, 3, 1; 5 Correcting the heap: 4, 2, 3, 1; 5 Maximum swapped with the end: 1, 2, 3; 4, 5 Correcting the heap: 3, 2, 1; 4, 5 Maximum swapped with the end: 1, 2; 3, 4, 5 Correcting the heap: 2, 1; 3, 4, 5; Maximum swapped with the end: 1; 2, 3, 4, 5 No need to correct the heap....
View
Full
Document
This note was uploaded on 10/03/2011 for the course CMPSC 465 taught by Professor Burchardcharles during the Spring '08 term at Penn State.
 Spring '08
 BURCHARDCHARLES

Click to edit the document details