Cse373 12wi Midterm1.Key
Cse373 12wi Midterm1.Key
Cse373 12wi 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(N1/2), O(N1/4), O(log3 N), O(N), O(N2 log N), O(log8 N), O(N5), O(2N), O(N3), O(N8),
O(log4 N), O(N log3 N), O( log2 N), O(log N), O(1), O(N3 log N), O(N4), O(NN), O(N6),
O(N log2 N), O(log log N), O(log4 N), O(N log 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 dont 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), O(n6), O(n8), O(n7)
Page 3 of 10
3. (19 pts total) Trees.
Minimum = 25 = 32
Maximum = 26 = 64
b) (4 pts) What is the minimum and maximum number of nodes in a balanced AVL tree of
height 6? Give an exact number (with no exponents) for both of your answers not a formula.
Minimum = 33
Maximum = 27 1 = 127
c) (2 pts) What is the maximum number of leaf nodes in a full tree of height 5?
Give an exact number (with no exponents) for your answer not a formula.
Maximum = 25 = 32
f) (1 pt) Is it AVL balanced (ignore the values, only look at the shape):
YES / NO
A
B D
K J E
M
Page 4 of 10
3. (cont) g) (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.)
Complete: _________c___________
Perfect: _______none________
Page 5 of 10
4. (11 pts total) Stacks Please ANSWER PART (a) and PART (b).
(a) (8 pts) Given the stack interface from homework 1, write Java code that will determine which
value in the stack is the largest and remove and return that value while otherwise leaving the
remainder of the stack in its original order.
In your solution, you may only declare extra DStacks or scalar variables (e.g. ints, doubles not
arrays or linked lists) if needed. You may not use other methods or classes from java collections.
You can assume that any DStacks you use will never become full and that the provided stack
will not contain duplicate values. Rather than throwing an exception, if the provided stack is
empty findAndRemoveLargest(DStack) will return the value -373.
Unlike homework 1, assume that the only implementation of DStack that currently exists is the
class LogStack. LogStack implements all of the operations of the DStack interface, but does
them each at a cost of O(log N). If you need to create any temporary DStacks in your code,
you will need to create new LogStacks. The interface for DStack is given below:
Please write your code for findAndRemoveLargest(DStack myStack) on the next page.
(b) (3 pts) Since the only implementation of DStack that currently exists is the class LogStack,
all DStack parameters passed to findAndRemoveLargest(DStack myStack) must be
LogStacks. Thus all stack operations (isEmpty, push, pop, peek) on any stacks in your
code take time O(log n) each.
What is the worst case Big-O running time of your implementation of findAndRemoveLargest?
For full credit give your answer in the most simplified form possible. (An explanation is not
required, but could help provide partial credit. No credit if your solution is not mostly correct.)
O(N log N)
Page 6 of 10
// Finds and removes the largest value in myStack.
// Returns the largest value, otherwise leaves myStack in original order.
// Returns -373 if myStack is empty.
public static double findAndRemoveLargest(DStack myStack) {
// Add your code here:
Page 7 of 10
5. (6 pts) AVL Trees Draw the AVL tree that results from inserting the keys: 1, 3, 7, 5, 6, 9
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 8 of 10
6. (12 pts) Algorithms & Running Time Analysis:
Describe the most time-efficient way to implement the operations listed below. 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, including knowing the number of values
currently stored (N).
Then, give the tightest possible upper bound for the worst case running time for each
operation in terms of N. **For any credit, you must explain why it gets this worst case
running time. You must choose your answer from the following (not listed 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(log log N)
a) Given a FIFO queue implemented as a linked list, find and remove all of the values
greater than 10, leaving the rest of the queue in its original order. Explanation: a)
Since you know the size of the queue, you can loop through all N values:
Each enqueue and dequeue operation takes time O(1), the total runtime is O(N).
There is no real worst case.
Alternately, since we can also consider this a member function we could traverse the linked list
directly from front to back (O(N)) and for each node, if it is > 10, then adjust pointers to remove
it from the list (O(1) per removal just setting pointers) also giving us running time O(N).
b) Given a binary search tree, print all of the odd values from largest to smallest. b)
Be specific about how you would get values in order from largest to smallest.
Explanation:
printOdd(node root) {
if root == null return
printOdd(root.right)
if (root.val % 2 == 1) print it
printOdd(root.left)
}
This is essentially an inorder traversal done in reverse. Since you must visit each node once, and
do constant time work at each node, overall time is O(N). There is no real worst case.
Page 9 of 10
6. (continued) Algorithms & Running Time Analysis:
Describe the most time-efficient way to implement the operations listed below. 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, including knowing the number of values
currently stored (N).
Then, give the tightest possible upper bound for the worst case running time for each
operation in terms of N. **For any credit, you must explain why it gets this worst case
running time. You must choose your answer from the following (not listed 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(log log N)
c) Given a binary search tree, find which value is the minimum value and delete it.
c)
Explanation:
To findmin in a BST, start at the root and go left until you encounter a null pointer
you have now found the minimum value. Deleting this node is easy, since it is either
a leaf node (just remove it) or a node with one child (just replace node with its child)
both of which take constant time.
In the worst case you are dealing with a linked-list tree slanting to the left, forcing you to
traverse all N nodes in order to find the minimum. Thus worst case runtime is O(N).
Whether the queue is empty or contains N values, the cost of an enqueue operation
is only constant time ( back.next = new node(x), back =back.next). So doing N enqueue
operations will take time O(N). There is no real worst case.
front back
Page 10 of 10