Midterm Exam 2
CPSC 331
Question 1 (22 Marks)
Short answer questions — you do not need to provide any justifications for your answers. Just
fill in your answer in the space provided. For each correct answer, 1 mark will be awarded.
(1.1) Using asymptotic notation, give a lower bound for the height of a red-black tree containing
n nodes.
Answer: Ω(log n)
(1.2) Using asymptotic notation, give an upper bound for the height of a binary search tree containing n nodes.
Answer: O(n)
(1.3) What is the expected number of comparisons for unsuccessful search in a hash table of size
15 in which 30 keys are stored, assuming that the chaining collision resolution mechanism
is used?
Answer: 2
(1.4) Name the assumption used in the average case analysis of hashing with chaining, which
states that each key is hashed to any location with the same probability and independently
from other keys.
Answer: Uniform Hashing
(1.5) The binary search algorithm has worst-case running time O(log n) when searching in
which data structure(s): ordered array, ordered linked list, or both?
Answer: ordered array
(1.6) Which sorting algorithm is the most efficient when the input array is almost sorted: selection sort, insertion sort, or bubble sort?
Answer: insertion sort
3 of 14
Midterm Exam 2
CPSC 331
(1.7) Name a data structure that is appropriate for implementing the priority queue ADT.
Answer: binary heap
(1.8) Consider the algorithm described in class for inserting a new key into a red-black tree. Is
the newly-inserted node colored red or black?
Answer: red
(1.9) Using the most accurate asymptotic notation, what is the height of a heap of size n?
Answer: Θ(log n)
(1.10) Which sorting algorithm can be used to sort an array in place: merge sort or heap sort?
Answer: heap sort
(1.11) Which hash table is more appropriate when the load factor of the hash table is high: hash
table with open addressing or hash table with chaining?
Answer: hash table with chaining
(1.12) Consider a binary search tree T of height h. What is the relation between the height of the
left subtree of T and h?
Answer: h L ≤ h − 1
(1.13) Consider a red-black tree T of black height bh. What is the maximum height of T in terms of
bh?
Answer: h ≤ 2bh
4 of 14
Midterm Exam 2
CPSC 331
(1.14) Consider a red-black tree T and a subtree in T that is rooted at node x. If the black height
of the subtree at x is bh, what is the maximum black height of the left subtree of x in terms of
bh?
Answer: bL ≤ bh
(1.15) Consider sorting an array of length 10. Which algorithm would you recommend to implement to sort this array efficiently: selection sort, merge sort or heap sort?
Answer: selection sort
(1.16) Consider the double hashing mechanism described in class. Give the expression for the hash
function with double hashing, i.e., h(i, k ).
Answer: h(i, k ) = (h0 (k ) + ih1 (k))
mod
m
(1.17) (6 Marks) Using the most accurate asymptotic notation, fill in the following table to indicate the worst-case asymptotic running time, for each of the algorithms listed below, as a
function of the input size n.
algorithm
worst-case running time
binary search tree, insertion
Θ(n)
red-black tree, insertion
Θ(log n)
hash table with chaining, insertion
Θ(n)
(assume table of size m)
max-heap, deleteMax
Θ(log n)
red-black tree, left rotation
Θ (1)
binary search tree, findMin
Θ(n)
5 of 14
Midterm Exam 2
CPSC 331
Question 2 (5 Marks)
The following questions deal with binary search trees.
(2.1) (1 Mark) Draw the resulting tree after deleting key 20 from the following binary search
tree. Use the algorithm presented in class.
12
3
20
1
7
15
5
10
17
6
Answer:
12
3
1
15
7
5
17
10
6
6 of 14
Midterm Exam 2
CPSC 331
(2.2) (4 Marks) Give pseudocode for a recursive algorithm for inserting a key into a binary
search tree. The following definitions are assumed:
public c l a s s BST<E extends Comparable<E> ,V> {
p r o t e c t e d bstNode<E , V> r o o t ;
p r o t e c t e d c l a s s bstNode<E , V> {
E key ;
V value ;
bstNode<E , V> l e f t ;
bstNode<E , V> r i g h t ;
bstNode ( E key , V value , bstNode<E , V> l e f t , bstNode<E , V> r i g h t ) ;
}
public void i n s e r t ( E key , V value )
{ r o o t = i n s e r t ( root , key , value ) ; }
where, you are being asked to complete the following recursive function:
bstNode<E,V> insert(bstNode<E,V> T, E key, V value) {
Solution:
bstNode<E , V> i n s e r t ( bstNode<E , V> T , E key , V value ) {
i f ( T == n u l l )
T = new bstNode<E , V>( key , value , null , n u l l ) ;
e l s e i f ( key . compareTo ( T . key ) < 0 )
T . l e f t = i n s e r t ( T . l e f t , key , value ) ;
e l s e i f ( key . compareTo ( T . key ) > 0 )
T . r i g h t = i n s e r t ( T . r i g h t , key , value ) ;
else
throw new KeyFoundException ( ) ;
return T ;
}
7 of 14
Midterm Exam 2
CPSC 331
Question 3 (5 Marks)
The following questions deal with red-black trees.
(3.1) (3 Marks) Consider the red-black tree depicted in the following figure. What is the relation
between the black heights of subtrees T1 , T3 and T4 ?
β
α
T1
T3
T4
T5
T2
Answer:
Let bh( T ) denote the black height of T:
bh( T3 ) = bh( T4 ) = 1 + bh( T1 )
(3.2) (2 Marks) Draw the tree that results from the right rotation of the tree at node γ.
Answer:
β
α
T1
T2
T3
T4
8 of 14
T5
Midterm Exam 2
CPSC 331
Question 4 (5 Marks)
Consider a hash table using the open addressing with linear probing collision resolution mechanism,
with table size m = 7 and the hash function
h(k, i ) = k + i
(mod 7),
for which we assume that the key k is an integer.
(4.1) (2 Marks) Draw the hash table (with the above table size and hash function) that would
be produced by inserting the following values, in the given order (from left to right), into an
initially empty table:
26, 32, 19, 11
Answer:
0
11
1
2
3
4 5 6
32 26 19
(4.2) (3 Marks) Give the pseudocode for an algorithm that can be used to delete a given key key
from a hash table T. The hash table T employs open addressing and linear probing using
the hash function described above.
void delete(int[] T, int key) {
Answer:
void d e l e t e ( i n t [ ] T , i n t key ) {
i = 0;
do {
j = k + i mod 7 ;
i f ( T [ j ] == key ) {
T [ j ] = DELETED ;
return ;
}
i ++;
while ( ( T [ j ] ! = NIL ) && ( i < 7 ) ) ;
throw new KeyNotFoundException ( ) ;
}
9 of 14
Midterm Exam 2
CPSC 331
Question 5 (5 Marks)
Recall that to search for an element in a sorted array, the binary search algorithm divides the
array into two (roughly) equal size subarrays and recursively searches one of the subarrays. We
modify this algorithm to divide the array into three (roughly) equal size subarrays. We call the
modified search algorithm the ternary search algorithm. The following is the signature of a search
function to search for an element key in an array A using the ternary search algorithm assuming
the elements of A are sorted in an ascending order:
i n t t e r n a r y S e a r c h ( i n t [ ] A, i n t key ) {
r e t u r n t S e a r c h (A, 0 , A. l e n g t h − 1 , key ) ;
}
where,
i n t t S e a r c h ( i n t [ ] A, i n t low , i n t high , i n t key ) {
i f ( low > high ) {
throw new KeyNotFoundException ( ) ;
}
else {
t 1 = low + ( high − low ) / 3 ;
t 2 = high − ( high − low ) / 3 ;
i f (A[ t 1 ] == key )
return t1 ;
i f (A[ t 2 ] == key )
return t2 ;
i f (A[ t 1 ] > key )
r e t u r n t S e a r c h (A, low , t1 − 1, key ) ;
i f (A[ t 2 ] < key )
r e t u r n t S e a r c h (A, t 2 +1 , high , key ) ;
r e t u r n t S e a r c h (A, t 1 +1 , t2 − 1, key ) ;
}
}
Let T (n) denote the worst-case running time of tSearch on arrays with n elements. Assume that
when tSearch divides an array of size n into three subarrays for recursive search, the largest
subarray contains dn/3e elements.
(5.1) (1 Mark) Complete the following recurrence relation for T (n). Ignore the ceiling operator.
c0
n = 0,
T (n) ≤
n
c1 + T ( ) n ≥ 1,
3
for some constants c1 > c0 > 0.
10 of 14
Midterm Exam 2
CPSC 331
(5.2) (3 Marks) Use the substitution technique to find an upper bound on T (n).
Answer:
Consider the running time for n ≥ 1:
n
T ( n ) ≤ c1 + T ( )
3
≤ c1 + [ c1 + T (
n
n
)] = 2c1 + T ( 2 )
2
3
3
≤ ···
≤ kc1 + T (
n
)
3k
For k = log3 n + 1, it is obtained that
T (n) ≤ (1 + log3 n)c1 + T (0)
Therefore,
T (n) ≤ c0 + (1 + log3 n)c1
(5.3) (1 Mark) Recall that the binary search algorithm has worst-case running time of O(log2 n).
How does the worst-case asymptotic running time of the ternary search algorithm compares
with that of the binary search algorithm?
Answer:
Given the upper bound on T (n), it can be concluded that T (n) ∈ O(log3 n). Thus, the
asymptotic behavior of both algorithms is the same.
Note that, for a, b > 0
loga n =
logb n
logb a
that is, the base of the logarithm only changes the constants in the asymptotic notation and
not the asymptotic behavior. Hence, we have T (n) ∈ O(log n) for both the ternary search
algorithm and the binary search algorithm regardless of the base of the logarithm function.
11 of 14
Midterm Exam 2
CPSC 331
Question 6 (8 Marks)
The following questions deal with the merge sort algorithm.
(6.1) (4 Marks) Give pseudocode for the merge sort algorithm. You may assume that the function
void merge(int[] A1, int[] A2, int[] B)
exists, and that upon completion, the array B contains the contents of A1 and A2 in ascending order.
void mergeSort(int[] A, int[] B) {
Answer:
void mergeSort ( i n t [ ] A, i n t [ ] B ) {
n = A. l e n g t h ;
i f ( n == 1 )
B [ 0 ] = A[ 0 ] ;
else {
t = c e i l ( n/2) ;
S e t A1 t o be A [ 0 ] , . . . , A[ t − 1 ] ;
S e t A2 t o be A[ t ] , . . . , A[ n − 1 ] ;
mergeSort ( A1 , B1 ) ;
mergeSort ( A2 , B2 ) ;
merge ( B1 , B2 , B ) ;
}
}
12 of 14
Midterm Exam 2
CPSC 331
(6.2) (4 Marks) Suppose that merge sort is called on the array
4
8 2
1
9
5 7
3
What arrays are input to the first four recursive calls of the algorithm (not including the
initial call with the entire array)?
First input array:
4
8 2
1
Second input array:
4
8
Third input array:
4
Fourth input array:
8
13 of 14
© Copyright 2025 Paperzz