1. Tear off this sheet and use it to keep your answers covered at all times. If you need more scratch paper, I have some.
  2. Write your name on the back of the test by the staple to facilitate returning the exams. Do not write your name at any other place on the exam.
  3. If you don't understand a question, come and ask me about it!
  4. Do NOT use ink. Programming often requires one to change his/her mind.
  5. No question is meant to have a syntax error. If it does, fix it or ask me how I want the error resolved.
  6. You are graded not only on the correct answer, but also the best answer.
  7. NO calculators are allowed on the test. They will not be needed.

class EdgeNode
{ public:
    int ToNode;  // Node number
    int FromNode;  // Node number
    bool Selected; // True if edge is selected as being part of the match
    EdgeNode *Next;  // Next edge in linked list
    EdgeNode( int F = -1, int T=-1, EdgeNode *N = NULL): ToNode( T ),
        FromNode(F), Next( N ){}
};
class GraphNode
{ public:
   char Element;  // Node code
   EdgeNode *Adj;  // Adjacency list
   bool visited;
};

class Graph
{  GraphNode G[MAX];
}



1 Final Exam CS 2200
Fall 2000
100 Points

Multiple Choice (3 points each)

  1. For the code below, which picture represents the complexity?

    void doit(int n)
    { int i;
      if (n <=2) return;
      if (n%2==0)
        doit(n/2);
      else doit(n/2);
    }

    tex2html_wrap128

  2. Left most child, next right sibling method of storage involves the following
    1. more space is required for faster access
    2. less space is required for slower access
    3. code is less complicated for more space
    4. by level traversals are facilitated
  3. What best describes what the following code does?
    int mystery(node * root)
    {  if (root== NULL) return -999;
       if (root->left ==NULL || root->right ==NULL) return root->val;
       return( max(mystery(root->left), mystery(root->right)));
    }
    1. It finds the value of the biggest leaf node.
    2. It finds the biggest value of the leaves or semi-leaf nodes it looks at.
    3. It checks to make sure the tree has the heap property.
    4. It finds the value of the biggest of all the nodes (internal and leaf).
  4. You have two solutions for the same problem. One is O(tex2html_wrap_inline104) and the other is O(n log n). When you compare the times for various problems, the O(n log n) algorithm is sometimes faster and sometimes slower. How do you explain this?
    1. The complexity is worse case. For some data sets, you can do significantly better.
    2. Your problem sizes are too large to see the advantages of the O(n log n) algorithm.
    3. You are running on a very fast machine.
    4. The O(tex2html_wrap_inline104) algorithm has a higher value for the constant.
  5. For the timing information below, what is the complexity?


    tabular53

    (a) O(1) (b) O(log n) (c) O(n) (d) O(n log n) (e) O(tex2html_wrap_inline104)

  6. From our theorem we know:
    T(n)= a T(n/b) + O(tex2html_wrap_inline110)
    if tex2html_wrap_inline112 T(n) is O(tex2html_wrap_inline114)
    if tex2html_wrap_inline116 T(n) is O(tex2html_wrap_inline118)
    if tex2html_wrap_inline120 T(n) is O(tex2html_wrap_inline110)

    Consider the following algorithm:

    int doit(int A[], int n){
        if (n <=1) return 1;
        int t;
        for (int i =0; i < n; i++)
           t++;
        t+= 4*doit(A,n/3)
        return t;
    }
    What is the complexity?
    (a) O(1) (b) O(log n) (c) O(n) (d) O(n log n) (e) O(tex2html_wrap_inline104)
  7. A breadth first traversal of a graph is commonly done using
    (a) a stack (b) a queue (c) a heap (d) a topological ordering (e) none of the above
  8. A collision resolution in which each key has an individualized increment is termed
    1. linear
    2. quadratic
    3. double hashing
    4. lazy
  9. Rehashing is done for the following reason
    1. The hash table is becoming too full
    2. The hash table has too many lazily deleted items
    3. The hash table is too large
    4. All of the above
  10. A merge sort would be an example of which type of algorithm?
    1. divide and conquer
    2. greedy
    3. exhaustive
    4. dynamic programming
  11. A leftist tree was designed to
    1. allow priority queues to be merged
    2. promote balance
    3. save storage
    4. facilitate bin packing
    5. none of the above
  12. In sorting, when a large array has just a few elements out of order, which algorithm is the best for restoring the order?
    1. mergesort
    2. insertion sort
    3. selection sort
    4. heap sort
  13. What does the following code accomplish?
    for (int i = n-1; i >= 1; i--)
    {
      H.DeleteMax(x);
      a[i+1]=x;
    }

    1. Priority queue
    2. heapsort
    3. union find initialization
    4. tournament tree
  14. A friend says, ``Keeping a tree balanced at all times is overkill. Most trees are reasonably balanced. Why not just use a regular binary search tree and rebuild it when it gets too off balance?'' An appropriate response would be

    1. That would be reasonable.
    2. This is not a good idea as it is difficult to define ``too off balanced''.
    3. This is a bad idea. Why pay the cost of an occassional O(n) operation when you can keep the tree balanced without increasing the O(log n) complexity?
    4. This is a bad idea as rebalancing is O(tex2html_wrap_inline104).

Short Answer

  1. (13 points) A binary tree is strictly binary if every node has exactly two children or none. Write the code to check if a tree is strictly binary. The code should return a true if the tree is strictly binary and a false otherwise.

  2. (15 points) Use the code from the cover page to store an undirected graph using an adjacency list representation. Assume that a graph has been stored and someone has found a matching of the graph such that the edges of the match are marked using the boolean ``Selected'' in the EdgeNode class.

    Write a routine to verify that the matching is a valid matching. We will consider a valid matching to mean that (a) no node is paired with more than one other node, and (b) there is no edge which connects two unmatched nodes.

  3. (15 points) The code below contains the dynamic programming solution for finding the length of the shortest path between any two nodes. The actual path has not been produced.

    Modify the code so that the actual shortest path between any two points will be stored as an array of nodes. For example, if the shortest path from node 5 to node 8 consists of the path tex2html_wrap_inline130 the ActPath array would contain {5,4,1,8}.

    class Path
    { int FromNode;
      int ToNode;
      int len;  // Length of path
      int ActPath[MAX];  // list of nodes in actual shortest path from FromNode to ToNode.
      int ActPathCt;  // number of nodes in ActPath
    }
    
    Path Short[MAX][MAX];
    
    for (k = 0; k<nodeCt;k++)
    
        for (i = 0; i<nodeCt;i++)
    
          if (Short[i][k].len <INFINITY){
    
            for (j = 0; j<nodeCt;j++)
    
               if (Short[i][k].len + Short[k][j].len < Short[i][j].len)
    
                     Short[i][j].len=Short[i][k].len + Short[k][j].len;
          }

  4. (15 points) The following code attempts to delete from a binary search tree. Correct any errors. Assume deleteSmall removes the smallest value in the tree passed to it and returns the value deleted.

    void deleteNode(Node * n, int delVal)
    {
      if (n == NULL) return;
    
      if (n->val < delVal) deleteNode(n->right,delVal);
    
    
      else if (n->val < delVal) deleteNode(n->left,delVal);
    
    
           else{  
    
    
                 int small = deleteSmall(n->right);
    
    
    
                 n->val = small;
    
               }
    }



Vicki Allan
Thu Apr 26 16:41:01 MDT 2001