CS 127
Midterm 2
Ondich
Due on paper at noon, Monday, March 8, 2004

For this exam, you may use your textbook, Java language references, my sample code, your own programs and notes, and divine guidance, if any is available to you. You may not consult other books, web pages, or code. If you get stuck, talk to me, but you may not talk to anyone else (not even the lab assistants or sympathetic roommates) 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.

Explain your answers clearly. Have fun.

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

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

    
    J
    E N
    C I K P
    B D G - - M O Q
    A - - - F H - - - - L - - - - R
    

    (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 J is the root, the children of N are K and P, and Q has a right child R but no left child.)

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

  3. (5 points) Start with an array of characters containing the letters FACETIOUSLY at indices 0 through 9. 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.

  4. (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.

  5. (6 points) Suppose you have a binary tree node class, like this:

    
    class BTNode
    {
        public int data;
        public BTNode left;
        public BTNode right;
    }
    

    Write a method to compute the maximum data value in a binary tree:

    
    /**
     * Returns the largest data value in the specified
     * binary tree, or -1 if root is null.  Assumes the
     * data values of nodes are non-negative integers.
     *
     * @param root -- the root of the tree
     */
    static int max( BTNode root )
    

    Don't forget: (1) this is a binary tree, not necessarily a binary search tree, and (2) recursion can be your friend.

  6. (9 points) Elmo, Zoe, and Grover are planning to implement a hash table that will have 1024 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.

  7. (2 points) Please direct me to a web site that you find amusing or enlightening. Or just weird. Or stupid in an entertaining way. Or disturbing. You pick.

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