Question: Suppose we are interested in dynamically maintaining a set S of integers, which is initially empty, while supporting the following two operations: add(v): Adds value

Suppose we are interested in dynamically maintaining a set S of integers, which is initially empty, while supporting the following two operations:

add(v): Adds value v to set S.

median( ): Returns the current median value of the set. For a set with even cardinality, we define the median as the average of the two most central values.

We will store each element of the set in one of two priority queues: amin-oriented priority queue, Q+, of all elements greater than or equal to the current median value, and amax-oriented priority queue, Q, of all elements less than the current median value.

a. Explain how to perform the operation median( ) in O(1) time given such a representation.

b. Explain how to performthe operation S.add(k) in O(logn) time, where n is the current cardinality of the set, while maintaining such a representation.

Step by Step Solution

3.52 Rating (162 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

a To perform the median operation in O 1 time we can simply look at the top element of either t... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Introduction to Algorithms Questions!