#include <iostream>
#include <fstream>
#include <string>
#include "treeNode.h"
using namespace std;
ifstream input;
ifstream conversion;
treeNode::tNode root;
treeNode::tNode tptr = new treeNode;
char searchTree(treeNode::tNode C, string key);
treeNode add(treeNode::tNode c, string morse, char key);
treeNode::tNode myNewNode(char key, string morseCode);
void PrintInOrder(treeNode::tNode c);
void PrintInPreOrder(treeNode::tNode c);
void PrintInPostOrder(treeNode::tNode c);
int main()
{
string codeToConvert;
char asciiChar;
string toConvert;
input.open("morseCodes.dat");
if(!input){
cout << "There was an error opening the file." << endl;
}
conversion.open("MorseCodemsg.dat");
if(!conversion){
cout << "There was an error opening the file." << endl;
}
while(input >> asciiChar >> codeToConvert){
add(root, codeToConvert, asciiChar);
}
while(conversion >> toConvert){
cout << searchTree(root, toConvert);
}
return 0;
}
treeNode add(treeNode::tNode c, string morse, char key){
treeNode *p;
p = NULL;
if(!c){
root = myNewNode(key, morse);
root->value = key;
root->morseCode = morse;
return *root;
}
while(c != NULL && c->value != key){
if(c->value < key){
p = c;
c = c->leftChild;
}
else{
p = c;
c = c->rightChild;
}
}
if(c != NULL){
return *c;
}
else{
c = myNewNode(key, morse);
}
if(p->value < key){
p->leftChild = c;
}
else{
p->rightChild = c;
}
return *c;
}
void PrintInOrder(treeNode::tNode c){
if(c == NULL){
return;
}
PrintInOrder(c->leftChild);
cout << c->value << " ";
PrintInOrder(c->rightChild);
}
void PrintInPreOrder(treeNode::tNode c){
if(c == NULL){
return;
}
cout << c->value << " ";
PrintInPreOrder(c->leftChild);
PrintInPreOrder(c->rightChild);
}
void PrintInPostOrder(treeNode::tNode c){
if(c == NULL){
return;
}
PrintInPostOrder(c->leftChild);
PrintInPostOrder(c->rightChild);
cout << c->value << " ";
}
treeNode::tNode myNewNode(char key, string morseCode){
treeNode *myRoot = new treeNode;
myRoot->value = key;
myRoot->morseCode = morseCode;
myRoot->leftChild = NULL;
myRoot->rightChild = NULL;
return myRoot;
}
char searchTree(treeNode::tNode c, string key){
if(c == NULL){
return NULL;
}
if(c->morseCode == key){
return c->value;
}
if(c->morseCode < key){
return searchTree(c->leftChild, key);
}
else if (c->morseCode > key){
return searchTree(c->rightChild, key);
}
cout << "Not found";
return 0;
}
Morse Code Tree not converting correctly
Page 1 of 17 Replies - 956 Views - Last Post: 13 April 2012 - 02:02 PM
#1
Morse Code Tree not converting correctly
Posted 12 April 2012 - 08:26 AM
I am having an issue with a morse code conversion program. The program is supposed to take a string of dots and dashes and convert them to ASCII characters, for some reason my conversion is only working on half of the tree. It is like h is the last ASCII character to convert, but I can print out the tree and all the nodes are there, here is my code, any pointers would be great, thanks.
Replies To: Morse Code Tree not converting correctly
#2
Re: Morse Code Tree not converting correctly
Posted 12 April 2012 - 08:40 AM
Please post your include file treeNode.h.
Jim
Jim
#3
Re: Morse Code Tree not converting correctly
Posted 12 April 2012 - 08:44 AM
jimblumberg, on 12 April 2012 - 09:40 AM, said:
Please post your include file treeNode.h.
Jim
Jim
Here is the include:
#include <iostream>
#include <string>
using namespace std;
class treeNode{
public:
typedef treeNode* tNode;
char value;
string morseCode;
tNode leftChild;
tNode rightChild;
treeNode();
};
and here is where I initialize the file:
#include "treeNode.h"
treeNode::treeNode(){
value = NULL;
morseCode = "";
leftChild = NULL;
rightChild = NULL;
}
#4
Re: Morse Code Tree not converting correctly
Posted 12 April 2012 - 08:59 AM
I don't see any glaring problems so please post a small sample of your input files.
Jim
Jim
#5
Re: Morse Code Tree not converting correctly
Posted 12 April 2012 - 09:07 AM
The morseCodes.dat file is formatted like this, all the way through z and then 1 through 0:
The morseCodeMsg.dat is layed out like so, with a single space being a new char and 3 spaces being an actual space in the message:
a .- b -... c -.-/>. d -.. e . f ..-. g --. h .... i .. j .--- k -.-/> l .-.. m -- n -.
The morseCodeMsg.dat is layed out like so, with a single space being a new char and 3 spaces being an actual space in the message:
- .- -..- .-.. --- - .-. . ..-. . .-. ... - --- -.-/> . . .--. .. -. --. ...
This post has been edited by adolf625: 12 April 2012 - 09:08 AM
#6
Re: Morse Code Tree not converting correctly
Posted 12 April 2012 - 10:13 AM
In your searchTree() function you are comparing c->MorseCode to your key, how do you know when MorseCode is equal to, greater than, or less than your key? Remember key is the Morse code symbol not the letter. You probably can not do a string search on the Morse code symbol and expect it to find the proper character.
Edit:
Jim
Edit:
Quote
One important feature of Morse code is coding efficiency. The length of each character in Morse is approximately inversely proportional to its frequency of occurrence in English. Thus, the most common letter in English, the letter "E," has the shortest code, a single dot.
Jim
This post has been edited by jimblumberg: 12 April 2012 - 10:20 AM
#7
Re: Morse Code Tree not converting correctly
Posted 13 April 2012 - 06:21 AM
If your Morse strings are in a sorted order then you should be able to find the proper letter. But they must be sorted. Are you sorting the strings before you insert them into the tree?
Jim
Jim
#8
Re: Morse Code Tree not converting correctly
Posted 13 April 2012 - 02:02 PM
They are sorted as they go into the tree, it is an ordered tree and the data is sorted from the root
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote



|