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

I really need help here thank you QUESTION Replace the tables and linked lists w

ID: 3845968 • Letter: I

Question

I really need help here thank you

QUESTION

Replace the tables and linked lists with binary search trees.

Cargo.h

#pragma once

#include <iostream>
#include <limits.h>
#include <string>
using namespace std;

class Cargo
{
public:
   static int read(void);
   static int charToCargo(char);
   static char cargoToChar(int);
   static bool isCargo(char);

   static const int BADCARGO = -1;
   static const int CABBAGES = 0;
   static const int GOAT = 1;
   static const int SHOWMAN = 2;
   static const int WOLF = 3;
   static const int NOCARGO = 4;
   static const int CARGO_AND_SHOWMAN = 5;
};

River.h

#pragma once
//#include <math.h>
#include "cargo.h"

class River
{
public:
   River(void);
   void show(void);
   void cross(int);
   bool stop(void);

private:
   struct Transition { unsigned near_side_state; unsigned far_side_state; };

   static const unsigned start_state = 0;
   static const unsigned stop_state = 15;
   bool _far_side[Cargo::CARGO_AND_SHOWMAN];
   bool _near_side[Cargo::CARGO_AND_SHOWMAN];
   void copy(bool[], bool[]);
   unsigned state(bool[]);
   bool goodCrossing(unsigned near_side_state, unsigned far_side_state);
};

Cargo.cpp

#include "Cargo.h"

int Cargo::charToCargo(char cargo)
{
   switch(cargo)
   {
   case 'w': case 'W':
       return WOLF;
   case 's': case 'S':
       return SHOWMAN;
   case 'g': case 'G':
       return GOAT;
   case 'c': case 'C':
       return CABBAGES;
   default:
       return NOCARGO;
   }
}
char Cargo::cargoToChar(int cargo)
{
   switch(cargo)
   {
   case WOLF:
       return 'W';
   case SHOWMAN:
       return 'S';
   case GOAT:
       return 'G';
   case CABBAGES:
       return 'C';
   case NOCARGO:
       return 'N';
   default:
       return 'B';
   }
}
bool Cargo::isCargo(char candidate)
{
   switch(candidate)
   {
   case 'w': case 'W':
   case 's': case 'S':
   case 'g': case 'G':
   case 'C': case 'c':
   case 'N': case 'n':
       return true;
   default:
       return false;
   }
}
int Cargo::read(void)
{
   char cargo;
   do
   {
       cout << "Enter W for Wolf"<< endl;
       cout << "Enter N for No Cargo" << endl;
       cout << "Enter G for Goat"<< endl;
       cout << "Enter C for Cabbages" << endl;
       cout << "Enter cargo - ";
       cin.get(cargo);
       cin.ignore(numeric_limits<int>::max(), ' ');
   }
   while(!isCargo(cargo));
   return charToCargo(cargo);
}

River.cpp

#include "River.h"

River::River(void)
{
   for (int pos = Cargo::CABBAGES; pos <= Cargo::WOLF; pos++)
       _far_side[pos] = false;
}
void River::show(void)
{
   for(int pos = Cargo::CABBAGES; pos <= Cargo::WOLF; pos++)
       if(_far_side[pos])
           cout << "- | " << Cargo::cargoToChar(pos) << endl;
       else
           cout << Cargo::cargoToChar(pos) << " | -" << endl;
   cout << endl;
}
void River::cross(int cargo)
{
   static const unsigned n_transitions = 7;
   static Transition badtransitions[] =
   { { 0,4 },{ 0,5 },{ 0,12 },
   { 1,5 },
   { 7,3 },
   { 8,12 },
   { 14,10 }};
   static string messages[] = {"No! The goat will eat cabbage and wolf will eat goat.",
                               "No! The wolf will eat the goat.",
       "No! The goat will eat the cabbage.",
       "No! The wolf will eat the goat.",
                               "No! The goat will eat the cabbage.",
                               "No! The goat will eat the cabbage.",
                               "No! The wolf will eat the goat.",
                               "No! The goat will eat the cabbage.",
                               "No! The wolf will eat the goat.",
                               "No! The goat will eat the cabbage and wolf will eat the goat.",
   "No! Doing this is impossible."};
   copy(_far_side, _near_side);
   _far_side[Cargo::SHOWMAN] = !_near_side[Cargo::SHOWMAN];
   _far_side[cargo] = !_near_side[cargo];
   unsigned near_side_state = state(_near_side);
   unsigned far_side_state = state(_far_side);

   if (!goodCrossing(near_side_state, far_side_state))
   {
       copy(_near_side, _far_side);
       int id;
       for(id = 0; id < n_transitions; id++)
           if(badtransitions[id].near_side_state == near_side_state && badtransitions[id].far_side_state == far_side_state)
               break;
       cout << endl << messages[id] << endl << endl;
   }
}
void River::copy(bool source[], bool sink[])
{
   for(int pos = Cargo::CABBAGES; pos <= Cargo::WOLF; pos++)
       sink[pos] = source[pos];
}
unsigned River::state(bool river[])
{
   unsigned _state = 0;
   for(unsigned pos = Cargo::CABBAGES; pos <= Cargo::WOLF; pos++)
       _state = _state + river[pos] * (int)pow(2, pos);
   return _state;
}
bool River::goodCrossing(unsigned near_side_state, unsigned far_side_state)
{
   static const unsigned n_transitions = 20;
   static Transition transitions[] =
   { {0,6},
   {1,7},{1,13},
   {2,6},{2,7},{2,14},
   {6,0},{6,2},
   {7,1},{7,2},
   {8,13},{8,14},
   {9,13},{9,15},
   {13,1},{13,8},{13,9},
   {14,2},{14,8},
   {15,9} };

   unsigned id;
   for (id = 0; id < n_transitions; id++)
       if (transitions[id].near_side_state == near_side_state && transitions[id].far_side_state == far_side_state)
           return true;
   return false;
}
bool River::stop(void)
{
   return state(_far_side) == stop_state;
}

Source.cpp

#include <iostream>

#include <string>
#include "cargo.h"
#include "river.h"
using namespace std;

int main()
{
   River river;
   river.show();
   do
   {
       int cargo = Cargo::read();
       river.cross(cargo);
       river.show();
   }
   while(!river.stop());

   system("pause");
   return 0;
}

I really need help here thank you QUESTION Replace the tables and linked lists with binary search trees. Cargo.h #pragma once #include #include #include using namespace std; class Cargo { public:    static int read(void);    static int charToCargo(char);    static char cargoToChar(int);    static bool isCargo(char);    static const int BADCARGO = -1;    static const int CABBAGES = 0;    static const int GOAT = 1;    static const int SHOWMAN = 2;    static const int WOLF = 3;    static const int NOCARGO = 4;    static const int CARGO_AND_SHOWMAN = 5; }; River.h #pragma once //#include #include "cargo.h" class River { public:    River(void);    void show(void);    void cross(int);    bool stop(void); private:    struct Transition { unsigned near_side_state; unsigned far_side_state; };    static const unsigned start_state = 0;    static const unsigned stop_state = 15;    bool _far_side[Cargo::CARGO_AND_SHOWMAN];    bool _near_side[Cargo::CARGO_AND_SHOWMAN];    void copy(bool[], bool[]);    unsigned state(bool[]);    bool goodCrossing(unsigned near_side_state, unsigned far_side_state); }; Cargo.cpp #include "Cargo.h" int Cargo::charToCargo(char cargo) {    switch(cargo)    {    case 'w': case 'W':        return WOLF;    case 's': case 'S':        return SHOWMAN;    case 'g': case 'G':        return GOAT;    case 'c': case 'C':        return CABBAGES;    default:        return NOCARGO;    } } char Cargo::cargoToChar(int cargo) {    switch(cargo)    {    case WOLF:        return 'W';    case SHOWMAN:        return 'S';    case GOAT:        return 'G';    case CABBAGES:        return 'C';    case NOCARGO:        return 'N';    default:        return 'B';    } } bool Cargo::isCargo(char candidate) {    switch(candidate)    {    case 'w': case 'W':    case 's': case 'S':    case 'g': case 'G':    case 'C': case 'c':    case 'N': case 'n':        return true;    default:        return false;    } } int Cargo::read(void) {    char cargo;    do    {        cout << "Enter W for Wolf" << endl;        cout << "Enter N for No Cargo" << endl;        cout << "Enter G for Goat" << endl;        cout << "Enter C for Cabbages" << endl;        cout << "Enter cargo - ";        cin.get(cargo);        cin.ignore(numeric_limits::max(), ' ');    }    while(!isCargo(cargo));    return charToCargo(cargo); } River.cpp #include "River.h" River::River(void) {    for (int pos = Cargo::CABBAGES; pos <= Cargo::WOLF; pos++)        _far_side[pos] = false; } void River::show(void) {    for(int pos = Cargo::CABBAGES; pos <= Cargo::WOLF; pos++)        if(_far_side[pos])            cout << "- | " << Cargo::cargoToChar(pos) << endl;        else            cout << Cargo::cargoToChar(pos) << " | -" << endl;    cout << endl; } void River::cross(int cargo) {    static const unsigned n_transitions = 7;    static Transition badtransitions[] =    { { 0,4 },{ 0,5 },{ 0,12 },    { 1,5 },    { 7,3 },    { 8,12 },    { 14,10 }};    static string messages[] = {"No! The goat will eat cabbage and wolf will eat goat.",                                "No! The wolf will eat the goat.",        "No! The goat will eat the cabbage.",        "No! The wolf will eat the goat.",                                "No! The goat will eat the cabbage.",                                "No! The goat will eat the cabbage.",                                "No! The wolf will eat the goat.",                                "No! The goat will eat the cabbage.",                                "No! The wolf will eat the goat.",                                "No! The goat will eat the cabbage and wolf will eat the goat.",    "No! Doing this is impossible."};    copy(_far_side, _near_side);    _far_side[Cargo::SHOWMAN] = !_near_side[Cargo::SHOWMAN];    _far_side[cargo] = !_near_side[cargo];    unsigned near_side_state = state(_near_side);    unsigned far_side_state = state(_far_side);    if (!goodCrossing(near_side_state, far_side_state))    {        copy(_near_side, _far_side);        int id;        for(id = 0; id < n_transitions; id++)            if(badtransitions[id].near_side_state == near_side_state && badtransitions[id].far_side_state == far_side_state)                break;        cout << endl << messages[id] << endl << endl;    } } void River::copy(bool source[], bool sink[]) {    for(int pos = Cargo::CABBAGES; pos <= Cargo::WOLF; pos++)        sink[pos] = source[pos]; } unsigned River::state(bool river[]) {    unsigned _state = 0;    for(unsigned pos = Cargo::CABBAGES; pos <= Cargo::WOLF; pos++)        _state = _state + river[pos] * (int)pow(2, pos);    return _state; } bool River::goodCrossing(unsigned near_side_state, unsigned far_side_state) {    static const unsigned n_transitions = 20;    static Transition transitions[] =    { {0,6},    {1,7},{1,13},    {2,6},{2,7},{2,14},    {6,0},{6,2},    {7,1},{7,2},    {8,13},{8,14},    {9,13},{9,15},    {13,1},{13,8},{13,9},    {14,2},{14,8},    {15,9} };    unsigned id;    for (id = 0; id < n_transitions; id++)        if (transitions[id].near_side_state == near_side_state && transitions[id].far_side_state == far_side_state)            return true;    return false; } bool River::stop(void) {    return state(_far_side) == stop_state; } Source.cpp #include #include #include "cargo.h" #include "river.h" using namespace std; int main() {    River river;    river.show();    do    {        int cargo = Cargo::read();        river.cross(cargo);        river.show();    }    while(!river.stop());    system("pause");    return 0; }

Explanation / Answer

Cargo.h

#pragma once

#include <iostream>
#include <limits.h>
#include <string>
using namespace std;

class Cargo
{
public:
   static int read(void);
   static int charToCargo(char);
   static char cargoToChar(int);
   static bool isCargo(char);

   static const int BADCARGO = -1;
   static const int CABBAGES = 0;
   static const int GOAT = 1;
   static const int SHOWMAN = 2;
   static const int WOLF = 3;
   static const int NOCARGO = 4;
   static const int CARGO_AND_SHOWMAN = 5;
};

River.h

#pragma once
//#include <math.h>
#include "cargo.h"

class River
{
public:
   River(void);
   void show(void);
   void cross(int);
   bool stop(void);

private:
   struct Transition { unsigned near_side_state; unsigned far_side_state; };

   static const unsigned start_state = 0;
   static const unsigned stop_state = 15;
   bool _far_side[Cargo::CARGO_AND_SHOWMAN];
   bool _near_side[Cargo::CARGO_AND_SHOWMAN];
   void copy(bool[], bool[]);
   unsigned state(bool[]);
   bool goodCrossing(unsigned near_side_state, unsigned far_side_state);
};

Cargo.cpp

#include "Cargo.h"

int Cargo::charToCargo(char cargo)
{
   switch(cargo)
   {
   case 'w': case 'W':
       return WOLF;
   case 's': case 'S':
       return SHOWMAN;
   case 'g': case 'G':
       return GOAT;
   case 'c': case 'C':
       return CABBAGES;
   default:
       return NOCARGO;
   }
}
char Cargo::cargoToChar(int cargo)
{
   switch(cargo)
   {
   case WOLF:
       return 'W';
   case SHOWMAN:
       return 'S';
   case GOAT:
       return 'G';
   case CABBAGES:
       return 'C';
   case NOCARGO:
       return 'N';
   default:
       return 'B';
   }
}
bool Cargo::isCargo(char candidate)
{
   switch(candidate)
   {
   case 'w': case 'W':
   case 's': case 'S':
   case 'g': case 'G':
   case 'C': case 'c':
   case 'N': case 'n':
       return true;
   default:
       return false;
   }
}
int Cargo::read(void)
{
   char cargo;
   do
   {
       cout << "Enter W for Wolf"<< endl;
       cout << "Enter N for No Cargo" << endl;
       cout << "Enter G for Goat"<< endl;
       cout << "Enter C for Cabbages" << endl;
       cout << "Enter cargo - ";
       cin.get(cargo);
       cin.ignore(numeric_limits<int>::max(), ' ');
   }
   while(!isCargo(cargo));
   return charToCargo(cargo);
}

River.cpp

#include "River.h"

River::River(void)
{
   for (int pos = Cargo::CABBAGES; pos <= Cargo::WOLF; pos++)
       _far_side[pos] = false;
}
void River::show(void)
{
   for(int pos = Cargo::CABBAGES; pos <= Cargo::WOLF; pos++)
       if(_far_side[pos])
           cout << "- | " << Cargo::cargoToChar(pos) << endl;
       else
           cout << Cargo::cargoToChar(pos) << " | -" << endl;
   cout << endl;
}
void River::cross(int cargo)
{
   static const unsigned n_transitions = 7;
   static Transition badtransitions[] =
   { { 0,4 },{ 0,5 },{ 0,12 },
   { 1,5 },
   { 7,3 },
   { 8,12 },
   { 14,10 }};
   static string messages[] = {"No! The goat will eat cabbage and wolf will eat goat.",
                               "No! The wolf will eat the goat.",
       "No! The goat will eat the cabbage.",
       "No! The wolf will eat the goat.",
                               "No! The goat will eat the cabbage.",
                               "No! The goat will eat the cabbage.",
                               "No! The wolf will eat the goat.",
                               "No! The goat will eat the cabbage.",
                               "No! The wolf will eat the goat.",
                               "No! The goat will eat the cabbage and wolf will eat the goat.",
   "No! Doing this is impossible."};
   copy(_far_side, _near_side);
   _far_side[Cargo::SHOWMAN] = !_near_side[Cargo::SHOWMAN];
   _far_side[cargo] = !_near_side[cargo];
   unsigned near_side_state = state(_near_side);
   unsigned far_side_state = state(_far_side);

   if (!goodCrossing(near_side_state, far_side_state))
   {
       copy(_near_side, _far_side);
       int id;
       for(id = 0; id < n_transitions; id++)
           if(badtransitions[id].near_side_state == near_side_state && badtransitions[id].far_side_state == far_side_state)
               break;
       cout << endl << messages[id] << endl << endl;
   }
}
void River::copy(bool source[], bool sink[])
{
   for(int pos = Cargo::CABBAGES; pos <= Cargo::WOLF; pos++)
       sink[pos] = source[pos];
}
unsigned River::state(bool river[])
{
   unsigned _state = 0;
   for(unsigned pos = Cargo::CABBAGES; pos <= Cargo::WOLF; pos++)
       _state = _state + river[pos] * (int)pow(2, pos);
   return _state;
}
bool River::goodCrossing(unsigned near_side_state, unsigned far_side_state)
{
   static const unsigned n_transitions = 20;
   static Transition transitions[] =
   { {0,6},
   {1,7},{1,13},
   {2,6},{2,7},{2,14},
   {6,0},{6,2},
   {7,1},{7,2},
   {8,13},{8,14},
   {9,13},{9,15},
   {13,1},{13,8},{13,9},
   {14,2},{14,8},
   {15,9} };

   unsigned id;
   for (id = 0; id < n_transitions; id++)
       if (transitions[id].near_side_state == near_side_state && transitions[id].far_side_state == far_side_state)
           return true;
   return false;
}
bool River::stop(void)
{
   return state(_far_side) == stop_state;
}

Source.cpp

#include <iostream>

#include <string>
#include "cargo.h"
#include "river.h"
using namespace std;

int main()
{
   River river;
   river.show();
   do
   {
       int cargo = Cargo::read();
       river.cross(cargo);
       river.show();
   }
   while(!river.stop());

   system("pause");
   return 0;
}