This repository was archived by the owner on Jan 27, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathdriver.cpp
More file actions
208 lines (168 loc) · 7.72 KB
/
Copy pathdriver.cpp
File metadata and controls
208 lines (168 loc) · 7.72 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*-------------------------------------------------------------------------+
| Name: Jerome Richards |
| Course: CNS-2400 Section F01 |
| |
| Project: AVL Binary Search Tree (Prog-08) |
| File: driver.cpp |
| |
| Test the use of an AVL Binary Search Tree |
+-------------------------------------------------------------------------*/
#include <string>
#include <iostream>
#include <fstream>
#include "AVLTree.h"
using namespace std;
void dispPrologue();
bool openFile( ifstream& tmpFile );
bool readData( ifstream& tmpFile, char& tmpData );
void printInOrder( AVLTree* tmpTree );
void printChar( char );
/*-------------------------------------------------------------------------+
| main() |
| |
| establishes the programs environment by displaying the programs author |
| and purpose, as well as the data file to be processed. |
| |
| The program will terminate if a filename is not entered |
+-------------------------------------------------------------------------*/
int main( int argc, char* argv[] )
{
AVLTree* theTree = NULL;
ifstream inFile;
char tmpData;
dispPrologue();
while( openFile( inFile ) )
{
// create the AVL search tree
theTree = new(nothrow) AVLTree();
if( ! theTree )
{
cout << "ERROR: could not instantiate the AVLTree class. . .program exiting." << endl;
exit( -1 );
}
// add all unique items to the tree
while( ! inFile.eof() )
{
if( readData( inFile, tmpData ) )
{
cout << "inserting. . . " << tmpData;
if( ! theTree->insertNode( tmpData ) )
cout << ". . .unable to add node" << endl;
else
cout << endl;
}
}
// close up the file and clean workspace
inFile.close();
inFile.clear();
// display the tree's contents using IN-Order traversal
printInOrder( theTree );
// clean up the dynamically allocated memory (delete the lists)
delete theTree;
theTree = NULL;
dispPrologue();
}
system( "pause" );
return 0;
}
/*-------------------------------------------------------------------------+
| printInOrder( AVLTree* tmpTree ) |
| |
| Purpose: print the contents of each node using In-Order traversal |
| |
| Parameters: tmpTree is an AVLTree object |
| Pre-conditions: none |
| Post-conditions: none |
| Returns: none |
+-------------------------------------------------------------------------*/
void printInOrder( AVLTree* tmpTree )
{
cout << endl << endl;
cout << "Printing Binary Search Tree using IN-Order:" << endl;
tmpTree->inOrder( printChar );
cout << "end of tree...press ENTER to continue ";
cin.get();
}
/*-------------------------------------------------------------------------+
| printChar( char tmpData ) |
| |
| Purpose: print the passed character to the console |
| |
| Parameters: tmpData is a character |
| Pre-conditions: none |
| Post-conditions: none |
| Returns: none |
+-------------------------------------------------------------------------*/
void printChar( char tmpData )
{
cout << tmpData << endl;
}
/*-------------------------------------------------------------------------+
| readData( ifstream& tmpFile, char& tmpData ) |
| |
| Purpose: read a character from the data file |
| |
| Parameters: tmpFile is the data stream, tmpData recieves the data |
| Pre-conditions: none |
| Post-conditions: tmpData is modified to contain the data read |
| Returns: true if successful, false otherwise |
+-------------------------------------------------------------------------*/
bool readData( ifstream& tmpFile, char& tmpData )
{
bool lOkay = false;
tmpFile >> tmpData;
if( ! tmpFile.fail() )
lOkay = true;
return lOkay;
}
/*-------------------------------------------------------------------------+
| openFile( ifstream& tmpFile ) |
| |
| Purpose: open a user specified data file |
| |
| Parameters: tmpFile is the data stream to open |
| Pre-conditions: none |
| Post-conditions: tmpFile will be opened if a file is found |
| Returns: true if successful, false otherwise |
+-------------------------------------------------------------------------*/
bool openFile( ifstream& tmpFile )
{
bool lOpen = false;
string fileName;
while( ! lOpen )
{
cout << "Enter file name (blank to exit): ";
getline( cin, fileName );
if( ! fileName.length() )
{
cout << endl << endl << "Press ENTER to exit program.";
cin.get();
exit( 0 );
}
tmpFile.open( fileName.c_str() );
if( !(lOpen = tmpFile.good()) )
{
cout << endl << "ERROR: could not open data file " << fileName << endl;
tmpFile.clear();
}
}
return lOpen;
}
/*-------------------------------------------------------------------------+
| dispPrologue() |
| |
| Purpose: display author and program |
| |
| Parameters: none |
| Pre-conditions: none |
| Post-conditions: the screen is cleared, and the prologue is displayed |
| Returns: none |
+-------------------------------------------------------------------------*/
void dispPrologue()
{
system( "cls" );
cout << " Name: Jerome Richards" << endl;
cout << " Course: CNS-2400 Section F01" << endl;
cout << "Project: AVL Search Tree (prog-07)" << endl;
cout << endl << endl;
}