forked from gaurav4ever/FresherAssignment2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Assignment3.java
364 lines (356 loc) · 9.96 KB
/
Assignment3.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
package Assignment3;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.TreeSet;
import java.util.Scanner;
class Node {
private int node_id;
private String node_name;
HashSet<Node> parents = null;
HashSet<Node> children = null;
Node() {
parents = new HashSet<Node>();
children = new HashSet<Node>();
}
//getter and setter methods for setting and retrieving the attributes of a node
public void setNodeId(int node_id) {
this.node_id=node_id;
}
public void setNodeName(String node_name) {
this.node_name=node_name;
}
public int getNodeid() {
return this.node_id;
}
public String getNodeName() {
return this.node_name;
}
public void addParent(Node parent) {
if(this.parents.contains(parent)) {
System.out.println("The given node is already added as a parent!");
}
else {
this.parents.add(parent);
}
}
public void addChild(Node child) {
if(this.children.contains(child)) {
System.out.println("The given node is already added as a child!");
}
else {
this.children.add(child);
}
}
public void deleteParent(Node parent) {
if(this.parents.contains(parent)) {
this.parents.remove(parent);
}
else {
System.out.println("No dependency found!");
}
}
public void deleteChild(Node child) {
if(this.children.contains(child)) {
this.children.remove(child);
}
else {
System.out.println("No dependency found!");
}
}
@Override
public String toString() {
return ("Node id-> "+this.getNodeid()+" Node name-> "+this.getNodeName());
}
}
class GraphNodes {
static private ArrayList<Node> nodeList=new ArrayList<Node>();
public static void createNode(int node_id,String node_name) {
Node node=new Node();
node.setNodeId(node_id);
node.setNodeName(node_name);
nodeList.add(node);
}
public static ArrayList<Node> getNodeList() {
return nodeList;
}
public static void deleteNode(int node_id) {
Node currentNode=null;
for(Node temp:nodeList) {
if(temp.getNodeid()==node_id) {
currentNode=temp;
break;
}
}
if(currentNode==null) {
System.out.println("Invalid Node Id!");
} else {
nodeList.remove(currentNode);
for(Node temp:nodeList) {
temp.parents.remove(currentNode);
temp.children.remove(currentNode);
}
System.out.println("Node with "+node_id+" deleted!");
}
}
public void displayParents(Node node) {
System.out.println("The parents of the node are ");
for(Node parent:node.parents) {
System.out.println(parent);
}
}
public void displayChildren(Node node) {
System.out.println("The children of the node are ");
for(Node child:node.children) {
System.out.println(child);
}
}
//recursively display the ancestors of a node
public void displayAncestors(Node node) {
HashSet<Node> parents=node.parents;
if(parents==null) {
System.out.println("There are no ancestors for the given node!");
return;
} else {
System.out.println("Ancestors for the given node are:");
for(Node p:parents) {
System.out.println(p);
displayAncestors(p);
}
}
}
//recursively display descendanta of a node
public void displayDescendants(Node node) {
HashSet<Node> children=node.children;
if(children==null) {
System.out.println("There are no descendants for the given node!");
return;
} else {
System.out.println("Descendants for the given node are :");
for(Node child:children) {
System.out.println(child);
displayDescendants(child);
}
}
}
}
public class Assignment3 {
static Scanner sc=new Scanner(System.in);
//hash set to store unique node_id
static HashSet<Integer> uniqueId=new HashSet<Integer>();
public static void main(String[] args) {
int choice=0;
while(choice!=9) {
System.out.println("--MENU--");
System.out.println("1. Get Immediate Parents of a node");
System.out.println("2. Get Immediate Chidren of a node");
System.out.println("3. Get ancestors of a node");
System.out.println("4. Get descendants of a node");
System.out.println("5. Delete dependency from a tree");
System.out.println("6. Delete a node from a tree");
System.out.println("7. Add dependency to a tree");
System.out.println("8. Add node to a tree");
System.out.println("9. Exit");
System.out.println("Select your option");
try {
choice = Integer.parseInt(sc.next());
} catch(NumberFormatException e) {
System.out.println("Invalid choice!");
}
switch(choice) {
case 1:getParents();
break;
case 2:getChildren();
break;
case 3:getAncestors();
break;
case 4:getDescendants();
break;
case 5:delDependency();
break;
case 6:delNode();
break;
case 7:addDependency();
break;
case 8:addNode();
break;
case 9:System.exit(0);
break;
default:System.out.println("Invalid choice!");
}
}
}
static void getParents() {
System.out.println("Enter node id");
int node_id=0;
try {
node_id=Integer.parseInt(sc.next());
ArrayList<Node> nodeList=GraphNodes.getNodeList();
Node tmpNode=null;
for(Node tmp:nodeList) {
if(tmp.getNodeid()==node_id) {
tmpNode=tmp;
}
}
if(tmpNode==null) {
System.out.println("Node id does not exist!");
} else {
GraphNodes graph_nodes=new GraphNodes();
graph_nodes.displayParents(tmpNode);
}
} catch(NumberFormatException e) {
System.out.println("Invalid input!");
}
}
static void getChildren() {
System.out.println("Enter node id");
int node_id=0;
try {
node_id=Integer.parseInt(sc.next());
ArrayList<Node> nodeList=GraphNodes.getNodeList();
Node tmpNode=null;
for(Node tmp:nodeList) {
if(tmp.getNodeid()==node_id) {
tmpNode=tmp;
}
}
if(tmpNode==null) {
System.out.println("Node id does not exist!");
} else {
GraphNodes graph_nodes=new GraphNodes();
graph_nodes.displayChildren(tmpNode);
}
} catch(NumberFormatException e) {
System.out.println("Invalid input!");
}
}
static void getAncestors() {
System.out.println("Enter node id");
int node_id=0;
try {
node_id=Integer.parseInt(sc.next());
ArrayList<Node> nodeList=GraphNodes.getNodeList();
Node tmpNode=null;
for(Node tmp:nodeList) {
if(tmp.getNodeid()==node_id) {
tmpNode=tmp;
}
}
if(tmpNode==null) {
System.out.println("Node id does not exist!");
} else {
GraphNodes graph_nodes=new GraphNodes();
graph_nodes.displayAncestors(tmpNode);
}
} catch(NumberFormatException e) {
System.out.println("Invalid input!");
}
}
static void getDescendants() {
System.out.println("Enter node id");
int node_id=0;
try {
node_id=Integer.parseInt(sc.next());
ArrayList<Node> nodeList=GraphNodes.getNodeList();
Node tmpNode=null;
for(Node tmp:nodeList) {
if(tmp.getNodeid()==node_id) {
tmpNode=tmp;
}
}
if(tmpNode==null) {
System.out.println("Node id does not exist!");
} else {
GraphNodes graph_nodes=new GraphNodes();
graph_nodes.displayDescendants(tmpNode);
}
} catch(NumberFormatException e) {
System.out.println("Invalid input!");
}
}
static void delDependency() {
System.out.println("Enter parent node_id");
try {
int parent_id=Integer.parseInt(sc.next());
System.out.println("Enter child node_id");
int child_id=Integer.parseInt(sc.next());
ArrayList<Node> nodeList=GraphNodes.getNodeList();
Node parent_node=null;
Node child_node=null;
for(Node tmp:nodeList) {
if(tmp.getNodeid()==parent_id) {
parent_node=tmp;
}
if(tmp.getNodeid()==child_id) {
child_node=tmp;
}
}
if(parent_node==null || child_node==null) {
System.out.println("Invalid Node id!");
}
else {
parent_node.deleteChild(child_node);
child_node.deleteParent(parent_node);
System.out.println("Dependency removed ");
}
} catch(NumberFormatException e) {
System.out.println("Invalid input type!");
}
}
static void delNode() {
System.out.println("Enter node id of node to be deleted");
try {
int node_id=Integer.parseInt(sc.next());
GraphNodes graph_nodes=new GraphNodes();
graph_nodes.deleteNode(node_id);
} catch(NumberFormatException e) {
System.out.println("Invalid Input type!");
}
}
static void addDependency() {
System.out.println("Enter parent node_id");
try {
int parent_id=Integer.parseInt(sc.next());
System.out.println("Enter child node_id");
int child_id=Integer.parseInt(sc.next());
ArrayList<Node> nodeList=GraphNodes.getNodeList();
Node parent_node=null;
Node child_node=null;
for(Node tmp:nodeList) {
if(tmp.getNodeid()==parent_id) {
parent_node=tmp;
}
if(tmp.getNodeid()==child_id) {
child_node=tmp;
}
}
if(parent_node==null || child_node==null) {
System.out.println("Invalid Node id!");
}
else {
parent_node.addChild(child_node);
child_node.addParent(parent_node);
System.out.println("Dependency added ");
}
} catch(NumberFormatException e) {
System.out.println("Invalid input type!");
}
}
static void addNode() {
System.out.println("Enter node_id");
try {
int node_id=Integer.parseInt(sc.next());
if(uniqueId.contains(node_id)) {
System.out.println("Unique Id is required!");
} else {
uniqueId.add(node_id);
System.out.println("Enter node name!");
String node_name=sc.next();
GraphNodes graph_nodes=new GraphNodes();
graph_nodes.createNode(node_id, node_name);
System.out.println("Node added!");
}
} catch(NumberFormatException e) {
System.out.println("Invalid input type!");
}
}
}