Quiz 3
Quiz 3
Quiz 3
1. (25%) Let S = {75, 123, 65, 9, 23, 67}. Consider a hash function h(k) = 1+((2k+17) mod m),
where m = 5.
(a) (20%) Compute the hash values of all the elements in S. Show the resulting hash table
after inserting all the numbers in S into the hash table.
(b) (5%) Explain how to use the hash table to answer a dictionary search query with value
34.
2. (25%) Assume an initially empty binary heap. Show the binary heap after inserting the
following integers (one at a time in the following order): 10, 9, 6, 5, 7, 4. You should use the
regular insertion algorithm discussed in class. To get full credit, you should show each step of
inserting a number into the binary heap and all the steps in reorganizing the heap if necessary.
3. (25%) Let T be a balanced binary search tree storing a set of n distinct integers.
(a) (18%) Give an O(n)-time recursive algorithm in pseudo code (with proper comments)
to report all the integers stored in the tree in ascending order.
Hint: The binary search tree has the property that the left subtree has keys smaller
than the root’s key, and that the right subtree has keys larger than the root’s key.
(b) (2%) Explain briefly how your algorithm works.
(c) (5%) Explain why your algorithm has O(n) time complexity.
4. Let S be a large set of integers and k be a fixed integer. New integers are read from S one
by one. You are asked to design an algorithm to maintain a set of the k largest integers in
the current elements of S read and to update the set as new elements in S are read. Your
algorithm must use O(k) space at all times, no matter how large S is (note that S increases
continuously, but your space cannot). Furthermore, it must process every integer insertion
in O(log k) time. [Hint: the solution was explained in one of the tutorials.]
For example, suppose k = 3, and the sequence of integers S is {83, 21, 66, 5, 24, 76, 92, 32, 43, ...}.
After reading 83, 21, 66, 5, 24 from S, your algorithm should keep the set {83, 66, 24}.
After further reading 76 from S, your algorithm should keep the set {83, 66, 76}.
After further reading 92, 32, 43 from S, your algorithm should keep the set {83, 76, 92}.
(a) (5%) Propose a data structure for maintaining the k largest integers. Explain your
answer to get credit.
(b) (6%) Specify clearly the primitive operations used (input arguments and outputs, if
any) for the data structure defined in (a). You can assume these primitive operations
without providing the pseudo code.
(c) (14%) Develop the pseudo code (with comments) for processing S and maintaining the k
largest integers as S is read, using the data structure in (a) and the primitive operations
specified in (b).
1
Answers
1. (a) The following are the hash values computed for S:
h(75) = 3, h(123) = 4, h(65) = 3, h(9) = 1, h(23) = 4, h(67) = 2.
The hash table has 5 entries as follows:
L1 → 9 → nil
L2 → 67 → nil
L3 → 75 → 65 → nil
L4 → 123 → 23 → nil
L5 → nil
(b) For the query with key 34, h(34) = 1. Hashing into the first location of the hash table
and scanning the linked list results in no matches. Hence, the query returns nil.
2. Each step corresponds to getting a new integer and reorganizing the tree. The following
solution corresponds to a minimum heap.
3. (a) Let each node in T has three fields p.left (left pointer), p.value (key value of node), and
p.right (right pointer).
We have a recursive algorithm to output the keys as follows.
L1 ascending(T )
2
L2 p ← T ; /* p points to the root of the tree */
L3 if p =nil then return; end /* p is nil; nothing to output */
L4 if p.left = nil and p.right = nil then /* at a leaf; output p.value */
L5 output p.value; return;
L6 else
L7 ascending(p.left) /* output all the keys of the left subtree */
L8 output p.value /* followed by the key of the root */
L9 ascending(p.right) /* followed by the keys of the right subtree */
L10 end
L11 end
(b) Explanation: The approach is to note that the root node has a key greater than those
in the left subtree and smaller than those in right subtree. Hence, we would output
the keys in the left subtree, followed by the key of the root, and the keys in the right
subtree.
(c) It has an O(n) complexity because each leaf node is traversed once (lines L2-L5) and
each non-leaf node is also traversed once (lines L7-L9), and there are n nodes in the
tree.
4. (a) We need a binary min-heap (BH) to keep the k largest integers. It allows the minimum
to be found in O(1) time, the insertion of a new element and the deletion of the minimum
can each be done in O(log n) time, and O(k) space. Each time you find a larger maximum
value in the stream than the current minimum in the min-heap, you replace the minimum
in the heap by the larger maximum found.
(b) We assume three primitive operations of BH to be available:
i. accessing and returning the minimum element b at the root (b = readMin(BH));
ii. inserting a new element b into BH (insertNew(BH,b));
iii. deleting the minimum element from BH (deleteMin(BH)).
(c) Pseudo code:
L1 Create an initially empty binary heap BH.
L2 repeat
L3 read a new integer e from S. /* continue to read from S *
/* if e read is larger than the min element (root) of BH,
then remove the minimum element from BH and add e to BH */
L4 if e > readMin(BH) then deleteMin(BH); insertNew(BH,e); end
L5 until S has no further data