-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab7FamilyTree.java
More file actions
326 lines (263 loc) · 6.25 KB
/
Lab7FamilyTree.java
File metadata and controls
326 lines (263 loc) · 6.25 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
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Lab7FamilyTree {
/**
* Lab Problem 7 -> on 03 March - 09 March
*
* Sample Input 1:
* 8
* Motilal Jawahar
* Jawahar Indira
* Motilal Kamala
* Indira Sanjay
* Sanjay Varun
* Indira Rajiv
* Rajiv Priyanka
* Rajiv Rahul
* 6
* Motilal child Jawahar
* Varun descendant Indira
* Priyanka sibling Varun
* Sanjay child Indira
* Sanjay ancestor Varun
* Kamala ancestor Rahul
*
*
*
* Sample Input 2:
* 9
* Prithviraj Raj
* Shashi Sanjana
* Prithviraj Shashi
* Raj Randhir
* Rishi Ranveer
* Randhir Bebo
* Randhir Lolo
* Raj Rishi
* Rishi Ridhima
* 7
* Bebo descendant Shashi
* Raj sibling Shashi
* Prithviraj ancestor Ridhima
* Lolo sibling Ridhima
* Bebo ancestor Shashi
* Prithviraj ancestor Raj
* Rishi descendant Raj
*
*
* @param args
*/
public static String output = "";
public static void main(String[] args) {
Scanner fin = null;
try {
fin = new Scanner(System.in);
int n = Integer.valueOf(fin.nextLine());
List<String> data = new ArrayList<>();
for(int i=0; i< n; i++) {
data.add(fin.nextLine());
}
int m = Integer.valueOf(fin.nextLine());
List<String> relation = new ArrayList<>();
relation = new ArrayList<>();
for(int i=0; i< m; i++) {
relation.add(fin.nextLine());
}
Node root = build(data);
StringBuffer buf = new StringBuffer();
for(String r : relation) {
boolean result = validateRelation(root, r);
if(result) {
buf.append("T ");
} else {
buf.append("F ");
}
}
System.out.println(buf.toString().trim());
printPreOrder(root);
System.out.println(output.trim());
} catch(Exception e) {
if(fin != null) {
fin.close();
}
}
}
public static boolean validate(int n, String m, List<String> data, List<String> relation) {
if(n<0 && n>99) {
return false;
}
Integer mi = Integer.valueOf(m);
if(mi<0 && mi>99) {
return false;
}
if(data.size() != n) {
return false;
}
if(relation.size() != mi) {
return false;
}
return true;
}
public static boolean validateRelation(Node root, String relationInput) {
boolean result = false;
String[] values = relationInput.split(" ");
String p1 = values[0];
String relation = values[1];
String p2 = values[2];
switch (relation) {
case "child":
result = isChild(root, p1, p2);
break;
case "descendant":
result = isDescendant(root, p1, p2);
break;
case "sibling":
result = isSibling(root, p1, p2);
break;
case "ancestor":
result = isAncestor(root, p1, p2);
break;
case "parent":
result = isParent(root, p1, p2);
break;
default:
break;
}
return result;
}
public static void printPreOrder(Node root) {
if(root.getValue() != null) {
output = output + root.getValue() + " ";
}
if(root.getLeft() != null) {
printPreOrder(root.getLeft());
}
if(root.getRight() != null) {
printPreOrder(root.getRight());
}
}
public static Node build(List<String> data) {
Node root = null;
for(String f : data) {
String[] x = f.split(" ");
root = insertNode(root, x[0], x[1]);
}
return root;
}
public static Node insertNode(Node node, String parent, String child) {
if(node == null) {
node = new Node(parent);
}
Node result = searchNode(node, parent);
if(result == null) {
return node;
}
if(result.getLeft() == null) {
Node temp = new Node(child);
result.setLeft(temp);
} else {
Node temp = new Node(child);
result.setRight(temp);
}
return node;
}
public static boolean isSibling(Node root, String p1, String p2) {
boolean r = false;
Node result1 = searchParent(root, p1);
Node result2 = searchParent(root, p2);
if(result1 != null && result2 != null) {
if(result1.getValue().equalsIgnoreCase(result2.getValue())) {
r = true;
}
}
return r;
}
public static boolean isAncestor(Node root, String p1, String p2) {
boolean r = false;
Node result = searchNode(root, p1);
if(result != null) {
Node temp = searchNode(result, p2);
if(temp != null) {
r = true;
}
}
return r;
}
public static boolean isDescendant(Node root, String p1, String p2) {
boolean r = false;
Node result = searchNode(root, p2);
if(result != null) {
Node temp = searchNode(result, p1);
if(temp != null) {
r = true;
}
}
return r;
}
public static boolean isParent(Node root, String p1, String p2) {
boolean r = false;
Node result = searchNode(root, p1);
if(result != null) {
if(result.getLeft() != null && result.getLeft().getValue().equalsIgnoreCase(p2)) {
r = true;
}
if(result.getRight() != null && result.getRight().getValue().equalsIgnoreCase(p2)) {
r = true;
}
}
return r;
}
public static boolean isChild(Node root, String p1, String p2) {
boolean r = false;
Node result = searchNode(root, p2);
if(result != null) {
if(result.getLeft() != null && result.getLeft().getValue().equalsIgnoreCase(p1)) {
r = true;
}
if(result.getRight() != null && result.getRight().getValue().equalsIgnoreCase(p1)) {
r = true;
}
}
return r;
}
public static Node searchParent(Node root, String element) {
if(root.getLeft() != null && root.getLeft().getValue().equalsIgnoreCase(element)) {
return root;
}
if(root.getRight() != null && root.getRight().getValue().equalsIgnoreCase(element)) {
return root;
}
if(root.getLeft() != null) {
Node result = searchParent(root.getLeft(), element);
if(result != null) {
return result;
}
}
if(root.getRight() != null) {
Node result = searchParent(root.getRight(), element);
if(result != null) {
return result;
}
}
return null;
}
public static Node searchNode(Node root, String element) {
if(root.getValue().equalsIgnoreCase(element)) {
return root;
}
if(root.getLeft() != null) {
Node result = searchNode(root.getLeft(), element);
if(result != null) {
return result;
}
}
if(root.getRight() != null) {
Node result = searchNode(root.getRight(), element);
if(result != null) {
return result;
}
}
return null;
}
}