CS140 Final

Spring 2015

Instructions

  1. Write your name clearly at the top of the exam
  2. You must answer all the questions on the exam. The exam has 9 problems. Make sure that you have all 9 problems.
  3. Write your answers on the exam pages and hand in the exam when you are finished.
  4. You have until the end of the class period to finish the exam. When time is called you must immediately drop your pen/pencil and hand in your exam.
  5. Ensure that you write clearly and legibly and that your diagrams are easy to read. Failure to do so may result in the deduction of points.
  6. The exam is closed note, closed book, closed neighbor.
  7. The total points available for this exam (lab coding questions plus this paper exam) is 120 points. You will be graded out of 100 however, so in effect you have 20 extra credit points to work with.
  8. Good luck!

  1. (8 points) Show the binary search tree that results if 300 is deleted from the tree below:
                               ---100---
                              /         \
                             50        -300-
                            /         /     \
                           25       125     400
                     	       /   \       \
                           	     110   200     500
                                 	      \
    			     	      250
    				     /   
    			           225   
    

  2. (6 points) Show the result of doing a single right rotation about the node 175. Do not worry if the rotation increases the height of the tree. All I care about is whether you know how to perform a rotation.
                                -300-
                               /     \
                             175	 400
                               \
    			   250
    		          /   \
    			200   275
    
  3. (12 points) 12 has just been inserted into the following AVL tree, causing it to violate the AVL condition:
                              20
                            /    \
                          10     40
                        /    \
                       8     16
                            /
                           12
    
  4. (6 points) What is the Big-O running time for the following functions?

    1. ___________________: T(n) = 5n2 + 10n * log n + 1000n
    2. ___________________: T(n) = 1000000
    3. ___________________: T(n) = 100n3 + 20n2 + 100 + 2n

  5. (2 points) If you had three programs with the above running times, which one would you prefer if you were unsure about your input size but wanted to assume the worst (i.e., that the input sizes could be fairly large). Circle one of the following three letters to denote which program you would choose:
         a        b        c
    

  6. (8 points) Behold the following 4 fragments of code:
    (a)
    int a, b; int sum; cin >> a; cin >> b; sum = a + b; cout << sum << endl;
    (b)
    for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { sum = 0; for (k = 0; k < n; k++) { sum += matrix1[i][k] * matrix2[k][j]; } result[i][j] = sum; } }
    (c)
    sum = 0 for (i = n; i >= 1; i = i/2) { sum += a[i]; }
    (d)
     for (i = 0; i < n; i++) {
         min[i] = a[i][j];
         for (j = 0; j < n; j++) {
             if (a[i][j] < min[i]) {
                  min[i] = a[i][j]
             }  
         }
     }
     for (i = 0; i < n; i++) {
         cout << min[i] << endl;
     }
         

    For each fragment of code, please circle its Big-O running time:

    a      O(1)    O(log n)   O(n)   O(n log n)   O(n2)  O(n3)   O(2n)
    
    b      O(1)    O(log n)   O(n)   O(n log n)   O(n2)  O(n3)   O(2n)
    
    c      O(1)    O(log n)   O(n)   O(n log n)   O(n2)  O(n3)   O(2n)
    
    d      O(1)    O(log n)   O(n)   O(n log n)   O(n2)  O(n3)   O(2n)
    
    

  7. (10 points) For each of the following questions give the average case and worst case running time for each operation on the specified data structure. Please use Big-O notation. For example, the average case and worst case running time for appending an element to the end of a list is O(1) and the average case and worst case running time for finding an element in an ordered list is O(n).

    Operation Average Case Worst Case
    Inserting an element into a hash
    table that uses separate chaining
      
    Finding an element in a binary search tree   
    Inserting an element into an AVL tree   
    Adding an element to the front of a vector   
    Removing an element from the top
    of a stack (i.e., pop)
      

  8. (10 points)
     
         a. array
         b. vector
         c. stack
         d. deque
         e. hash table
         f. list
         g. binary search tree
         h. AVL tree
         i. BTree
    

    For each of the following questions choose the best answer from the above list. Assume that the size of an array is fixed once it is created, and that its size cannot be changed thereafter. Sometimes it may seem as though two or more choices would be equally good. In those cases think about the operations that the data structures support and choose the data structure whose operations are best suited for the problem. You may have to use the same answer for more than one question:

    1. ____________ The most time efficient data structure you could use to implement a stack in which there is an upper limit, max, on the number of elements that can be stored on the stack.

    2. ____________ The most time efficient data structure you could use to implement a stack in which the the number of elements that can be stored on the stack is unlimited.

    3. ____________ The data structure you should use if you want an in memory tree to store keys and the keys to be inserted are inserted in a random order

    4. ____________ The data structure you should use if you want an in memory tree to store keys and the keys to be inserted are inserted in a nearly sorted order

    5. ____________ The data structure used to manage the frames associated with each function (i.e., the frame that gets created when a function is called to store its parameters and local variables, and then gets destroyed when the function returns).

  9. (8 points) You are given the following B-tree of order 5. Show the new btree that results when 375 is inserted into it.