Exam2 - Seattle University

advertisement
Mid-Term Exam #2
CPSC 250 Data Structures
NAME:___________________________
Score: ______________________
Note: 42 points in total.
Part I: Multiple choices (12 points with 3 points each)
(1) Suppose T is a binary tree with 14 nodes. What is the minimum possible height of T (height is defined
as the number of levels on the tree)?
A. 0
B. 3
C. 4
D. 5
(2) Given the BST below. Suppose we remove node 11, what node can be used to replace it?
8
/
\
A. 6
B. 10
C. 40
D. 14
4
11
/ \ / \
2 6 10 40
/ /
9 30
(3) Consider the node of a complete binary tree whose value is stored in the array data[i] for array
representation. If this node has a right child, where will the right child's value be stored?
A. data[i+1]
B. data[i+2]
C. data[2*i + 1]
D. data[2*i + 2]
(4) Select the one FALSE statement about binary search trees (BST)
A. Search performance is O(logn) on a BST.
B. Inorder traversal of a BST produces a sorted list.
C. The item with the minimum key must be the leftmost element on the tree.
D. Every non-root node has exactly one parent.
Part II: AVL trees (5 points)
Below is an AVL tree. If we insert the key 25, is it still an AVL tree? If not, what rotation(s) should we
perform to rebalance it? Show the rotation(s) if necessary and the AVL tree after insertion. (6 points)
Seattle University, Fall 2010
Page 1
60
/ \
20 70
/ \
\
10 40 80
/ \
30 50
Part III: Short answers (6 points)
Given a binary tree below, answer the following two questions:
(1) Write the values in the order of inorder traversal.
(2) Write the values in the order of postorder traversal.
60
/ \
100 20
/ \
\
10 70 80
/ \
50 40
Part IV: Hashing (9 points)
Suppose that the following character codes are used: ‘A’=1, ‘B’=2, …, ‘Y’ = 25, ‘Z’=26 (as shown in the
table below). Using a hash table with 11 locations and the hash function h(identifier) = average % 11,
where average is the average of the codes of the first and last letters in an identifier:
average = (first-letter-code + last-letter-code) / 2
//integer division
Show the hash table that results when the following identifiers are inserted in the order given: BETA,
RATE, FREQ, ALPHA, MEAN, SUM, NUM, BAR, WAGE, PAY, KAPPA.
A
1
N
14
B
2
O
15
C
3
P
16
D
4
Q
17
Seattle University, Fall 2010
E
5
R
18
F
6
S
19
G
7
T
20
H
8
U
21
I
9
V
22
G
10
W
23
K
11
X
24
L
12
Y
25
M
13
Z
26
Page 2
(1) Assume that collisions are resolved using quadratic probing. (5 pts)
0
1
2
3
4
5
6
7
8
9
10
(2) List all the collision resolution techniques that we have discussed in class. (4 pts)
Part V: Programming (10 pts)
Given the following BST class which stores unique elements, implement a member function level(x)
which determines the level of a given item x on the tree. The root of the BST is at level 0, and its children
are at level 1, and so on. Return -1 if the item is not on the tree. This function must be implemented as a
non-recursive function.
typedef int DataType;
class BST {
public:
…
int level(DataType x); //Need to be implemented
private:
class BinNode { //BST node class
public:
Seattle University, Fall 2010
Page 3
DataType data;
BinNode* left;
BinNode* right;
BinNode() : left(0), right(0) { }
BinNode(DataType item) : data(item), left(0), right(0) { }
};
BinNode* root;
};
Seattle University, Fall 2010
Page 4
Seattle University, Fall 2010
Page 5
Seattle University, Fall 2010
Page 6
Download