EECS 311 Data Structures Midterm Exam: Don't Panic!
EECS 311 Data Structures Midterm Exam: Don't Panic!
1. (10 pts) In each box below, show the AVL trees that result from the successive
addition of the given elements. Show the nodes, links and balance factors. Draw Comment [CKR1]: Common
mistakes:
intermediate trees and clearly indicate rotations, if any, and in what direction. • neither balance factors nor heights
• balance factors not ±
• one balance factor for entire tree
1. After adding 35 to an empty tree. 2. After adding 87 to the previous tree.
35 35 Comment [CKR2]: Common
0 0 0 1 mistakes:
• not indicating rotations clearly
• suggesting a single rotation when
87 double rotations required
0 0
3. After adding 64 to the previous tree 4. After adding 78 to the previous tree.
. 35 35 64 64
0 2 0 2 1 1 1 2
65 35 87 35 87
87 0 1 0 0
1 0 0 0 0 0 1 0
64 87 78
0 0 0 0 0 0
5. After adding 81 to the previous tree. 6. After adding 85 to the previous tree.
64 64 64 81
1 3 1 3 1 2 64
1 3 2 2
35 87 35 87 35 81
0 0 1 1
0 0 2 0 0 0 2 0 35 81 64 87
78 0 0 1 2 1 1 1 0
87
78 81 0 0 0
1 0
0 1 78 87 35 78 85
81 78 0 0 1 0 0 0 0 0 0 0
0 0 0 0
85
0 0
1
April 25, 2007 Name _____________________________
2. (10 pts) In each box below, show the red-black trees that result from the successive
additions of the given elements. Use doubled lines for red links Draw intermediate trees
and clearly indicate recolorings and rotations, if any, and in what direction. Comment [CKR3]: Common
mistakes:
• not clearly indicating rotations
1. After adding 35 to an empty tree. 2. After adding 87 to the previous tree. • suggesting single rotation when double
rotations needed
35 35
• not indicating recolorings
• recoloring too soon (no points lost)
87 • doing an AVL rotation in step 6 instead
of a red-black rotation
• building an invalid red-black tree
(unbalanced black links)
3. After adding 64 to the previous tree. 4. After adding 78 to the previous tree.
35 35 64 64
recolor
87 87 35 87 35 87
64 64 78
5. After adding 81 to the previous tree. 6. After adding 85 to the previous tree.
64 64 64 64
recolor
35 87 35 87 35 81 35 81
78 81 78 87 78 87
81 78
85
2
April 25, 2007 Name _____________________________
3. (10 pts) Draw the B-trees that result when adding the following values in succession, Comment [CKR4]: I accepted either
true B-trees, or B+trees (the book has
starting with an empty tree. Assume each node can only hold 2 keys. To save drawing B+trees mislabeled as B-trees – footnote
time, you can choose to only draw a new tree when a split occurs, but make it clear which p 161)
value caused the split. Comment [CKR5]: Common
mistakes:
• more than 2 values in leaves
Values: 35, 87, 64, 78, 81, 85, 22, 31 • nodes with # children <= # keys; should
always be #keys + 1
64 64 64 81 • keys moving downwards
35 35 87 • completely empty nodes
• splits creating full nodes full, instead of
half-full
35 87 35 78 87 35 78 87 • leaves at different depths
64 81 64 81 64 81
35 78 85 87 22 35 78 85 87 22 35 78 85 87
64
31 81
22 35 78 85 87
4. (5 pts) Give the Big-Oh complexity and a reasoned argument for the following
algorithm (in pseudo C++) for finding the position in s1 of a longest common substring
of two strings s1 and s2, of lengths M and N, respectively. string::compare() returns
0 for equality, like C’s strcmp().
for i from 0 to M
for len from 1 to M – i
for j from 0 to N - len
if s1.compare(i, len, s2, j, len) == 0 Comment [CKR6]: Most common
if len > result_len mistake: treating compare() as O(1). It
result = i clearly depends on len, which is O(M), or
O(min(M,N)) assuming it stops with the
result_len = len shorter string.
There are three nested loops with bounds O(M), O(M) and
and O(N)
respectively. The comparison will be up to O(M) characters,
characters, or more
accurately, O(min(M, N)).
N)). The assignments
assignments inside the IF are O(1).
Therefore the worst case running time is O(M3N).
3
April 25, 2007 Name _____________________________
5. (10 pts total) a) Assume a 10-element hashtable, with hash(x) = x mod 10 and linear Comment [CKR7]: FYI: this is
Exercise 1 in Chapter 5.
probing. Show what locations would be probed, in order, for each value in the table, and
put the value in its final resting place, if any, in the array:
Array:
0 1 2 3 4 5 6 7 8 9
b) Repeat, with the same hash(), but using double hashing with hash2(x) = 7 – (x mod 7). Comment [CKR8]: Most common
mistakes:
• Not using hash(x) + i * hash2(x)
4371 mod 7 = 3 1323 mod 7 = 0 6173 mod 7 = 6 4199 mod 7 = 6 • Using x mod 7, not 7 – (x mod 7)
4344 mod 7 = 4 9679 mod 7 = 5 1989 mod 7 = 1 • Linear probing (x, x + 1, x + 2, …)
instead of x, x + hash2(x), x + 2 hash2(x),
…
I only counted each mistake once, if other
wrong answers were at least consistent.
Value Locations probed
4371 1
1323 3
6173 3, 4 (because 7 – 6173mod7 = 1)
4199 9
4344 4, 7 (because 7 – 4344mod7 = 3)
9679 9, 1, 3, 5 (because 7 –9679mod7 = 2)
1989 9, 5, 1, 7, 3, 9 (because 7 – 6173mod7 = 1) – no space found
Array:
0 1 2 3 4 5 6 7 8 9
4
April 25, 2007 Name _____________________________
6. (10 pts) Using the (space-wasting) C++ tree and node classes below, implement
rotateRight() so that node.rotateRight() rotates node clockwise (rightward)
through its parent. Each node has a pointer to its parent and a flag indicating if it’s a right
child of the parent. Drawing a picture first is not required but strongly recommended. Be
sure to update all affected fields of all affected nodes.
1 6
template <typename T> class Tree {
private: A
B
struct Node {
Node *parent, *left, *right; 2
5
bool isRight; B 3
T data; 1 A
void rotateRight(); 4
…}; 3
Node * root; 1 2
2 3
…};