CS 127
Midterm 2
Ondich
Due on paper at noon, Friday, March 7, 2003

For this exam, you may consult your textbook, C++ language references, my sample code, and your own programs and notes. You may not use other books, web pages, or code. If you get stuck, talk to me, but please don't talk to anyone else (not even the lab assistants) about the exam. You may discuss syntax and system problems with the lab assistants, but you must tell them that you are working on an exam.

  1. (10 points) Imagine starting with an empty binary search tree whose nodes have characters for keys, and adding the letters C A R L E T O N to the tree in that order. Show the resulting tree if:

  2. (5 points) Start with an array of characters containing the letters C A R L E T O N at indices 1 through 8. Now apply the heap formation algorithm that forms the first stage of Heapsort (using a min-heap). Show the resulting array. Then, delete the smallest item from the heap. Show the resulting array.

  3. (12 points) For each of the following scenarios, recommend a data structure. Briefly justify your recommendation, using complexity arguments and big-oh notation where appropriate.

  4. (8 points) Suppose you have a Set class defined like this:

    
    template <class T>
    class Set
    {
    private:
    	// A list of unique items, sorted in increasing order.
    	list<T>		mItems;
    
    public:
    	...
    	// Modifies *this to be the union of *this with b. 
    	// For example, if both a and b are declared as type
    	// Set<int>, then a.Union( b ) changes a to be
    	// the union of a and b.
    	void		Union( const Set<T>& b );
    	...
    };
    

    Write an implementation for the Union function. Don't forget that a set is a collection of distinct items.

  5. (9 points) Elmo, Zoe, and Grover are planning to implement a hash table that will have 2048 slots. They have agreed to resolve collisions with chaining. The items the hash table will contain will be English words. They are arguing over what hash function to use. Criticize (positively or negatively) each of these proposals.

  6. (2 points) Tell me a joke, if you wish.

  7. (6 points) Consider the following binary search tree, whose nodes' keys are letters of the alphabet:

    
    R
    J U
    D L S V
    C H - M - T - Y
    B - F I - - - - - - - - - - - -
    

    (In this representation of a tree, I have written each row of the tree from left to right, with a "-" wherever there is no node. So R is the root, the children of U are S and V, and V has a right child Y but no left child.)

    Show the tree that results when you delete the root (R) from the tree using the BST node deletion algorithm in your textbook.

  8. (9 points) Consider a graph G with 9 nodes numbered 0 through 8, whose edge set is {(0,6), (0,1), (0,2), (1,7), (2,7), (2,4), (8,6), (5,8), (5,3), (3,7)}.