You are on page 1of 3

SRN

PES University, Bengaluru

15CS202

(Established under Karnataka Act No. 16 of 2013)

DECEMBER 2016: PRACTICE END SEMESTER ASSESSMENT


15CS202: Data Structures
Time: 3 Hrs (180 minutes)

Answer All Questions

Max Marks: 50

Instructions: This examination is closed-book, but you are allowed one double-sided cheat sheet of
handwritten notes. No electronic devices (calculators, mobile phones, etc.) are permitted in the testing
area. This examination has FOUR questions and TWO pages.
Question 1 (10 points) We want to implement the following function that extracts a given node from
the doubly-linked list that contains it, and returns the integer data in this node:
int extract(DLL_node *node);
Write the implementation for the above function. For full credit, your code should run in O(1) time.
Assume that node != NULL. If you need to assume anything further, please state it clearly.
Solution:
int extract(DLL_node *node) {
DLL_node *p = node->prev;
DLL_node *n = node->next;
int ans = node->data;
free(node);
if(p != NULL) { p->next = n; }
if(n != NULL) { n->prev = p; }
return ans;
}

1/3

SRN

Question 2 (10 points) Consider the following modified definition of binary tree nodes, with an extra
field (numNodes) that indicates the number of nodes present in the sub-tree rooted at this node:
typedef struct node {
int data;
struct node *left;
struct node *right;
int numNodes;
} tNode;
Write the code for the following makeNode function that creates a new tNode and initializes all its
fields correctly: tNode *makeNode(int D, tNode *L, tNode *R);
For full credit, your function must run in O(1) time.
Solution:
#define size(x) ((x) == NULL ? 0 : (x)->numNodes)
tNode *makeNode(int D, tNode *L, tNode *R) {
tNode *node = (tNode *) malloc(sizeof(tNode));
if(node == NULL) { return NULL; }
node->data = D;
node->left = L;
node->right = R;
node->numNodes = 1 + size(L) + size(R);
return node;
}
Question 3 (15 points) An algorithm initially builds a data structure D containing n integers. Next,
the algorithm processes m pairs of integers. For each pair (p, q):
if there is some element x in D such that x + p = q, the algorithm removes x from D
otherwise, the algorithm adds p to D
Describe the best possible data-structure D for this algorithm. For full credit, the complete algorithm
should run in O( (m+n) log(m+n) ) time.
Solution: Note that testing whether some x in D satisfies x + p = q is equivalent to testing whether D
contains q p. Hence, we can implement D as a balanced binary search tree (e.g., an AVL tree). The
initial time to build D is O(n log n). Note that the maximum size of D is m + n. Hence, each of the m
subsequent steps of the algorithm (finding/inserting/deleting) take O(log (m + n)). Thus, the total
running time is O(n log n + m log(m + n)), which is O( (m+n) log(m+n) ).
Note: We can also use a hashtable to implement D as a set of integers. With a good hash function, all
finds, inserts and deletes will take O(1) time, and the total running time will be O(m + n).

2/3

SRN

Question 4 (15 points) Consider the following hashCode function for lists.
int hashCode(List list) {
int result = 1;
for(ListEntry e = start(list); e != NULL; e = next(list, e)) {
result = 31 * getData(e) + result;
}
return result;
}
(a) (7 points) Explain why this hashCode function can lead to excessive collisions.
(b) (8 points) Improve this hashCode function in a way that reduces the number of collisions.
Solution:
(a) If the list contains data elements a1, a2, , an then the above hashCode function computes its
hash value as: 31a1 + 31a2 + + 31an + 1. Note that any permutation of this list will result in the
same hash value. This will cause excessive collisions.
(b) Here is a better hashCode function:
int hashCode(List list) {
int result = 1;
for(ListEntry e = start(list); e != NULL; e = next(list, e)) {
result = 31 * result + getData(e);
}
return result;
}

3/3

You might also like