-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDigitTree.java~
More file actions
590 lines (515 loc) · 12.5 KB
/
DigitTree.java~
File metadata and controls
590 lines (515 loc) · 12.5 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
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
import java.util.Iterator;
import java.io.FileInputStream;
import java.util.Scanner;
/**
* DigitTree class creates a digit tree
* and allows users to munipulate
* the tree in certain ways
* <br><br>
* @author Allen McDermott
* @since 11/16/14
*/
public class DigitTree implements Iterable {
//variable root is the root of the Digit tree
node root;
//size is the size of the Digit tree
int size;
//nodeCount is the amount of nodes in the Digit tree
int nodeCount;
//String data was origannally going to be used to make a string of all
//the variables but then it would be inefficient and would make the
//Digit tree arbitrary but i decided not to delete it
String data;
/** DigitTree is the constructor for the DigitTree class
* it creates a new node for the root and sets size,nodeCount to 0
* and data to "" but data isn't used in this class
**/
public DigitTree()
{
node k = new node("",null);
root = k;
size = 0;
nodeCount = 0;
data="";
}
/**
* getRoot returns the root of the digitTree
* @return root of the Digit tree
* time function 1 - no matter how many elements are in the list it will
* take the same amount of time
* time complexity O(1)
**/
public node getRoot() {
return root;
}
/**
* getSize returns the size of the Digit tree
* @return size of the Digit tree type int
* time function 1 - no matter how many elements are in the list it will
* take the same amount of time
* time complexity O(1)
**/
public int getSize() {
return size;
}
/**
* clear clears the DigitTree by making a new one and setting all
* the variables to 0
**/
public void clear() {
node k = new node("",null);
root = k;
size = 0;
nodeCount = 0;
data="";
}
/**
* add adds a string to the digit tree
* adds calls contains to make sure that it doesn't add a
* string that is already in the tree
* also throws DigitFormatException if string contains non numeric values
* @param string to be added to tree
* @return boolean false if item was not added to the list true if successfull
* time complexity O(n) - n being the length of string s
**/
public boolean add(String s) throws DigitFormatException {
try{
if(contains(s)==true) {
return false;
}
}
catch(DigitFormatException e) {
throw new DigitFormatException();
}
data+=s+",";
node k;
k = root;
char c;
int i;
while(s.length()>0){
c = s.charAt(0);
i = Integer.parseInt(c+"");
if(k.getNext(i)!= null) {
k = k.getNext(i);
}
else {
k.setNext(c+"",k);
nodeCount++;
k = k.getNext(i);
}
s = s.substring(1);
}
k.setNext("k",k);
size++;
return true;
}
/**
* size returns size of digitTree
* @return size of digit tree
* time function 1 - no matter how many elements are in the list it will
* take the same amount of time
* time complexity O(1)
**/
public int size() {
return size;
}
/**
* nodeCount returns the amount of nodes in the digit tree
* @return number of nodes in the digit tree
* time function 1 - no matter how many elements are in the list it will
* take the same amount of time
* time complexity O(1)
**/
public int nodeCount() {
return nodeCount;
}
/**
* contains takes in a string and check if it is in the digit tree
* contains also throws DigitformatException if the string contains non numeric values
* @param s is string that will be checked
* @return boolean true if string s is in the tree false if not
* time complexity O(n) - n being the length of string s
*
**/
public boolean contains(String s) throws DigitFormatException {
node k;
k = root;
char c;
int i;
int iwaq=0;
c = s.charAt(0);
while(iwaq<10) {
if(c == Character.forDigit(iwaq,10)) {
iwaq=11;
}
iwaq++;
}
if(iwaq-1<10) {
throw new DigitFormatException();
}
i = Integer.parseInt(c+"");
if(k.getNext(i)==null) {
return false;
}
while(s.length()>0){
c = s.charAt(0);
iwaq=0;
while(iwaq<10) {
if(c == Character.forDigit(iwaq,10)) {
iwaq=11;
}
iwaq++;
}
if(iwaq-1<10) {
throw new DigitFormatException();
}
i = Integer.parseInt(c+"");
if(k.getNext(i)!=null) {
k = k.getNext(i);
}
else if(k.getNext(i)==null) {
return false;
}
s = s.substring(1);
}
if(k.getNext(10)!=null) {
return true;
}
return false;
}
/**
* delete takes in a string if that string is in the digit tree it will be removed
* delete also throws DigitFormatException if it tries to delete a string with a noon numeric value
* @param String s which will be deleted in the tree if its in the tree
* @return boolean false if item wasn't deleted, true if it was
* time complexity O(n) - n being the length of String s
**/
public boolean delete(String s) throws DigitFormatException{
try {
if(contains(s)==false) {
return false;
}
}
catch(DigitFormatException e){}
String[] qwap = data.split(",");
for(int ill = 0;ill<qwap.length;ill++) {
if(qwap[ill]==s) {
qwap[ill]="";
}
data+=qwap[ill]+",";
}
node k;
node temp;
k = root;
char c;
int i;
System.out.println("WTF");
while(s.length()>0){
c = s.charAt(0);
i = Integer.parseInt(c+"");
k=k.getNext(i);
s = s.substring(1);
}
System.out.println(k.getNext(10).getData());
k.setNode(10);
System.out.println(k.getParent().getData());
nodeCount--;
size--;
while(k.inList()==false&&k!=root) {
temp = k;
k = k.getParent();
temp = null;
nodeCount--;
}
return true;
}
/**
* sortList is not used in this class because it is ineffeciant and arbitrary
**/
public void sortList() {
String[] go = data.split(",");
boolean swap = true;
int j = 0;
while (swap) {
swap = false;
j++;
for (int i = 0; i < go.length - j; i++) {
if (go[i].compareTo(go[i + 1]) > 0) {
String tmp = go[i];
go[i] = go[i + 1];
go[i + 1] = tmp;
swap = true;
}
}
}
data="";
for(int i = 0; i<go.length;i++) {
data+=go[i]+",";
}
}
/**getListt uses to global variable that are only used for this method
* a is an array of Strings
* its is used to track the index of a when items ar being added to it
**/
String[] a;
int its;
/**
* getListt puts strings in digit tree to an array in
* a lexographic listing
* this method uses recursion to traverse threw the tree
* @param node k which is
* @return a string of the items in the digit tree
**/
public String getListt(node k,String s) {
if(k.inList()==false) {
return "";
}
if(k.getNext(10)!=null) {
a[its]=s+k.getData();
its++;
}
if(k.noK()==false) {
return "";
}
s+=k.getData();
int imp = 0;
while(imp<10) {
if(k.getNext(imp)!=null) {
getListt(k.getNext(imp),s);
}
imp++;
}
return "";
}
/**
* printList calls getListt and then outputs the items
* in lexographic order
* @return an array of the items in the tree in lexographic order
**/
public String[] printList() {
a = new String[size];
its = 0;
getListt(root,"");
for(int i = 0;i<a.length;i++) {
}
return a;
}
/**
* Iterator returns and iterator which will iterate threw the digit tree
* time complexity O(n) - n being the number of nodes
**/
public Iterator iterator() {
Iterator t = null;
System.out.println();
a = new String[size];
its = 0;
getListt(root,"");
for(int i = 0;i<a.length;i++) {
System.out.println(a[i]);
}
return t;
}
/**
* stringRec uses recursion to make a single string that represents all the items
* in the digit tree
* @param node k starts out as the root but is used for all the other nodes in the tree
* @param s string that will represent all the items in the tree
* @return string representing all the items in the tree
**/
public String stringRec(node k, String s) {
if(k==null) {
return "";
}
if(k.noK()==false) {
return "";
}
int ip = 0;
int imp = 0;
while(imp<10) {
if(k.getNext(imp)!=null) {
if(ip!=1) {
s+="("+k.getNext(imp).getData()+" ";
s = s+stringRec(k.getNext(imp),"");
ip++;
}
else {
s+= k.getNext(imp).getData()+" ";
s = s+stringRec(k.getNext(imp),"");
}
}
imp++;
}
s= s+")";
return s;
}
/**
* this method simply calls stringRec and returns the string created by
* stringRec
* @return string representing all the items in the tree
* time complexity O(n) - n being the number of nodes
**/
public String toString() {
return stringRec(root,"");
}
/**
* intersectRec uses to global variables only used by this method
* uni is an array strings
* kills is used to track the index's of uni
**/
String[] uni;
int kills;
/**
* intersectRec takes two digitTrees and puts all the items that are only in one of the
* trees and puts them into an array
* this method uses recursion
* @param k node of one of the digit trees
* @param n node of the other digit tree
* @param s is the item that is used to travers the tree
* @return string of the traversal threw the tree
**/
public String intersectRec(node k,node n, String s) {
if(k==null||n==null) {
return "";
}
if(k.getNext(10)!=null&&n.getNext(10)!=null) {
uni[kills]=s+k.getData();
kills++;
}
if(k.noK()==false||n.noK()==false) {
return "";
}
int ip = 0;
int imp = 0;
s+=k.getData();
while(imp<10) {
if(k.getNext(imp)!=null&&n.getNext(imp)!=null) {
intersectRec(k.getNext(imp),n.getNext(imp),s);
}
imp++;
}
return "";
}
/**
* intersect takes in a digit tree and makes one that this and others don't
* have in common
* @param other is a digit tree that will be compared to the current digit tree
* @return an intersection digit tree between others and the current digit tree
* time complexity O(n+|nodeCount for other|) - n being the number of nodes
**/
public DigitTree intersect(DigitTree other) {
if(other.getSize()>size) {
uni = new String[other.getSize()];
}
else {
uni = new String[size];
}
kills=0;
DigitTree temp = new DigitTree();
intersectRec(root,other.getRoot(),"");
for(int i=0;i<uni.length;i++) {
if(uni[i]!=null) {
try{
temp.add(uni[i]);
}
catch(DigitFormatException e){}
}
}
return temp;
}
/**
* union adds both items from others and the current tree
* and makes a new one with both those items
* @param other is a digit tree that will be added to make a new one
* @return digit tree containing items from both others and the current tree
* time complexity O(n+|nodeCount for other|) - n being the number of nodes
**/
public DigitTree union(DigitTree other) {
String[] kool = other.printList();
String[] caal = printList();
DigitTree temp = new DigitTree();
for(int it =0; it<kool.length;it++) {
try{
temp.add(kool[it]);
}
catch(DigitFormatException e){
System.out.println("error in union");}
}
for(int it =0; it<caal.length;it++) {
try{
temp.add(caal[it]);
}
catch(DigitFormatException e){System.out.println("error in union2");}
}
return temp;
}
/**
* sub and brains are global variables only used by subRec
* sub is an array of Strings
* brains is an int that tacks the index's of sub
**/
String[] sub;
int brains;
/**
* subRec is a recursive method that subtracts one digit tree from another
* @param node k first tree
* @param node n tree that will be deleted from k
* @param s is the string that tracks the items
* @return string of items
**/
public String subRec(node k,node n, String s) {
if(k==null) {
return "";
}
if(n!=null) {
if(k.getNext(10)!=null&&n.getNext(10)==null) {
sub[brains]=s+k.getData();
brains++;
}
}
else{
if(k.getNext(10)!=null) {
sub[brains]=s+k.getData();
brains++;
}
}
if(k.noK()==false) {
return "";
}
int ip = 0;
int imp = 0;
s+=k.getData();
while(imp<10) {
if(k.getNext(imp)!=null) {
if(n!=null) {
subRec(k.getNext(imp),n.getNext(imp),s);
}
else {
subRec(k.getNext(imp),n,s);
}
}
imp++;
}
return "";
}
/**
* subtarct uses subRec to get items into an array and add them to
* a new digit tree
* @param digit tree to delete from current tree
* @return digit tree that has been subtracted
* time complexity O(n+|nodeCount for other|) - n being the number of nodes
**/
public DigitTree subtract(DigitTree other) {
DigitTree temp = new DigitTree();
sub = new String[size];
subRec(root,other.getRoot(),"");
for(int its = 0;its<sub.length;its++) {
if(sub[its]!=null) {
try{
temp.add(sub[its]);
}
catch(DigitFormatException e){}
}
}
return temp;
}
}