Uploaded by Mohit Barai

exam3-key

advertisement
CS 357 Exam 3
Fall 2008
Solution
Grade will be based on your best 7 solutions to these 8 problems. Either omit one or try all eight.
1. Let H denote the given max-ordered heap.
a. First redraw the heap after calling removeMax( ) on H.
b. Also redraw the heap after inserting key 11 into the original heap H.
10
8
2
3
a.
9
7
6
5
4
0
1
9
3
2
1
b.
8
7
6
5
0
4
11
8
2
3
9
10
6
7
4
1
0
5
2. Let H denote a min-ordered heap with seven distinct values, and let sorted order mean either
ascending or descending order. Answer each question below, and justify each answer by
either drawing such a heap H or explaining why no such heap H can possibly exist.
a. Can a pre-order traversal of H yield the values of H in sorted order?
b. Can a post-order traversal of H yield the values of H in sorted order?
c. Can an in-order traversal of H yield the values of H in sorted order?
a.Yes
3
1
b.Yes
2
5
4
6
7
7
1
5
2
6
4
3
c. No. The root’s value is
smaller than both of
its children’s values.
But the root is visited
after its left child and
before its right child.
3. Write the C++ code for method Heap::insert, assuming the heap is a min-ordered heap
represented using an array A.
class Heap {
int size, A[MAX+1];
public: Heap( ): size(0) { }
void insert (int);
// Write this method.
int removeMin( );
};
void Heap::insert (int key) {
if (size==MAX) { cout << “Error: array is full” << endl; return; }
A[++size]=key;
for (int j=size; j>1 && A[j]<A[j/2]; j/=2) {
int temp=A[j]; A[j]=A[j/2]; A[j/2]=temp;
}
}
4. Consider representing a priority queue PQ using several possible data structures. Express the
worst-case running time of each operation as a simple function of n, where n = size of PQ.
PQ is represented PQ is represented PQ is represented PQ is represented
as an unsorted
as a sorted
as a min-ordered as a max-ordered
sequence
sequence
heap
heap
insert
(1)
(n)
(lg n)
(lg n)
minKey
(n)
(1)
(1)
(n)
removeMin
(n)
(1)
(lg n)
(n)
maxKey
(n)
(1)
(n)
(1)
removeMax
(n)
(1)
(n)
(lg n)
5. Begin with an empty hash table of size 7 and use the hash function H(x) = x % 7. Insert the keys
22, 36, 43, 71, 30, 16 (in that order), and resolve collisions using each technique specified below.
a. Quadratic probing.
0
1
2
3
4
5
6
22
36
71
16
43
30
b. Double hashing using the secondary
hash function H’(x) = 5 – (x % 5).
0
30
1
22
2
71
3
43
4
5
36
6
16
6. Write a C++ hash function that takes two parameters, a string S and an integer M. It should
map the string S to an integer value in the range 0…M–1, such that hash values are
distributed as uniformly as possible.
int H (string S, int M) {
// Better algorithms exist, but this one is simple and satisfactory:
int total=0;
for (int k=0; k<S.length( ); k++)
total += S[k];
return total % M;
}
7. Suppose M randomly selected keys are hashed into the range 0…N–1 using a uniform
hashing function. Determine the probabilities that (i) all M keys yield the same hash value,
and (ii) all M keys yield distinct hash values.
(i) N*(1/N)M = 1/NM–1
(ii) (N/N)*((N–1)/N)*((N–2)/N)*…*((N–M+1)/N) = N!/((N-M)!*NM) if M N, otherwise 0
8. Let SL denote the skip list as shown.
a. Circle the nodes of SL that are traversed while searching for key 12.
b. Also redraw the skip list after inserting keys 2, 4, 8, 10 (in that order) into SL, assuming
the coin tosses yield Tail, Head, Tail, Head, Head, Tail, Head, Head, Head, Tail.
-
+
6
3
-
-
1
3
+
6
5
6
9
7
9
+
11
a. The traversed
nodes are now
underlined.
+
b.
-
+
-
-
-
-
1
2
3
4
3
4
5
10
+
10
+
+
6
8
6
8
9
10
8
9
10
6
7
11
+
Download