0% found this document useful (0 votes)
1 views9 pages

F24 CS112 Final Regular

This document is an exam for CS112 Data Structures, consisting of various problems related to data structures, algorithms, and graph theory. It includes sections on Big-O notation, binary search trees, hash tables, graphs, and sorting algorithms, with specific tasks such as writing code and analyzing data structures. The exam is structured across multiple pages and is worth a total of 150 points.

Uploaded by

candyoran8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views9 pages

F24 CS112 Final Regular

This document is an exam for CS112 Data Structures, consisting of various problems related to data structures, algorithms, and graph theory. It includes sections on Big-O notation, binary search trees, hash tables, graphs, and sorting algorithms, with specific tasks such as writing code and analyzing data structures. The exam is structured across multiple pages and is worth a total of 150 points.

Uploaded by

candyoran8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

CS112 Data Structures - Final - Fall 2024

Name: _____________________________________ NetID: __________________________

• WRITE your name and NetID on EVERY page.


• DO NOT REMOVE the staple on your exam.
• WRITE NEATLY AND CLEARLY. If we cannot read your handwriting, you will not receive credit.
Please plan your space usage. No additional paper will be given.
• This exam is worth 150 points.
• This exam has 9 pages, make sure you have all the pages.

Problem 1 – Miscellaneous Part 1 (15 points)

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.

Data Structure Search


unsorted array
unsorted linked list

sorted array

sorted linked list


regular BST

2-3 Tree
red-black BST

2. (3 points) Is the array below representing a Min-PQ (Yes/No)? _______________

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.

for (int k = n/2; k >= 1; k--)


sink(a, k, n);

index 0 1 2 3 4 5 6 7 8 9 10 11

1
CS112 Data Structures - Final - Fall 2024

Name: _____________________________________ NetID: __________________________

Problem 2 – Miscellaneous Part 2 (15 points)


1. (9 points) A way to build a balanced binary search tree from a sorted array is to use an approach
similar to binary search: the middle element is the root, the elements to the left belong to the left
subtree, and the elements to the right belong to the right subtree.

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.

For example, suppose that you have the array:

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):

public class BSTNode {


public int data;
public BSTNode left,right;
}

Write Java code for the method:

public static BSTNode buildTree(int[] a,int first, int last) {

2
CS112 Data Structures - Final - Fall 2024

Name: _____________________________________ NetID: __________________________

2. (3 points) Given the following 2-3 tree:

Draw the tree after inserting 7.

3. (3 points) Given the following 2-3 tree:

Draw the equivalent Left Leaning Red Black tree. Label red links with an R.

3
CS112 Data Structures - Final - Fall 2024

Name: _____________________________________ NetID: __________________________

Problem 3 - Hash Table (40 points)


1. (20 points) Write a resize method for the Separate Chaining hash table class:
public class SCHashST<Key,Value>{
private static class Node{
private Key key;
private Value val;
private Node next;
. . .

private int m = 10; // size of the hash table


private Node[] st = new Node[m];
. . .
public void put(Key key, Value val){
. . .

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).

Write java code for the method:

private void resize(int m) { // m is the size of the hash table

4
CS112 Data Structures - Final - Fall 2024

Name: _____________________________________ NetID: __________________________

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.

Show the contents of each one of the “chains”.

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.

Show the contents of the keys array.

5
CS112 Data Structures - Final - Fall 2024

Name: _____________________________________ NetID: __________________________

Problem 4 – Graphs (40 points)


1. (5 points) Assume an undirected 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
beginning of the linked list):
(3, 4) (5, 0) (1, 0) (9, 12) (4, 5) (2, 0) (12, 11) (2, 10) (5, 6) (6, 7)

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

Name: _____________________________________ NetID: __________________________

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

Name: _____________________________________ NetID: __________________________

Problem 5 – Sorting (40 points)

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

Which statement is correct?


a. The algorithm might be either selection sort or insertion sort.
b. The algorithm is not selection sort, but it might be insertion sort.
c. The algorithm might be selection sort, but it is not insertion sort.
d. The algorithm is neither selection sort nor insertion sort.

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

Name: _____________________________________ NetID: __________________________

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

After 1st call to partition()


0 1 2 3 4 5 6 7 8 9

After 2nd call to partition()


0 1 2 3 4 5 6 7 8 9

After 3rd call to partition()


0 1 2 3 4 5 6 7 8 9

After 4th call to partition()


0 1 2 3 4 5 6 7 8 9

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy