Final 12sp Solution
Final 12sp Solution
(a) Draw the resulting BST after 5 is removed, but before any rebalancing takes place. Label each node in the
resulting tree with its balance factor. Replace a node with both children using an appropriate value from the
node's left child.
1 of 12
(b) Now rebalance the tree that results from (a). Draw a new tree for each rotation that occurs when rebalancing the
AVL Tree (you only need to draw one tree that results from an RL or LR rotation). You do not need to label
these trees with balance factors.
We show the intermediate rotation from the RL rotation along with the final answer below:
2 of 12
2. Hashing (9 Points)
Simulate the behavior of a hash set storing integers given the following conditions:
(Show your work on the next page to get partial credit in case you make a mistake somewhere.)
(a) What are the indices of the values in the final hash set?
(b) Did any values fail to be inserted? If so, which ones and why did the insertion fail?
3 of 12
3. Disjoint Sets (9 Points)
Trace the sequence of operations below, using a disjoint set with union by size (without path compression). If there is
a tie when performing a union, the tree with the smaller-valued root should be the root of the union. Assume there are
initially 10 sets {1},{2},{3},…,{10}.
union(7, 8);
union(3, 6);
union(2, 5);
union(7, 10);
union(1, 4);
union(3, 7);
union(2, 7);
union(1, 9);
x = find(7);
y = find(9);
union(x, y);
(a) Draw the final forest of up-trees that result from the operations above.
(b) Draw the new forest of up-trees that results from doing a find(9) with path compression on your forest of up-
trees from (a).
4 of 12
4. Maximum Spanning Trees (10 Points)
For this problem, you will be finding the maximum spanning tree. You can still use Prim’s and Kruskal’s algorithms
if you just switch “smallest” with “biggest” when examining edges.
(a) Using Prim’s algorithm starting with vertex "A", list the vertices of the graph below in the order they are added to
the maximum spanning tree.
A, D, I, G, H, J, F, C, E, B
(b) Using Kruskal’s algorithm, list the edges of the maximum spanning tree of the graph below in the order that they
are added.
(G, H)
(C, F)
(B, E)
(H, J)
(A, D)
(F, H)
(E, F)
(A, I)
(G, I)
5 of 12
5. Graphs (15 Points)
(a) Suppose that the Graph class in Homework #7 (Kevin Bacon) was a directed graph. Write a method named
reverse that would replace all edges (v, w) with (w, v). The picture below shows a graph before and after a call to
reverse.
BEFORE AFTER
if (!newAdjacencyMap.containsKey(target)) {
newAdjacencyMap.put(target, new HashMap<V, EdgeInfo<E>>());
}
adjacencyMap = newAdjacencyMap;
}
(b) What is the running time of your method? Explain your answer.
The method processes each edge exactly once while accessing each vertex on the vertex list: O(|V| + |E|)
6 of 12
6. What’s the Point? (4 Points)
(a) What is the purpose of AVL trees?
AVL trees are binary search trees that are guaranteed to be balanced. They guarantee O(log n) operations on the tree.
Heaps are how most priority queues are implemented. They allow you to quickly find the minimum value from the
values stored in the heap without costly adds.
Is the graph…
(a) directed
(b) cyclic
(c) unweighted
Examples:
8. Sorting (4 Points)
In the Arrays class, there is a static sort method:
Arrays.sort(int[] a)
The method sorts the specified array into ascending numerical order. The sorting algorithm is a tuned quicksort.
Quicksort has a worst case running time of O(n2) whereas mergesort has a worst case running time of O(n log n), but
quicksort is generally chosen over mergesort. Why? Explain your answer.
Quicksort generally runs in O(n log n) time. The constant factor is better than mergesort, because it does the sort with
simple swaps, whereas mergesort has to create a new array to store the merged data.
7 of 12
9. Hash Functions (9 Points)
Override the hashCode method of the following (poorly-designed) class. Your method should follow proper hashing
principles and minimize collisions. You may assume that any non-primitive field in RentalCar has an equals
methods and a good implementation of hashCode.
public class RentalCar {
private int year;
private String color;
private String model;
private Person renter; // null if available
...
RentalCar o = (RentalCar)other;
return
(year / 10 == o.year / 10) // car model year in same decade
&& model.equals(o.model)
&& ((renter == null && o.renter == null) ||
(renter != null && renter.equals(o.renter)));
if (renter == null) {
result = 37 * result + 1;
} else {
result = 37 * result + renter.hashCode();
}
return result;
}
8 of 12
10. B-Trees (9 Points)
(a) What is the purpose of B-Trees?
B-Trees are trees that take into account that most data is not stored in main memory. It’s designed to minimize the
number of disk accesses that must be performed when operating on the tree.
(b) Describe how to compute M and L in terms of variables and constraints. Define your variables.
For example:
A = number of apples
B = weight of bananas
C = size of carrots
B ⎡ B ⎤
Find smallest integer M where M ≥10⋅ A + . Equivalently, M = ⎢10⋅ A + ⎥
C ⎢ C ⎥
⎢ B +K ⎥
M = ⎢ ⎥
⎣ K +P ⎦
⎢ B ⎥
L = ⎢ ⎥
€ ⎣ R ⎦
9 of 12
11. Miscellaneous (10 Points)
(a) Write a method that given an array of integers will print out a list of duplicates (separated by spaces). The
contents of the array do not have to be preserved. The duplicates should appear exactly once, though they do not have
to be in any particular order. It is OK to have an extra space at the end of the output. The method may use any
auxiliary data structures.
if (foundDuplicate) {
System.out.print(lastDuplicate);
}
System.out.println();
}
System.out.println();
}
10 of 12
(b) What is the runtime of your implementation? Explain your answer.
The runtime of the first method that uses sorting is dominated by the sort and takes O(n log n) time. Finding the
duplicates from a sorted list takes linear time and is subsumed by the runtime of the sort.
The runtime of the second is linear. It goes over the array and accesses the hash per element. Accessing the hash
table takes constant time, so the total running time is linear. Printing the elements requires going over the hash values
and only printing values that have a count greater than one. That also takes only linear time. O(n)
(a) Write (in order) the list of vertices that would be visited by running a breadth-first search (BFS) starting from G
GDEHIJABCF
(b) Write (in order) the list of vertices that would be visited by running a depth-first search (DFS) starting from G.
GDABCEFHJI
11 of 12
Grading criteria:
1. AVL Trees 7. More Graphs
(a) 4 points 1 point for (a), (b), (c)
2 element correctly removed 2 points each for (d), (e) (answer must be reasonable)
2 balance factors
(b) 6 points 8. Sorting
3 attempts rotations to get final AVL tree 1 point if bad answer
3 correct 2 points if partial answer
12 of 12