Sets By Varick Erickson Introduction For this project you will implement a Set using a hash based chain implementation (each bucket can contain multiple elements). template class SetT { public:...

1 answer below »
its need to be done in c++



Sets By Varick Erickson Introduction For this project you will implement a Set using a hash based chain implementation (each bucket can contain multiple elements). template class SetT { public: SetT(); SetT(int numBucks); ~SetT(); void Add(T elem); void Remove(T elem); bool Contains(T elem); SetT Union(SetT otherSet); SetT Difference(SetT otherSet); SetT Intersection(SetT otherSet); int Size() { return numElems }; void operator+(T elem); // Add void operator-(T elem); // Remove SetT operator+(SetT& other); // Union SetT operator*(SetT& other); // Intersection SetT operator-(SetT& other); // Difference void reset(); // Reset iterator bool HasNext(); T GetNextItem(); private: forward_list** buckets; // An array of forward_list's (ie, each index is a forward_list pointer) int numBuckets; int getHashIndex(const T& elem); int numElems; // Iterator variables int currBucket; // what bucket is the iterator on? mutable typename forward_list::iterator bucketIter; // the iterator of the current bucket // Any other private functions and variables you want/need }; Your job is to implement each of the methods in the header class. You are also responsible for creating a test driver (similar to Chunklist) that takes input files and generates output files. You only need to test using SetT sets. forward_list Each bucket contains should contain a linked list of elements of type T. For this assignment, you will be using a Standard Template Library (STL) implementation of a singly linked list. Here a code snippet that should be useful: #include #include int main () { std::forward_list mylist; mylist.push_front(42); mylist.push_front(55); mylist.push_front(32); mylist.push_front(5); std::cout < "mylist="" contains:";="">::iterator it; // Iterator for forward_list for (it = mylist.begin(); it != mylist.end(); ++it ) std::cout < '="" '="">< *it;="" std::cout="">< '\n';="" mylist.remove(42);="" removes="" 42="" from="" the="" list="" std::cout="">< "mylist="" contains:";="" for="" (it="mylist.begin();" it="" !="mylist.end();" ++it="" )="" std::cout="">< '="" '="">< *it;="" std::cout="">< '\n';="" return="" 0;="" }="" operator="" overloading="" operator="" overloading="" allows="" the="" programmer="" to="" override="" the="" behavior="" of="" operators="" such="" as="" +,="" -,="">, and<. while="" this="" may="" sound="" complicated,="" it="" actually="" straight="" forward.="" the="" following="" shows="" how="" we="" overload="" the="" +="" operator="" to="" add="" and="" element="" to="" the="" set:=""> void SetT::operator+(T elem) { this->Add(elem); } Note that the operator behavior is based on the type of the argument on the right side. In this case, the argument is of type T. If the argument was of type SetT, then the + would indicate Union. Test Driver You should have a test driver similar to the list driver from the previous program. In particular, it should support the following commands: • Add (this should test add and the + operator) • Remove (this should test remove and the - operator) • Intersection (this should test intersection and the * operator) • Union (this should test union and the + operator) • Difference (this should test difference and the - operator) • Size • Print Part 3: Questions 1. What is the advantages/disadvantages of a tree approach rather than a hash based approach? 2. What is the average big O of: • add • remove • contains • intersection • union • difference • size Deliverables • Your template class implementations for SetT • Your test driver • Input to the test driver demonstrating functionality • The output file generated from the test driver • Answers to the questions for part 3 Introduction forward_list Operator Overloading Test Driver Part 3: Questions Deliverables
Answered Same DayApr 29, 2021

Answer To: Sets By Varick Erickson Introduction For this project you will implement a Set using a hash based...

Ria answered on May 04 2021
149 Votes
SetDataStructure.cpp
SetDataStructure.cpp
// SetDataStructure.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h"
#include "SetT.h"
using namespace std;
int main()
{
    // You should create your own test driver 
that takes input files and generates output files.
    // The input/output should demonstrate functionality of the SetT ADT
    // (The input below is NOT sufficient for full testing)
    SetT a;
    SetT b;
    SetT c;
    b.Add(7);
    b.Add(9);
    a.Add(1);
    a.Add(1);
    a.Add(2);
    a.Add(3);
    a.Add(4);
    a.Add(5);
    a + 6;      // Adds 6 to a
    a.reset();
    while (a.HasNext()) {
        cout << a.GetNextItem() << endl;
    }
    c = a + b;  // Finds the union between a and b and stores into c
    c.reset(); cout << endl;
    while (c.HasNext()) {
        cout << c.GetNextItem() << endl;
    }
    return 0;
}
SetT.h
#pragma once
#include
#include
#include
#include
using namespace std;
template
class SetT
{
public:
    SetT();
    SetT(int numBucks);
    ~SetT();
    void Add(T elem);
    void Remove(T elem);
    bool Contains(T elem);
    SetT Union(SetT otherSet);
    SetT Difference(SetT otherSet);
    SetT Intersection(SetT otherSet);
    int Size() { return numElems; };
    void operator+(T elem);         // Add
    void operator-(T elem);         // Remove
    SetT operator+(SetT& other); // Union
    SetT operator*(SetT& other); // Intersection
    SetT operator-(SetT& other); // Difference
    void reset();    // Reset iterator
    bool HasNext();
    T GetNextItem();
private:
    forward_list** buckets;    // An array of forward_list's (ie, each index is a forward_list pointer)
    int numBuckets;
    int getHashIndex(const T& elem);
    int numElems;
    // Iterator variables
    int currBucket;                                            // what bucket is the iterator on?
    mutable typename forward_list::iterator bucketIter;    // the iterator of the current bucket
    // Any other private functions and variables you want/need
};
template
int SetT::getHashIndex(const T& key)
{
    // This is done... No touching!
    unordered_map mapper;
    typename unordered_map::hasher hashFunction = mapper.hash_function();
    return static_cast(hashFunction(key) % numBuckets);
}
template
SetT::SetT()
{
    // Create an array of forward_lists and initially set each bucket to null
    buckets = new forward_list * [5];
    for (int i = 0; i < 5; i++) {
        buckets[i] = new...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here