-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRoutingMapTree.java
434 lines (407 loc) · 10.6 KB
/
RoutingMapTree.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
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
import java.util.*;
public class RoutingMapTree
{
Exchange root;
int depthBaseStation, maxDepth;
public RoutingMapTree()
{
root = new Exchange(0);
depthBaseStation = -1;
maxDepth = 0;
}
public boolean containsNode(Exchange e)
{
if(root != null)
{
if (root.equals(e)) return true;
int n = root.numChildren();
for(int i=0; i < n; i++)
{
if(root.subtree(i).containsNode(e))
return true;
}
}
return false;
}
public int depth(Exchange e)
{
int ans = -1;
Exchange p = e;
while(p != null)
{
ans++;
p = p.parent();
}
return ans;
}
public MobilePhone getMobile(int m)
{
return root.getMobile(m);
}
public void switchOn(MobilePhone m, Exchange e)
{
if(m == null) throw new RuntimeException("Error - Mobile phone does not exist");
if(e == null) throw new RuntimeException("Error - Exchange does not exist");
if(depth(e) != maxDepth)
throw new RuntimeException("Error - Exchange with identifier " + e.id + " is not a base station");
if(!m.status())
{
Exchange ex = m.exchange, p;
while(ex != null)
{
ex.residentSet().Delete(m);
ex = ex.parent();
}
if(depthBaseStation == -1) depthBaseStation = maxDepth;
m.switchOn();
m.exchange = e;
e.residentSet().Insert(m);
p = e.parent();
while(p != null)
{
p.residentSet().Insert(m);
p = p.parent();
}
}
else
throw new RuntimeException("Error - Mobile phone with identifier " + m.number() + " is already on");
}
public void switchOff(MobilePhone m)
{
if(m == null) throw new RuntimeException("Error - Mobile phone does not exist");
if(m.status)
m.switchOff();
else
throw new RuntimeException("Error - Mobile phone with identifier " + m.number() + " is already off");
}
public void switchOnMobile(int a, int b)
{
MobilePhone m = getMobile(a);
Exchange e = getExchange(b);
if(e != null)
{
if(m == null) m = new MobilePhone(a);
try
{
switchOn(m, e);
return;
}
catch(RuntimeException ex)
{
throw ex;
}
}
throw new RuntimeException("Error - Exchange with identifier " + b + " does not exist");
}
public void switchOffMobile(int a)
{
MobilePhone mobile = getMobile(a);
if(mobile != null)
{
try
{
switchOff(mobile);
}
catch(RuntimeException e)
{
throw e;
}
}
else
throw new RuntimeException("Error - No mobile phone with identifier " + a + " found in the network");
}
public Exchange getExchange(int e)
{
if(root != null)
{
if(root.id == e) return root;
int n = root.numChildren();
Exchange tmp;
for(int i=0; i < n; i++)
{
tmp = root.subtree(i).getExchange(e);
if(tmp != null) return tmp;
}
}
return null;
}
public void addExchange(int a, int b)
{
Exchange p = getExchange(a);
Exchange e = getExchange(b);
if(e != null)
throw new RuntimeException("Error - Exchange with identifier " + b + " already exists");
if(p != null)
{
if(depthBaseStation == -1 || depth(p) < depthBaseStation)
{
e = new Exchange(b);
e.parent = p;
p.addChild(e);
maxDepth = Math.max(maxDepth, depth(p) + 1);
return;
}
throw new RuntimeException("Error - Can't add an exchange to a base station");
}
throw new RuntimeException("Error - Exchange with identifier " + a + " does not exist");
}
public int queryNthChild(int a, int b)
{
Exchange e = getExchange(a);
if(e != null)
{
try
{
return e.child(b).id;
}
catch(RuntimeException ex)
{
throw ex;
}
}
throw new RuntimeException("Error - Exchange with identifier " + a + " does not exist");
}
public int[] queryMobilePhoneSet(int a)
{
Exchange e = getExchange(a);
if(e != null)
{
return e.getMobiles();
}
throw new RuntimeException("Error - Exchange with identifier " + a + " does not exist");
}
public Exchange findPhone(MobilePhone m)
{
if(m == null) throw new RuntimeException("Error - No such mobile phone exists in the network");
if(!m.status()) throw new RuntimeException("Error - Mobile phone with identifier " + m.number() + " is currently switched off");
return m.location();
}
public Exchange lowestRouter(Exchange a, Exchange b)
{
if(a == null || b == null) throw new RuntimeException("Error - Either exchange does not exist");
if(depth(a) != maxDepth) throw new RuntimeException("Error - Exchange with identifier " + a.id + " is not a base station");
if(depth(b) != maxDepth) throw new RuntimeException("Error - Exchange with identifier " + b.id + " is not a base station");
Exchange t1 = a, t2 = b;
while(t1 != null && t2!= null && !t1.equals(t2))
{
t1 = t1.parent();
t2 = t2.parent();
}
return t1;
}
public ExchangeList routeCall(MobilePhone a, MobilePhone b)
{
if(a == null || b == null) throw new RuntimeException("Error - Either mobile phone does not exist");
ExchangeList route = new ExchangeList();
Exchange e1,e2;
try
{
e1 = a.location();
e2 = b.location();
}
catch(RuntimeException e)
{
throw e;
}
Exchange lca = lowestRouter(e1, e2);
while(e1 != lca)
{
route.Insert(e1);
e1 = e1.parent();
}
route.Insert(lca);
ExchangeList tmp = new ExchangeList();
while(e2 != lca)
{
tmp.InsertHead(e2);
e2 = e2.parent();
}
for(int i=0; i<tmp.size(); i++)
route.Insert(tmp.getMember(i));
return route;
}
public void movePhone(MobilePhone a, Exchange b)
{
if(a == null) throw new RuntimeException("Error - Mobile phone does not exist");
if(b == null) throw new RuntimeException("Errror - Exchange does not exist");
if(depth(b) != maxDepth) throw new RuntimeException("Error - Exchange with identifier " + b.id + " is not a base station");
if(!a.status()) throw new RuntimeException("Error - Mobile phone with identifier " + a.number() + " is currently switched off");
switchOff(a);
switchOn(a,b);
}
public static boolean isInteger(String s)
{
boolean isValidInteger = false;
try
{
Integer.parseInt(s);
isValidInteger = true;
}
catch (NumberFormatException ex)
{
// s is not an integer
}
return isValidInteger;
}
public String performAction(String actionMessage)
{
String[] query = actionMessage.split("\\s");
if(query.length == 3 && query[0].equals("addExchange") && isInteger(query[1]) && isInteger(query[2]))
{
int a = Integer.parseInt(query[1]);
int b = Integer.parseInt(query[2]);
try
{
addExchange(a,b);
return "";
}
catch(RuntimeException e)
{
return actionMessage + ": " + e.getMessage();
}
}
else if(query.length == 3 && query[0].equals("switchOnMobile") && isInteger(query[1]) && isInteger(query[2]))
{
int a = Integer.parseInt(query[1]);
int b = Integer.parseInt(query[2]);
try
{
switchOnMobile(a,b);
return "";
}
catch(RuntimeException e)
{
return actionMessage + ": " + e.getMessage();
}
}
else if(query.length == 2 && query[0].equals("switchOffMobile") && isInteger(query[1]))
{
int a = Integer.parseInt(query[1]);
try
{
switchOffMobile(a);
return "";
}
catch(RuntimeException e)
{
return actionMessage + ": " + e.getMessage();
}
}
else if(query.length == 3 && query[0].equals("queryNthChild") && isInteger(query[1]) && isInteger(query[2]))
{
String str = actionMessage + ": ";
int a = Integer.parseInt(query[1]);
int b = Integer.parseInt(query[2]);
try
{
str += queryNthChild(a,b);
}
catch(RuntimeException e)
{
str += e.getMessage();
}
return str;
}
else if(query.length == 2 && query[0].equals("queryMobilePhoneSet") && isInteger(query[1]))
{
String str = actionMessage + ": ";
int a = Integer.parseInt(query[1]);
try
{
int[] mobileSet = queryMobilePhoneSet(a);
for(int i=0; i<mobileSet.length-1; i++)
str = str + mobileSet[i] + ", ";
if(mobileSet.length > 0)
str += mobileSet[mobileSet.length-1];
}
catch(RuntimeException e)
{
str += e.getMessage();
}
return str;
}
else if(query.length == 2 && (query[0].equals("findPhone") || query[0].equals("queryFindPhone")) && isInteger(query[1]))
{
int a = Integer.parseInt(query[1]);
String str = "queryFindPhone " + a + ": ";
MobilePhone m = getMobile(a);
if(m == null)
return str + "Error - No mobile phone with identifier " + a + " found in the network";
try
{
str += findPhone(m).id;
}
catch(RuntimeException e)
{
str += e.getMessage();
}
return str;
}
else if(query.length == 3 && (query[0].equals("lowestRouter") || query[0].equals("queryLowestRouter")) && isInteger(query[1]) && isInteger(query[2]))
{
int a = Integer.parseInt(query[1]);
int b = Integer.parseInt(query[2]);
String str = "queryLowestRouter " + a + " " + b + ": ";
Exchange e1 = getExchange(a), e2 = getExchange(b);
if(e1 == null)
return str + "Error - No exchange with identifier " + a + " found in the network";
if(e2 == null)
return str + "Error - No exchange with identifier " + b + " found in the network";
try
{
str += lowestRouter(e1,e2).id;
}
catch(RuntimeException e)
{
str += e.getMessage();
}
return str;
}
else if(query.length == 3 && (query[0].equals("findCallPath") || query[0].equals("queryFindCallPath")) && isInteger(query[1]) && isInteger(query[2]))
{
int a = Integer.parseInt(query[1]);
int b = Integer.parseInt(query[2]);
String str = "queryFindCallPath " + a + " " + b + ": ";
MobilePhone m1 = getMobile(a), m2 = getMobile(b);
if(m1 == null)
return str + "Error - No mobile phone with identifier " + a + " found in the network";
if(m2 == null)
return str + "Error - No mobile phone with identifier " + b + " found in the network";
try
{
ExchangeList route = routeCall(m1,m2);
int n = route.size();
for(int i=0; i<n-1; i++)
str += route.getMember(i).id + ", ";
str += route.getMember(n-1).id;
}
catch(RuntimeException e)
{
str += e.getMessage();
}
return str;
}
else if(query.length == 3 && query[0].equals("movePhone") && isInteger(query[1]) && isInteger(query[2]))
{
int a = Integer.parseInt(query[1]);
int b = Integer.parseInt(query[2]);
MobilePhone m = getMobile(a);
Exchange e = getExchange(b);
String str = actionMessage + ": ";
if(m == null)
return str + "Error - No mobile phone with identifier " + a + " found in the network";
if(e == null)
return str + "Error - No exchange with identifier " + b + " found in the network";
try
{
movePhone(m,e);
return "";
}
catch(RuntimeException ex)
{
return str + ex.getMessage();
}
}
return actionMessage + ": Error - Invalid Input";
}
}