F24 CS112 Final Regular
F24 CS112 Final Regular
1. (7 points; 1 point each) Write the Big-O notation for the Search operation in the worst case for each
data structure implementation in the table below, assuming N items are in the collection and N > 0.
sorted array
2-3 Tree
red-black BST
index 0 1 2 3 4 5 6 7 8 9 10 11
3 11 3 16 12 5 8 16 19 13 20
3. (5 points) Given the array contents above, write the array contents after building a MAX-heap in place
(rearranging the keys in the array) by performing the heap construction code below. Assume the sink()
method is properly defined, “a” contains the reference to the array, and “n” is the number of keys in
the array.
index 0 1 2 3 4 5 6 7 8 9 10 11
1
CS112 Data Structures - Final - Fall 2024
Write a recursive method buildTree(a,first,last) that returns the root of a binary search tree
given an array a and two indices: first and last.
int[] a={3,5,8,10,12,15,17,20};
the instruction buildTree(a,3,5)returns the root node of the binary search tree (from position 3
to position 5 of the array – positions are zero-indexed):
Assume that you have the following BSTNode class (for simplicity assume it only holds integers as
data, which is the key):
2
CS112 Data Structures - Final - Fall 2024
Draw the equivalent Left Leaning Red Black tree. Label red links with an R.
3
CS112 Data Structures - Final - Fall 2024
Your resize method must create a temporary SCHashST object, assign the new size m and instantiate
the array to the new size in your temporary object. Then, put in your temporary object each element of
the original SCHashST. After all the elements are in the temporary object, just assign both class
variables (m and st) in your temporary object to your original object (which can be accessed by using
this).
4
CS112 Data Structures - Final - Fall 2024
2. (10 points) Suppose that the following keys with the hash values given below, are inserted in the given
order (from left to right) into an initially empty table of size 7 using separate chaining with no resizing.
Insert to the beginning of the chain.
3. (10 points) Suppose that the following keys with the hash values given below, are inserted in the given
order (from left to right) into an initially empty table of size 12 using linear probing with no resizing.
5
CS112 Data Structures - Final - Fall 2024
1.1. (5 points) Assume a recursive method dfs(G, v) performs DFS on graph G from vertex v. If the
source vertex v is 5, write the order of vertices visited when performing dfs(G, v) given the
undirected graph G constructed above.
1.2. (4 points) If a boolean array marked[] is used to keep track of the vertices visited, write the array
contents after the call to dfs(G, 5) above.
index 0 1 2 3 4 5 6 7 8 9 10 11 12
marked[]
1.3. (5 points) Assume a method bfs(G, s) performs BFS on graph G from source vertex s. If the source
vertex s is 5, write the sequence of vertices visited based on the undirected graph G constructed
above.
6
CS112 Data Structures - Final - Fall 2024
2. (5 points) Assume a directed Graph is represented with an adjacency list; show the vertex-indexed array
of linked lists if there are 13 vertices with the following edges (v, w) added in sequence (add to the end of
the linked list):
(2, 0) (12, 11) (2, 10) (5, 6) (6, 7) (4, 8) (3, 4) (5, 0) (1, 0) (9, 12) (4, 5)
2.1 (4 points) If a boolean array marked[] is used to keep track of the vertices visited, write the array
contents after the call to bfs(G, 4) based on the undirected graph constructed above.
index 0 1 2 3 4 5 6 7 8 9 10 11 12
marked[]
3. (12 points; 2 points each) Assume E represents the set of edges and V represents the vertices. What is
the worst-case running time for each of the operations on the undirected graph implementations in the
table below (in terms of E and V or functions of E and V)?
iterate through
Graph check if w is
Space needed vertices
Implementation adjacent to v
adjacent to v
adjacency matrix
adjacency lists
7
CS112 Data Structures - Final - Fall 2024
1. (5 points) Show one outer loop iteration of insertion sort in the following array. The array is being
sorted in ascending order.
Write your solution in this array
index 0 1 2 3 4 5 6 7 index 0 1 2 3 4 5 6 7
a 6 2 9 11 5 7 1 3 a
2. (5 points) Show one outer loop iteration of selection sort in the following array. The array in being
sorted in ascending order.
Write your solution in this array
index 0 1 2 3 4 5 6 7 index 0 1 2 3 4 5 6 7
a 6 2 9 11 5 7 1 3 a
3. (5 points) Suppose that we are sorting an array of 8 (eight) integers in increasing order using a
quadratic sorting algorithm. After four iterations of the algorithm's outer loop, the array elements
are ordered as shown here: 3,9,17,20,41,11,7,2
4. (5 points) Heapsort and Quicksort runtime complexity is O(n log n). Why is Quicksort preferred
when sorting large arrays? Note: for quicksort, we have an O(nlogn) runtime because the array is
shuffled and the pivot is not chosen poorly.
8
CS112 Data Structures - Final - Fall 2024
5. (8 points) Show one iteration of heapsort’s sortdown (second phase) in the following array. The
array is being sorted in ascending order.
Write your solution in this array
index 0 1 2 3 4 5 6 7 index 0 1 2 3 4 5 6 7
a 10 8 9 4 7 5 6 2 a
6. (12 points) Given the following unsorted array, write the array content after each of the 1st, 2nd, 3rd,
and 4th partition, assuming the first element of the array/subarray is used as the pivot (partition
point) for the partitions.
0 1 2 3 4 5 6 7 8 9
6 15 2 9 11 5 7 14 3 8