CS140 Final Exam - Spring, 2012 - James S. Plank

Answers and Grading


Question 1

This is straight from the lecture notes. My code differs a little, but it's functionally equivalent:

int Sudoku::Recursive_Solve(int r, int c)
{
  int i, nr, nc;

  if (r == 9) return 1;

  nc = c+1;
  nr = r;
  if (nc == 9) { nc = 0; nr++; }
  if (Grid[r][c] != ' ') return Recursive_Solve(nr, nc);
    
  for (i = '1'; i <= '9'; i++) {
    Grid[r][c] = i;
    if (Is_Row_Valid(r) && Is_Col_Valid(c) && 
        Is_Panel_Valid(r, c) && Recursive_Solve(nr, nc)) return 1;
  }
  Grid[r][c] = ' ';
  return 0;
}

Grading

10 points. Basically, if you had the right code, you started with 10 points and were deducted for errors. If you didn't have the right code, I assigned you the number of points that I thought reflected how well you communicated to me that you understood some of the principles.


Question 2

Grading

One point per question.


Question 3

Part A: No rebalancing required:

Part B: On this one, you need to rebalance:

Part C: On this one, you need to rebalance too, but it's more subtle, as the imbalance is higher up the tree:

Grading

Two points for part A, and three for each of the other trees.


Question 4

Part A: There are imbalances at nodes I and K.

Part B: There are two legal answers. Either you recursively delete I and replace J with I, or you recursively delete K and replace J with K:

Grading

Three points for Part A. To receive full credit, you had to specify a node that was imbalanced. Also, three points for Part B.


Question 5

Part A: When you delete F, node G is imbalanced, and the rebalancing required is a Zig-Zag: Double Rotate About H. The resulting tree will be balanced.

Part B: When you delete B, you replace it with A. Node E is imbalanced, and the rebalancing required is a Zig-Zig: Rotate About G. The resulting tree will be balanced.

Part C: When you delete L, node M is imbalanced, and you must perform a double rotation about N. When that's done, node K is imbalanced, and that too is a Zig-Zag, requiring a double rotation about G. Thus, the answer is:

Grading

You lost a point per part if you drew me trees rather than specifying rotations, and you had to give me the correct tree.


Question 6

You read each team into a vector of names. Then you sort the vector and construct a string from the vector, which contains each name in sorted order separated by a space (or a colon, or any other non-lowerspace letter). That string becomes a key that you insert into a set. Since you don't insert duplicate keys into a set, whenever you try to insert the same team into the set twice, the second insertion fails. Thus, the size of the set is the number of unique teams:

#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;

main()
{
  set <string> teams;
  vector <string> team;
  string key, n;
  int i;

  while (1) {
    team.clear();
    for (i = 0; i < 7; i++) {
      if (cin >> n) {
        team.push_back(n);
      } else {
        cout << teams.size() << endl;
        exit(0);
      }
    }
    sort(team.begin(), team.end());
    key = "";
    for (i = 0; i < 7; i++) {
      key += team[i];
      key += " ";
    }
    teams.insert(key);
  }
}

Obviously, you only need one string, but two make it clearer.

Grading

This one was 15 points. As with question 1, you either started with 15 and were deducted, or your answer was off-base, and I assigned partial credit according to how well you communicated to me that you could do various parts of this.

If you gave me an n2 solution, you received half credit.