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



1 CS 2200 - EXAM 2
Fall 2000
100 Points

Multiple Choice (3 points each)

  1. When inserting into a priority queue stored as an array (a heap), which is true?
    1. You insert at the root and repeatedly exchange its location with the bigger of the two children.
    2. You insert at the next element of the array (max+1) and repeatedly exchange with parents (as needed).
    3. You insert as a left child of the last leaf.
    4. Insertion is not defined for a heap.
  2. What is true of the following code?
    while (p < n){
      int winp = W->Winner(p);
      if (avail[winp] < item) p++;
      p *=2;
    }
    1. The code is working with bin packing in a tournament tree. winP is the amount of space in bin p.
    2. The code is working with bin packing in a tournament tree. n is the maximum subscript of the array in which the tree is stored.
    3. The code is part of the max heap insert. The heap is stored in avail.
    4. The code is part of the max heap insert. n is the array size.
    5. None of the above.
  3. What is the significance of the fact that a tree is leftist?
    1. Reheapifying is faster as the largest item is to the left.
    2. You save time because you don't have to check both children.
    3. By going right you always touch the shortest part of the tree, which can't be more than log n (if n things are in the tree).
    4. The tree is balanced, giving better complexity.
    5. None of the above

  4. Consider the following problem: You are having a party. You have told a bunch of people they can come and bring their friends. The friends of the friends may also be invited (recursively). You need to find out how many people might be coming to your party. Which technique will you use to help determine how many friends have been invited?
    1. union-find
    2. priority queue
    3. leftist tree
    4. huffman coding
    5. party queue
  5. Consider the following problem. Each department gives scholarships to its best people. Scholarship applications arrive randomly, but may be given out at various times throughout the year. At each point in time, you want to be able to determine which of the applicants is the most qualified. At the end of the year, all applicants are combined into one scholarship pool and five more scholarships are given to the most qualified applicants from all departments. Which technique will you use to help in giving out scholarships?
    1. stack
    2. union-find
    3. priority queue
    4. leftist tree
    5. huffman coding
  6. In the union-find problem, which is true after the path compression that occurs as part of the commend find(p)
    1. Each item points directly to its root.
    2. Each item which has the same root as p points directly to their root.
    3. Every item on the path from p to the root, points directly to its root.
    4. p points directly to the root.
    5. All of the above.
  7. In the union-find problem, which is true after union(x,y)?
    1. either x's parent is y or y's parent is x
    2. either x's root will have a parent of y or y's root will have a parent of x
    3. either x's root will point to y's root or y's root will point to x's root
    4. nothing happens if their roots are different
    5. All of the above

  8. GoodStuff a[max] is a binary tree stored as an array. Assume PrintInfo() is a method in the GoodStuff class. Assuming the root of the tree is at subscript 0, the code to print the parent of the node stored at subscript location K is as follows:

    1. a[K/2 -1].PrintInfo();
    2. if (K!=0) a[K/2 - 1].PrintInfo();
    3. a[K*2].PrintInfo();
    4. if (K>0) a[(K-1)/2].PrintInfo();
  9. A huffman coding for a given input set provides:
    1. Binary codes for each letter in the input set will be unique
    2. The huffman code for a letter in the input set will not be a prefix of another letter's code (For example, 01 and 011 can't both be codes).
    3. All elements of the input set are at the leaves.
    4. The huffman tree is strictly binary (every node has two children or none).
    5. All of the above
  10. You would use a leftist tree rather than a regular priority queue in order to
    1. sort
    2. traverse a tree without using recursion
    3. reduce complexity of max delete
    4. allow for two priority queues to be merged
  11. Which of the following trees is leftist?

    tex2html_wrap108

  12. Which of the following does a postorder traversal of a tree stored in leftmost child next right sibling form?

    1. void doit(node * n)
      {
         if (n==NULL) return;
         doit(n->left);
         doit(n->right);
      }
    2. void doit(node * n)
      {
         if (n==NULL) return;
         for (node * k = n->right; k !=NULL; k = k->left) doit(k);
         cout << n->val;
      }
    3. void doit(node * n)
      {
         if (n==NULL) return;
         for (node * k = n->left; k !=NULL; k = k->right) doit(k);
         cout << n->val;
      }
    4. void doit(node * n)
      {
         if (n==NULL) return;
         cout << n->val;
         for (node * k = n; k !=NULL; k = k->right) doit(k);
      }
    5. none of the above
  13. The algorithm to use a winner tournament tree to perform first fit bin packing uses the following technique
    1. Start at the root of the winner tree and go left as long as the node represented as the winner has enough room for the item in question.
    2. When a winner node is reached that cannot hold the item, examine the right child of the parent.
    3. After placing the item in a bin, replay the tournament.
    4. All of the above

  14. For the following letter frequencies, which tree represents a huffman tree for the values?
    a 3, b 4, c 5, d 3, e 7, f 8, g 8

    tex2html_wrap110

  15. You need to do a postorder traversal but you are not allowed to do it recursively. What is your choice?
    1. There is no choice. You must use recursion.
    2. There is no problem. Just use a hash table to aid the recursion.
    3. There is no problem. Just use a priority queue to aid the recursion.
    4. There is no problem. Just use a stack to aid the recursion.

Short Answer

  1. (20 points)
    1. What does the following code do? Note, whenever I ask what a code segment does, I do not want a description of what each statement does, but a BRIEF description of what the statements do collectively.
    2. Show what is output for the tree below.

      tex2html_wrap112

    3. Correct any errors.

    void BinaryTree::doit( ) { 
       char all[MAX];  
       int size=0; 
       doit(root, all,size);
    }
    
    void BinaryTree::doit( TreeNode * n, char a[],int theSize)
    {
      if (n==NULL) return;
      if (n->Left == NULL && n->Right==NULL) 
      {   cout <<  n->Element << a << endl;
          return;
      }
      a[theSize]='0';
      doit(n->Left, a, theSize+1);
      a[theSize]='1';
      doit(n->Right, a, theSize+1);
    };

  2. (15 points) A semi-leaf is a node having one child. Write a recursive function which will count all the semi-leaves in a binary tree. For example, the following tree has 3 semi-leaves (marked by X). As always, no global variables are allowed.

    tex2html_wrap114

  3. (20 points) Write the code to determine if a tree stored in leftmost child/next right sibling mode is odd. We will say a tree is odd if every node either has an odd number of children or no children. For example in the figure below, only the tree on the left is odd. Write the function isOdd which returns TRUE if the tree is odd and FALSE otherwise.

    tex2html_wrap116



Vicki Allan
Thu Mar 22 10:31:33 MST 2001