Test 1 Key

advertisement
CPSC 2120-001
Test #1
Name: Answer Key
May 25, 2015
Closed notes, closed laptop, calculators OK. Please use a pencil. Weight of each question
in parentheses. If you need more space, use the back of the sheet.
1. (10) What is the order of complexity of each of the following.
O(log N) Searching for a value in a sorted array using binary search.
O(N)
Printing the diagonal of an (N x N x N) array.
O(2N)
Printing all values that can be represented in an N-bit register
O(N!)
Enumerating all permutations of N distinct values.
O(1)
Dequeue-ing the first element of a queue implemented as a singly-linked list
2. Consider a circular-array implementation of a queue shown on the
right. The values front=rear=0 represent the initial empty queue
state. (5) Write a pseudo code method called enqueue (Object
item) that adds an element to the queue. (5) Write a pseudo code
method called Object dequeue() that removes and returns an
element from the queue. Your methods should recognize both fullqueue and empty-queue situations. The methods have been started
for you.
void enqueue(Object item) {
|
|
rear = (rear+1)%Q.length);
|
if (front==rear)
|
throw QueueFullException; |
Queue[rear] = item;
|
|
} // enqueue
|
Object dequeue() {
if (front==rear)
throw QueueEmptyException;
front = (front+1)%Q.length;
return Q[front];
} // dequeue
3. (10) Series. What is the sum of each of the following?
(a) 1 + 3 + 9 + 27 + … + 325 = (326 − 1)/(3 − 1)
(an+1 – 1)/(a–1)
(b) 1 + 4 + 9 + 16 + … + 352 = (35)(36)(71)/6
n(n+1)(2n+1)/6
(c) 25 + 30 + 35 + 40 + … 2555 = 5(5 + 6 + 7 + … + 511) = 5((511)(512)/2 – 4(5)/2)
4. Your friend has introduced you to what she calls an expression tree (below left).
(a) (5) Give a postorder traversal of the tree.
7122++*6420–+/+
(b) (5) Assume that during a postorder traversal
of the tree, leaves return their values to their
parents when leaves are “visited”. Assume
further that an interior node operation is
performed on values returned by its children when the interior node is “visited” and returns
the result to its parent. What value does the root return? 36 Show how you arrived at
your answer.
(7 * (1 + (2 + 2))) + (7 / (4 + (2 – 0))) = 36
5. (a) (5) What is the output of function “mystery” when called thus: mystery(19); ?
Note that the function returns a string (i.e., “+” is concatenation, not addition).
void string mystery(int n) {
if (n < 2)
return ("" + n);
else
return mystery(n/2) + (n%2);
} // mystery
returns “10011”. In general, function returns the binary
representation of the passed value.
(b) (5) Consider the binary tree on the right.
2 a) What is the degree of the tree?
5 b) How many interior nodes does the tree have?
2 c) What is the height of node C?
1 d) What is the depth of node B?
e) The tree is: (circle one) complete, perfect,
degenerate, full, leafy, none of the above.
Answer: none of the above
6. (5) Consider the tree in Question #5. Then enter values into the array below so that the
result is a one-array representation of the tree whose root is at element 1 of the array.
val:
i:
___
0
A
1
B
2
C
3
D
4
E
5
F
6
G
7
___
8
H
9
___ ___ ___ ___
10 11 12 13
I
14
J
15
(5) Consider the tree in Question #5. Then enter values into the arrays below so that the
result is a three-array representation of the tree.
val:
___
B
___
G
A
___
lchild: ___ ___ ___
8
___ 12
3
rchild: ___ ___ ___
1
___
9
3
4
5
i:
F
0
E
1
2
D
J
C
___
___ ___ ___
0
___ ___ ___ ___ ___
10
___
15 ___
5
___ ___ ___ ___ ___
6
7
8
10
9
11
I
12
___ ___
13
14
H
15
7. (a) (5) Write a recursive method int sumOneChildNodes(Node* t) that returns the
sum of the values contained in the one-child nodes of a of a binary tree rooted at t.
int sumOneChildNodes(Node* t) {
if (t==NULL)
return 0;
// base case
int value=0;
if ((t->getLeft()==NULL && t->getRight()!=NULL) ||
(t->getLeft()!=NULL && t->getRight()==NULL))
value = t->getVal();
return value +
sumOneChildNodes(t->getLeft() +
sumOneChildNodes(t->getRight());
} // sumOneChildNodes
(b) (5) If root is the root of a binary tree and the main method executes the following
statement, what is printed out? Be precise.
cout << mystery(root);
Method mystery()is defined below:
int mystery (Node* t) {
if (t == null)
return 0;
return mystery(t.left()) + mystery(t.right()) + 1;
} // mystery
Answer: the function returns the number of nodes in the tree.
8. (10) An algorithm takes 0.5 ms for input size 100. How large a problem can be solved
in 1 minute (assuming that low-order terms are negligible) if the running time T(N) is:
a) T(N) = N
Let X = input size that can be solved in 1 minute
given that the algorithm can solve a problem of size
100 in 0.5 ms.
X = 100 (60 seconds/0.5 ms) = 6000 / (0.5 x 10 -3)
= 6000 /(5 x 10-4) = 1200 x 104 = 12,000,000
b) T(N) = N log N
Ambiguously worded. All answers accepted.
9. (5) Consider the following k-ary tree. Convert it to a binary tree. Show the final tree.
See next page
(5) Consider the following binary tree. Convert it to one or more k-ary trees. Show the
final tree. (If you need more space, put your answer on the back of this sheet.)
See next page
10. (10) Consider the binary tree on the left. Traverse the trees in preorder, postorder, and
inorder. In what order are the nodes visited? Put your answers in the space provided
on the right.
Preorder: 7 3 1 0 2 6 4 5 12 9 8 11 10 13 15 14
Inorder:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Postorder: 0 2 1 5 4 6 3 8 10 11 9 14 15 13 12 7
Bonus: (5) Define the term “internal path length” or IPL of a tree. What is the IPL of the
tree in Question #10?
Answer: The IPL is the sum of the path lengths from the root to each of the nodes of a tree.
Another definition: the IPL is the sum of the depths of each of the nodes of a tree.
The IPL of the tree in Question #10 is 2(1) + 4(2) + 6(3) + 3(4) = 40
Download