Cse373 10sp Midterm1.Key
Cse373 10sp Midterm1.Key
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!
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)
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);
}
}
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:
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: _______________________
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:
Page 10 of 10