CS140 -- Final Exam. December 10, 2008 - Answers & Grading


Question 1 - 7 points

Part 1: You have to split the first external node into two nodes. This would put an extra link coming from the root node, which means that it too must be split. Here's the result:

Some of you simply shifted the 9 into the next external node. While that results in a correct B-tree, and it is an optimization, that is not the basic B-Tree operation. Part 2: The point of B-trees is to store data on disk. For them to work efficiently, nodes should all fit within one disk block, which is a fixed size. Internal nodes hold keys and links. External nodes hold keys and data. Therefore, a different number of keys/links than keys/data may fit into a single disk block. This means that different values of M may be required for internal and external nodes.

Grading


Question 2 - 7 points

Part 1: "Teachers" has a hash value of 47, so it should go into index 47%16 = 15. Since there is a value already there, we try index 0. There is a value there, and in indoces 1 and 2, so we put "Teachers" into index 3. "Critics" has a hash value of 170, so it should go into index 170%16 = 10. There is a value there, but not in index 11, so it should go there. The answer:

Part 2: Try to insert something with a hash value of 1. That index is occupied, so you try index 1+1*1 = 2. That's occupied, so you now try index 1+2*2 = 5. Also occupied, so you try index 1+3*3 = 10. That's occupied too, so now you try 1+4*4 = 17, which equals 1 mod 16. Hmmmm. 1+5*5 = 26%16 = 10. 1+6*6 = 37%16 = 5. 1+7*7 = 50%16 = 2. You will keep getting this cycle, over and over, and you will never be able to put the value into the hash table, even though it is largely empty. To fix this, change your table size to be prime. Then you have no problems.

Extra Credit: This is from Devo's classic song "Jocko Homo," from the album "Q: Are We Not Men? A: We Are Devo!" 0.5 points.

I always enjoy seeing the guesses that y'all make. Those of you who study from past exams figured that 70's prog rock bands are a good guess, and in general you're right, but not here. Here were your guesses:

Grading



Question 3 - 6 points

f(x), g(x) and h(x) are all equivalent from a big-O perspective, because they increase linearly as x gets large. Therefore, A, B, D, E, G and H are all true. The last function, j(x), has a maximum value of three. Unlike the others, it does not go to infinity as x grows. Therefore, it is a smaller function than the others. Since smaller functions are big-O of bigger functions, this means that J, K and L are true too. So the answer is A, B, D, E, G, H, J, K and L.

Grading

You got 0.5 points for assigning each potential answer correctly. Since there are 12 potential answers, that's a total of 6 points.


Question 4 - 2 points

When D is first inserted into the splay tree, it gets put as the right child of C:

It now must be splayed. It is in a zig-zag position, so you perform a double rotation about it:

Since its parent is the root node, you now need to perform a single rotation. Here is the answer (answer I):

Grading

Two points for I. Half a point for A, D or E.


Question 5 - 3 points

When R is first inserted into the splay tree, it gets put as the right child of P:

It now must be splayed. It is in a zig-zig position, so you rotate about P, and then about R:

Since its parent is the root node, you now need to perform a single rotation. Here is the answer (answer H):

Grading

Two points for H. Half a point for B, D, F or I.


Question 6 - 3 points

When G is first inserted into the AVL tree, it gets put as the left child of H:

Now we move to the root, trying to find a height imbalance. There is no imbalance at H, F or D, but there is at the root. This is solved by a double-rotation about F The answer is therefore I:

Grading

Two points for I. No points for the others.


Question 7 - 7 points

Parts A-C: To perform disjoint_find(), you chase the links until you get to a node whose link field is -1. Therefore:

Therefore, the answers will be no, yes and yes.

For Part D, we want to either make links[4] = 7 or links[7] = 4, and we want to update ranks and sizes accordingly. Since we are doing "union by rank", we make the one with the smaller rank the child of the one with the higher rank. Therefore, links[7] is set to 4. We need to update sizes[4] to be (sizes[4]+sizes[7]), so sizes[4] is set to 6. We don't need to update ranks[4] because it is greater than ranks[7].

Finally, we need to decrement nsets.

Grading

One point for each of parts A, B and C. Part D: 1 point for setting links[7] to 4, one point for setting sizes[4] to 6, 1 point for decrementing nsets and 1 points for doing nothing else. You got a half a point if you made 7 the parent instead of 4.


Question 8 - 4 points

My apologies for the problems some of you had understanding the problem. I wanted a recursive enumeration and not a program that read standard input (I would have said "lines on standard input" if that's what I meant). Regardless, I should have given an example. Here's the answer (in q8.c). I've reduced the point value of this one because of the confusion, but if you answered the other problem correctly, you received full credit.

#include <stdio.h>
#include <stdlib.h>

void enum_strings(char *s, int a, int b, int start)
{
  if (a == 0 && b == 0) {
    s[start] == '\0';
    printf("%s\n", s);
    return;
  }
  if (a > 0) {
    s[start] = 'A';
    enum_strings(s, a-1, b, start+1);
  }
  if (b > 0) {
    s[start] = 'B';
    enum_strings(s, a, b-1, start+1);
  }
}

main(int argc, char **argv)
{
  char *s;
  int a, b;

  if (argc != 3 || sscanf(argv[1], "%d", &a) == 0 || 
                   sscanf(argv[1], "%d", &b) == 0 || 
                   a < 0 || b < 0) {
    fprintf(stderr, "usage: q8 a b\n");
    exit(1);
  }

  s = (char *) malloc(sizeof(char) * (a + b + 1));
  enum_strings(s, a, b, 0);
}

Grading


Question 9 - 5 points

This is a very straightforward recursive tree traversal.

int tree_size(Node *n)
{
  int size;
  size = 1;
  for (i = 0; i < n->nchildren; i++) size += tree_size(n->children[i]);
  return size;
}

Grading

This one is pretty much hit or miss. You got points according to how well I thought your solution was on mark.