CS140 Final Exam: December 11, 2006 - Answers


Question 1: 8 points


Question 2: 6 points


Question 3: 4 Points

The answer is p. Straight from the definition of big-O. The answers with b and n0 are meaningless since neither is assigned a value. All of the rest of the answers are false, with the exception of a, g and m. Unfortunately those would prove that g(n) = O(f(n)).

Grading: 4 points for p. 2 points each for j, x and v. 1.5 points for for d, l,r and o. 1 point for f. Zero for the rest.


Question 4: 8 points

First, I would check whether it is a binary search tree. In other words, each node has at most two children. All nodes in the subtree rooted by a node's left child have keys less than the node, and all nodes in the subtree rooted by a node's right child have keys greater than the node. All subtrees of the tree are AVL trees. Finally, the heights of the two subtrees of a node must be equal or must differ by one.

We like AVL trees because find and insert operations on AVL trees with n nodes take O(log n) time.

Grading: 8 points, allocated as follows:


Question 5: 7 points

a: Since 20 has two children, you must find the smallest node in the right subtree, delete it, and replace 20 with it. This is pictured below. Equivalently, you could find the largest node in the left subtree (15), delete it, and replace 20 with it.

Grading: 2 points for this one.

b: First you insert 43 as if the tree were a regular binary search tree:

Then you splay it -- this is a zig-zag splaying, which means you rotate twice about 43. Here is the final tree:

Grading: 2 points for this one. 1 point if you had 43 at the top of the tree, but didn't rotate correctly.

c: First you insert 2 as if the tree were a regular binary search tree:

Then you splay it. First, you do a zig-zig splaying, which means you rotate 5, then rotate 2:

Finally, since 2's parent is the root, you do a single rotation about 2:

Grading: 3 points for this one. 1 points if you had 2 at the top of the tree, but didn't rotate correctly.


Question 6: 9 points

This one is straight from the in-class labs. It requires a recursive, postorder traversal: Find out the height of each of your children -- return the maximum of these plus one. You must have two procedures -- one to find the height, and one to print it:
int height(Tree *t)
{
  Dllist tmp;
  int h, mh;
  Tree *c;
 
  mh = 0;
  dll_traverse(tmp, t->children) {
    c = (Tree *) tmp->val.v;
    h = height(c);
    if (h > mh) mh = h;
  }
  return mh+1;
}

void print_height(Tree *t)
{
  printf("%5d\n", height(t));
}

Grading


Random Extra Credit Question

The postorder traversal is from the song "Scorched Earth" by Van Der Graaf Generator (or Peter Hammill). A dark song, full of brooding intensity, from the 1970's, most likely relegated to obscurity as I don't see it listed in itunes. Besides having a great section in 5/4 time, Hamill works vocabulary words such as "spoor" and "ambuscade" into the lyrics.... Yes, "vein" is not spelled correctly....

Guesses from the class, in order from best to worst: