Question

In: Computer Science

In this problem we consider a data structure for maintaining a multi-set M. We want to...

In this problem we consider a data structure for maintaining a multi-set M. We want to support the following operations:

• Init(M): create an empty data structure M.

• Insert(M, i): insert (one copy of) i in M.

• Remove(M, i): remove (one copy of) i from M.

• F requency(M, i): return the number of copies of i in M.

• Select(M, k): return the kth element in the sorted order of elements in M.

Solutions

Expert Solution

Code in C++ using STL :-

#include <bits/stdc++.h>
using namespace std; 

void Insert(multiset <int>& M, int i)
{
        M.insert(i);
        cout<<i<<" Inserted \n";
}

void Remove(multiset <int>& M, int i)
{
        int num = M.erase(i);
        cout<<"("<<num<<") values matching "<<i<<" Removed \n";
}

int Frequency(multiset <int>& M, int i)
{
        int freq = M.count(i);
        return freq;
}

int Select(multiset <int>& M, int k)
{
        multiset <int> :: iterator it = M.begin();
        for(int i=0; i<k-1; i++)
                it++;
        return *it;
}

void PrintSet(multiset <int>& M)
{
        multiset <int> :: iterator it;
        cout<<"Printing the multiset: ";
        for (it = M.begin(); it != M.end(); ++it) 
    { 
        cout<< *it << '\t';
    } 
    cout << endl;
}

int main() 
{ 
        // empty multiset container Init()
        multiset <int> M; 
        
        // insert elements 
        Insert(M, 40);   
        Insert(M, 30);   
        Insert(M, 60);   
        Insert(M, 20);   
        Insert(M, 30);   
        Insert(M, 40);
        Insert(M, 10);
        Insert(M, 30);
        Insert(M, 60);
        Insert(M, 50);
        // Print the Multiset
        PrintSet(M);
        
        // Remove Elements
        Remove(M, 40);
        Remove(M, 10);
        PrintSet(M);
        
        // Check frequency of an element
        cout<<"Frequency of 30 in multiset: "<<Frequency(M, 30)<<endl;
        
        // Select a Kth element from the multiset
        cout<<"Element at Position 3: "<<Select(M, 3)<<endl;
        
        return 0;
} 

Hope this helps.

Good Luck :-)


Related Solutions

Consider one of the subset regression models for each data set obtained in Problem Set 4...
Consider one of the subset regression models for each data set obtained in Problem Set 4 and answer the following questions. (i) Draw the scatter plot matrix, residual vs. predictor variable plots and added variable plots. Comment on the regression model based on these plots. (ii) Draw the normal-probability plot and comment. (iii) Draw the correlogram and comment. (iv) Detect leverage points from the data. (v) Compute Cook’s distance statistics and detect all outlier points from the data. (vi) Compute...
Consider one of the subset regression models for each data set obtained in Problem Set 4...
Consider one of the subset regression models for each data set obtained in Problem Set 4 and answer the following questions. (i) Draw the scatter plot matrix, residual vs. predictor variable plots and added variable plots. Comment on the regression model based on these plots. (ii) Draw the normal-probability plot and comment. (iii) Draw the correlogram and comment. (iv) Detect leverage points from the data. (v) Compute Cook’s distance statistics and detect all outlier points from the data. (vi) Compute...
Using the R built-in data set called Chick Weight, we want to compare the mean weight...
Using the R built-in data set called Chick Weight, we want to compare the mean weight across the different types of Diet. IMPORTANT: We only want to compare chicks at the final value of Time, 21. In this problem, use ?? = 0.05. Make a boxplot to compare weight across the different types of Diet. Based on the boxplot, describe any differences (or lack of differences) you see. Run an ANOVA to compare weight across the different types of Diet....
3. Using the R data set called warpbreaks (See ?warpbreaks for more info), we want to...
3. Using the R data set called warpbreaks (See ?warpbreaks for more info), we want to compare the mean breaks across both the different types of wool and the different levels of tension. In this problem, use ?? = 0.10. a. Make a boxplot to compare breaks across both wool and tension. Color-code the three different tension levels for easier visibility. Within wool A, describe the relationship between tension and breaks. Within wool B, describe the relationship between tension and...
Hi there, We want to make a correct formulation of the problem that want to apply...
Hi there, We want to make a correct formulation of the problem that want to apply Linear programming on it.. we went to a store that is selling surveillance system (Esp. cameras called UNV (uniview products)) we need to know what is the possible constraints to be considered in order to maximize the profit. :)
Problem: Clique In this problem IS stands for Independent Set. The usual IS problem, that we...
Problem: Clique In this problem IS stands for Independent Set. The usual IS problem, that we showed in class in NP-complete is as follows: Input: Unidrected graph G(V, E) and number k, with 1 ≤ k ≤ n. Output: YES if G has an independent set of containing at least k vertices.   NO if all independent sets of G contain strictly less than k vertices. Definition: Let G(V, E) be an undirected graph and let V ′ be a proper...
We have been using the same set of data (Data Set One) in the notes to...
We have been using the same set of data (Data Set One) in the notes to illustrate production and costs. I have provided Data Set One in both tables below. When costs were calculated in the notes, fixed costs were $200. By using the term fixed costs economists are only referring to the fact that a firm must pay this expense no matter how much output it produces or sells. An example of a fixed cost could be the rent...
We have been using the same set of data (Data Set One) in the notes to...
We have been using the same set of data (Data Set One) in the notes to illustrate production and costs. I have provided Data Set One in both tables below. When costs were calculated in the notes, fixed costs were $200. By using the term fixed costs economists are only referring to the fact that a firm must pay this expense no matter how much output it produces or sells. An example of a fixed cost could be the rent...
We have been using the same set of data (Data Set One) in the notes to...
We have been using the same set of data (Data Set One) in the notes to illustrate production and costs. I have provided Data Set One in both tables below. When costs were calculated in the notes, fixed costs were $200. By using the term fixed costs economists are only referring to the fact that a firm must pay this expense no matter how much output it produces or sells. An example of a fixed cost could be the rent...
We have been using the same set of data (Data Set One) in the notes to...
We have been using the same set of data (Data Set One) in the notes to illustrate production and costs. I have provided Data Set One in both tables below. When costs were calculated in the notes, fixed costs were $200. By using the term fixed costs economists are only referring to the fact that a firm must pay this expense no matter how much output it produces or sells. An example of a fixed cost could be the rent...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT