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
- Statement A: You can delete any node in a binary search tree: False
- Statement B: Destructors are called on local variables when their procedures
return: False
- Statement C: The only time you copy megabytes with an assignment statement is
when the two variables are objects, not pointers to objects. Pointers just copy pointers: True
- Statement D: See the lecture notes -- statement H is the correct one: False
- Statement E: Exactly: True
- Statement F: See the lecture notes on big-O: True
- Statement G: That's the whole point of B-Trees: Nodes are stored in disk blocks,
and disk reads are expensive. Searching for keys is fast: True
- Statement H: See Statement D: True
- Statement I: One child has to have height h-1. The other may have height
h-1 or h-2: False
- Statement J: Nope, it's an inorder traversal: False
- Statement K: All leaf nodes are the same distance from the root, so this one is: True
- Statement L: Gotta think about this -- log2(f(n)) is less than f(n). Therefore, (f(n) + log2(f(n))) is less than 2f(n). So,
by Statement F, this is: True
- Statement M: Definition of B-Trees: False
- Statement N: They are both O(log2(n)): False
- Statement O: If it's not balanced, this is: True
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:
- Rotate about N.
- Rotate about N.
- Rotate about G.
- Rotate about G.
Grading
- 2 points for Part A.
- 2 points for Part B.
- 4 points for Part C.
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.