-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBST_Test_Driver.cpp
More file actions
110 lines (91 loc) · 3.43 KB
/
BST_Test_Driver.cpp
File metadata and controls
110 lines (91 loc) · 3.43 KB
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
#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <string>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <string>
#include "BST.h"
#include "WordPair.h"
#include "ElementAlreadyExistsInBSTException.h"
#include "ElementDoesNotExistInBSTException.h"
using namespace std;
void display(WordPair& anElement) {
cout << anElement.getEnglish() << ":" << anElement.getTranslation() << endl;
} // end of display
int main() {
BST<WordPair>* theTranslator = new BST<WordPair>();
string aLine = "";
string aWord = "";
string englishW = "";
string translationW = "";
string filename = "dataFile.txt";
string delimiter = ":";
size_t pos = 0;
WordPair translated;
ifstream myfile (filename);
if (myfile.is_open()) {
// cout << "Reading from a file:" << endl;
while (getline(myfile, aLine)) {
// cout << aLine << '\n'; // For debugging purposes
pos = aLine.find(delimiter);
englishW = aLine.substr(0, pos);
aLine.erase(0, pos + delimiter.length());
translationW = aLine;
//cout << "Read: " << englishW << ":" << translationW << endl; // For debugging purposes
WordPair aWordPair(englishW, translationW);
//cout << "Read: " << aWordPair.getEnglish() << ":" << aWordPair.getTranslation() << endl; // For debugging purposes
try {
theTranslator->insert(aWordPair);
}
catch (ElementAlreadyExistsInBSTException &anException) {
cout << anException.what() << " => " << aWordPair.getEnglish() << ":" << aWordPair.getTranslation()
<< endl;
}
}
myfile.close();
cout << endl;
WordPair pair = WordPair("cloud");
cout << "Test Retrieve for 'cloud': " << theTranslator->retrieve(pair).getTranslation() << endl;
cout << endl;
cout << "Element Count: " << theTranslator->getElementCount() << endl;
cout << endl;
cout << "Element Count: " << theTranslator->nodesCount() << endl;
cout << endl;
WordPair minElement;
try {
minElement = theTranslator->min();
cout << "Min Element: " << minElement.getEnglish() << ":" << minElement.getTranslation() << endl;
} catch(ElementDoesNotExistInBSTException& e) {
cout << e.what() << endl;
}
cout << endl;
WordPair maxElement;
try {
maxElement = theTranslator->max();
cout << "Max Element: " << maxElement.getEnglish() << ":" << maxElement.getTranslation() << endl;
} catch(ElementDoesNotExistInBSTException& e) {
cout << e.what() << endl;
}
cout << endl;
WordPair dupPair = WordPair("leaf");
cout << "Number of copies of 'leaf': " << theTranslator->duplicate(dupPair) << endl;
cout << endl;
cout << "Removing leaf..." << endl;
try {
theTranslator->remove(dupPair);
} catch(ElementDoesNotExistInBSTException& e) {
cout << e.what() << endl;
}
cout << "Number of copies of 'leaf' now: " << theTranslator->duplicate(dupPair) << endl;
cout << endl;
cout << "Test inOrderTraverse: " << endl;
cout << endl;
theTranslator->traverseInOrder(display);
} else {
cout << "Unable to open file";
}
return 0;
}