I have a tree that looks like the following (a simple example, can be a variable size).
1 (root)
|---------------------------------|
1-1 1-2
|------------| |--------------|
1-1-1 1-1-2 1-2-1 1-2-2
I've written a Java class to generate this tree based on NumberOfSiblings (horizontal) and NumberOfChildren (vertical). I'm now looking to write a class that will generate the path of a given entry in the tree, for example:
Entry: 1-1-1
Path: /1/1-1/1-1-1
I believe that I need some sort of recursive method that will count the number of dashes and take off the last part of the entry (not always length() - 2 though as it could be 10-10-10).
This is the method that will generate the tree:
public static void generateCatalog(String parent, int siblings, int children, int level) {
for (int a = 1; a <= siblings; a++) {
String child = parent + "-" + a;
System.out.println(child);
if (level <= children) {
level++;
generateCatalog(child, siblings, children, level);
level--;
}
}
}
I've been trying something like this to generate the path but it does not work very well.
public static void getPath(String category, String path) {
System.out.println("Category: " + category);
System.out.println("Current path: " + path);
int numberOfDashes = category.length() - category.replace("-", "").length();
System.out.println("Number of dashes: " + numberOfDashes);
while (numberOfDashes > 1) {
path = category + "/" + category.substring(0, category.length() - 2);
getPath(category, path);
}
System.out.println("New path: " + path);
}
Could someone please tell me the best way to do this?