CS 2010 : Data Structures and Algorithms : Mid-Sem Exam
IIT Hyderabad
29 September, 2012
Instructions
• Please work out the any four of the five following problems. Write your solutions carefully
and submit them.
• For all the algorithm problems, please argue the correctness of the algorithm and the running
time. You may get less credit for a correct algorithm with an inferior running time.
• You may refer to any result that was covered in class in any of the problems. When using a
result from class, please make sure to state the result which is being used and explain how
it is being used. For instance, if you are using an algorithm from class as a subroutine, state
what you choose to give as inputs, and what you obtain as outputs, and what is the running
time of the algorithm being used.
• Clearly state any assumptions that you may be using.
• Try to be short and precise in your solutions. Whatever be your chosen mode of explanation
(pseudo-code or English prose or diagrams), please make sure that there is no ambiguity. Any
imprecise or vague solutions will not receive any credit.
• If any question is unclear or seems wrong, please ask me for clarifications.
• You have till 10:30 am to finish. Good luck!
Questions
1. [15 marks] Show how to implement a queue using two stacks. Analyze the running time of
the queue operations.
(a) Show that for a sequence of n queue operations, the implementation takes a worst case
running time of O(n).
(b) If there are a maximum of k elements in the queue at a given time, what is the worst
case running time to perform one queue operation?
Note that when you use the stack for implementing a queue, you are only allowed to use the
stack commands, namely makenew(S), top(S), pop(S), push(x, S) and isempty(S). Please
keep in mind to explain how you would implement all the queue commands like makenew(Q),
front(Q), enqueue(x, Q), dequeue(Q) and isempty(Q).
1
2. (a) [10 marks] Explain how you can implement a queue using a circular singly linked list.
Show how you can do enqueue and dequeue operations in O(1) time.
Note: A circular singly linked list is a singly linked list where the tail element points
back to the head of the linked list (instead of pointing to a null element). The linked
list is accessed by an external pointer pointing to one of the elements.
(b) [5 marks] Consider a hash table with m slots. Under the simple uniform hashing
assumption, what is the probability that after the first 5 insertions, the first 10 slots are
not filled?
3. [15 marks] Suppose we have a hash table with m slots where we store n items. In class, we
have seen chaining – a method to resolve collisions. In chained hash tables, each entry is not
a single item, but rather a pointer to a linked list containing items that hash to that hash
table slot.
Instead of maintaining a linked list at each hash table slot, what would happen if we use a
secondary hash table associated to each hash table slot? Assume that the secondary hash
table also has m slots. With the assumption of simple uniform hashing (in the primary hash
table and all the secondary hash tables), derive the worst case running time required for an
unsuccessful search. Use the O(·) notation and write your answers as a function of m and n.
4. Suppose we know the preorder and postorder traversal sequences of a binary tree T .
(a) [5 marks] Can we uniquely determine the binary tree? Answer with a short justification.
(b) [5 marks] Suppose we know that all the non-leaf nodes of T have two children. Explain,
using an algorithm, on how we can recover T from the two sequences. You need not
explain the running time complexity of this algorithm.
(c) [5 marks] Let the preorder traversal sequence of T be 100, 34, 16, 9, 8, 38, 11, 4, 81 and
postorder traversal sequence be 34, 9, 11, 4, 38, 81, 8, 16, 100. If all the non-leaf nodes of
T have two children, identify T .
5. (a) [10 marks] Can you give an example of a binary tree (no need of labels) which has height
20 but whose average path length is less than or equal to 7? You have to substantiate
your example with necessary calculations. (i.e., show that the height and average path
length are within the required limits.)
P
Recall that the average path length of a tree T is x∈T d(x, T )/n, where n is the total
number of nodes in T and d(x, T ) is the depth of x in T , which is the length of the
shortest path from the root of T to the node x.
Hint: Consider a tree where one child of the root heads a linked list and the other child
is the root of a complete binary tree.
(b) [5 marks] In the following binary search tree, draw what remains when you delete the
root.