CPSC 289: Data Structures & Algorithms
Information and Review Questions for Exam 2
Spring 2009
General Information
The second exam will be on Tuesday April 7, 2009.
It will cover material from
Chapters 8, 9, and 10 in the text.
It will be a closed book exam (no notes, books, or neighbors)
except you will be allowed a one page (8.5x11), two-side
cheat sheet which must be turned in with the exam.
There will be 5 questions, each worth 20 points, for a total of 100 points.
Remember to show your work - partial credit will be given.
Material
All of the following are considered to be fair game:
-
Reading.
Chapters 8 (except 8.5),
9 (only 9.1 and 9.2), and
10 (only 10.1, 10.3, 10.5, 10.6) in the text.
-
Lectures. You are responsible for things that we went over that
were either not covered in the text, or that were done differently than
in the text.
-
Homeworks.
Know how to do all the problems on homeworks 5-7.
-
Quizzes.
Understand and know the answers to all the questions on Quizzes 5-7.
Review Questions
Below are some suggested review exercises/problems for the exam.
It is strongly recommended that you be able to do all of these
problems. Solutions will not be handed out.
Finally, be sure to check your email -- clarifications
and/or explanations will be mailed to the class if needed.
-
Study Homeworks 5-7, Quizzes 5-7, and the in class assignments;
variations of some of these questions
will appear on the exam.
-
Hashing.
Consider inserting the following keys, in this order, into a
hash table of size m=13.
keys to insert (in this order): 9, 1, 22, 14, 27
- Suppose you use chaining with the hash function
h(k) = k mod 13. Illustrate the result of inserting
the keys above using chaining.
- Suppose you use open addressing with the primary hash function
h1(k) = k mod 13. Illustrate the result of inserting
the keys above using linear probing, i.e.,
h(k,i) = (h1(k) + i) mod 13.
- Suppose you use open addressing with hash functions
h1(k) = k mod 13 and h2(k) = 1 + (k mod 12).
Illustrate the result of inserting
the keys above using double hashing, i.e.,
h(k,i) = (h1(k) + h2(k)*i) mod 13.
-
Search Trees.
-
Binary Search Tree (BST).
-
What are the best and worst case times for inserting
an item into a BST? What about removing an item from
a BST? Provide the best answer you can in terms of
n, the number of internal nodes in the BST.
-
Draw the shortest and the tallest possible BST tree
containing 11 items, with keys 1-11.
-
Consider the following sequence of keys:
(5, 16, 22, 45, 2, 10, 18, 30, 50, 12, 1)
Draw the BST that would result from inserting the sequence
of keys (in that order) into an initially empty BST.
-
Now, remove the item with key 1, and draw the resulting tree.
-
Now, remove the item with key 16, and draw the resulting tree.
-
AVL Trees. Repeat the question above using an AVL tree instead
of a BST tree.
-
Sorting.
Consider the following comparison-based sorting algorithm.
Fast-Sort(array L of integers)
if (L has less than 4 elements)
then sort L by brute-force and return L;
else
L1 := Fast-Sort(1st quarter of L);
L2 := Fast-Sort(2nd quarter of L);
L3 := Fast-Sort(3rd quarter of L);
L4 := Fast-Sort(4th quarter of L);
L := 4-way-Merge(L1,L2,L3,L4);
return L;
endif
end /*Fast-Sort*/
Assume the complexity of 4-way-Merge(L1,L2,L3,L4) is
O(n1 + n2 + n3 + n4),
where ni is the number of elements
in Li, i=1,2,3, 4.
Derive and solve the recurrence relation for the
asymptotic running time of Fast-Sort;
clearly state any assumptions you make.
(You don't have have to solve the recurrence relations
for exam 2 because you are only just starting to cover them
in discrete math now.)
-
Sorting.
Suppose you are given a list of n integers to sort that
contains only the values 1, 2, 3, and 4.
Consider how the presence of duplicate elements affects the
running time.
-
Suppose you use insertion sort.
What is the absolute number of comparisons (approximately) that will
be done in the worst-case, and how does this compare to the situation
in which all n elements are distinct?
What is the order of the number of comparisons that will be done in
the worst-case, and how does this compare to the situation
in which all n elements are distinct?
-
Is radix sort a good algorithm to use in this situation? Why or why not?
-
Is there an in-place algorithm for this problem that runs
in O(n) time? If not, explain why not. If so, give the algorithm.