0% found this document useful (0 votes)
158 views10 pages

Cse373 10sp Midterm1.Key

This document contains instructions for a midterm exam in CSE 373 Spring 2010. It has 7 questions worth a total of 78 points to be completed within 50 minutes. The questions cover big-O notation, analysis of algorithms, binary search trees, binary heaps, and AVL trees. Students are instructed to show their work for partial credit and circle their final answers.
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)
158 views10 pages

Cse373 10sp Midterm1.Key

This document contains instructions for a midterm exam in CSE 373 Spring 2010. It has 7 questions worth a total of 78 points to be completed within 50 minutes. The questions cover big-O notation, analysis of algorithms, binary search trees, binary heaps, and AVL trees. Students are instructed to show their work for partial credit and circle their final answers.
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/ 10

Name: _____________________________________

Email address: _____________________________________

CSE 373 Spring 2010: Midterm #1


(closed book, closed notes, NO calculators allowed)

Instructions: Read the directions for each question carefully before answering. We may
give partial credit based on the work you write down, so if time permits, show your
work! Use only the data structures and algorithms we have discussed in class or which
were mentioned in the book so far.

Note: For questions where you are drawing pictures, please circle your final answer for
any credit.

Good Luck!

Total: 78 points. Time: 50 minutes.

Question Max Points Score


1 16
2 8
3 14
4 6
5 6
6 10
7 18
Total 78

Page 1 of 10
1. (16 pts) Big-O
For each of the functions f(N) given below, indicate the tightest bound possible (in other
words, giving O(2N) as the answer to every question is not likely to result in many
points). Unless otherwise specified, all logs are base 2. You MUST choose your
answer from the following (not given in any particular order), each of which could be
re-used (could be the answer for more than one of a) – h)):

O(N2), O(N½)O(N3 log N), O(N log N), O(N), O(N2 log N), O(N5), O(2N), O(N3),
O(log N), O(1), O(N4), O(N12)O(NN), O(N6), O(N8), O(N9)

You do not need to explain your answer.

a) f(N) = N  (N2 log N + N2) __________________

b) f(N) = log16 (2N) __________________

c) f(N) = (N/4) log (N/4) + N/4 __________________

d) f(N) = (2N + 2N) 3 __________________

e) f(N) = N½ + log N __________________

f) f(N) = N log (1003) __________________

g) f(N) = (N  N  N  N)3 __________________

h) f(N) = (N3 + 2N) / N __________________

Page 2 of 10
2. (8 pts) Big-Oh and Run Time Analysis: Describe the worst case running time of
the following pseudocode functions in Big-Oh notation in terms of the variable n.
Showing your work is not required (although showing work may allow some partial
credit in the case your answer is wrong – don’t spend a lot of time showing your
work.). You MUST choose your answer from the following (not given in any
particular order), each of which could be re-used (could be the answer for more than
one of I. – IV.):
O(n2), O(n3 log n), O(n log n), O(n), O(n2 log n), O(n5), O(2n), O(n3),
O(log n), O(1), O(n4), O(nn)
I. void silly(int n, int x, int y) {
if (x < y) { Runtime:
for (int i = 0; i < n; ++i)
for (int j = 0; j < n * i; ++j)
System.out.println(”y = ” + y);
} else {
System.out.println(”x = ” + x);
}
}

II. int silly(int n, int m) {


if (n < 1) return n;
else if (n < 100)
return silly(n - m, m);
else
return silly(n - 1, m);
}

III. void silly(int n) {


j = 0;
while (j < n) {
for (int i = 0; i < n; ++i) {
System.out.println(”j = ” + j);
}
j = j + 5;
}
}

IV. void silly(int n) {


for (int i = 0; i < n * n; ++i) {
for (int j = 0; j < n; ++j) {
for (int k = 0; k < i; ++k)
System.out.println(”k = ” + k);
for (int m = 0; m < 100; ++m)
System.out.println(”m = ” + m);
}
}
}

Page 3 of 10
3. (14 pts total) Trees.

a) ( 3 pts) What is the minimum and maximum number of nodes at depth d in a full
binary tree? Be sure to list the nodes at depth d. Do not include nodes at depth d-1 or
d+1 or other depths. (Hint: the root is at depth 0)

Minimum =

Maximum =

b) (3 pts) Give traversals of the tree shown at the bottom of this page:

Pre-Order:

Post-Order:

In-Order:

c) (1 pt) What is the height of the tree shown below:

d) (1 pt) Is it AVL balanced (ignore the values, only look at the shape):
YES / NO

B C

D E F G

H I J K

Page 4 of 10
3. (cont) (6 pts) Given the following six trees a through f:

List the letters of all of the trees that have the following properties: (Note: It is possible
that none of the trees above have the given property, it is also possible that some trees
have more than one of the following properties.)

Full: _______________________

Complete: _______________________

AVL balanced: _______________________

Page 5 of 10
4. (6 pts total) Binary Search Trees & Binary Min Heaps

a) (3 pts) Given the binary search tree shown below. Draw what the tree would look
like after deleting the value 7. Use one of the methods for deleting described in class or in
the book.
Correction – delete 9!
9

6 12

4 8 11 33

7 88

b) ( 3 pts) You are given a binary min heap of height 6. The minimum and
maximum number of comparisons we might have to do when doing a deletemin is:

Minimum =

Maximum =

Page 6 of 10
5. (6 pts) AVL Trees Draw the AVL tree that results from inserting the keys:
2, 8, 3, 9, 5, 4, 10 in that order into an initially empty AVL tree. You are only
required to show the final tree, although drawing intermediate trees may result in partial
credit. If you draw intermediate trees, please circle your final tree for ANY credit.

Page 7 of 10
6. (10 pts total) Binary Min Heaps

(a) [6 points] Draw the binary min heap that results from inserting 9, 3, 7, 4, 8, 2, 6, 5 in
that order into an initially empty binary min heap. You do not need to show the array
representation of the heap. You are only required to show the final tree, although
drawing intermediate trees may result in partial credit. If you draw intermediate trees,
please circle your final result for any credit.

Page 8 of 10
6. (cont.)

(b) [2 points] Draw the result of one deletemin call on your heap draw in part (a).

(c) [2 points] Draw the result of one deletemin call on your heap draw in part (b).

Page 9 of 10
7. (18 pts) Running Time Analysis: Give the tightest possible upper bound for the worst
case running time for each of the following in terms of N. You MUST choose your
answer from the following (not given in any particular order), each of which could be re-
used (could be the answer for more than one of a) – f)):

O(N2), O(N½)O(N3 log N), O(N log N), O(N), O(N2 log N), O(N5), O(2N), O(N3),
O(log N), O(1), O(N4), O(N12)O(NN), O(N6), O(N8), O(N9)

**For any credit, you must explain your answer. Assume that the most time-efficient
implementation is used. Assume no duplicate values and that you can implement the
operation as a member function of the class – with access to the underlying data structure.
a)
a) Pop a value off a stack containing N elements implemented as an array.
Explanation:

b) Printing out all the odd values stored in a binary search tree containing N positive b)
integers in ascending order. Explanation:

c)
c) Finding the minimum value in a binary search tree of size N. Explanation:

d) Moving the values from a binary min heap, into an initially empty array of the same d)
size. The final contents of the array should be sorted from low to high. Explanation:

e) Finding the maximum value in a binary min heap of size N. Explanation: e)

f) Printing out the values in an AVL tree in post-order. Explanation: f)

Page 10 of 10

You might also like