CS140 Final

Fall 2010

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 12 problems. Make sure that you have all 12 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 I call time you must immediately drop your pen/pencil and hand in your exam.
  5. Ensure that you write clearly and legibly. Failure to do so may result in the deduction of points.
  6. The exam is open note, open book, closed neighbor.
  7. Good luck!

  1. (14 points) For each of the following questions choose the best data structure to use from the below list. You may have to use the same answer for more than one question:
    Hash Table				Linked List		Array		
    Binary Search Tree (unbalanced)		AVL Tree		Stack
    Heap
    
    1. _________________________ You are writing a waiting list application for the door person at a popular night club. As each party of customers arrive, the door person assigns the party a priority number based on the party's perceived desirability in the club. The door person then adds them to your computerized wait list and when space becomes available, the door person admits the next party with the lowest priority number. What data structure should you use to implement the wait list?

    2. _________________________ You are writing an address book application. A user should be able to insert (name, address) pairs into the address book, lookup addresses by entering a name, and delete (name,address) pairs from the address book. These are the only operations that your address book must support. What data structure should you use to implement the address book?

    3. _________________________ You are reading input lines from a file. Each input line contains a student name and a midterm grade. With each student you want to store the set of midterms associated with that student. For each student, the midterms should be stored in the order in which they are read. You know that each student will have 3 midterms. What data structure should you use to store the midterms for each student?

    4. _________________________ When computing taxes on selling the shares of a stock, you are allowed to select a last-in, first-out (LIFO) option, which means that the last shares that you purchased are the first ones that you consider sold. If you have a computer application that reads a set of stock transactions, what data structure should you use to store the stock transactions in order to facilitate implementing the LIFO option?

    5. _________________________ You want to read a file that contains the names of contestants on America's Next Top Model and the number of votes that they received. You want to be able to be able to perform a variety of range queries, such as finding the top 5 contestants, the bottom 5 contests, or the number of contestants receiving between x and y votes. What data structure should you use, assuming that you feel the vote totals will be presented in a random order?

    6. You are reading the names of golfers and their scores from a file. Each input line contains the name of a golfer, the golf course where the round was played, and the score for that round. For each golfer you want to store the information for each round played by that golfer. There is no limit to the number of rounds per golfer. After reading the input, the user should be able to query for a golfer and obtain the golfer's average score and a print out of the rounds played by that golfer. The insertion operation and the query operation are the only operations performed on the data.

      1. _________________________ What data structure should be used to store the golfers?

      2. _________________________ For each golfer, what data structure should be used to store the information about the golfer's rounds? You should assume that each round has one record and the rounds may be stored in any order.

  2. (4 points) For the following problem, give a one line statement that achieves the desired outcome: Divide an integer, n, by 16 and assign the result to n. Your answer must use a bit expression, not division.
  3. (10 points) Show the hash table that results if the integers 22, 34, 11, 21, 44 are inserted into the following hash table using:
               -----------------------
          0    |                     |
    	   |                     |
               -----------------------
          1	   |                     |
    	   |                     |
               -----------------------
          2    |                     |
    	   |                     |
               -----------------------
          3	   |                     |
    	   |                     |
               -----------------------
          4	   |                     |
    	   |                     |
               -----------------------
          5	   |                     |
    	   |                     |
               -----------------------
          6	   |                     |
    	   |                     |
               -----------------------
          7	   |                     |
    	   |                     |
               -----------------------
          8	   |                     |
    	   |                     |
               -----------------------
          9	   |                     |
    	   |                     |
               -----------------------
          10   |                     |
    	   |                     |
               -----------------------
    
    
  4. (10 points) Show the hash table that results if the integers 51, 46, 23, 81, 21, 38, 30, 44, 56, and 69 are inserted into the following hash table using: You should append integers to the end of your lists.
               -----------------------
          0    |                     |
    	   |                     |
               -----------------------
          1	   |                     |
    	   |                     |
               -----------------------
          2    |                     |
    	   |                     |
               -----------------------
          3	   |                     |
    	   |                     |
               -----------------------
          4	   |                     |
    	   |                     |
               -----------------------
          5	   |                     |
    	   |                     |
               -----------------------
          6	   |                     |
    	   |                     |
               -----------------------
    

  5. (4 points) Based on the discussion in class about load factor considerations and the best types of numbers to choose for hash table sizes, what table size should you choose for a data set that contains 14 elements if you use:

    1. separate chaining:

    2. linear probing:

  6. (4 points) Suppose you are given the following type declaration:
    typedef struct {
      int salary;
      char *name;
    } employee;
    
    Declare a pointer variable named workers that is a pointer to an array of employee pointers. Then malloc an array of 20 employee pointers and assign the result to workers. You can combine the declaration and the malloc into a single statement if you wish.

  7. (4 points) Write a statement that declares a pointer variable named deleteEntry that points to a function that returns a bool and that takes as a parameter a pointer to a void *.

  8. (8 points) Show the binary search tree that results if 150 is deleted from the tree below:
                               ---185---
                              /         \
                            150         200
                            /  \           \
                          100  175         300
                         /     /
                        90   160
                               \
                               170
    
  9. (6 points) Show the result of doing a single left rotation about the node 400. 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 350
    
  10. (10 points) 160 has just been inserted into the following AVL tree, causing it to violate the AVL condition:
                              85
                            /    \
                          50     200
                        /       /   \
                       25     150   300
                             /   \
                           100  175
                                /
                              160
    

  11. (12 points) Suppose you are given the following heap:
                          ----5----
                         /         \      
                       10           8
                     /    \        /  \  
                   15      11     13  15
                  /  \    /  \    /
                 20  17  16  21  15
         
    1. Draw the array represented by this heap (i.e., draw the array representation).
    2. Show the heap that results from inserting 4. If you want partial credit then show your intermediate steps. Please draw the resulting heap as a tree.
    3. Show the heap that results from performing a deleteMin on the original heap. If you want partial credit then show your intermediate steps. Please draw the resulting heap as a tree.
  12. (14 points) Assume that you have a binary tree node that has been declared as follows:
    typedef struct node {
      int salary;     // an employee's salary
      char *name;     // an employee's name
      struct node *leftChild;
      struct node *rightChild;
    } TreeNode;
    
    Further assume that you have a binary search tree that is sorted based on an employee's salary. Write a function named printRange that traverses a binary tree and prints in sorted order all employees with a salary greater than or equal to a specified value. For example, here is some sample input and output, based on the assumption that your function is requested to print all employees with a salary greater than or equal to 26000:
    inputoutput
    smiley 33000
    brad 40000
    mickey 20000
    daffy 17000
    tom 3000
    dick 26000
    mary 38000
    susan 30000
    joe 45000
    dick : 26000
    susan : 30000
    smiley : 33000
    mary : 38000
    brad : 40000
    joe : 45000

    Your program should specifically meet the following specifications:

    1. its parameters are a pointer to the root node of a binary tree and an integer representing the threshold salary.
    2. it returns nothing
    3. it uses recursion to traverse the tree and print the employees whose salary is greater than or equal to the threshold salary.
    4. it prints employee names in a left-justified field that is 10 characters wide, followed by a colon (:), followed by a space, and then the salary in a right-justified field that is 7 characters wide.
    5. it does not show me any include statements
    6. it does not use my binary search tree library.
    7. it assumes that the tree has already been established for it by the calling function.

    This problem is different than the one you did in lab. You do not have a linked list you can use to traverse the tree. You must traverse it using recursion.

    Alternative problem (worth 10/14 points): If you cannot think of a way to answer the above problem, then you can write a function named printTree that takes a pointer to the root node of a binary search tree and prints the employee's in descending order based on salary. printTree should return nothing. It should print employees using the same print specifications given above.