Spring2017 Exam 2 PDF
Spring2017 Exam 2 PDF
Spring 2017
Wednesday, April 12, 2017
6:30-7:30pm
CL50 224
Read and sign the Academic Honesty Statement that follows:
“In signing this statement, I hereby certify that the work on this exam is my own, that I have
not copied the work of any other student while completing it, that I have not obtained help
from any other student, and that I will not provide help to any other student. I understand
that if I fail to honor this agreement, I will receive a score of ZERO for this exam and will
be subject to disciplinary action as outlined in the course policy.”
Printed Name:
Signature:
If the statement is not signed, the exam will not be graded and it will not be returned.
This study source was downloaded by 100000823315664 from CourseHero.com on 03-31-2022 15:23:11 GMT -05:00
https://www.coursehero.com/file/30552192/Spring2017-Exam-2pdf/
1. Height-Balanced Binary Search Trees (12 points) (Learning Objective 1)
You are given the following height-balanced binary search tree:
15
8 58
1 10 20 60
3 16 50 63
18
For each of the following insertion and deletion operations (in questions 1(a)–(c)), you perform
the operation on the preceding height-balanced binary search tree. Do not apply the operations
successively.
If the operation results in an unbalanced tree, you have to perform rotation(s) to balance the
tree. Draw the height-balanced binary search tree after each operation.
If a deletion operation involves a node that has two child nodes, replace the key in that node with
its immediate in-order predecessor (the key right before the deleted key in an in-order traversal),
and delete the node containing the immediate in-order predecessor instead.
(a) (4 points) Insert 61. (b) (4 points) Delete 60.
This study source was downloaded by 100000823315664 from CourseHero.com on 03-31-2022 15:23:11 GMT -05:00
https://www.coursehero.com/file/30552192/Spring2017-Exam-2pdf/
(c) (4 points) Delete 15.
This study source was downloaded by 100000823315664 from CourseHero.com on 03-31-2022 15:23:11 GMT -05:00
https://www.coursehero.com/file/30552192/Spring2017-Exam-2pdf/
24 else if ( tmp [ j ] < tmp [ i ]) { r [ m ] = tmp [ j ++]; }
25 else { r [ m ] = tmp [ i ++]; }
26 } [1, 2, 3, 4, 9, 10, 14, 16]
27 }
The figure to the right shows the application [1, 2, 3, 4] [9, 10, 14, 16]
of Iterative mergesort by calling in the main
function:
[1, 4] [2, 3] [9, 16] [10, 14]
Now, you are given a different merge sort algorithm. The two functions below are new.
The Natural mergesort function iteratively looks for non-descending subarrays within the given
array r and merges these non-descending subarrays using the Merge function (Lines 15–27). The
Natural mergesort function calls the function find end of run to find the index of the last
item in a non-descending subarray.
28 void Natural_mergesort ( int *r , int size )
29 {
30 int number_of_runs ; // number of non - descending subarrays
31 do {
32 number_of_runs = 0; // no non - descending subarrays initially
33
34 int i = 0; // index of left subarray
35 while ( i < size ) { // left subarray exists
36 number_of_runs ++; // found one non - descending subarrays
37
38 // find end of left non - descending subarray
39 int end_of_left_run = find_end_of_run (r , size , i );
40
41 // check whether a right subarray exists
42 if ( end_of_left_run + 1 < size ) { // right subarray exists
43 // find end of right non - descending subarray
44 int end_of_right_run =
45 find_end_of_run (r , size , end_of_left_run +1);
46 Merge (r , i , end_of_left_run , end_of_right_run );
47 i = end_of_right_run + 1; // index of next left subarray
48 } else { // absence of right subarray
49 break ; // break from inner while - loop
50 }
51 } // while left subarray exists
52 } while ( number_of_runs > 1); // do while two or more subarrays
53 }
This study source was downloaded by 100000823315664 from CourseHero.com on 03-31-2022 15:23:11 GMT -05:00
https://www.coursehero.com/file/30552192/Spring2017-Exam-2pdf/
54 int find_end_of_run ( int * array , int size , int start_of_run )
55 {
56 int end_of_run = start_of_run ; // assume a single - integer subarray
57 // look for a smaller item to end the non - descending subarray
58 while (( end_of_run +1 < size )
59 && ( array [ end_of_run ] <= array [ end_of_run +1])) {
60 end_of_run ++;
61 }
62 return end_of_run ;
63 }
In questions 2(a)–(c), consider the application of Natural mergesort as follows:
int r[] = {4, 1, 3, 2, 16, 9, 10, 14, 8, 7};
Natural_mergesort(r, 10);
(a) (4 points) Enter in the following table the corresponding values of i, end of left run, and
end of right run calculated in the first iteration of the do-while loop (Lines 31–52). If there
are no corresponding values of end of left run and/or end of right run, put “N.A.” in the
corresponding table entries. There may be more columns than necessary in the table.
i 0
end of left run
end of right run
(b) (4 points) Draw a figure to demonstrate the merging of non-descending subarrays performed
in the first iteration of the do-while loop. Clearly indicate the subarrays that are being merged
and the results of the merging operations.
(c) (4 points) Draw a figure to demonstrate the merging operations performed in the remaining
iterations of the do-while loop. Clearly indicate the subarrays that are being merged and the
results of the merging operations.
This study source was downloaded by 100000823315664 from CourseHero.com on 03-31-2022 15:23:11 GMT -05:00
https://www.coursehero.com/file/30552192/Spring2017-Exam-2pdf/
(d) (4 points) When Natural mergesort is applied to an array r containing n integers, what is
the best-case time-complexity in terms of n? What is the worst-case time-complexity in terms of
n. Justify your answers.
Best-case time-complexity:
Worst-case time-complexity:
h 8 g 4 f
4 9
2
a 11 i 7 4 e
6
13 7 9
b c d
6 2
A min-heap is used to implement the priority queue PQ. In the class, we build a max-heap of n
integers stored in array r[0..n-1] as follows:
This study source was downloaded by 100000823315664 from CourseHero.com on 03-31-2022 15:23:11 GMT -05:00
https://www.coursehero.com/file/30552192/Spring2017-Exam-2pdf/
// heapify r[i] downward in r[i..ub]
Downward heapify(r[], i, ub):
temp r ← r[i];
while (2*i+1 <= ub) {
j ← 2*i + 1;
if j < ub and r[j] < r[j+1]
// build a max-heap of integers j ← j+1;
for i ← n/2 - 1 down to 0 if temp r ≥ r[j]
Downward heapify(r[], i, n-1); break;
else {
r[i] ← r[j];
i ← j;
}
}
r[i] ← temp r;
Assume that the edges in the given graph are initially stored in an array with the following
order:
(a, b) (a, h) (b, c) (b, h) (b, i) (c, d) (c, i) (d, e) (d, f ) (d, g) (e, f ) ( f , g) (g, h) (g, i)
Show the ordering of the edges (not their weights) in the array of the min-heap data structure
constructed in Line 02 of Kruskal-MST using an approach similar to that for the construction of a
max-heap of integers with Downward heapify.
This study source was downloaded by 100000823315664 from CourseHero.com on 03-31-2022 15:23:11 GMT -05:00
https://www.coursehero.com/file/30552192/Spring2017-Exam-2pdf/
(b) (5 points) Consider the following pseudo code for a disjoint-set implementation (as covered
in class).
The disjoint-set data structure constructed by the for-loop in Lines 03–04 of the Kruskal-MST
routine is as follows, with each p[x] field depicted by an arrow. The integer beside each node is
the rank.
0 a 0 b 0 c 0 d 0 e 0 f 0 g 0 h 0 i
Show the disjoint-set data structure after the following edges have been dequeued and pro-
cessed (Lines 05–11): (c, d), (g, i), (a, h), (d, f ), (g, f ), (b, c), (c, i). (Note that only these edges
do not form the complete set of of edges and the ordering given is not related to the min-heap you
have constructed in 3(a).) For your convenience, the graph is reproduced here:
h 8 g 4 f
4 9
2
a 11 i 7 4 e
6
13 7 9
b c d
6 2
This study source was downloaded by 100000823315664 from CourseHero.com on 03-31-2022 15:23:11 GMT -05:00
https://www.coursehero.com/file/30552192/Spring2017-Exam-2pdf/
Powered by TCPDF (www.tcpdf.org)