A Level-2-Tree app with AddObject NLSTree
Here is a niffty algorithm to display create a child-parent tree structure for the
AddObject NLSTree tool.
I'm not paid by the AddObject folks but did find the NLSTree tool sufficiently awesome enough write-up a use application.
It takes an input of level-node entries and produces an output of
tree.add(..) statements.
Well, as an example consider this tree of colors for
in.txt where white is a child and yellow a grandchild of blue.
in.txt
1 blue
2 white
2 red
3 yellow
4 orange
3 violet
2 cyan
3 green
4 black
5 brown
Each Java application needs method static main(..) so here is the
Level2TreeX.java class instancing
Level2Tree.java dynamic class
Level2TreeX.java
public class Level2TreeX {
public static void main(String[] argc) {
Level2Tree l2t = new Level2Tree();
l2t.process(argc[0]);
}
}
Each row in the file is tab delimited and parsed with the Java String split("\t") in method fileGetLine().
You'll notice method stringFilter(..) handles the task of a regular expression but in atomic i-just-gotta-code-it-myself sytle.
Really for the life of me i had no idea how to filter and replace undesired characters but if it works.
Actually, this is the first time i used the StringBuffer() class for this purpose so it was quite an encouraging path (for an old-time newbie like myself).
Method algorithm() is the star of the show with a hack-up logic contruct - it works, yeah it's my fault but don't know how it got coded!.
Level2Tree.java
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.FileReader;
public class Level2Tree {
public void process(String filename) {
fileStart(filename);
ArrayList out = algorithm();
fileEnd();
for (int i=0; i < out.size(); ++i) {
System.out.println(out.get(i));
}
}
public ArrayList algorithm() {
// control vars
ArrayList parent = new ArrayList();
ArrayList tree = new ArrayList();
String[] fields;
while (null != (fields = fileGetLine())) {
String rowLevel = fields[0];
String rowNode = stringFilter(fields[1]);
int level = Integer.parseInt(rowLevel, 10);
if (parent.size() == level) {
// has same parent
} else if (parent.size() < level) {
// is new parent
// assume levels always increase by one
parent.add(rowNode);
} else {
// regress to earlier parent
while (level <= parent.size()) {
// remove last
parent.remove(parent.size()-1);
}
parent.add(rowNode);
}
int pos = parent.size()-2;
String parentID;
if (pos < 0) {
parentID = "null";
} else {
parentID = (String) parent.get(pos);
}
String line =
"tree.add(\"" +
rowNode + "\",\"" +
parentID + "\",\"" +
rowNode + "\",\"\",\"\",0,0,\"\",\"" +
rowNode + "\");";
tree.add(line);
}
return tree;
}
public String stringFilter(String str) {
String out;
if (null == str) {
out = "";
} else {
str = str.trim();
StringBuffer sb = new StringBuffer();
for (int i=0; i < str.length(); ++i) {
char c = str.charAt(i);
String toSuffix = String.valueOf(c);
if (false) {
// rid of double quote
} else if (c == '"') {
toSuffix = "";
// replace ampersand
} else if (c == '&') {
toSuffix = "&";
}
sb.append(toSuffix);
}
out = sb.toString();
}
return out;
}
BufferedReader br;
void fileStart(String filename) {
try {
// set up a file reader to read each expression line.
FileReader fr = new FileReader(filename);
br = new BufferedReader(fr);
} catch (Exception e) {
System.out.println("error, msg: " + e.getMessage());
System.exit(0);
}
}
String[] fileGetLine() {
String[] out;
String line;
out = null;
try {
// read new code line.
if ((line = br.readLine()) != null) {
line = line.trim();
out = line.split("\t");
}
} catch (Exception e) {
System.out.println("error, msg: " + e.getMessage());
System.exit(0);
}
return out;
}
void fileEnd() {
try {
br.close();
} catch (Exception e) {
System.out.println("error, msg: " + e.getMessage());
System.exit(0);
}
}
}
In any case, the output result here is neatly consumed by the AddObject Object NLSTree tool.
out.txt
tree.add("blue","null","blue","","",0,0,"","blue");
tree.add("white","blue","white","","",0,0,"","white");
tree.add("red","blue","red","","",0,0,"","red");
tree.add("yellow","white","yellow","","",0,0,"","yellow");
tree.add("orange","yellow","orange","","",0,0,"","orange");
tree.add("violet","white","violet","","",0,0,"","violet");
tree.add("cyan","blue","cyan","","",0,0,"","cyan");
tree.add("green","cyan","green","","",0,0,"","green");
tree.add("black","green","black","","",0,0,"","black");
tree.add("brown","black","brown","","",0,0,"","brown");
On the next article, i'll show you how to wrap-up this core functionality into a displayable tree.