CS 127
Midterm 2
Ondich
Due Monday, March 9, 1998

This takehome is open book, open notes, and open computer. Do not consult with people other than yourself and Jeff Ondich.

  1. (12 points) For each of the following scenarios, tell me what data structure I should use, and defend your recommendation. If implementation details matter (such as whether you'll use an array or a linked structure), include them in your discussion.

  2. (12 points) Consider a "double-ended priority queue" (DEPQ) that supports the operations AddToDEPQ, DeleteMax, and DeleteMin. For each of the following DEPQ implementations, give Omega/Theta/Big-Oh descriptions of the worst-case performance of AddToDEPQ, DeleteMax, and DeleteMin.

  3. (12 points) The following is an adjacency table for a 10-node weighted graph, with nodes numbered 0 through 9.

    
        0 1 2 3 4 5 6 7 8 9
       ---------------------
    0 | 0 1 0 0 2 0 0 5 0 0
    1 | 1 0 1 0 0 0 0 0 0 0
    2 | 0 1 0 2 0 2 0 0 0 0
    3 | 0 0 2 0 0 0 1 0 0 1
    4 | 2 0 0 0 0 1 0 0 0 0
    5 | 0 0 2 0 1 0 3 0 0 0
    6 | 0 0 0 1 0 3 0 2 1 0
    7 | 5 0 0 0 0 0 2 0 0 0
    8 | 0 0 0 0 0 0 1 0 0 2
    9 | 0 0 0 1 0 0 0 0 2 0
    

    Using the graph represented by this table, do the following.



  4. (3 points) It occurred to me recently that for the last ten or fifteen years, I have been completely out of touch with popular culture (except, of course, for that portion of popular culture whose audience is 2- to 8-year-olds). For example, I don't think I could name a chart-topping song from the nineties, an MTV host, or a cast member from Melrose Place. So help me out here. Tell me something I should read or watch or listen to if I hope to get caught up before my children start rolling their eyes at me.

  5. (9 points) Suppose you start with an empty BST, or AVL tree, or Max Heap whose keys are integers. If you add items with the keys 14, 18, 9, 11, 4, 2, 13, 10, and 12, to the data structure in that order, what does the final data structure look like if: Circle and line drawings are fine in all three cases.

  6. (15 points) The code below is a simplified and de-commented version of a class that I use a lot in my own programs. Answer the following questions about my SymbolTable class.

    
    
    
    
    #include	
    #include	
    
    const int kMaxKeyLength = 20;
    
    struct STHashNode
    {
    	char            key[kMaxKeyLength];
    	int             value;
    	STHashNode      *next;
    
    	virtual         ~STHashNode( void );
    };
    
    
    class SymbolTable
    {
    	int             nBuckets;
    	STHashNode      **hashTable;
    
    	int             HashFunction( char *key );
    
    public:
    	                SymbolTable( int size );
    	virtual         ~SymbolTable( void );
    
    	int&            operator[]( char *key );
    	int             IsIn( char *key );
    	int             IsIn( char *key, int& );
    };
    
    
    static void LowerCaseCopy( char *destination, char *source )
    {
    	int	i;
    
    	for( i=0; source[i] != '\0'; i++ )
    		destination[i] = tolower( source[i] );
    
    	destination[i] = '\0';
    }
    
    ///////////////////////////////////////////////////////////////
    //	STHashNode::~STHashNode
    ///////////////////////////////////////////////////////////////
    
    STHashNode::~STHashNode( void )
    {
    	if( next )
    		delete next;
    }
    
    ///////////////////////////////////////////////////////////////
    //	SymbolTable::HashFunction
    ///////////////////////////////////////////////////////////////
    
    int SymbolTable::HashFunction( char *key )
    {
    	char result = 0;
    	for( int i=0; key[i] != '\0'; i++ )
    		result = result ^ key[i];
    	
    	return( (int)(result) % nBuckets );
    }
    
    ///////////////////////////////////////////////////////////////
    //	SymbolTable::SymbolTable
    ///////////////////////////////////////////////////////////////
    
    SymbolTable::SymbolTable( int size )
    {
    	nBuckets = size;
    	hashTable = new STHashNode* [size];
    	for( int i=0; i < nBuckets; i++ )
    		hashTable[i] = 0;
    }
    
    
    ///////////////////////////////////////////////////////////////
    //	SymbolTable::~SymbolTable
    ///////////////////////////////////////////////////////////////
    
    SymbolTable::~SymbolTable( void )
    {
    	for( int i=0; i < nBuckets; i++ )
    		if( hashTable[i] )
    			delete hashTable[i];
    
    	delete [] hashTable;
    }
    
    
    ///////////////////////////////////////////////////////////////
    //	SymbolTable::operator[]
    ///////////////////////////////////////////////////////////////
    
    int& SymbolTable::operator[]( char *key )
    {
    	char lowerCaseKey[kMaxKeyLength];
    
    	LowerCaseCopy( lowerCaseKey, key );
    	
    	int index = HashFunction( lowerCaseKey );
    	STHashNode *current = hashTable[index];
    
    	while( current )
    	{
    		if( strcmp( lowerCaseKey, current->key ) == 0 )
    			return( current->value );
    		current = current->next;
    	}
    	
    	current = new STHashNode;
    	current->next = hashTable[index];
    	hashTable[index] = current;
    	strncpy( current->key, lowerCaseKey, kMaxKeyLength );
    	return( current->value );
    }
    
    
    ///////////////////////////////////////////////////////////////
    //	SymbolTable::IsIn
    ///////////////////////////////////////////////////////////////
    
    int SymbolTable::IsIn( char *key )
    {
    	char lowerCaseKey[kMaxKeyLength];
    
    	LowerCaseCopy( lowerCaseKey, key );
    	
    	int index = HashFunction( lowerCaseKey );
    	STHashNode *current = hashTable[index];
    
    	while( current )
    	{
    		if( strcmp( lowerCaseKey, current->key ) == 0 )
    			return( 1 );
    		current = current->next;
    	}
    
    	return( 0 );
    }
    
    
    ///////////////////////////////////////////////////////////////
    //	SymbolTable::IsIn
    ///////////////////////////////////////////////////////////////
    
    int SymbolTable::IsIn( char *key, int& value )
    {
    	char lowerCaseKey[kMaxKeyLength];
    
    	LowerCaseCopy( lowerCaseKey, key );
    	
    	int index = HashFunction( lowerCaseKey );
    	STHashNode *current = hashTable[index];
    
    	while( current )
    	{
    		if( strcmp( lowerCaseKey, current->key ) == 0 )
    		{
    			value = current->value;
    			return( 1 );
    		}
    		current = current->next;
    	}
    
    	return( 0 );
    }
    




Jeff Ondich, Department of Mathematics and Computer Science, Carleton College, Northfield, MN 55057, (507) 646-4364, jondich@carleton.edu