Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

131 Data Structures 13270 Fall 2015 Project 2 The purpose of this project is to

ID: 3772502 • Letter: 1

Question

131 Data Structures 13270 Fall 2015 Project 2

The purpose of this project is to familiarize you with the map abstract data type (ADT) and its several implementations. Second, it is to reinforce the concepts of interfaces, inheritance, polymorphism, and the idea of empirical analysis.

Project Statement

In the project les posted on Titanium you will nd the Map.h le as well as several skeleton les. Map.h denes the interfaces for each of the template classes you will be implementing in this project. To complete this project you must implement the following three template classes:

• HashMap • TreeMap • SearchTable Each of the above template classes must extend the given abstract Map class. For each of the above classes you must implement the “Big-3” (Copy constructor, overloaded assignment operator, and destructor) in addition to the below requirements.

HashMap

This class will implement the map ADT by using a hash table. You may use either separate chaining or open addressing to resolve hash collisions. If you choose open addressing, you must use linear probing. If you you use separate chaining you are free to use the ArrayList class or the LinkedList class from project 1.

1

n Put Erase Find Size

1 0 0 0 0

1001 0 0.000999001 0.001998 0

... ... ... ... ...

9001 0 0.0885457 0 0.0433285

Table 1: CSV File Structure

TreeMap

This class will implement the map ADT by using a binary search tree. The binary search tree can be implemented in a separate le (BinarySearchTree.h). This class will contain a private data member for the binary search tree which is used to implement the methods of TreeMap.

SearchTable

This class will implement the map ADT by using a sorted search table and the binary search algorithm. A private function Sort for sorting the array using a sorting method of your choice must be implemented, as well as a method Search which implements the binary search algorithm.

Once these classes have been implemented, use the main.cpp provided le to generate a CSV le of timing results. The timing results for HashMap, TreeMap, and SearchTable will be found in hm-out.csv, tm-out.csv, and stout.csv respectively. The structure for both of the CSV les is shown in Table 1. In the table, the n column denotes the input size and the next four columns give the average run-time in ms of the given operations. Plot these results using software like L ATEX, Excel, Matlab, Mathematica, GNU Plot, etc. Given these plots, answer the following questions:

1. What are the big-O run-times of the four Map operations for HashMap? Do your empirical results agree with this? Why or why not?

2. What are the big-O run-times of the four Map operations for TreeMap?

2

Do your empirical results agree with this? Why or why not?

3. What are the big-O run-times of the four Map operations for SearchTable? Do your empirical results agree with this? Why or why not?

4. Draw a class diagram of the classes in this project. Label the inheritance relationships as either being has-a or is-a.

Deliverables

These are the following les required for this assignment:

1. HashMap.h

2. HashMap.cpp (optional)

3. TreeMap.h

4. TreeMap.cpp (optional)

5. SearchTable.h

6. SearchTable.cpp (optional)

7. BinarySearchTree.h (optional)

8. BinarySearchTree.cpp (optional)

9. Report.pdf

In the list of les above, the .cpp les are optional because the function implementations can be placed directly inside the .h les. Due to the way C++ implements templates, the function implementations must be visible when compiling the template class. This can be accomplished by either by providing the implementation inside the class itself, or by including the .cpp at the end of the .h le. If you do not have .cpp les, your .h les must contain the function implementations.

HERE THE FILES NEEDED

Main:

#include <iostream>

#include <fstream>

#include <ctime>

#include "Map.h"

#include "HashMap.h"

#include "SearchTable.h"

#include "TreeMap.h"

#define NUM_TST 10000

using namespace std;

const static int ERROR = -1357;

const static int MAP_SIZE = 10000;

const static int SPACING = 1000;

//Used to generate timing results

//fn: The filename

//map: The map to time

template <typename T1, typename T2>

void CreateCSV(const string& fn, Map<T1, T2>& map);

//Used to test the erase function

//size: The size of the map to test

//map: The map to test

template <typename T1, typename T2>

void EraseTest(unsigned int size, Map<T1, T2>& map);

//Used to test the find function

//size: The size of the map to test

//map: The map to test

template <typename T1, typename T2>

void FindTest(unsigned int size, Map<T1, T2>& map);

//Used to test the put function

//size: The size of the map to test

//map: The map to test

template <typename T1, typename T2>

void PutTest(unsigned int size, Map<T1, T2>& map);

/**

* Used for timing tests

*/

template <typename T1, typename T2>

long double RunTest(void (*tst)(unsigned int, Map<T1, T2>&), unsigned int numTrials, Map<T1, T2>& map);

//Notice the parameter is that of the super class

//But we are allowed to pass a subclass as long as we

//Only use functions defined by the superclass interface

template <typename T1, typename T2>

void TestMap(Map<T1, T2>& map);

//Used to test the size function

//size: The size of the map to test

//map: The map to test

template <typename T1, typename T2>

void SizeTest(unsigned int size, Map<T1, T2>& map);

int main()

{      //Test the TreeMap implementation

       TreeMap<int, double> tmmap;

       try

       {

              TestMap(tmmap);

       }

       catch(int error)

       {

              if(error == tmmap.ELE_DNE)

              {

                     cout << "TreeMap: A test failed. ";

                     return -1;

              }

       }

       CreateCSV("tm-out.csv", tmmap);

       cout << "TreeMap: All tests passed! ";

       //Test the HashMap implementation

       HashMap<int, double> hmap;

       try

       {

              TestMap(hmap);

       }

       catch(int error)

       {

              if(error == hmap.ELE_DNE)

              {     

                     cout << "HashMap: A test failed. ";

                     return -1;

              }

       }

       CreateCSV("hm-out.csv", hmap);

       cout << "HashMap: All tests passed! ";

       //Test the SearchTable implementation

       SearchTable<int, double> stmap;

       try

       {

              TestMap(stmap);

       }

       catch(int error)

       {

              if(error == stmap.ELE_DNE)

              {

                     cout << "SearchTable: A test failed. ";

                     return -1;

              }

       }

       CreateCSV("st-out.csv", stmap);

       cout << "SearchTable: All tests passed! ";

       //Done; exit with 0 (success)

       return 0;

}

template <typename T1, typename T2>

void CreateCSV(const string& fn, Map<T1, T2>& map)

{

       ofstream csvFile;

       csvFile.open(fn.c_str());

       for(unsigned int i = 1; i <= MAP_SIZE; i += SPACING)

       {      //Run each test and append the results to the CSV file

              csvFile << i << ",";

              csvFile << RunTest(PutTest<T1, T2>, i, map) << ",";

              csvFile << RunTest(FindTest<T1, T2>, i, map) << ",";

              csvFile << RunTest(SizeTest<T1, T2>, i, map) << ",";

              csvFile << RunTest(EraseTest<T1, T2>, i, map) << " ";

       }

       csvFile.close();

}

template <typename T1, typename T2>

void EraseTest(unsigned int size, Map<T1, T2>& map)

{     

       for(unsigned int i = 0; i < size; ++i)

              map.Erase(T1(i));

}

template <typename T1, typename T2>

void FindTest(unsigned int size, Map<T1, T2>& map)

{     

       for(unsigned int i = 0; i < size; ++i)

              map.Find(T1(i));

}

template <typename T1, typename T2>

void PutTest(unsigned int size, Map<T1, T2>& map)

{     

       for(unsigned int i = 0; i < size; ++i)

              map.Put(T1(i), T2(i));

}

template <typename T1, typename T2>

long double RunTest(void (*tst)(unsigned int, Map<T1, T2>&), unsigned int numTrials, Map<T1, T2>& map)

{      //Start time and run tests

       clock_t strt, end;

       strt = clock();

       try

       {

              tst(numTrials, map);

       }

       catch(int errCode)

       {

              if(errCode == Map<T1, T2>::ELE_DNE)

              {

                     cout << "Error: element does not exist ";

                     exit(1);

              }

       }

       end = clock();

       return 1000.0 * ((end - strt) / ((long double) numTrials * CLOCKS_PER_SEC));

}

template <typename T1, typename T2>

void SizeTest(unsigned int size, Map<T1, T2>& map)

{     

       for(unsigned int i = 0; i < size; ++i)

              map.Size();

}

template <typename T1, typename T2>

void TestMap(Map<T1, T2>& map)

{

       int numTimes = 20;

       for(int i = 0; i < NUM_TST; ++i)

       {

              //test Size

              if(map.Size() != 0)

                     throw ERROR;

              //Test Put

              for(int j = 0; j < numTimes; ++j)

                     map.Put(j, j);

              //Test Find

              for(int j = 0; j < numTimes; ++j)

                     map.Find(j);

              //test Size

              if(map.Size() != numTimes)

                     throw ERROR;

              //Test erase

              for(int j = 0; j < numTimes; ++j)

                     map.Erase(j);

              //test Size

              if(map.Size() != 0)

                     throw ERROR;

       }

}

HASHMAP HEADER FILE:

#ifndef HASHMAP_H

#define HASHMAP_H

//Needed for rand() function

#include <cstdlib>

typedef unsigned int Index;

/**

* A map implementation that uses a hash table

* with a multiplicative hash function internally.

*/

template <typename T1, typename T2>

class HashMap : public Map<T1, T2>

{

public:      

       /**

       * Attempts to erase the (key, value) pair

       * with key = k. The int ELE_DNE is thrown

       * if the key is not in the map

       * @param k Is the key of the pair to erase

       */

       virtual void Erase(const T1& k)

       {

              //TODO

       }

       /**

       * Attempts to find the corresponding value

       * for a given key. The int ELE_DNE is thrown

       * if the key is not in the map

       * @param k Is the key to search for

       * @return The value corresponding to k

       */

       virtual T2& Find(const T1& k) const

       {     

              //TODO

       }

      

       /**

       * Create a map with a default initial

       * capacity

       */

       HashMap()

       {

              //TODO

       }

      

      

       /**

       * Create a map with a initial capacity

       * @param capc The initial capacity

       */

       HashMap(unsigned int capc)

       {

              //TODO

       }

      

       //Copy constructor

       HashMap(const HashMap& hm)

       {

              //TODO

       }

      

       //Desctructor

       virtual ~HashMap()

       {

              //TODO

       }

      

       /**

       * Overloaded assignment operator. Perform

       * a deep copy of the parameter.

       * @param rhs The object to copy

       * @return A reference to the calling object

       */

       HashMap& operator=(const HashMap& rhs)

       {

              //TODO

       }

      

       /**

       * Adds a (key, value) pair to the map

       * @param k Is the key

       * @param v Is the value

       */

       virtual void Put(const T1& k, const T2& v)

       {     

              //TODO

       }

      

       /**

       * Returns the number of elements in the Map

       * @return: Number of elements in map object

       */

       virtual unsigned int Size() const

       {

              //TODO

       }

      

private:

       //Default capacity of the underlying array

       const static int DEF_CAPC = 10;

       /**

       * Private class for specifying the (key, value)

       * pair. The template <typename T1, T2> is not needed

       * here as it is implied from the top level template

       * declaration on ListMap itself. The implied template

       * parameters are T1 and T2.

       */

       class KeyValue

       {      //Make the data members public for easy access

       public:

              KeyValue()

              {      //Mark the entry as being open

                     open = true;

              }

              //The key

              T1 key;

              //The value

              T2 val;

              //Whether the entry is open

              bool open;

       };

      

       /**

       * Resize the map's array the requiring a

       * rehash. A new random hash function is computed.

       * @param The new size of the array

       */

       void Rehash(unsigned int cap)

       {      //Don't forget to create a new hash function

              //TODO

       }

       /**

       * The hash function

       * @param The element to be hashed

       * @return The resulting hash index

       */

       Index F(const T1& ele) const

       {

              return (a * ele + b) % max;

       }

       //The hash table

       KeyValue* arr;

       //Number of elements, capcity, and hash function values

       unsigned int n, max, a, b;

};

#endif

MAP HEADER FILE:

#ifndef MAP_H

#define MAP_H

/**

* A abstract class defining the interface

* for the Map class. Two template parameters

* are used. T1 is the type of the key

* T2 is the type of the value in a

* (key, value) pair.

* Note: Map contains pure virtual functions

* and therefore is abstract and cannot be

* instantiated. Use the subclass ListMap

* HashMap, TreeMap, or SearchTable instead.

*/

template <typename T1, typename T2>

class Map

{

public:

       //Defines an error code used for throwing

       //integer exceptions

       const static int ELE_DNE = -2468;

       /**

       * Attempts to erase the (key, value) pair

       * with key = k. The int ELE_DNE is thrown

       * if the key is not in the map

       * @param k Is the key of the pair to erase

       */

       virtual void Erase(const T1& k) = 0;

      

       /**

       * Attempts to find the corresponding value

       * for a given key. The int ELE_DNE is thrown

       * if the key is not in the map

       * @param k Is the key to search for

       * @return The value corresponding to k

       */

       virtual T2& Find(const T1& k) const = 0;

      

       /**

       * Returns the number of elements in the Map

       * @return: Number of elements in map object

       */

       virtual unsigned int Size() const = 0;

      

       /**

       * Adds a (key, value) pair to the map

       * @param k Is the key

       * @param v Is the value

       */

       virtual void Put(const T1& k, const T2& v) = 0;

      

};

#endif

SEARCH TABLE HEADER FILE:

#ifndef SEARCHTABLE_H

#define SEARCHTABLE_H

/**

* A class that implements the Map.h interface.

* This implements the Map ADT using a search table

* T1 is the type of the key T2 is the type of the

* value in a (key, value) pair.

*/

template <typename T1, typename T2>

class SearchTable : public Map<T1, T2>

{

public:      

       /**

       * Attempts to erase the (key, value) pair

       * with key = k. The int ELE_DNE is thrown

       * if the key is not in the map

       * @param k Is the key of the pair to erase

       */

       virtual void Erase(const T1& k)

       {      //Attempt to erase the element

              //TODO

       }

      

       /**

       * Attempts to find the corresponding value

       * for a given key. The int ELE_DNE is thrown

       * if the key is not in the map

       * @param k Is the key to search for

       * @return The value corresponding to k

       */

       virtual T2& Find(const T1& k) const

       {      //Attempt to find the element

              //TODO

       }

      

       /**

       * Overloaded assignment operator. Perform

       * a deep copy of the parameter.

       * @param rhs The object to copy

       * @return A reference to the calling object

       */

       SearchTable& operator=(const SearchTable& rhs)

       {

              //TODO

       }

      

       /**

       * Adds a (key, value) pair to the map

       * @param k Is the key

       * @param v Is the value

       */

       virtual void Put(const T1& k, const T2& v)

       {      //Check if resize is necessary

              //TODO

       }

       //Default constructor

       SearchTable()

       {

              //TODO

       }

       //Copy constructor

       SearchTable(const SearchTable& st)

       {

              //TODO

       }

      

       /**

       * Takes as argument an array of keys and

       * and array of values. Copies of the key value pairs

       * are internally and then sorted. Both arrays

       * must have the same size

       * @param keys The array of keys

       * @param vals The array of values

       * @param numEle The number of key-value pairs

       */

       SearchTable(const T1* keys, const T2* vals, unsigned int numEle)

       {

              //TODO

       }

       //Virtual destructor

       virtual ~SearchTable()

       {

              //TODO

       }

       /**

       * Returns the number of elements in the Map

       * @return: Number of elements in map object

       */

       virtual unsigned int Size() const

       {     

              //TODO

       }

      

private:

       //Default capacity

       const static int DEF_CAPC = 10;

       /**

       * Private class for specifying the (key, value)

       * pair. The template <typename T1, T2> is not needed

       * here as it is implied from the top level template

       * declaration on ListMap itself. The implied template

       * parameters are T1 and T2.

       */

       class KeyValue

       {      //Make the data members public for easy access

       public:

              T1 key;

              T2 value;

       };

      

       /**

       * Performs a binary search on the sorted array

       * @param k The key to search for

       * @return The index of the key if found; -1 if not found

       */

       int BinarySearch(const T1& k) const

       {

              //TODO

       }

      

       /**

       * Resize the list's array

       * @param The new size of the array

       */

       void ResizeArr(unsigned int cap)

       {

              //TODO

       }

       /**

       * Sort the search table

       */

       void Sort()

       {

              //TODO

       }

      

       //Number of elements in the SearchTable

       unsigned int n;

       //Capacity of arr

       unsigned int max;

       //The underlying array

       KeyValue* arr;

};

#endif

TREEMAP HEADER FILE:

#ifndef TREEMAP_H

#define TREEMAP_H

/**

* A class that implements the Map.h interface.

* This implements the Map ADT using a binary

* search tree. T1 is the

* type of the key T2 is the type of the

* value in a (key, value) pair.

*/

template <typename T1, typename T2>

class TreeMap : public Map<T1, T2>

{

public:

       /**

       * Attempts to erase the (key, value) pair

       * with key = k. The int ELE_DNE is thrown

       * if the key is not in the map

       * @param k Is the key of the pair to erase

       */

       virtual void Erase(const T1& k)

       {     

              //TODO

       }

       /**

       * Attempts to find the corresponding value

       * for a given key. The int ELE_DNE is thrown

       * if the key is not in the map

       * @param k Is the key to search for

       * @return The value corresponding to k

       */

       virtual T2& Find(const T1& k) const

       {      //Attempt to find the element

              //TODO

       }

       /**

       * Overloaded assignment operator. Perform

       * a deep copy of the parameter.

       * @param rhs The object to copy

       * @return A reference to the calling object

       */

       TreeMap& operator=(const TreeMap& rhs)

       {

              //TODO

       }

      

       /**

       * Adds a (key, value) pair to the map

       * @param k Is the key

       * @param v Is the value

       */

       virtual void Put(const T1& k, const T2& v)

       {     

              //TODO

       }

       /**

       * Returns the number of elements in the Map

       * @return: Number of elements in map object

       */

       virtual unsigned int Size() const

       {     

              //TODO

       }

      

       //Default constructor

       TreeMap()

       {

              //TODO

       }

      

       //Copy constructor

       TreeMap(const TreeMap& tm)

       {

              //TODO

       }

      

       //Virtual destructor

       virtual ~TreeMap()

       {

              //TODO

       }

      

private:

       /**

       * Private class for specifying the (key, value)

       * pair. The template <typename T1, T2> is not needed

       * here as it is implied from the top level template

       * declaration on ListMap itself. The implied template

       * parameters are T1 and T2.

       */

       class KeyValue

       {      //Make the data members public for easy access

       public:

              T1 key;

              T2 value;

       };

       //BST implementation goes here

       /**

       * This class represents a node in the search tree

       */

       class Node

       {

       public:

              Node() { right = left = NULL; }

              Node* right;

              Node* left;

              KeyValue val;

       };

       //The root of the tree and the number of children (n)

       Node* root;

       unsigned int n;

};

#endif

Explanation / Answer

Answer:

Since the header files are given. So I implemented only .cpp files

HashMap.cpp

#include <iostream>

#include <fstream>

#include <ctime>

#include "Map.h"

#include "HashMap.h"

using namespace std;

template <typename T1, typename T2>

       virtual void Erase(const T1& k)

       {

              for(int k1=0;k1<n;k1++)

          {

              if(arr[k1].key==k)

              {

                   for(int k2=k1;k2<n-1;k2++)

                   {

                        arr[k2]=arr[k2+1];

                   }

                   return;

              }

          }

          throw ELE_DNE;

       }

       /**

       * Attempts to find the corresponding value

       * for a given key. The int ELE_DNE is thrown

       * if the key is not in the map

       * @param k Is the key to search for

       * @return The value corresponding to k

       */

       virtual T2& Find(const T1& k) const

       {    

              for(int k1=0;k1<n;k1++)

               {   

              if(arr[k1].key==k1)

                   return arr[k1].val;

          }

          throw ELE_DNE;

       }

     

       /**

       * Create a map with a default initial

       * capacity

       */

       HashMap()

       {

              arr=new KeyValue[DEF_CAPC];

          max=DEF_CAPC;

       }

     

     

       /**

       * Create a map with a initial capacity

       * @param capc The initial capacity

       */

       HashMap(unsigned int capc)

      {

              arr=new KeyValue[capc];

          max=capc;

       }

     

       //Copy constructor

       HashMap(const HashMap& hm)

       {

              n=hm.size();

               for(int k1=0;k1<n;k1++)

               {

              arr[k1]=rm.arr[k1];

               }

         

       }

     

       //Desctructor

       virtual ~HashMap()

       {

             delete[] arr;

       }

     

       /**

        * Overloaded assignment operator. Perform

        * a deep copy of the parameter.

        * @param rhs The object to copy

        * @return A reference to the calling object

        */

       HashMap& operator=(const HashMap& rhs)

       {

              n=rhs.size();

          for(int k1=0;k1<n;k1++)

          {

              arr[k1]=rhs.arr[k1];

          }

          return this;

       }

     

       /**

       * Adds a (key, value) pair to the map

       * @param k Is the key

       * @param v Is the value

       */

       virtual void Put(const T1& k, const T2& v)

       {    

if(n<max)

{

              KeyValue k=KeyValue();

          k.key=k;

          k.val=v;

          arr[n++]=k;

}

else

throw ELE_DNE;      

}

     

       /**

        * Returns the number of elements in the Map

        * @return: Number of elements in map object

        */

       virtual unsigned int Size() const

       {

              return n;

     }

     

     

       /**

       * Resize the map's array the requiring a

       * rehash. A new random hash function is computed.

       * @param The new size of the array

       */

       void Rehash(unsigned int cap)

       {     

     int k=rand()%cap;

       }

       /**

       * The hash function

       * @param The element to be hashed

       * @return The resulting hash index

       */

       Index F(const T1& ele) const

       {

              return (a * ele + b) % max;

       }

SearchTable.cpp:

#include <iostream>

#include <fstream>

#include <ctime>

#include "Map.h"

#include "SearchTable.h"

using namespace std;

template <typename T1, typename T2>

virtual void Erase(const T1& k)

       {           for(int k1=0;k1<n;k1++)

          {

              if(arr[k1].key==k)

              {

                   for(int k2=k1;k2<n-1;k2++)

                   {

                        arr[k2]=arr[k2+1];

                   }

                   return;

              }

          }

          throw ELE_DNE;

       }

       

       /**

       * Attempts to find the corresponding value

       * for a given key. The int ELE_DNE is thrown

       * if the key is not in the map

       * @param k Is the key to search for

       * @return The value corresponding to k

       */

       virtual T2& Find(const T1& k) const

       {      for(int k1=0;k1<n;k1++)

              {   

              if(arr[k1].key==k1)

                   return arr[k1].val;

          }

          throw ELE_DNE;

       }

     

       /**

        * Overloaded assignment operator. Perform

        * a deep copy of the parameter.

        * @param rhs The object to copy

        * @return A reference to the calling object

        */

       SearchTable& operator=(const SearchTable& rhs)

       {

              n=rhs.size();

          for(int k1=0;k1<n;k1++)

          {

              arr[k1]=rhs.arr[k1];

          }

          return this;

       }

     

       /**

       * Adds a (key, value) pair to the map

       * @param k Is the key

       * @param v Is the value

       */

       virtual void Put(const T1& k, const T2& v)

       {      if(n<max)

          {

                   KeyValue k=KeyValue();

              k.key=k;

              k.val=v;

              arr[n++]=k;

          }

          else

              throw ELE_DNE;

       }

       //Default constructor

       SearchTable()

       {

             

          max=DEF_CAP;

          n=0;

          arr=new KeyValue[max];

       }

       //Copy constructor

       SearchTable(const SearchTable& st)

       {

                   n=st.size();

              for(int k1=0;k1<n;k1++)

              {

              arr[k1]=st.arr[k1];

              }

     

       /**

       * Takes as argument an array of keys and

       * and array of values. Copies of the key value pairs

       * are internally and then sorted. Both arrays

       * must have the same size

       * @param keys The array of keys

       * @param vals The array of values

       * @param numEle The number of key-value pairs

       */

       SearchTable(const T1* keys, const T2* vals, unsigned int numEle)

       {

          arr=new KeyValue[numEle];

          n=0;

          for(int k1=0;k1<numEle;k1++)

          {

              arr[k1].key=keys[k1];

              arr[k1].val=vals[k1];

              n++;

          }             

       }

       //Virtual destructor

       virtual ~SearchTable()

       {

              delete[] arr;

       }

       /**

       * Returns the number of elements in the Map

       * @return: Number of elements in map object

       */

       virtual unsigned int Size() const

       {    

              return n;

       }

     

      

       /**

        * Performs a binary search on the sorted array

        * @param k The key to search for

        * @return The index of the key if found; -1 if not found

        */

       int BinarySearch(const T1& k) const

       {

              int f=0;

          int l=n-1;

          int m=(f+l)/2;

          while(f<l)

          {

              if(arr[m].key<k)

                   f=m+1;

              else if(arr[m].key==k)

              {

                   return arr[m].key;

              }

              else

                   l=m-1;

              m=(f+l)/2;

          }

       }

     

       /**

        * Resize the list's array

        * @param The new size of the array

        */

       void ResizeArr(unsigned int cap)

       {

              delete[] arr;

          arr=new KeyValue[cap];

          max=cap;

       }

       /**

        * Sort the search table

        */

       void Sort()

       {

              for(int k1=0;k1<n;k1++)

          {

              for(int k2=k1+1;k2<n;k2++)

              {

                   if(arr[k1].key>arr[k2].key)

                   {

                        KeyValue k=arr[k1];

                        arr[k1]=arr[k2];

                        arr[k2]=k;

                   }

              }

          }

       }

TreeMap.cpp

#include <iostream>

#include <fstream>

#include <ctime>

#include "Map.h"

#include "TreeMap.h"

using namespace std;

virtual void Erase(const T1& k)

    {  

        KeyValue newk = KeyValue();

        newk.key = k;

        try {

            bst.Remove(newk);

        } catch (int ex) {

            if (ex== -2467)

                cout << "UNABLE TO ERASE";

        }

    }

virtual T2& Find(const T1& k) const

       {     

          KeyValue newkey=KeyValue();

          newkey.key=k;

          return bst.Find(newkey).value;

       }

       /**

        * Overloaded assignment operator. Perform

        * a deep copy of the parameter.

        * @param rhs The object to copy

        * @return A reference to the calling object

        */

       TreeMap& operator=(const TreeMap& rhs)

       {

              bst=BinarySearchTree<KeyValue>(rhs.bst);

          return bst;

       }

     

       /**

       * Adds a (key, value) pair to the map

       * @param k Is the key

       * @param v Is the value

       */

       virtual void Put(const T1& k, const T2& v)

       {    

              KeyValue newkey=KeyValue(k,v);

          bst.insert(newkey);

       }

       /**

       * Returns the number of elements in the Map

       * @return: Number of elements in map object

       */

       virtual unsigned int Size() const

       {    

              return n;

       }

     

       //Default constructor

       TreeMap()

       {

              bst=BinarySearchTree<KeyValue>();

       }

     

       //Copy constructor

       TreeMap(const TreeMap& tm)

       {

              bst=BinarySearchTree<KeyValue>(tm.bst);

       }

     

       //Virtual destructor

       virtual ~TreeMap()

       {

             bst.~BinarySearchTree<KeyValue>();

       }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote