-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBST_ClassMain.java
405 lines (307 loc) · 12.4 KB
/
BST_ClassMain.java
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package p8_package;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class BST_ClassMain
{
public static final char COMMA = ',';
public static final char SEMICOLON = ';';
public static final char SPACE = ' ';
public static final char MINUS_SIGN = '-';
public static final int MAX_INPUT_CHARS = 80;
public static final int EOF_MARKER = -1;
private static FileReader fileIn;
private static final boolean SHOW_INPUT = false;
public static void main(String[] args)
{
StateData_BST_Class testClass, copiedClass;
testClass = uploadData( "inData.txt" );
System.out.println( "\nInorder Display:" );
testClass.displayTree( StateData_BST_Class.IN_TRAVERSE );
System.out.println( "\nPreorder Display:" );
testClass.displayTree( StateData_BST_Class.PRE_TRAVERSE );
System.out.println( "\nPostorder Display:" );
testClass.displayTree( StateData_BST_Class.POST_TRAVERSE );
System.out.println( "\nRemoving North Carolina" );
removeItem( testClass, "North Carolina" );
System.out.println( "\nInorder Display:" );
testClass.displayTree( StateData_BST_Class.IN_TRAVERSE );
System.out.println( "\nRemoving Alabama" );
removeItem( testClass, "Alabama" );
System.out.println( "\nInorder Display:" );
testClass.displayTree( StateData_BST_Class.IN_TRAVERSE );
System.out.println( "\nRemoving Wyoming" );
removeItem( testClass, "Wyoming" );
System.out.println( "\nInorder Display:" );
testClass.displayTree( StateData_BST_Class.IN_TRAVERSE );
System.out.println( "\nRunning Copy Constructor" );
copiedClass = new StateData_BST_Class( testClass );
System.out.println( "\nCopy Constructor Test:" );
copiedClass.displayTree( StateData_BST_Class.IN_TRAVERSE );
}
public static StateDataClass removeItem( StateData_BST_Class bst,
String stateName )
{
return bst.removeItem( new StateDataClass( stateName, "",
0, 0.0, 0, 0 ) );
}
public static StateDataClass searchForItem( StateData_BST_Class bst,
String stateName )
{
return bst.search( new StateDataClass( stateName, "", 0, 0.0, 0, 0 ) );
}
/**
* uploads data from requested file
*
* @param fileName name of file to access
*
* @return Boolean result of data upload
*/
private static StateData_BST_Class uploadData( String fileName )
{
String stateName, stateInit;
int numInst, lowTemp, highTemp;
double avgTemp;
StateDataClass dataItem;
StateData_BST_Class newData = new StateData_BST_Class();
try
{
// Open FileReader
fileIn = new FileReader( fileName );
// get first line - state name
stateName = getALine( MAX_INPUT_CHARS, COMMA );
while( compareStrings( stateName, "EndOfFile" ) != 0 )
{
// get state initials
stateInit = getALine( MAX_INPUT_CHARS, COMMA );
// get number of institutions
numInst = getAnInt( MAX_INPUT_CHARS );
// get average temperature
avgTemp = getADouble( MAX_INPUT_CHARS );
// get lowest temperature
lowTemp = getAnInt( MAX_INPUT_CHARS );
// get highest temperature
highTemp = getAnInt( MAX_INPUT_CHARS );
// add data to StateDataClass, and then to tree
dataItem = new StateDataClass( stateName, stateInit,
numInst, avgTemp, lowTemp, highTemp );
newData.insert( dataItem );
if( SHOW_INPUT )
{
System.out.println( dataItem.toString() );
}
stateName = getALine( MAX_INPUT_CHARS, COMMA );
}
}
catch( FileNotFoundException fnfe )
{
System.out.println( "DATA ACCESS ERROR: Failure to open input file" );
return null;
}
return newData;
}
/**
* gets a string up to a maximum length or to specified delimiter
*
* @param maxLength maximum length of input line
*
* @param delimiterChar delimiter character to stop input
*
* @return String captured from file
*/
private static String getALine( int maxLength, char delimiterChar )
{
int inCharInt;
int index = 0;
String strBuffer = "";
try
{
inCharInt = fileIn.read();
// consume leading spaces
while( index < maxLength && inCharInt <= (int)( SPACE ) )
{
if( inCharInt == EOF_MARKER )
{
return "EndOfFile";
}
index++;
inCharInt = fileIn.read();
}
while( index < maxLength && inCharInt != (int)( delimiterChar ) )
{
if( inCharInt >= (int)( SPACE ) )
{
strBuffer += (char)( inCharInt );
index++;
}
inCharInt = fileIn.read();
}
//inCharInt = fileIn.read();
}
catch( IOException ioe )
{
System.out.println( "INPUT ERROR: Failure to capture character" );
strBuffer = "";
}
return strBuffer;
}
/**
* gets an integer from the input string
*
* @param maxLength maximum length of characters
* input to capture the integer
*
* @return integer captured from file
*/
private static int getAnInt( int maxLength )
{
int inCharInt;
int index = 0;
String strBuffer = "";
int intValue;
boolean negativeFlag = false;
try
{
inCharInt = fileIn.read();
// clear space up to number
while( index < maxLength && !charInString( (char)inCharInt, "0123456789+-" ) )
{
inCharInt = fileIn.read();
index++;
}
if( inCharInt == MINUS_SIGN )
{
negativeFlag = true;
inCharInt = fileIn.read();
}
while( charInString( (char)inCharInt, "0123456789" ) )
{
strBuffer += (char)( inCharInt );
index++;
inCharInt = fileIn.read();
}
}
catch( IOException ioe )
{
System.out.println( "INPUT ERROR: Failure to capture character" );
strBuffer = "";
}
intValue = Integer.parseInt( strBuffer );
if( negativeFlag )
{
intValue *= -1;
}
return intValue;
}
/**
* gets an integer from the input string
*
* @param maxLength maximum length of characters
* input to capture the integer
*
* @return integer captured from file
*/
private static double getADouble( int maxLength )
{
int inCharInt;
int index = 0;
String strBuffer = "";
boolean negativeFlag = false;
double doubleValue;
try
{
inCharInt = fileIn.read();
// clear space up to number
while( index < maxLength && !charInString( (char)inCharInt, ".0123456789+-" ) )
{
inCharInt = fileIn.read();
index++;
}
if( inCharInt == MINUS_SIGN )
{
negativeFlag = true;
inCharInt = fileIn.read();
}
while( charInString( (char)inCharInt, ".0123456789" ) )
{
strBuffer += (char)( inCharInt );
index++;
inCharInt = fileIn.read();
}
}
catch( IOException ioe )
{
System.out.println( "INPUT ERROR: Failure to capture character" );
strBuffer = "";
}
doubleValue = Double.parseDouble( strBuffer );
if( negativeFlag )
{
doubleValue *= -1;
}
return doubleValue;
}
/**
* compares two strings without consideration for case
*
* @param oneString one of the strings to be tested
*
* @param otherString other string to be tested
*
* @return first greater than second: greater than zero;
* first equal to second: equals zero
* first less than second: less than zero
*/
public static int compareStrings( String oneString, String otherString )
{
int difference, index = 0;
while( index < oneString.length() && index < otherString.length() )
{
difference = toLower( oneString.charAt( index ) )
- toLower( otherString.charAt( index ) );
if( difference != 0 )
{
return difference;
}
index++;
}
return oneString.length() - otherString.length();
}
/**
* tests and reports if a character is found in a given string
*
* @param testChar character to be tested against the string
*
* @param testString string within which the character is tested
*
* @return Boolean result of test
*/
private static boolean charInString( char testChar, String testString )
{
int index;
for( index = 0; index < testString.length(); index++ )
{
if( testChar == testString.charAt( index ) )
{
return true;
}
}
return false;
}
/**
* changes upper case letter to a lower case letter
*
* @param inChar letter to be tested and potentially modified
*
* @return lower case letter if upper case letter is input;
* otherwise character is returned unchanged
*/
private static char toLower( char inChar )
{
if( inChar >= 'A' && inChar <= 'Z' )
{
return (char)( inChar - 'A' + 'a' );
}
return inChar;
}
}