CS 2005, B Term 1999
Data Structures and Programming Techniques
Solutions to the Practice Problems for Test 3

Your Name: _____________________________________

Your Lab Section: ______________     Your Login Name:_________________

Instructions.   Read each problem carefully. Write your solutions in ink in the spaces provided. Write neatly and explain your answers as clearly as possible.

Refer to the Table class definition that appears below.

    template <class RecordType>
    class Table {
        static const size_t TABLE_SIZE = 811;
        Table( );
        Table(const Table& source);
        ~Table( );
        void insert(const RecordType& entry);
        void remove(int key);
        void operator =(const Table& source);
        void find(int key, bool& found, RecordType& result) const;
        bool is_present(int key) const;
        size_t size( ) const { return total_records; }
        Node<RecordType> *data[TABLE_SIZE];
        size_t total_records;
        size_t hash(int key) const;
        Node<RecordType>* find_node(int key) const; };
Assume the following Node template declaration:
    template <class Item>
    struct Node     {
        Item data;
        Node *link; };
Use the following type for all records:
   struct MyRecType {
      int key;
      int data;     };

  1. For each of the following, write a C++ program segment that accomplishes the stated objective. Table accesses must use only the public members of the Table class. Declare all variables used in your program segment.

  2. Implement the Table member function is_present() specified below without using any linked list toolkit functions. Use the private member variables defined above. Note that this is a chained hashing implementation.
    //   bool is_present(const int& targetkey) const
    //     Postcondition: The return value is true if there is a record in the
    //     Table with the specified key. Otherwise, the return value is false.
    template <class RecordType>
    bool Table<RecordType>::is_present(const int& targetkey) const {
       size_t index = hash(targetkey); 
       Node<RecordType> *node_ptr = data[index];
       while ((node_ptr!=NULL) && ((node_ptr->data).key!=targetkey))
          node_ptr = node_ptr->link;
       return ( node_ptr!=NULL );

  3. Suppose you've taken on a job as a grader in the CS department. The 30 exams you've just graded are sitting in a randomly ordered pile on your desk. The professor (your boss) has asked you to return the graded exams to him/her in a sorted pile, with the highest grades at the top of the pile.

  4. Fill in the blanks below to obtain a correct implementation of the quicksort algorithm. Assume that the partition function returns the correct value in the pivot_index argument.
    void quicksort(int data[ ], size_t n)
        size_t pivot_index; // Array index for the pivot element
        size_t n1;          // Number of elements before the pivot element
        size_t n2;          // Number of elements after the pivot element
        if (n > 1)
            // Partition the array, and set the pivot index
            partition(data, n, pivot_index);
            // Compute the sizes of the subarrays
            n1 = pivot_index;
            n2 = n - n1 - 1;
            // Recursive calls to sort the subarrays
            quicksort(data, n1);
            quicksort((data + pivot_index + 1), n2);

  5. Circle all true statements among those on the following list:
       Every AVL tree is balanced in the sense that all of 
       its leaf nodes are at the same depth.
    -->Insertion sort is faster than mergesort for array sizes
       less than 20 or so.
       A derived class inherits all members of the base class
       as public members.
    -->A graph with n nodes has an adjacency matrix of size nxn