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
|
/*Alexander Pickering
*Doctor Tan
*4-10-15
*/
import java.util.Random;
public class ShowPath
{
public static void main(String[] args)
{
int point = 0;
BinaryNodeInterface<Character> root = generateTree();
System.out.println("Your string returned from the method getAllPaths() is as follows:");
String str = getAllPaths(root);
System.out.println(str);
System.out.println("Your result of using the method getPathTo() for every character is as follows:");
for(int i = 0; i < 52; i++)
{
if(i < 26)
{
System.out.println("Path to " + (char) ('a' + i) + " " + getPathTo(root, (char) ('a' + i)));
}
else
{
System.out.println("Path to " + (char) ('A' + (i - 26)) + " " + getPathTo(root, (char) ('A' + (i - 26))));
}
}
System.out.println();
if(!checkGetAllPaths(root, str))
{
point = point + 5;
}
System.out.println("Your current point is " + point + ".");
if(!checkGetPathTo(root))
{
point = point + 5;
}
System.out.println("Your total point is " + point + ".");
}
private static String getAllPaths(final BinaryNodeInterface<Character> root)
{
return getAllPathsHelper(root,"");
}
private static String getAllPathsHelper(final BinaryNodeInterface<Character> root,String pre)
{
String output = "\n";
if(root != null)
{
output += root.getData() + " " + pre;
}
if(root.hasLeftChild())
{
output += getAllPathsHelper(root.getLeftChild(),pre+"0");
}
if(root.hasRightChild())
{
output += getAllPathsHelper(root.getRightChild(),pre+"1");
}
return output;
}
private static String getPathTo(final BinaryNodeInterface<Character> root, char c)
{
return getPathToHelper(root,c,"");
}
private static String getPathToHelper(final BinaryNodeInterface<Character> root, char c,String cur)
{
String output = "";
if(root != null)
{
if(root.getData() == c)
{
return cur;
}
}
if(root.hasLeftChild())
{
output = getPathToHelper(root.getLeftChild(),c,cur+"0");
if(output != "")
{
return output;
}
}
if(root.hasRightChild())
{
output = getPathToHelper(root.getRightChild(),c,cur+"1");
if(output != "")
{
return output;
}
}
return output;
}
private static boolean checkGetAllPaths(BinaryNodeInterface<Character> root, String str)
{
boolean error = false;
String[] list = str.split("\n");
// check for duplicate characters and number of character
for(int i = 0; i < list.length; i++)
{
if(!list[i].equals(""))
{
char c = list[i].charAt(0);
for(int j = i + 1; j < list.length; j++)
{
if(!list[j].equals(""))
{
if(c == list[j].charAt(0))
{
System.out.println("There are more than one character '" + c + "' in your output string.");
error = true;
}
}
}
}
}
// Check paths
for(int i = 0; i < list.length; i++)
{
String temp = list[i];
if(!temp.equals(""))
{
char c = temp.charAt(0);
String path = temp.substring(2);
BinaryNodeInterface<Character> currentNode = root;
for(int j = 0; j < path.length(); j++)
{
if(path.charAt(j) == '0')
{
currentNode = currentNode.getLeftChild();
}
else
{
currentNode = currentNode.getRightChild();
}
}
char nodeCharacter = currentNode.getData();
if(c != nodeCharacter)
{
System.out.println("The line \"" + list[i] + "\" says the path to the character " + c + " is " + path + ".");
System.out.println("But the path " + path + " goes to the node containing the character " + nodeCharacter + ".");
error = true;
}
}
}
System.out.print("Testing the method getAllPath(): ");
if(!error)
{
System.out.println("PASS");
}
else
{
System.out.println("FAIL");
}
return error;
}
private static boolean checkGetPathTo(BinaryNodeInterface<Character> root)
{
boolean error = false;
for(int i = 0; i < 52; i++)
{
char c;
if(i < 26)
{
c = (char) ('a' + i);
}
else
{
c = (char) ('A' + (i - 26));
}
String path = getPathTo(root, c);
BinaryNodeInterface<Character> currentNode = root;
for(int j = 0; j < path.length(); j++)
{
if(path.charAt(j) == '0')
{
currentNode = currentNode.getLeftChild();
}
else
{
currentNode = currentNode.getRightChild();
}
}
char nodeCharacter = currentNode.getData();
if(c != nodeCharacter)
{
System.out.println("Your method getPathTo() says the path to the character " + c + " is " + path + ".");
System.out.println("But the path " + path + " goes to the node containing the character " + nodeCharacter + ".");
error = true;
}
}
System.out.print("Testing the method getPathTo(): ");
if(!error)
{
System.out.println("PASS");
}
else
{
System.out.println("FAIL");
}
return error;
}
private static BinaryNodeInterface<Character> generateTree()
{
char[] charList = new char[52];
for(int i = 0; i < 52; i++)
{
if(i < 26)
{
charList[i] = (char) ('a' + i);
}
else
{
charList[i] = (char) ('A' + (i - 26));
}
}
Random rand = new Random();
rand.setSeed(System.currentTimeMillis());
for(int i = 0; i < 1000; i++)
{
int index1 = rand.nextInt(52);
int index2 = rand.nextInt(52);
char temp = charList[index1];
charList[index1] = charList[index2];
charList[index2] = temp;
}
BinaryNodeInterface<Character> root = new BinaryNode<Character>(charList[0]);
SimpleQueue<BinaryNodeInterface<Character>> queue = new SimpleQueue<BinaryNodeInterface<Character>>();
queue.enqueue(root);
for(int i = 1; i < 52; i++)
{
BinaryNodeInterface<Character> currentNode = queue.getFront();
BinaryNodeInterface<Character> newNode = new BinaryNode<Character>(charList[i]);
if(!currentNode.hasLeftChild())
{
currentNode.setLeftChild(newNode);
}
else if(!currentNode.hasRightChild())
{
currentNode.setRightChild(newNode);
}
else
{
queue.dequeue();
currentNode = queue.getFront();
currentNode.setLeftChild(newNode);
}
queue.enqueue(newNode);
}
return root;
}
}
|