2011 Fall2017 Final NO Solutions
2011 Fall2017 Final NO Solutions
Final Examination
April 9, 2017
Instructions:
Question Points
1 / 10
FIRST NAME: ___________________________
2 / 15
1
1. True/False Choice [10 points]
Indicate whether each of the following statements is true or false:
Question Answer
2. A self-organizing list should implement ’move to front’ heuristics if we want True False
the list to be able to quickly adapt to a changing access pattern.
4. Consider the order in which the leaves of a proper binary tree are
visited by preorder, postorder and inorder traversal. In all 3 cases the
leaves are visited in different order. True False
6. In an AVL tree, after inserting a key, we have to do at most one True False
(single or double) rotation to maintain the balance property.
7. In a m-ary heap, if the heap stores N items, then an insert() is more True False
costly than removeMin().
8. A binary search tree with n elements can be converted into a heap in True False
O(n) time.
Open addressing works well even when there are more keys than can
be stored directly in the hash table. True False
9.
10. Suppose that instead of a linked list, each bucket of a hash table ‘with
separate chaining’ is implemented as an AVL tree. In that case the True False
worst case running time to insert n keys into the table is O(log(n)).
2
2. Basics Potpourri [15 points]
int add(a,b) {
if (a==0) return b;
else if (a<0) return add(a+1, b-1);
else return add(a-1, b+1) }
Solution:
O(1)
int i=1;
int sum=0;
while (i<n2) {
for (int j=n2; j<1; j=j/2) {
sum++;
}
i += 2;
}
Solution:
O(n2)
3
(d) Linked-list coding [6 points]
Write the code that will turn the below “before” picture into “after” picture by modifying links between
the nodes shown. There may be more than one way to write the code, but you are NOT allowed to
change any existing node’s data field value. You also should not create new ListNode objects, but
you may create a ListNode variable(s) to refer to any existing node if you like.
To help maximize partial credit in case you make mistake, you need to include (brief) comments with
your code that describes the links you are trying to change. Your code should consist of no more
than 7 to 8 lines of code.
Before After
Note: “front” and “temp” in the “before” picture are reference variables pointing to the first node of the
respective lists. You can also assume that you are using the ListNode class as defined in class:
Solution:
4
3. Implementation [11 points]
input output
2 2
5 10
36 121
while (!s.isEmpty()) {
if (!q.getFront().equals(s.peek()))
return false;
s.pop();
q.dequeue();
}
return true;
}
5
4. Priority Queue [9 points]
Consider the following algorithm for sorting an array (a) with the help of a priority queue (q).
Assuming that n is the length of the input array, what is the big-O complexity of this
algorithm, if the priority queue is implemented using of the following? (Justify your answer!)
a) an unsorted array
Solution:
b) a binary heap
Solution:
c) a sorted array
Solution:
Note, even if you assume that the search for the location of a new element is done using
binary search (runs in log(n)), once this location is found a new ‘spot’ for the new element
has to be made available (i.e., subsequent element have to be shifted) – hence the overall
time of insert is O(n) in the worst case.
6
5. AVL Tree [15 points]
5.1 [6 points]
Take a look at the following two binary trees. Which of them is an AVL tree? Justify your
answer.
Tree A Tree B
Solution:
5.2 [9 points]
Insert 30 into the below tree using the AVL insertion algorithm. Write down the final AVL tree.
7
Solution:
8
6. Heaps [15 points]
0 1 2 3 4 5 6 7 8 9
17 28 34 32 97 36 52 35
Suppose the following operations are performed (in this order!) on the given heap:
How will the heap, i.e. the underlying array, look like now? (For full credit, provide the
content of the underlying array after each of the 4 operations.)
Solution:
0 1 2 3 4 5 6 7 8 9
15 32 34 35 97 36 38 52
15
32 34
35 97 36 38
52
9
7. Hash Table [15 points]
The value that was previously at index 6 has been deleted, which is represented by the XXX
in the hash table. Which of the following values might have been stored there, before it was
deleted?
26 2 17 24 6 13
There may be several correct answers, and you should write down (i.e., circle) all of those
that apply. Additionally, provide a brief justification of your answer.
Solution:
Because of probing, an element is either stored at the index corresponding to its hash or a
greater index. So, the deleted value’s hash could have been any number ending with 4, 5, or
6. Out of the given numbers, the ones that satisfy this requirement are:
26, 24, 6
H1(k) = k mod 7
Draw the table that results after inserting, in the given order, the following values:
H’(k) = 5 – (k mod 5)
10
Solution:
Solution:
Example 1 – searching for the minimum element takes O(log(n) in BST, and O(n) in hash
table.
Example 2 – printing all elements in increasing order takes O(n) in BST, and O(n2) in hash
table.
11
8. Algorithm Design [10 points]
Design an algorithm that takes:
• an array containing n distinct natural numbers
• a number k≤n
and calculates the sum of the k largest numbers in the array.
For example, if the array is {3, 7, 5, 12, 6} and k= 3, then the algorithm should return
25=(12+7+6).
You may freely use standard data structures and algorithm from the course in your solution.
Write down your algorithm as pseudocode – you do not need to write fully detailed Java
code.
Solution:
h = new heap
for each x in array
h.insert(x)
if h.size() > k then h.deleteMin()
sum = 0
while h not empty do
sum = sum + h.min()
h.deleteMin()
The idea is to loop through the array and keep adding these elements to heap h while ensuring that h
is no larger than k, and those k elements are the greatest elements of the array seen so far.
Whenever h contains (k+1) elements, we remove the smallest one so that it only contains the k
greatest elements.
Afterwards we sum up and dismantle the whole heap.
12