Points off 1 2 3 4 Admin Total off Net Score
Name____________________________________
Last 4 digits of SSN / Student ID ______________
Class Unique ID ___________________________
Instructions:
1.
There are 4 questions on this test. Question 1 is worth 40 points, all others are worth 20 points each.
2.
You will have 3 hours to complete the test.
3.
You may not use a calculator.
4.
When code is required, write Java code.
5.
You may not use any classes or methods from the Java Standard Library other than the ones specifically mentioned on each question.
6.
The style guide is not in effect.
7.
Please make your answers legible.
1. Java mechanics and short answer questions. (2 points each) Write the answer to each question in the space provided. If code results in an error indicate if it is a compile error or runtime error.
A. The following numbers are inserted in the order shown into a binary search tree with no checks to ensure or maintain balance. The tree is initially empty. Draw the resulting tree.
61 64 10 7 42 3 8 24
CS 307 – Final Exam – Summer 2002 1
For parts B, C, and D consider the following binary tree. For each question assume when a node is processed the value in the node is printed out by the statement:
System.out.print( currentNode.data + " " ); root of tree
-10
-7
12
35
B. What is the output of a preorder traversal of the tree?
____________________________________________________
C. What is the output of a inorder traversal of the tree?
____________________________________________________
D. What is the output of a postorder traversal of the tree?
____________________________________________________
0
-10
-5
CS 307 – Final Exam – Summer 2002 2
E. What is the maximum number of leaves(a node with 0 children) in a binary tree with 10 nodes?
______________________
F. What is the minimum number of leaves in a binary tree with 10 nodes?
________________________
G. What is the maximum number of nodes in a binary tree with a height of 4? The height of a binary tree is the path length from the root of the tree to the deepest leaf or leaves.
________________________
H. Consider a LinkedList class. The linked list class must have the following methods. addFront, addBack, getFront, removeFront and getSize . The nodes for this linked list class are singly linked. What instance vars does the LinkedList class need to implement all 5 methods in O(1) time?
________________________________________________________
I. Consider a priority queue. It has operations similar to that of a regular queue, but every item inserted has a corresponding priority. Items with a higher priority are moved in front of items with a lower priority when they are enqueued. What is the average case Big O of the enqueue operation for a priority queue that contains N elements and uses a doubly Linked List as its internal storage container?
________________________________________________________
J. An algorithm takes 5 seconds to execute on a data set of 1,000,000 items. The algorithms is
O(N ^ 3). How long do you expect the algorithm to execute on a data set of 3,000,000 items?
_____________________________________________
CS 307 – Final Exam – Summer 2002 3
K. An algorithm takes 10 seconds to execute on a data set of 1,000,000 items. The algorithm is
O(N log N). How long do you expect the algorithm to execute on a data set of 2,000,000 items? log 1,000,000 ~= 20. You leave your answer in the form of a fraction.
_____________________________________________
L. What is the Big O of this code? println , top,isEmpty and pop all have a Big O of
O(1).
Stack s = new Stack();
// code to fill Stack not shown while( !s.isEmpty() )
{ System.out.println( s.top() );
} s.pop();
______________________________________
M. What is the Big O of the following code?
int[] list = new int[N]; // N is a positive int
// code to fill list not shown int min = 0 for(int i = 1; i < list.length; i++)
{ if( list[i] < list[min] )
} min = i; int temp = 0; for(int i = min; i > 0; i--)
{ temp = list[i – 1]; list[i-1] = list[i];
} list[i] = temp;
_______________________________________
N. What is the worst case Big O for inserting a new item into a properly coded Red Black tree that contains N items?
_______________________________________
CS 307 – Final Exam – Summer 2002 4
O. What is the Big O of the following method? public void red( int N )
{ if( N <= 100)
} return 3; else return 2 * red(N – 3);
_______________________________________
P.
Consider a sorting algorithm where the actual number of executions is ½ * N ^ 2. On average how many times is each item in an unsorted list examined while sorting the list?
____________________________________________
Q. A programmer claims to have a sorting algorithm that runs in O(N) time and the actual number of executions is N. If so how many times, on average, is an item in the list examined before being placed in its correct, sorted spot?
____________________________________________
R. A programmer claims to have devised an algorithm to multiply two N by N matrices that is less than O(N ^ 3). (The Big O of the best method you currently have.) When N equals 100 the new algorithm takes 10 seconds. When N equals 200 the new algorithm takes 60 seconds. When N equals 400 the new algorithm takes 360 seconds. Is the programmer correct about the efficiency being better than O(N^3)? Explain why or why not.
____________________________________________
CS 307 – Final Exam – Summer 2002 5
S. Consider a Stack class designed to hold integers. It has the following methods: public Stack() //create an empty stack public void push(int x) public void pop() //remove top element public int top() //access top element without removing it public void makeEmpty() public boolean isEmpty() public int size()
What is the output of the following code?
Stack s = new Stack(); for(int i = -5; i < 6; i++) s.push(i); for(int j = 0; j < s.size(); j++)
{ System.out.print( s.top() + " " );
} s.pop();
__________________________________________
T. Consider the following class: public class Node
{ public int data; public Node next; public Node prev;
}
Draw the Objects including arrows for references after the following code has been executed.
Node n1 = new Node();
Node n2 = new Node();
Node n3 = n1; n1.data = 12; n3.next = n2; n1.prev = n3; n2.next = n3; n2.next.prev = n1.next;
__________________________________________
CS 307 – Final Exam – Summer 2002 6
CS 307 – Final Exam – Summer 2002 7
2. (Trees) Write a method that determines the depth of node, n. Recall the depth of a node is equal to the path length from the root of the tree to that node. Your method must run in no worse than O(N) time where N is the number of nodes in the tree for which root is the root . You may create helper methods if you wish.
/* pre: root != null, n != null post: return depth of n in relation to root or –1 if n is not a descendant of root.
*/ public int getDepth(BTNode root, BTNode n)
CS 307 – Final Exam – Summer 2002 8
CS 307 – Final Exam – Summer 2002 9
3. (Recursion) Connect four. It just won't go away. What are you going to do next semester with no connect 4 assignments? Anyway, we are picking teams for connect 4. The goal is to minimize the difference in total IQ between the two teams. Write a method that accepts an array of ints that represent the IQs of the people who will be playing and returns the minimum possible IQ difference between the two teams. Every person must be on one and only one team. Note, you don't actually need to return what the teams are, only the minimum possible IQ difference between the teams.
For example if the IQs were 100, 101, 120, 100, and 130 then the minimum possible difference would be 49. (One team would have the 100, 100, 101 and the other the 120 and 130.)
You may create helper methods if you wish.
/* pre: IQs != null, IQs.length > 1 post: return min possible IQ difference between two teams of players as described above
*/ public int getMinPossibleIQDiff(int[] IQs)
CS 307 – Final Exam – Summer 2002 10
CS 307 – Final Exam – Summer 2002 11
4. ( Using data structures and implementing data structures) In this question you will be implementing two of the methods for a priority queue. A priority queue has the same operations as a regular queue, but every item inserted has a corresponding priority. Items with a higher priority are moved in front of items with a lower priority when they are enqueued. For this example every item enqueue has a corresponding priority assigned. All priorities are between 1 and 100 inclusive. 1 is the highest priority, 100 is the lowest priority. The internal storage container is a native array of regular Queue s.
} public class PriorityQueue
{ private Queue[] myCon = new Queue[100]; private int iMySize;
// get number of items in Priority Queue public int getSize()
All of the Queue s in myCon are initialized.
The Queue class has the following methods public class Queue
{ // create an empty queue public Queue()
// add item, getSize = old getSize + 1 public void enqueue(Object item)
// get front item, if getSize = 0, exception thrown public Object front()
// remove front item, if getSize = 0, exception thrown public void dequeue()
// number of items in queue public int getSize()
}
CS 307 – Final Exam – Summer 2002 12
Complete the following two methods for the PriorityQueue class.
{
A.
/* pre: 1 <= pri <= 100 post: item enqueued correctly, getSize = old getSize + 1
*/ public void enqueue(int pri, int item)
CS 307 – Final Exam – Summer 2002 13
B.
/* pre: getSize > 0 post: item with priority closest to or equal to 1 returned. If there are 2 or more items with the priority closest to or equal to one, the item that has been in the PriorityQueue longest is returned
*/ public Object front()
{
CS 307 – Final Exam – Summer 2002 14