CSE 2320 Section 001,
Exam 2
Time: 80 mins
Fall 2015
Name:
.
Student ID:
.
Total exam points: 100.
Question
1
2
3
4
5
6
7
8
Total
Points
Out of
12
10
10
10
10
13
14
21
100
If you have the smallest doubt about what a question asks of you, or whether or not
you can use certain code, ASK US! (Even if you think that it is a really silly
question).
All the answers need to be justified. No credit will be given if the answer is
provided, but with no justification.
1
Question 1 (12 points)
We define a function F(N), for integers N >= 0, as follows:
F(N) = 1, for all N<=1
F(N) = min( 2* F(N-2) , 3+F(N-1)) ) for all N >= 2
Is it possible to write a function in C that computes F(N) in O(Ν) time? If not, why
not? If yes, write the function.
2
Question 2 – 10 points (Heap operations. No code needed.)
a) (5 points) Insert 37 in the heap below.
50
40
30
20
35
17
29
1
10
1
b) (5 points) Remove the maximum value from the heap below.
50
40
30
17
29
20
1
35
1
10
3
Question 3 – 10 points (Mergesort. No code needed.)
Given the array
a = {7, 0, 5, 4, 1, 5, 8, 3}
show how mergesort sorts it. Make sure you:
- show the array after each call to merge(a, l, m, r);
- highlight (e.g. circle/underline) the data that changed.
The mergesort code is provided below for your reference.
Item aux[maxN];
merge(Item a[], int l, int m, int r)
{ int i, j, k;
for (i = m+1; i > l; i--) aux[i-1] = a[i-1];
for (j = m; j < r; j++) aux[r+m-j] = a[j+1];
for (k = l; k <= r; k++)
if (less(aux[i], aux[j]))
a[k] = aux[i++]; else a[k] = aux[j--];
}
void mergesort(Item a[], int l, int r)
{ int m = (r+l)/2;
if (r <= l) return;
mergesort(a, l, m);
mergesort(a, m+1, r);
merge(a, l, m, r);
}
4
Question 4 – 10 points (Is selection sort stable?)
Is selection sort stable? If your answer is yes, justify it (clearly explain or prove
it). If your answer is no, give an example that shows it is not stable (give an array
and show what the algorithm does that makes it not stable).
The code is provided below for your reference.
void selection(Item a[], int l, int r)
{ int i, j;
for (i = l; i < r; i++)
{ int min = i;
for (j = i+1; j <= r; j++)
if (less(a[j], a[min])) min = j;
exch(a[i], a[min]);
}
}
5
Question 5 – 10 points (Tree. No code needed.)
12
6
4
15
3
2
1
14
20
a) (3 points) List the nodes in inorder:
b) (3 points) List the nodes in preorder:
c) (3 points) List the nodes in postorder:
6
Question 6 – 13 points A binary tree structure is defined as follows:
struct Tree
{
int item;
struct Tree * left;
struct Tree * right;
}
If a left child or right child does not exist, the corresponding member variable is set
to NULL.
Write a recursive function int same_children(struct Tree * tree) that returns 1 if
every node has the property that the item in the left child node equals the item in
the right child node. If there is at least one node that does not have this property, it
returns 0. If a node has only one child, this also counts as NOT having the
property.
7
Question 7 – 14 points (Knapsack. No code needed.)
a) (5 points) Recover the items in the solution for Knapsack(10) given below.
Index
Item
Value
0
A
0
1
A
10
2
B
25
3
A
35
4
C
60
5
A
70
6
B
85
7
A
95
8
C
120
9
A
130
10
B
145
Where the items are:
Item:
Item Weight:
Item Value:
A,
1,
10,
B,
2,
25,
C
4
6
b) (9 points) Using the bottom-up algorithm, continue the computations and
compute the optimal value for weight 11. You must show all the steps in
computing the value. The code is provided below for your reference:
int knapsack(int max_weight, struct Items items)
{
int * solutions = malloc(sizeof(int) * (max_weight + 1));
int weight, item;
solutions[0] = 0;
for (weight = 1; weight <= max_weight; weight++)
{
int max_value = 0;
for (item = 0; item < items.number; item++)
{
int rem = weight - items.weights[item];
if (rem < 0) continue;
int value = items.values[item] + solutions[rem];
if (value > max_value) {
max_value = value;
}
}
solutions[weight] = max_value;
}
8
9
Question 8 – 21 points Recursion
a) (13 points) Write a recursive function, reverse(int* arr, int N) that reverses
an array of integers, arr, of length N. You can reverse it in place, or return
the reversed array. You can write helper functions if you need to.
b) (5 points) Give an example array and show both the recursive calls (however
you want) and how the array is getting reversed at each call.
c) (3 points) Give the recurrence formula (you do not need to solve it).
10
11
12
© Copyright 2026 Paperzz