Download - Backpack

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Data Structures and Algorithms (CSE-102)
Endsem Exam
Time: 90 min.
Marks: 35
Date: April 28, 2014
1. Consider a hash table with 9 slots. The hash function is h(k) = k mod 9. The collisions are resolved by chaining.
The following 9 keys are inserted in the order: 5, 28, 19, 15, 20, 33, 12, 17, 10. Find out the maximum, minimum,
and average chain lengths in the hash table.
2
2. A hash table of length 10 uses open addressing with hash function h(k)=k mod 10, and linear probing. After
inserting 6 values into an empty hash table, the table is as shown below.
a) Which one of the following choices gives a possible order in which the key values could have been inserted in the
table? Explain your answer.
2
(A) 46, 42, 34, 52, 23, 33
(B) 34, 42, 23, 52, 33, 46
(C) 46, 34, 42, 23, 52, 33
(D) 42, 46, 33, 23, 34, 52
b) How many different insertion sequences of the key values using the same hash function and linear probing will
result in the hash table shown above? Explain your answer.
3
3. Write two simple (but efficient) functions for performing Union and Find operations for disjoint set data structure.
What are the complexities of your functions.
3
4. A program P reads in 50000 integers in the range [0..100] representing the scores of 50000 students. It then prints
the frequency of each score above 50. Write an algorithm to do this efficiently. What are the time and memory
requirement for your algorithm (marks will only be awarded if you write an efficient algorithm!!)
3+1+1
5. (a) You are given a set of n distinct elements and an unlabeled binary tree with n nodes. Explain in how many
ways can you populate the tree with the given set so that it becomes a binary search tree?
2
(b) The preorder traversal sequence of a binary search tree is 25, 15, 5, 10, 20, 18, 34, 30, 37. Find the postorder
traversal sequence of the same tree, if it is unique. If not, explain why it is not unique.
3
(c) For each node in a binary search tree, write a function to create a new duplicate node, and insert the duplicate as
the left child of the original node. The resulting tree should still be a binary search tree.
5
So the tree...
2
/\
1 3
Is changed to...
2
/\
2 3
/ /
1 3
/
1
6. (a) Start with an empty Red-Black tree. Insert the keys 13, 20, 31, 23, 28 (in that order) and draw the tree after
each insertion.
3
(b) Start with an empty AVL tree. Insert the keys 5,2,4,3,0,1,8,6,7,10,9 (in that order) and draw the tree after each
insertion.
4
(b) What is the maximum height of a Red-Black Tree with 14 nodes? Explain. Draw an example of a tree with 14
nodes that achieves this maximum height.
3
7. (Bonus Question): (a) Consider the following code snippet in C. The function print() receives root of a Binary
Search Tree (BST) and a positive integer k as arguments.
3
// A BST node
struct node {
int data;
struct node *left, *right;
};
int count = 0;
void print(struct node *root, int k)
{
if (root != NULL && count <= k)
{
print(root->right, k);
count++;
if (count == k)
printf("%d ", root->data);
print(root->left, k);
}
}
What is the output of print(root, 3) where root represent root of the following BST.
15
/
\
10
20
/\
/\
8 12 16 25
(b) Consider the same code as given in above question. What does the function print() do in general? The function
print() receives root of a Binary Search Tree (BST) and a positive integer k as arguments.
2
Related documents