HERE - UF CISE

advertisement
COP-3530
DATA STRUCTURES AND ALGORITHMS
Instructor: Manuel Bermúdez
Fall 2010
MIDTERM EXAM 2
Closed Books, Closed Notes, 50 Minutes
Problem 1 _______ (15p.)
Problem 2 _______ (15p.)
Problem 3 _______ (20p.)
Problem 4 _______ (25p.)
Problem 5 _______ (25p.)
SCORE _______ (100p.)
UFID :
Name :
Note: Turn in your work on this exam only
Problem 1 (15 pts).
Start with an empty Binary Search Tree.
(a) Insert the keys 10, 18, 12, 15, 5, 22, 13 into the tree, in the order given. Show the
resulting Binary Search Tree.
(b) Using this tree, which comparisons would be made when searching for the key 12?
Which comparisons would be made for 14?
(c) Label all nodes with their leftSize values. Use the tree you drew in part (a).
(d) Show the various comparisons and rank values as you search for the element with
index 4 in this Indexed Binary Search Tree.
Problem 2 (15 pts).
Draw the height biased max leftist tree that results when the two height biased max leftist
trees below are melded by following their rightmost paths. Use the strategy used in the
meld algorithm of the text, and discussed in class. Show the steps used to arrive at the
melded tree.
80
\
70
/ \
/
/
\
55
48
/ \
/\
50
52 25 46
/
33
/
\
61
64
/ \
/ \
58
59 53
45
Problem 3 (20 pts).
Start with an empty AVL search tree and insert the following keys in the given order: 25,
35, 45, 20, 22, 27. Draw the trees following each insertion, and also after each rotation.
Specify the rotation types, and the balancing factors.
Problem 4 (25 pts):
An array such as [-,1,2,3,4,5,6,7,8,9,10,11] represents a complete binary tree. Write a
C++ function to convert this tree into a max heap. Your function MUST carry out the
operation in O(n) time.
void MaxHeapify( int a [], int n) {
for (i=n/2; i >= 1; i--) {
j=i;
while (j <= n){
c1=j*2; c2=c1+1; // indices of two kids
if (c1 > n && c2 > n)
{j=c1; continue;} // j is leaf; bail
if (c1 > n)
{ swap (a,c2,j); j=c2; continue;}
// no child 1; swap with child 2; move to c2
if (c2 > n)
{ swap (a,c1,j); j=c1; continue;}
// no child 2; swap with child 1; move to c1
c = a[c1]>a[c2]?c1:c2;
swap(a,c,j);
// swap j with child c with larger value
j=c; // move to c
}
}
}
Problem 5 (25 pts.)
In the bin-packing problem, we’ve seen how to use a max tournament tree to implement
the “first-fit” strategy. Consider the “last-fit” strategy: the next item to be packed will
go in the last of the bins in which it will fit, and into a new bin if it doesn’t fit in any
existing one. For example, assume bins have capacity 10, and our list of items is 4,7,2:
First-fit: 4 goes to bin 1, 7 goes to bin 2, 2 goes to bin 1.
Last fit: 4 goes to bin 1, 7 goes to bin 2, 2 goes to bin 2.
Last-fit is not merely the mirror-image of the first-fit strategy. In the “mirror-image”
strategy, we merely renumber bins 1,2,3 as 3,2,1:
“Mirror-image-of-first-fit” : 4 goes to bin 3, 7 goes to bin 2, 2 goes to bin 3.
Describe, using pseudo-code, how to use a max winner tree to implement the “last-fit”
strategy. Specifically, write the LastFitPack(int n) method.
Hint: consider your initial setup carefully, and open new bins only when needed.
Download