-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStateData_BST_Class.java
360 lines (286 loc) · 8.79 KB
/
StateData_BST_Class.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
package p8_package;
public class StateData_BST_Class
{
private int displayCounter;
static int IN_TRAVERSE = 102;
static int POST_TRAVERSE = 103;
static int PRE_TRAVERSE = 101;
private StateDataClass rootNode;
StateData_BST_Class()
{
this.rootNode = null;
displayCounter = 0;
}
StateData_BST_Class(StateData_BST_Class copied)
{
this.rootNode = copyConstructorHelper(copied.rootNode);
}
void clearTree()
{
this.rootNode = null;
}
int compareStrings(String one, String other)
{
//If first string parameter is greater than the second, method returns positive value
//if first string parameter is less than second, return negative value;
//if first string parameter and second string parameter are equal, returns zero
int lengthMinimum = Math.min(one.length(), other.length());
int strOneChar, strTwoChar;
int index;
//this part lets us know if strOne is greater than strTwo if its a positive or negative number
for(index = 0; index < lengthMinimum; index++ )
{
strOneChar = (int)one.charAt(index);
strTwoChar = (int)other.charAt(index);
if(strOneChar != strTwoChar )
{
return strOneChar - strTwoChar;
}
}
//check to see if part of word is in other word.
return one.length() - other.length();
}
private StateDataClass copyConstructorHelper(StateDataClass wkgRef)
{
//Copy constructor helper
//Recursively copies nodes using pre order traversal
if(wkgRef != null)
{
StateDataClass node = wkgRef;
node.leftChildRef = copyConstructorHelper(wkgRef.leftChildRef);
node.rightChildRef = copyConstructorHelper(wkgRef.rightChildRef);
//check left
return node;
}
return null;
}
private void displayInOrder(StateDataClass wkgRef)
{
//Provides inOrder traversal action using recursion
// In order means go left, print parent, then go right
// The method only needs about a half dozen lines all together
// In order means go left
if(wkgRef != null)
{
displayInOrder(wkgRef.leftChildRef);
//print parent
displayCounter++;
System.out.print(displayCounter + ": ");
System.out.println(wkgRef.toString());
//go right
displayInOrder(wkgRef.rightChildRef);
}
}
private void displayPostOrder(StateDataClass wkgRef)
{
if(wkgRef != null)
{
displayPostOrder(wkgRef.leftChildRef);
displayPostOrder(wkgRef.rightChildRef);
displayCounter++;
System.out.print(displayCounter + ": ");
System.out.println(wkgRef.toString());
}
//Provides postOrder traversal action using recursion
}
private void displayPreOrder(StateDataClass wkgRef)
{
//Provides preOrder traversal action using recursion
if(wkgRef != null)
{
displayCounter++;
System.out.print(displayCounter + ": ");
System.out.println(wkgRef.toString());
displayPreOrder(wkgRef.leftChildRef);
displayPreOrder(wkgRef.rightChildRef);
}
}
public void displayTree(int traverseCode)
{
displayCounter =0;
if(traverseCode == IN_TRAVERSE)
{
displayInOrder(this.rootNode);
}
else if(traverseCode == POST_TRAVERSE)
{
displayPostOrder(this.rootNode);
}
else if(traverseCode == PRE_TRAVERSE)
{
displayPreOrder(this.rootNode);
}
}
public void insert(StateDataClass inData)
{
//Insert method for BST
//Note: calls the insert helper to implement all the data insertions
rootNode = insertHelper(rootNode, inData);
}
private StateDataClass insertHelper(StateDataClass wkgRef,
StateDataClass inData)
{
if(wkgRef != null)
{
int compareCheck = compareStrings(wkgRef.state, inData.state);
if(compareCheck > 0)
{
wkgRef.leftChildRef = insertHelper(wkgRef.leftChildRef, inData);
}
else if(compareCheck < 0)
{
wkgRef.rightChildRef = insertHelper(wkgRef.rightChildRef, inData);
}
else
{
wkgRef.setData(inData);
}
}
else
{
wkgRef = new StateDataClass(inData);
}
return wkgRef;
}
public boolean isEmpty()
{
//Test for empty tree
return rootNode == null;
}
private StateDataClass removeFromMin(StateDataClass parentRef,
StateDataClass childRef)
{
//Searches tree from given working reference to minimum value found below it in farthest left node,
//stores data value found, unlinks the found node,
//links the parent node's left node to the child's right node,
//and returns the child/found node to the calling method
if(parentRef.leftChildRef != null)
{
return removeFromMin(childRef, childRef.leftChildRef);
}
return parentRef.leftChildRef = childRef.rightChildRef;
}
public StateDataClass removeItem(StateDataClass outData)
{
//Removes data StateDataClass node from tree using given key
//Note: uses remove helper method
//Note: uses search initially to get value, if it is in tree;
//if value found, remove helper method is called, otherwise returns null
//call search, and get the value (outdata)
StateDataClass result = search(outData);
//check if search result was not null
if(result != null)
{
//assign the result reference to new node using result object
//call the remove helper, assign to rootnode
result = new StateDataClass(result);
rootNode = removeItemHelper(rootNode, result);
}
//end if
return result;
//return result
}
private StateDataClass removeItemHelper(StateDataClass wkgRef,
StateDataClass outData)
{
//Remove helper for BST remove action
//Note: Recursive method returns updated local root to maintain tree linkage
//Note: uses removeFromMin method
//shopping list one
if(wkgRef != null)
{
int key = compareStrings(wkgRef.state,outData.state);
//1) is the value less than current node
if(key > 0)
{
//-recurse left
wkgRef = removeItemHelper(wkgRef.leftChildRef, outData);
}
//is node greater than current node
else if(key < 0)
{
//-recurse right
wkgRef = removeItemHelper(wkgRef.rightChildRef, outData);
}
//we found it
else
{
//shopping list two
//2) is the node to left of current empty/null
if(wkgRef.leftChildRef == null)
{
//-return right node reference
wkgRef = wkgRef.rightChildRef;
}
//is the node to the right of current empty/ null
else if(wkgRef.rightChildRef == null)
{
//-return left node reference
wkgRef = wkgRef.leftChildRef;
}
//there are two children
else
{
//shopping list three
//3) is the right child of the current node's left child null/ empty
if(wkgRef.rightChildRef.leftChildRef != null)
{
//- take data from left child and replace the current node data
// You have to take the data out of this node and put it in the current node
wkgRef = removeFromMin(wkgRef, wkgRef.rightChildRef);
}
else
{
//- link from current node to the left child's left child.
// You have to take the data out of the right child and put it in the current node
wkgRef.setData(wkgRef.rightChildRef);
wkgRef.rightChildRef = wkgRef.rightChildRef.rightChildRef;
}
}
//There are two children
}
}
return wkgRef ;
//Node removal shopping list
//1) is the value less than current node
//-recurse left
//is node greater than current node
//-recurse right
//we found it
//2) is the node to left of current empty/null
//-return right node reference
//is the node to the right of current empty/ null
//-return left node reference
//there are two children
//3) is the right child of the current node's left child null/ empty
//- take data from left child and replace the current node data
//- link from current node to the left child's left child.
//otherwise, assume left child has a right child
}
public StateDataClass search(StateDataClass searchData)
{
//Searches for data in BST given StateDataClass referencewith necessary key
return searchHelper(rootNode, searchData);
}
private StateDataClass searchHelper(StateDataClass wkgRef,
StateDataClass searchData)
{
//Helper method for recursive BST search action
//case if ref is null or key is present
// check for root not null
if(wkgRef != null )
{
int key = compareStrings(wkgRef.state,searchData.state);
if(key > 0)
{
//search data's state is greater than wkgRef state
return searchHelper(wkgRef.leftChildRef, searchData);
}
if(key < 0)
{
return searchHelper(wkgRef.rightChildRef, searchData);
}
}
return wkgRef;
}
}