CS II: Data Structures
Discussion worksheet: Week 15
Exam Review
1. The height of a Binary Search Tree (BST) depends on the order in which the keys are inserted
into an empty tree if no balancing operation is performed. Given an initially empty BST, in
what order will you insert the keys 1, 2, 3, 4, 5, 6, 7 so that the height of the BST is a
minimum.
2. Give a breadth first search traversal of this graph starting with B: If multiple nodes
are discovered, go in alphabetical order.
3. Give an input that results in worst case runtime for insertion sort. Give your answer as an
array of 5 integers, written in the blanks below:
___ ___ ___ ___ ___
4. Fill in the Doubly Linked List Class below, fill in getNode and detachNode.
Assume that
all the given methods are fully implemented and work correctly.
public class DLList {
public int size = 0;
private DNode sentinel = new DNode(null, null, null, null);
/** Creates a new node and inserts it at the front of the DLL. */
public void addToFront(String k, String v) {
insertFront(new DNode(k, v, null, null));
}
/** Inserts the given node into the front of the DLL. */
public void insertFront(DNode toInsert) { ... }
/** Returns node containing k, or null if node doesn’t exist. */
public DNode getNode(String k) {
}
/** Unlinks the given node from its position in the list. Also fix the pointers (next and prev) of the nodes */
private void detachNode(DNode toDetach) {
}
public static class DNode {
public String key, value;
public DNode prev, next;
public DNode(String k, String v, DNode p, DNode n) {
key = k;
value = v;
prev = p;
next = n;
}
}
}
5. Give 2 topological orderings of this acyclic directed graph:
6. An airport is developing a computer simulation of air-traffic control that handles
events such as landings and takeoffs. Each event has a time stamp that denotes the
time when the event will occur. The simulation program needs to efficiently perform
the following two fundamental operations:
a. Insert an event with a given time stamp (that is, add a future event).
b. Extract the event with the smallest time stamp (that is, determine the next
event to process).
Which data structure should be used for the above operations and why?
7. Given the starter code for the following array-based Heap, fill in the insert method. Assume
the methods given and are implemented correctly.
public class HeapPriorityQueue<K,V> extends AbstractPriorityQueue<K,V> {
/** primary collection of priority queue entries */
protected ArrayList<Entry<K,V>> heap = new ArrayList<>();
/** Moves the entry at index j higher, if necessary, to restore the heap property. */
protected void upheap(int j) {
while (j > 0) {
// continue until reaching root (or break statement)
int p = parent(j);
if (compare(heap.get(j), heap.get(p)) >= 0) break; // heap property verified
swap(j, p);
j = p;
// continue from the parent's location
}
}
@Override
public Entry<K,V> insert(K key, V value) throws IllegalArgumentException {
}
class PQEntry{
public PQEntry(K key, V value){ ... };
}
8.
Fill in the method multipleOfDepthRecursive below. Then filling the methods for the
implemented ReduceFunction, and lastly, fill in the one line in the main method.
/* Takes a depth n and a ReduceFunction f
and returns the combination of all elements multiplied in the tree at depth n,
where depth=0 is the root.
"multiply" means the result of starting with f.initialValue
and "multiplying" each element using f.combine
Requirement: must be recursive
*/
public T multipleOfDepthRecursive(int n, ReduceFunction<T,T> f) {
return multipleOfDepthRecursive(n, root, f, f.initialValue());
}
private T multipleOfDepthRecursive(int depth, TreeNode<T> node, ReduceFunction<T,T> f, T multi){
}
//Next, fill in an implementation of the reduce function
private static class IntegerMultiple implements ReduceFunction<Integer, Integer> {
@Override
public Integer combine(Integer soFar, Integer x) {
}
@Override
public Integer initialValue() {
}
}
//Fill in the main method by calling the method above and getting the multiplied values of depth
public static void main(String args[]){
BinaryTree<Integer> bt = new BinaryTree();
int multiplied = ________________________________________________________;
System.out.println(multiplied);
}
9. Give the worst case runtime in Θ(·) notation as a function of N of the following methods:
public static void f1(int N) {
int sum = 0;
for (int i = N; i > 0; i -= 1) {
for (int j = 0; j < i; j += 1) {
sum += 1;
}
}
}
public static void f2(int N) {
int sum = 0;
for (int i = 1; i <= N; i = i*2) {
for (int j = 0; j < i; j += 1) {
sum += 1;
}
}
}
© Copyright 2026 Paperzz