1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <algorithm>
#include <cstring>
#include <cassert>
#include <iomanip>
#include <vector> // a lot of these aren't needed!
#include <cstdio>
using namespace std;
class BST {
private:
struct node {
string code;
string letter;
node *left;
node *right;
};
node* head; // used to reference the top of the tree.
void Insert(node* head, string key, string letter);
void PrintInOrder(node *Ptr);
bool Search(node *head, string target);
public:
BST(void);
void Insert(string key, string letter);
void PrintInOrder(void);
bool Search(string target);
};
BST::BST(void) {
head = new node;
head->letter = "START";
head->code = "";
head->left = head->right = NULL;
void BST::Insert(string key, string letter) {
Insert(head, key, letter);
}
void BST::Insert(node* head, string key, string letter) {
if(!head || key.empty()) return;
if(key[0] == '.') {
if (head->left) {
head->letter = letter;
return;
}
if(!head->left) {
head->left = new node;
}
Insert(head->left, key.substr(1), letter);
} else if (key[0] == '-') {
if (head->right) {
head->letter = letter;
return;
}
if(!head->right) {
head->right = new node;
}
Insert(head->right, key.substr(1), letter);
}
}
void BST::PrintInOrder(void) {
if (head != nullptr) {
PrintInOrder(head);
}
}
void BST::PrintInOrder(node *Ptr) {
if(Ptr->left != nullptr) {
PrintInOrder(Ptr->left);
}
if(Ptr->right != nullptr) {
PrintInOrder(Ptr->right);
}
cout << Ptr->letter << endl;
}
bool BST::Search(string target) {
Search(head,target);
}
bool BST::Search(node *head, string target) {
if (head == NULL) {
return false;
} else if(head->code == target) {
return true;
} else if(target <= head->code) {
return Search(head->left,target);
} else {
return Search(head->right,target);
}
}
int main(int argc, char *argv[]) {
BST myTree;
std::string morseCode; // stores only the morse cod
std::string letter; // stors the letter corresponding to the morse code i.e E .
std::list<std::string> letterList; // HAVE to use string lists
std::list<std::string> morseCodeList;
if (argc < 2) {
printf("Please, enter the path to the file!");
return 1;
}
std::ifstream morseCodeFile(argv[1]);
if(morseCodeFile) {
while(std::getline(morseCodeFile, letter, ',') && std::getline(morseCodeFile, morseCode)) {
letterList.push_back(letter);
morseCodeList.push_back(morseCode);
}
for(std::list<std::string>::iterator letter=letterList.begin(), morse=morseCodeList.begin();(letter!=letterList.end())&&(morse!=morseCodeList.end());++letter, ++morse) {
myTree.Insert(*morse,*letter);
}
myTree.PrintInOrder();
if(myTree.Search("TEST23423") == true) { //produces the wrong thing
printf("FOUND THE MORSE CODE\n");
} else {
printf("NOT FOUND!!\n");
}
morseCodeFile.close();
} else {
printf("%s isn't a valid file!",argv[1]);
return 1;
}
return 0;
}
|