2013 CS140 Final Exam - Answers and Grading

James S. Plank


Question 1 - Basic Programming - 10 Points

Here you simply read the lines and put them into a data structure which allows you to retain the last n lines efficiently. It's not easy with a vector, because you cannot insert or delete efficiently from the front of a vector -- you do better to use a deque or list from the STL, or a Queue or Dlist from the lecture notes/labs. In the code below, I create the deque in reverse order, and then create the return vector from it (this in q1.cpp):


#include <vector>
#include <deque>
#include <string>
#include <iostream>
using namespace std;

vector <string> get_n_lines(int n)
{
  vector <string> rv;
  deque <string> q;
  string s;
  int i;

  while (getline(cin, s)) {
    q.push_front(s);
    if (q.size() > n) q.pop_back();
  }
  for (i = 0; i < q.size(); i++) rv.push_back(q[i]);
  return rv;
}

Grading


Question 2 - Basic Data Structures and Big-O - 12 Points

Grading: 1 point per question. In your grade files, I used the following abbreviations:


Question 3 - Were you in this class? - 10 Points

Straight from the lecture notes:


void Solve(Towers *t, int from, int to, int npieces)
{
  int i, other;

  if (npieces == 1) {
    t->Make_Move(from, to);
    return;
  }

  for (i = 0; i < 3; i++) if (i != from && i != to) other = i;
  Solve(t, from, other, npieces-1);
  t->Make_Move(from, to);
  Solve(t, other, to, npieces-1);
}

Grading was subjective here. I gave you some credit if you at least gave me a base case and some recursion, but not much if it wasn't too cogent.


Question 4 - Binary search trees 10 points

I quote from the lecture notes:

Case 1: The node has no children (it's a leaf node). You can simply delete it.

Case 2: The node has just one child. To delete the node, replace it with that child.

Case 3: The node has two children. In this case, you find the node in the tree whose value is the greatest value less than (or equal to) the node's value. That will be the rightmost node in the subtree rooted by the left child. That node will not have a right child. First, delete it. Then use it to replace the node that you are deleting.

Grading


Question 5 - Stack em up - 10 points

Again, straight from the lecture notes:


void Stack::Push(string s)
{
  Stacknode *newnode;

  newnode = new Stacknode;
  newnode->s = s;
  newnode->next = top;
  top = newnode;
}

string Stack::Pop()
{
  string rv;
  Stacknode *oldtop;

  if (top == NULL) {
    cerr << "Error: pop() called on an empty stack\n";
    exit(1);
  }

  oldtop = top;
  top = oldtop->next;
  rv = oldtop->s;
  delete oldtop;
  return rv;
}

Grading

Five points for each method.


Question 6 - Traversals - 12 Points

Grading

Three points per traversal.


Question 7 - Traversals - 12 points

Tree 1: When you insert N, you get the left tree below. That is imbalanced at node J, and it's a Zig-Zag, so you must perform a double-rotation about node L to get the right tree. The answer is I.

Tree 2: When you delete M, you replace it with L. That tree is fine, so the answer is J.

Tree 3: When you insert X, you get the left tree below. That is imbalanced at node T, and it's a Zig-Zig, so you must perform a rotation about node V to get the right tree. The answer is L.

Tree 4: When you delete G, you get the left tree below. That is imbalanced at node J, and it's a Zig-Zag, so you must perform a double rotation about node P to get the right tree. The answer is F.

Tree 5: When you delete W, you get the left tree below. That is imbalanced at node V, and it's a Zig-Zig, so you must perform a single rotation about node R to get the right tree. The answer is L.

Tree 6: When you delete B, you get an imbalance about D, which you fix by a double-rotation about G. That is pictured to the left. That is imbalanced at node J, and it's a Zig-Zag, so you must perform a double rotation about node P to get the right tree. The answer is F.