
CPSC 212-1 Name: _____________________________
Test #2 July 21, 2004
Closed books. Closed Notes. Calculators OK. 105 points. 75 minutes. Weight of each
question in parentheses. Please use a pencil. If you need more space, use the back of the
sheet.
1. (10) Insert the following values into an initially empty AVL tree. Indicate the type of
rotations performed (“S” for single, “D” for double). The end should be the final tree:
5 8 9 10 21 16 7 4 23 19 17 25
2. (10) Your friend says that he can use an array, a[i], to represent a tree using the
following rules. (a) The root is stored in a[0]. (b) The left and right children of the node
in a[i] are in a[2i+1] and a[2i+2], respectively. (c) The parent of the node in a[i] is
stored in a[(i-1)/2]. Note that his scheme uses array element a[0] and is different
from the 1-array representation we learned in class.
Does his scheme work? If so, fill in the values in the array to represent the tree on the
below. If not, explain why it doesn’t work.
i: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
a[i]: __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __
Tree: root: I
children of I: C J children of F: E G
children of C: B F children of L: K M
children of J: _ L children of E: D _
children of B: A _ children of G: _ H
3. (10) Insert the following values into a B-Tree with L=M=3 (i.e., max number of keys
in a leaf is 3, maximum num of keys in an interior node is 2). Then answer the questions
following.
17 15 33 24 61 36 48 4 12 9 18 27 35 43 50 64 38
_____ (a) How many leaves does the tree have?
_____ (b) What values are contained in the second leaf from the left.
_____ (c) What values are contained in the root?
_____ (d) How many children does the root have?
_____ (e) What values are contained in the rightmost child of the root?
4. (10) Insert the following values into a 17-element hash table using hash function:
h1(x) = x%17. Use quadratic probing to handle collisions.
5 0 29 34 51 67 17 1 13 22 52 35
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___