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

in C++ In this assignment you will be using a BST to convert English characters

ID: 3680702 • Letter: I

Question

in C++

In this assignment you will be using a BST to convert English characters to Morse code strings. Morse code is a famous coding scheme that assigns a series of dots and dashes to each letter of the alphabet, each digit, and a few special characters. In sound-oriented systems, the dot represents a short sound and the dash represents a long sound. Other representations of dots and dashes are used with light-oriented systems and signal-flag systems (from Deitel and Deitel C How to Program).

1. (15 pts) Defining the BSTNode structure              

For the first part of the assignment, you should start by designing the BSTNode class for the BST. Create a class for the BSTNode data that will have as its members a character and a string. The character will hold the English text character, and the string will hold the corresponding Morse code characters for that English text character. You should also define left and right child pointers that point to BSTNode objects. You must have a constructor that accepts arguments to set the English text character and Morse code string.

Explanation / Answer

I have written a c++ code for the same:

working code:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <ctime>
#include <string>
#include <cctype>

using namespace std;

class BST
{
private:
struct Node
{
string letter;
string code;
Node *left;
Node *right;
};
Node *root;

public:
BST()
{
root = NULL;
}
void Insert(Node *&r, string letter, string code)
{
if(r == NULL)
{
r = new Node; r->letter = letter;
r->left = r->right = NULL; r->code = code;
}
if(r->letter > letter) Insert(r->left, letter, code);
if(r->letter < letter) Insert(r->right, letter, code);
}
void Insert(string letter, string code){Insert(root, letter, code);}
void DisplayPreOrder(Node *r)
{
if(r != NULL)
{
// (P)(LC)(RC)
cout << r->letter << " ";
DisplayInOrder(r->left);
DisplayInOrder(r->right);
}
}
void DisplayInOrder(Node *r)
{
if(r != NULL)
{
// (LC)(P)(RC)
DisplayInOrder(r->left);
cout << r->letter << " ";
DisplayInOrder(r->right);
}
}
// Overrides DisplayInOrder(Node * )
void DisplayInOrder(){DisplayInOrder(root);}
void DisplayPreOrder(){DisplayPreOrder(root);}
void DisplayPostOrder(Node *r)
{
if(r != NULL)
{
// (LC)(P)(RC)
DisplayPostOrder(r->left);
DisplayPostOrder(r->right);
cout << r->letter << " ";
}
}
void Encode(char x)
{
Node* r = SearchAndReturn(root, x);
if(r != NULL) cout << r->code;
else cout << "Error.";
}
void Decode(string x)
{
Node* r = SearchAndReturnString(root, x);
if(r->code == x) cout << r->letter;
else cout << r->code << " with x being " << x << endl; cout << "Error.";
}
Node* SearchAndReturn(Node *r, char x)
{
if(r != NULL)
{
if(r->letter[0] == x) {return r;}
else if(r->letter[0] > x) {SearchAndReturn(r->left, x);}
else {SearchAndReturn(r->right, x);}
}
else return NULL;
}
Node* SearchAndReturnString(Node *r, string x)
{
if(r != NULL)
{
if(r->code == x) {cout << r->code << " matches " << x << endl; return r;}
else if(r->code > x) {SearchAndReturnString(r->left, x);}
else {SearchAndReturnString(r->right, x);}
}
else return NULL;
}
};

struct alphaTree
{
string letter;
string code;
};

int main()
{
time_t u;
time(&u);
cout << ctime(&u) << endl;

BST t; string message; char* morseCode = new char; char ans;
alphaTree array[27] = {{"E", ". "}, {"T", "- "}, {"I", ".. "}, {"A", ".- "}, {"N", "-. "}, {"M", "-- "},
{"S", "... "}, {"U", "..- "}, {"R", ".-. "}, {"W", ".-- "}, {"D", "-.. "}, {"K", "-.- "},
{"G", "--. "}, {"O", "--- "}, {"H", ".... "}, {"V", "...- "}, {"F", "..-. "}, {"L", ".-.. "},
{"P", ".--. "}, {"J", ".--- "}, {"B", "-... "}, {"X", "-..- "},
{"C", "-.-. "}, {"Y", "-.-- "}, {"Z", "--.. "}, {"Q", "--.- "}, {" ", "---- "}};

t.Insert("0", "");

for(int i = 0; i < 27; i++)
{
t.Insert(array[i].letter, array[i].code);
}

do
{
cout << "Enter your message: ";
getline(cin, message);
int len = message.length();

for(int i = 0; i < len; i++)
{
message[i] = toupper(message[i]);
char c = message[i];
if(c == ' ')
cout << "/ ";
t.Encode(c);
}
cout << endl;

cout << "Enter your Morse code, separated by /, ended by *: ";
cin.getline(morseCode, 100, '*');
char* token = strtok(morseCode, "/");
while(token != NULL)
{
cout << endl << "Decoding: " << token << endl;
string newCode = token;
t.Decode(newCode);
token = strtok(NULL, "/");
}
cout << "Continue: ";
cin >> ans; cin.ignore(); cout << endl;
}while(ans == 'y');   
return 0;
}