I was trying to implement a class Node to build a tree of Nodes. Basically, each Node can have children, so if I specify multiple nodes I can build a tree out of it. As an example:
node1 (the root) has node2 and node3 as children
node2 has node4 and node5 as children
The problem I am having problems to solve is to build this tree and find all children of a given element (in this case node1 would have 4 children, since it has node2 and node3 in the first place, and node2 has 2 children, so in total 4). Does anyone have any suggestion?
EDIT:
package ex1;
import java.sql.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class Node {
private String name;
private String description;
private ArrayList<Node> children = new ArrayList<>();
Node(String name, String description){
this.name = name;
this.description = description;
}
private void setName(String name){
this.name = name;
}
private void setDescription(String description) {
this.description = description;
}
public void addChildren(Node child) {
this.children.add(child);
}
public String getName() {
return this.name;
}
public String getDescription() {
return this.description;
}
public boolean hasDescription() {
return !description.isEmpty();
}
public Collection<Node> getChildren() {
return this.children;
}
/*
public Node findNodeByName(String name, Node t) {
if (t.getName().equals(name))
return t;
t.getChildren().forEach(node -> node.findNodeByName(name,node));
return null;
}*/
public Node findNodeByName(String name, Node t){
if(t.getName().equals(name)){
return t;
}
else if (t.getChildren().size() != 0){
for(Node c: children){
Node ret = c.findNodeByName(name,c);
if(ret != null){
return ret;
}
}
}
return null;
}
// IMPORTANT: Don't change this method!
private String toString(int indentNo) {
String indent = "\t".repeat(indentNo);
StringBuffer b = new StringBuffer();
b.append(indent);
b.append(getClass().getSimpleName() + " '" + getName() + "' ");
if (hasDescription()) {
b.append("(description: " + getDescription() + ")");
}
b.append("\n");
for (Node node : getChildren()) {
b.append(node.toString(indentNo + 1));
}
return b.toString();
}
@Override
public String toString() {
return toString(0);
}
}
Method where I make use of the class:
Path path = Path.of(pathname);
String fileContent = null;
try {
fileContent = Files.readString(path);
} catch (IOException e) {
throw new RuntimeException(e);
}
List<String> lines = new ArrayList<>(Arrays.asList(fileContent.split("\n")));
String[] firstLine = lines.get(0).split(",");
Node parentNode = new Node(firstLine[0], firstLine[1]);
lines.remove(0);
/* This was just to test findNodeByName
for(String line: lines) {
String[] params = line.split(",");
System.out.println(params[2] + ": " + (parentNode.findNodeByName(params[2], parentNode) != null));
}*/
//Now we read all remaining data
Node tmpNode;
for(String line: lines) {
String[] params = line.split(",");
if (parentNode.findNodeByName(params[2])==null || parentNode.findNodeByName(params[0])!=null) //if parent was not found or name already exists
throw new IOException();
tmpNode = parentNode.findNodeByName(params[2]);
tmpNode.addChildren(new Node(params[0],params[1]));
}
CSV file I am getting the data from:
uni,"This is my university folder",
firstyear,,uni
secondyear,,uni
analysis,"folder for the analysis course",firstyear
ai,"folder for the artificial intelligence course",secondyear
db,"folder for the database course",firstyear