-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.java
391 lines (296 loc) · 9.52 KB
/
tree.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
class Tree{
private String dataFirst;
private String dataLast;
private String dataEmail;
private String dataPhone;
private Tree childLeft;
private Tree childRight;
public Tree(){
this.setThis("", "", "", "", null, null);
}
public Tree(String firstName, String lastName){
this.setThis(firstName, lastName, "", "", null, null);
}
public Tree(String firstName, String lastName, String email, String phoneNum){
this.setThis(firstName, lastName, email, phoneNum, null, null);
}
public Tree(String firstName, String lastName, String email, String phoneNum,
Tree leftTree, Tree rightTree){
this.setThis(firstName, lastName, email, phoneNum, leftTree, rightTree);
}
public Tree(Tree t){
this.setThis(t.getFirstName(), t.getLastName(), t.getEmail(), t.getPhoneNum(), t.getRightNode(), t.getLeftNode());
}
private void setThis(String firstName, String lastName, String email, String phoneNum,
Tree leftTree, Tree rightTree){
/* Object warppers for params needs to be parsed before throwing to wrapper
this.dataFirst = new String( firstName);
this.dataLast = new String( lastName);
this.dataEmail= new String( email);
this.dataPhone = new String( phoneNum);
this.childLeft = new Tree(leftTree);
this.childRight = new Tree(rightTree);
*/
this.dataFirst = firstName;
this.dataLast = lastName;
this.dataEmail= email;
this.dataPhone = phoneNum;
this.childLeft = leftTree;
this.childRight = rightTree;
}
private String hash(){
String hash = new String("");
String[] vals = new String[4] ;
vals[0] = this.getLastName();
vals[1] = this.getFirstName();
vals[2] = this.getPhoneNum();
vals[3] = this.getEmail();
for(String val:vals ){
if ( !val.isEmpty())
hash = hash + val.toUpperCase();
}
return hash;
}
public boolean insert(Tree t){
/* Function to add a given tree to the current tree
@param: Tree t be a valid tree
@returns: Boolean signifying sucessful insertion
*/
String thisHash, tHash;
thisHash = this.hash();
tHash = t.hash();
int checkLength = thisHash.length();
if( checkLength > tHash.length() )
checkLength = tHash.length();
//RN Interate through smallest string and check for precedence
if(thisHash.isEmpty()){
this.setThis(t.getFirstName(), t.getLastName(), t.getEmail(), t.getPhoneNum(), t.getRightNode(), t.getLeftNode());
return true;
}
else if(thisHash == tHash){
System.out.println("Duplicate node");
return false;
}
for ( int i=0; i<checkLength; i++){
if(thisHash.charAt(i) < tHash.charAt(i) && this.getRightNode() == null ){
return this.setRightNode(t);
}
else if(thisHash.charAt(i) < tHash.charAt(i) && this.getRightNode() != null ){
return this.getRightNode().insert(t);
}
else if(thisHash.charAt(i) > tHash.charAt(i) && this.getLeftNode() == null ){
return this.setLeftNode(t);
}
else if(thisHash.charAt(i) > tHash.charAt(i) && this.getLeftNode() != null ){
return this.getLeftNode().insert(t);
}
}
System.out.println("Didnt insert node failed tests ");
System.out.println("In method for: " + this.getName());
System.out.println("checkLength: " + checkLength);
System.out.println("thisHash: " + thisHash + " thash: "+tHash);
return false;
}
public boolean add(String firstName, String lastName, String email, String phoneNum){
/* Function to add a given tree to the current tree, allows a param argument instead
of a tree object
@param: Tree t be a valid tree
@returns: Boolean signifying sucessful insertion
*/
//RN Check to see if node exists
boolean result = this.insert(new Tree(firstName, lastName, email, phoneNum) );
if( result ) {
//RN Node doesn't exist
System.out.println("Added new node for: "+ firstName);
return true;
}
else{
return false;
}
}
public boolean nameDelete(String firstname, String lastName){
/* Function to delete a given node in the the current tree
@param: Tree t be a valid tree
@returns: Boolean signifying sucessful insertion
*/
Tree t= this.lookUp(new Tree(firstname, lastName));
if ( t!= null){
//RN Node doesn't exist
System.out.println("Deleting node for "+ firstname + " "+lastName);
return this.remove(t);
}
else{
System.out.println("Node not deleted for "+ firstname + " "+lastName );
return false;
}
}
public boolean remove(Tree t){
String compareHash = this.hash();
String checkHash = t.hash();
//RN Case 1: root node needs to be removed
//RN Case 2:
//RN Case 3
if( compareHash.startsWith( checkHash) ){
if(this.getLeftNode() != null && this.getRightNode() !=null){
//RN If 2 children
//RN Do a comlplicated shifteroo
//RN have to get parent and set it to null
this.setThis( this.lastLeftNode().getFirstName(), this.lastLeftNode().getLastName(),
this.lastLeftNode().getEmail(), this.lastLeftNode().getPhoneNum(), this.lastLeftNode().getRightNode(),
this.lastLeftNode().getRightNode());
this.lastLeftNode().setThis("", "", "", "", null, null);
return true;
}
else if(this.getLeftNode() != null && this.getRightNode() == null){
//RN If only left child ;
//RN replace this w/ left node
this.setThis( this.getLeftNode().getFirstName(), this.getLeftNode().getLastName(),
this.getLeftNode().getEmail(), this.getLeftNode().getPhoneNum(), this.getLeftNode().getLeftNode(),
null);
this.setLeftNode(null);
return true;
}
else if(this.getLeftNode() == null && this.getRightNode() != null){
//RN If only right child ;
//RN replace this w/ right node
this.setThis( this.getRightNode().lastLeftNode().getFirstName(), this.getRightNode().lastLeftNode().getLastName(),
this.getRightNode().lastLeftNode().getEmail(), this.getRightNode().lastLeftNode().getPhoneNum(), this.getRightNode(),
this.getRightNode().lastLeftNode().getRightNode());
this.getRightNode().setRightNode(null);
return true;
}
else if(this.getRightNode() == null & this.getLeftNode() == null){
//RN If no children;
//RN just kill this node, it'll get overwritten when inserting
this.setThis("","","","", null, null);
return true;
}
}
String thisHash, tHash;
thisHash = this.hash();
tHash = t.hash();
int checkLength = thisHash.length();
//RN Interate through smallest string and check for precedence
if( checkLength > tHash.length() )
checkLength = tHash.length();
for ( int i=0; i<checkLength; i++){
if(thisHash.charAt(i) < tHash.charAt(i) && this.getRightNode() != null ){
return this.getRightNode().remove(t);
}
else if(thisHash.charAt(i) > tHash.charAt(i) && this.getLeftNode() != null ){
return this.getLeftNode().remove(t);
}
}
System.out.println("Coulnd't find for delete: "+t.getName());
return false;
}
public void print(){
System.out.println("First: "+ this.getFirstName() );
System.out.println("Last: "+ this.getLastName() );
System.out.println("Email: "+ this.getEmail() );
System.out.println("Phone: "+ this.getPhoneNum() );
}
public Tree getRightNode(){
return this.childRight;
}
public Tree getLeftNode(){
return this.childLeft;
}
public boolean setRightNode(Tree rNode){
if (this.childRight == null){
this.childRight = rNode;
return true;
}
else {
return false;
}
}
public boolean setLeftNode(Tree lNode){
if (this.childLeft == null){
this.childLeft = lNode;
return true;
}
else{
return false;
}
}
public void nameLookUp(String firstName, String lastName){
Tree result = this.lookUp ( new Tree( firstName, lastName) );
if( result !=null){
System.out.println("Found Match for: "+ firstName);
result.print();
}
else
System.out.println("No match found for : " + firstName);
}
public Tree lookUp(Tree t){
String thisHash, tHash;
thisHash = this.hash();
tHash = t.hash();
int checkLength = thisHash.length();
if( checkLength > tHash.length() )
checkLength = tHash.length();
//RN Full hash match
if(thisHash == tHash){
return this;
}
//RN Have to allow a partial string serach to accomodate first naem lookups
else if(thisHash.startsWith(tHash) ){
return this;
}
else if(thisHash.isEmpty() || tHash.isEmpty()){
return null;
}
for ( int i=0; i<=checkLength; i++){
if( thisHash.charAt(i) < tHash.charAt(i) && !thisHash.isEmpty() && !tHash.isEmpty() && this.getRightNode() != null ){
return this.childRight.lookUp(t);
}
else if( thisHash.charAt(i) > tHash.charAt(i) && !thisHash.isEmpty() && !tHash.isEmpty() && this.getLeftNode() != null){
return this.childLeft.lookUp(t);
}
if( thisHash.charAt(i) < tHash.charAt(i) && this.getRightNode() == null){
return null;
}
else if( thisHash.charAt(i) > tHash.charAt(i) && this.getLeftNode() == null ){
return null;
}
}
return null;
}
private Tree lastRightNode(){
if( this.getRightNode() == null)
return this;
else
return this.getRightNode().lastRightNode();
}
private Tree lastLeftNode(){
if( this.getLeftNode() == null)
return this;
else
return this.getLeftNode().lastLeftNode();
}
public String getFirstName(){
return this.dataFirst;
}
public String getLastName(){
return this.dataLast;
}
public String getName(){
return this.dataFirst+" "+ this.dataLast;
}
public String getPhoneNum(){
return this.dataPhone;
}
public String getEmail(){
return this.dataEmail;
}
public void printAll(){
if(this.getLeftNode() != null){
this.getLeftNode().printAll();
}
this.print();
if(this.getRightNode()!=null){
this.getRightNode().printAll();
}
}
}