Question: The very first container class we implemented was the random bag, which supported two operations: add, which adds an element to the random bag, and

The very first container class we implemented was the random bag, which supported two operations:
add, which adds an element to the random bag, and
removeRandom, which chooses a random element from the bag and returns it.
Below is our implemented code for RandomBag class. First, RandomBag.hppNext, RandomBag, cpp:Your task in this problem is to answer the following questions. Before that, recall the following important fact:Fact: The vector: : erase () function, takes time proportional to the distance between the element being removed and the last element of the Vector. The reason for this is that whenever a gap opens up in the vector, it has to shift all the elements after the gap down one spot, As a result, removing from the very end of a vector is quite fast - it runs in time O(1)- but removing from the very front of a vector takes time O(n).Q1(5 points) Look at the implementation of our RandomBag: :removeRandom function. What is its worst-case time complexity? How about its best-case time complexity? How about its average-case time complexity?Q2(5 points) Based on your answer to the previous part of this question, what is the worst-case time complexity of removing all the elements of an n-element RandomBag? What's the best-case time complexity? How about its average case?Q3(10 points) In the preceding discussion, we mentioned that removing the very last element of a vector is much more efficient that removing an element closer to the middle. Write another member function RandomBag: :removeRandomFast so that it always runs in worst-case O(1) time. In fact, there should only be two lines of code that are different from our current implementation.
The very first container class we implemented was

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock 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 Programming Questions!