## Second set of practice questions--CS251--Winter 1999

This set contains practice questions on augmented data structures and on priority queues.

 Question 1 Searchable stack. The ADT searchable stack is supposed to handle the following operations on on sets of data with two keys (for example, student name, and student ID number). The operations are PUSH, POP, TOP, MAKENULL, SEARCH-BY-KEY-1, and SEARCH-BY-KEY-2. Using the principle of augmentation of a data structure, suggest a data structure for this situation, such that PUSH, POP and both SEARCH operations take time O(log n) (n is the number of elements in the data structure at the time of the call), and the other operations take time O(1). Question 2 Maxstack. The ADT maxstack is like an ordinary stack, with an extra operation, MAX, which returns the maximum of the key values of the elements in the stack. Show an implementation that allows all operations to be performed in O(1) worst-case time. Question 3 The ADT median-priority-queue (or MPQ for short). The ADT median-priority-queue operates on a set of elements with different key values, and involves the following operations: makenull, insert, delete-median, makeMPQ. It is known that the number of elements never exceeds a given number n. The operation makeMPQ takes a given number of elements and inserts these globally (not one by one; recall buildheap for a similar situation). For delete-median, note that the median of a set of 2n elements is the n-th smallest, and that the median of a set of 2n+1 elements is the n+1-st smallest element. Suggest, adapt or invent a data structure so that the following can be achieved: Insert and delete-median take O(log n) worst-case time. MakeMPQ takes O(n) time in the worst case, where n is the number of elements involved. Your answer should be such that a good programmer would know how to implement your data structure. You must tell us therefore in words how makeMPQ, insert, delete-median are to be implemented, and you must draw the data structure. Question 4 k-ary heaps. A k-ary heap is a generalization of a standard binary heap so that there are k children per node. Give an implicit data structure implementation, with details on how to find the i-th child of node j, and the parent of node j. Estimate the worst-case number of comparisons (as a function of n and k, where n is the number of elements) for a standard operation DELETEMIN. Same question for the operation INSERT. Question 5 k-ary heaps (continued). Let a k-ary heap with n elements be given. Let the number of DELETEMIN and INSERT operations be equal; the sequence of the operations is such that the size of the heap remains about n. Argue why a 4-ary heap is best (you may need a calculator). Question 6 Beap. In 1976, Munro and Suwanda proposed the beap (biparental heap) as a possible implementation for a priority queue-like ADT supporting the operations makenull, insert, deletemin and search. A beap of n elements should be visualized as a triangular matrix such as the one shown below which has 10 elements. The elements are added in cross-diagonal fashion, so that the inherent order is 1, 2, 4, 6, 5, 9, 21, 8, 20 and 13 in the example shown below. ``` 1 4 9 13 ... 2 5 20 ... 6 8 ... 21 ... ``` The beap has the property that along any row or column, the elements are encountered in increasing order. Each element can be thought of as having two coordinates, say (c,r) where c stands for column and r stands for row. We use the convention that the first element is at (0,0). In our example, the element at (2,1) is 20, and the last element, at (3,0), is 13. If we place elements in cross-diagonal fashion, then the n-th element ends up at coordinates (c(n),r(n)), where c(.) and r(.) are certain simple functions of n that are given. Thus, (c(1),r(1))=(0,0), (c(2),r(2))=(0,1), (c(3),r(3))=(1,0), (c(4),r(4))=(0,2), and so forth. Define an implicit data structure for the beap, assuming that n never exceeds "maxnum". (This question is very easy.) Question 7 Beap (continued). Inspired by similar operations on heaps, describe how you would implement makenull, insert, search, and deletemin. Question 8 Beap (continued). Give O and Omega statements about the worst-case complexity of your operations insert and deletemin. Give the worst-case time complexity of beapsort, a sorting method in which n elements are inserted in a beap, and then deleted by n calls to deletemin. For use as a future event set in discrete event simulation, would you prefer a beap over a heap? The heap, the sorted array, the unsorted array and the beap are four structures capable of holding elements with the property that the position of an element in the structure contains information about the relative size of the element vis-a-vis some other elements. This property is not shared by pointer-based structures. Of the four structures cited above in boldface, which one is best, in the worst-case complexity sense, for the following tasks: makenull, followed by n insert's, and then n deletemin's. One search operation in a structure holding n elements. makenull, followed by n insert's and n search operations.

Copyright © 1999 Luc Devroye. Email: luc@cs.mcgill.ca.