You are on page 1of 4

Data Structures Exam

First Semestre 2013


Professor: Loreto Bravo Question: Points: Score: 1. Suppose that the following keys are inserted in some order into an initially empty linearprobing hash table of size 7 (assuming no resizing), using the following table of hash values: key hash key hash A 5 2 B 5 C D 1 4 E F 1 3 G 5pt (a) Give the contents of the linear-probing array if the keys are inserted in alphabetical order: A, B, C, D, E, F, G. Solution: 5pt 0 G 1 D 2 B 3 F 4 E 5 A 6 C 1 15 2 14 3 16 4 15 Total 60

(b) Which of the following could be the contents of the linear-probing array if the keys are inserted in some other order? 0 1 2 3 4 5 6 I. A F D B G E C 0 1 2 3 4 5 6 II. F A D B G E C 0 1 2 3 4 5 6 III. C A B G F E D Circle the best answer.

Certamen (a) I only. (b) I and II only. (c) I and III only.

23 de Mayo, 2013 (d) I, II and III. (e) None

Solution: Alternative (a). Indeed, Table I can be obtained by inserting the keys in the following order: FDBGECA. 5pt (c) What is the purpose of re-hashing? Solution: When a hash table has a high load factor, the eciency of search approaches O(n) (the worst case). In order to address this problem, a threshold is specied in such a way that if the load factor is higher than that value, then a bigger hash table is built and using a new hash function all the elements of the hash table are moved into the new hash table. 2. Joe and Bob are employees of the same corporation. The inventory of the products they sell is a database of over 1,000 entries where each entry has: A catalog number which is a string of 6 characters: 3 letters followed by 3 digits The number of products in stock for that catalog number: an integer The cost of the product: a real number A description of the product: a variable length string of at most 20 characters (Note: the questions to follow do not depend on the cost and the description). Joe and Bob discuss a number of data structures that could be best applied to their individual needs. The choices are: Arrays, Linked Lists, Binary Search Trees, Heaps, AVL Trees or Hash Tables. 7pt (a) Joe is a salesperson. His job is to convince the customer to buy a product. Once the customer has chosen a product by looking at the printed catalog, the only thing he needs is to nd its availability and decrease the number in stock (if available). Give his choice for a data structure (choose from one of the above) which will lead to the most ecient way to handle his task and explain his reason for the choice in one or two sentences: Solution: The main task isFind(product)so a hash table with the product number as a key is the most appropriate structure. Joe will perform his task in O(1) 7pt (b) Bob handles emergency situations. As soon as a product has its stock below a certain threshold he should reorder some from the manufacturer. Give his choice for a data structure (choose from one of the above) which will lead to the most

Page 2 of 4

Continue in the next page. . .

Certamen 23 de Mayo, 2013 ecient way to handle his task and explain his reason for the choice in one or two sentences: Solution: The main task is Findmin(availability)which is best handled with a binary heap (priority queue) with the minimum property based on stock availability. Bob can do a Findmin in O(1) and see if the product at the root of the heap needs reordering. 4pt 3. (a) Given a AVL tree with n nodes, write in pseudocode the algorithm that generates an ordered array of the initial tree in O(n). Solution: By visiting the tree using an In-order traversal we can populate an array with the elements in order. 4pt (b) Given an ordered array of size n, write in pseudocode the algorithm that builds an AVL tree in O(n). Solution: The solution is do a recursive algorithm, like this: 1. Get the middle of the array and make it root. 2. Recursively do same for left half and right half. a ) Get the middle of left half and make it left child of the root created in step 1. b ) Get the middle of right half and make it right child of the root created in step 1. 8pt (c) The method join can be described as follow: Given two AVL trees A and B, which have n and m nodes respectively, it is possible dene join(A,B) like a AVL tree with n + m nodes, that is the result combine trees A and B in only one. Explain, using words, how can you calculate the join of two AVL trees in O(n + m). Hint: The algorithm should use the algorithms in (a) and (b). Solution: The solution is the following: 1. Using the solution of the part a, obtain an array arrA from the AVL A (O(n)) and an array arrB from the AVL B (O(m)). 2. Merge both arrays in only one array arrC in O(n + m). It can be done looking the smallest element in each array, selecting the lower of them, putting it in the array arrC, move to the next element in the array that contains the recently copied element, repeat these steps iteratively. 3. Finally, using the solution of the part b, create an AVL tree using the array arrC (O(n + m)).

Page 3 of 4

Continue in the next page. . .

Certamen 23 de Mayo, 2013 4. Consider the following operations sequences and draw the corresponding tree. Assumption: Initially the tree is empty. 5pt (a) Heap (The root contains the smaller of the keys) insert(10),insert(1),insert(5),insert(45),removeMin(), insert(8),insert(80),insert(2),removeMin(),insert(40),removeMin() Solution:

5pt

(b) Heap (The root contains the bigger of the keys) insert(10),insert(1),insert(5),insert(45),removeMin(), insert(8),insert(80),insert(2),removeMin(),insert(40),removeMin() Solution:

5pt

(c) AVL insert(7),insert(100),insert(2),insert(134),insert(111), insert(200),insert(90),insert(55),remove(111) Solution: When applying remove(111) there are two possible resulting trees (depending if we choose the maximum of the left subtree or the minimum of the right subtree to replace the removed node.

Page 4 of 4

End of the exam.

You might also like