Download 2014 Final - Randomly Philled

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Big O notation wikipedia , lookup

Large numbers wikipedia , lookup

Non-standard calculus wikipedia , lookup

Function (mathematics) wikipedia , lookup

History of the function concept wikipedia , lookup

Elementary mathematics wikipedia , lookup

Transcript
CIS 55
Final Exam, May 20, 2014
Multiple Choice (30 pts)
For each question, select the best answers that apply. More than one answer can be circled
and D is always going to be “None of the above”. This means no question will have multiple
answers with D selected (such as A and D). The minimum to circle is at least one answer
and the most would be A, B and C.
1) Which of the following is true about using a 2-3-4 tree?
a. It is designed to minimize node visits while keeping to an O(log n) search
performance
b. It is designed to self-balance as new values are inserted into the tree
c. As soon as a node becomes full, it performs the split routine
d. None of the above
2) Which of the following is true about a binary search tree?
a. Delete is O(log n) unless the tree is balanced
b. Insert is O(log n) if the data set being entered contains randomly distributed
values
c. Search is O(log n) in most cases because the algorithm is similar to the
binary search algorithm
d. None of the above
3) Which
a.
b.
c.
d.
of the following sort algorithms is usually implemented using recursion?
Quicksort
Mergesort
Bubblesort
None of the above
4) Which
a.
b.
c.
of the following is true about the concept of recursion?
It can simplify the amount of code that is written
The base case of the function is where the recursion stops
Recursive functions contain code that calls at least one function of the same
name
d. None of the above
5) Which
a.
b.
c.
d.
of the following is true about using a standard simple linked list?
The insert at the front of the list function performs at O(n)
The append function performs at O(n)
The delete function, where it must first find the given value, performs at O(n)
None of the above
6) Which of the following is a reason to implement a stack as a linked list (with only a
head pointer and no tail) instead of as an array?
a. As a linked list, the stack will only use memory when it needs to
b. The push function as a linked list will use the append function which makes
the performance O(1)
c. Stacks are not usually designed to perform searches for a particular value or
displaying the contents in a certain order. This minimizes the disadvantages
when using a linked list because search and display are not as important
d. None of the above
7) If a priority queue is implemented as a linked list, which of the following is true?
a. If the insert function is O(1), the remove function will be O(n)
b. If the insert function is O(n), the remove function will be O(1)
c. If using a linked list, using an O(n) insert minimizes the performance cost
because shifting elements does not require a loop to perform
d. None of the above
8) Which of the following is true about the manner in which items are added and
removed with stacks and queues?
a. A stack is FIFO (First In First Out)
b. A queue is LIFO (Last In First Out)
c. Stacks perform push and pop at O(n)
d. None of the above
9) Which of the following is true about the sorting algorithms?
a. Selection sort is preferred over Bubble sort because the number of swaps is
guaranteed to be O(n)
b. Mergesort pays a price in memory usage because a duplicate size temporary
array is necessary to perform the merging and sorting
c. Quicksort is not as fast as Bubble sort
d. None of the above
10) Which of the following is true about the delete function for a binary search tree?
a. If the node to delete is a leaf, it is simply removed
b. If the node to delete has one child, the parent node adopts the child node of
the node being removed
c. If the node to delete has 2 children, the successor node can be the node that
is closest in value and greater than the node that is being removed
d. None of the above
Expected Output Section
Exercise 1 (10 pts):
There is something wrong with the findMedian Java function below. This one is designed to
only deal with an odd number of array elements. The median value of an array is the
element value of the middle index of the array when it is sorted. For example, {5, 2, 53, 1,
4}, the median is 4 because when it is sorted, the middle index has an element value of 4.
In this exercise, the code uses the partitionIt function which is the same function the
quickSort algorithm uses. After reviewing the code, answer the following questions:
1) What is the expected output of this particular program?
2) What changes to the code are needed to fix the issue(s)?
public static int[] array = {5, 2, 53, 10, 4};
public static int findMedian(int lo, int hi, int medianIndex)
{
int partitionIndex = partitionIt(lo, hi);
if (medianIndex == partitionIndex)
return array[partitionIndex];
if (medianIndex < partitionIndex)
return findMedian(partitionIndex + 1, hi, medianIndex);
else
return findMedian(lo, partitionIndex - 1, medianIndex);
}
public static void main (String args[])
{
System.out.println(findMedian(0, 4, 2));
}
Exercise 2 (15 pts):
The following sequence of numbers will be inserted into a simple binary search tree:
128, 512, 64, 2, 4, 256, 1024, 2048, 16, 8
Answer the following questions:
1) What will the tree look like after the numbers are inserted?
2) What will the tree look like after the following numbers are deleted: 2048, 64, 128
(use the delete algorithm discussed in class where the successor is found going right
then left)
3) What will the inorder, preorder and postorder traversals look like after the insert and
deletes are complete?
4) What will the 2-3-4 tree look like after the numbers are inserted? (do not worry
about the numbers to delete from part 2)
Exercise 3 (15 pts):
Given the same sequence of numbers:
128, 512, 64, 2, 4, 256, 1024, 2048, 16, 8
Show the iterations of the following sorting algorithms applied this set:
1) Insertion
2) Selection
3) Quicksort (circle the pivot number each time the partition function is called)
Algorithm writing
Exercise 1 (15 pts)
Given the following definition of a 2-3-4 Tree node (using Java):
public class Node234
{
public int keys[3];
public Node234 children[4];
};
Design an algorithm (pseudocode, C++, Java, or Python) to print the numbers of the tree in
order. Assume blank key values are represented with as -1 and should not be printed. Child
nodes that do not exist will be null. The algorithm should use the following function
signature:
public printInOrder(Node234 current)
The function will be called starting at the root as in:
printInOrder(root);
Exercise 2 (15 pts)
Examine the following sequence of numbers:
1, 4, 8, 13, 19, 26, 34, 43, 53, ...
1) Identify the pattern that is occurring with this sequence of numbers and state it in
general terms. You should have 2 cases given term number n:
Term number n when n = 1:
Term number n when n > 1:
2) Based on this formula, write the recursive function that returns the term at the nth
location. Your function should use the following signature (in Java):
public int findTerm (int n)
Hint: The factorial and Fibonacci sequences are very similar...
Extra Credit (+5 points)
Given the same sequence of numbers earlier:
128, 512, 64, 2, 4, 256, 1024, 2048, 16, 8
Show what the heap will look like when these values are inserted in this sequence.