Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

115 marks CS 213(M) Quiz 2 11:00-13:00, 27/2/19

Problem 1:(a)[10 marks] Suppose the numbers 1, 2, 3, 4, 5, 6, 7 are inserted into an initially
empty binary search tree in some order. (i) Which numbers if inserted in step 1, will imply that
the final result tree will be not balanced? (ii) Which numbers if inserted in step 3 will imply that
the final result tree will not be balanced? (iii) Which numbers if inserted in step 7 will imply that
the the final result will not be balanced?
(b)[10 marks] In the code below which pointers are dangling pointers? Which pointers are such
that the memory they point to will leak? Be precise: if v is of type A*, then state clearly whether
the memory for *v leaks or the memory for *(v->x). Very little explanation, if any, is expected.

struct A{
int *x;
A(){ x = new int[10]; }
};

A* f(A* &u){
A *p = new A, *q = new A, r;
u = p;
return &r;
}

int main(){ A *s, *t; t = f(s); delete t; delete s;}

Solution:
(a)(i) All except 4. (ii) 4 (iii) 4, 2, 6.
grading scheme:
(a)(i) 3 marks for the correct answer. 1 mark, if any of the numbers match to the correct answer. (ii)
4 marks for the correct answer. 1 mark, if any of the numbers match to the correct answer. (iii) 3
marks for the correct answer. 1 mark, if any of the numbers match to the correct answer.

(b) The memory s− > x points to leaks. t is a dangling pointer. Memory that r− > x points to
is a leak. The memory that q points to and q− > x points to leaks
Grading scheme:
2 marks for mentioning t as a dangling pointer. 2 marks each for mentioning memory pointing to
s− > x, r− > x, q and q− > x as memory leak.
Problem 2: A vector (from math) is sparse if only a small number of its elements are non-zero.
In such a case it is useful to keep track of indices which correspond to the non-zero elements, and
the values of those non-zero elements. In particular, the zero elements are not explicitly stored.
It is natural to represent a sparse math vector using a map as follows.

map<int,double> v; // all elements considered 0.


v[10] = 3.5; // v[10] becomes 3.5, others considered 0.

(a)[5 marks] Write a function void print(map<int,double> v, int n) which will print ele-
ments 0 through n-1 on the screen. Note that a 0 should be printed for elements which were not
explicitly assigned.
(b)[15 marks] Write a function double dot(map<int, double> v, map<int,double> P w) which
returns the dot product of the math vectors v, w. In other words, it should return i v[i]w[i] where
i ranges over all valid indices. Minimize the work done by your function.
(c)[5 marks] A matrix is likewise considered sparse if it contains mostly 0s. Suppose we wish
to represent sparse matrices in a manner similar to above. Further, to assign 3.14 to i,jth entry
of matrix A we would like to write A[i][j] = 3.14. What would the declaration of A be?

2
//(a)
void print(map<int,double> v, int n){
for(int i=0; i<n; i++){
if(v.count(i)>0) cout << v[i] << endl;
else cout << 0 << endl;
}
}
//(b)
double dot(map<int, double> v, map<int,double> w){
auto vi = v.begin(), wi = w.begin();
double res = 0;
while(vi != v.end() && wi != w.end()){
if(vi->first < wi->first) vi++;
else if(vi->first == wi->first){
res += vi->second * wi->second;
vi++; wi++;
}
else wi++;
}
return res;
}
// Time = O(sum of number of non zero entries in v and w)
// Alternate algorithm: scan over non-zeroes in v, if w has non-zeroes at same
// index, multiply and accumulate. Time = O((nonzeroes in v) * log (nonzeroes in w))

//(c)
map<int,map<int,double> > A;

grading scheme :
(a) Code that checks for vi[i] == 0 or vi[i] == NULL are awarded only 3 marks. Incorrect solutions
are awarded 0 marks.
(b) Code that is modifying the map while checking for the presence of a key is awarded only 5 marks.
(c) Any answer that doesn’t allow us address the element like A[i][j] = 3.14 are awarded 0 marks.
Answers using vectors are also awarded 0 marks because they are not sparse.

3
Problem 3: Consider a vector class which has indexing, the usual push back operation and a
new pop back operation, which causes the last element to be removed from the vector. Here is
a possible implementation. At each point during the execution we will have allocated an array
of some M elements. Of these, some N ≤ M will be in use, to store the current vector. If we
perform a push back, and the required length N + 1 becomes bigger than M , we allocate a fresh
array of size 2N . We copy the vector elements into the newly allocated array, and delete the old
array. If we perform a pop back operation, and if the new vector length N − 1 becomes smaller
than M/3, then we allocate an array of size N − 1, copy the vector into the new array, and delete
the old array. And of course we update N, M appropriately.
Suppose some n operations are performed including creation, push back, pop back and index-
ing.
(a) [10 marks] Suppose the rth operation among these is a pop back and causes memory
allocation and copying of Q elements. Show that for some constant c, the cQ operations preceding
the rth operation do not involve memory allocation.
(b) [10 marks] Show something similar in case the rth operation is a push back.
(c) [5 marks] Using the preceding two parts, show that the total work for the n operations
must be O(n).
Suppose the rth operation was pop back, and the memory allocating operation preceding it is a
pop back. Then we must have 2Q pop back operations between. Suppose the preceding operation
was push back. Then at that time if our vector had size R, then the allocated area was 2R. Then this
got to Q=2R/3. So there would be at least R/3=Q/2 popback operations.
Suppose rth is pushback and preceding is also pushback. Then Q pushbacks must have happened.
Suppose rth is pushback and preceding is popback. We copied Q for the pushback. So we must
have copied Q for the popback also. So before the popback we must have at least Q/2 popback
operations which do not involve memory allocation.
So for each 2 allocations involving O(Q) copying, we have at least Q/2 unique non allocating oper-
ations. So the copying cost when distributed over non-copying operations is still O(1) per operation.

Grading scheme:
(a) 5 - 5 marks for taking both cases of push back and pop back (if description is not satisfactory
then 3 marks will be given)
(b) 5 - 5 marks for taking both cases of push back and pop back (if description is not satisfactory
then 3 marks will be given)
(c) 5 marks for perfect description and 3 marks for less satisfactory description.

4
Problem 4:[20 marks] Suppose we have a binary search tree, in which each node has members
value, left, right as usual, but in addition there is a member size which gives the number of
nodes in the subtree of the node (including the node itself). Write functions to (a) insert a value
into the tree, and (b) to determine the number of values in the tree smaller than a given x.
1 void insert ( struct node * & Node , int value ) // -2 if you dont pass by referenc
2 {
3 if ( Node == NULL )
4 {
5 Node = new node ;
6 Node -> left = NULL ;
7 Node -> right = NULL ;
8 Node -> value = value ; // 1 mark for above
9 Node -> size = 1; // 2 marks
10 }
11 else if ( value < ( Node -> value ))
12 {
13 Node -> size ++; // 2 marks
14 insert ( Node -> left , value ); // 1 mark
15 }
16 else if ( value > ( Node -> value ))
17 {
18 Node - > size ++; // 2 marks
19 insert ( Node -> right , value ); // 1 mark
20 }
21 else // if value == Node -> value
22 {
23 // 1 mark ( for doing nothing )
24 }
25 }

5
1 int smaller ( struct node * node , int value )
2 {
3 if ( node == NULL )
4 return 0; // 1 mark
5 else if (( node -> value ) < value )
6 {
7 if (( node -> left ) != NULL ) // 2 marks
8 return (( node -> left -> size ) + 1) + smaller ( node -> right , valu
9 else // 2 marks
10 return 1 + smaller ( node -> right , value );
11 }
12 else if ( node -> value == value )
13 {
14 if ( node -> left != NULL )
15 return node -> left -> size ; // 2 marks
16 else
17 return 0; // 1 marks
18 }
19 else
20 return smaller ( node -> left , value ); // 2 mark
21 }

6
Problem 5:(a)[10 marks] What is the minimum number of keys that a 2-3 tree must have so that
if a key is inserted its height increases to 3? Note that a tree of height 3 has 4 levels of vertices.
Draw the picture of such a tree. Your tree must have distinct positive integers and you should
minimize the largest integer in the tree. State what you insert and also draw the result.
(b)[5 marks] Suppose I have a 2-3 tree of height h. What is the minimum number of nodes it
can have?
(c)[10 marks] What is the minimum number of keys that can be inserted into a tree of part (b)
so that its height increases to h + 1? Answer for h = 0, 1, 2, 3 and see if you can infer a formula.
You dont have to prove the formula, enough if it matches h = 0, 1, 2, 3.
(a) There should be minimum 14 keys such that on insertion of new key with value 15, the height
of 2-3 tree will increase to 3. Image of tree before insertion and after inserting value 15 is shown.
(b) Minimum number of keys in a tree of height h is 2h+1 − 1.
(c) Minimum number of keys in a tree of height h is 2h+1 − 1 and that of height h + 1 is 2h+2 − 1.
Hence the minimum number of keys need to be added is 2h+2 − 1 − (2h+1 − 1) = 2h+1 . Hence minimum
2h+1 keys need to be added. Alternative method is also to guess the formula and substitute with value
of h = 0, 1, 2, 3 to see if it is correct.
grading scheme :
(a) 4 marks for drawing the 2-3 tree of height 2 before inserting 15 then 4 marks for drawing tree after
inserting 15 and 2 marks for stating that minimum element that need to be added is 15. (b) 5 marks
for correctly writing that minimum number of nodes in 2-3 tree of height h is 2h+1 − 1. (c) 5 marks
for writing the formula that 2h+1 and 5 marks for explanation. The explanation could be either of the
two methods proposed in the solution.

7
8

You might also like