I have a little algorithmic problem. I think I missed something, but I can’t understand exactly what.
I want to go to the tree containing the rows and exit with a unique row.
Here is a graphic example of a tree that I would like to analyze.

My trees will have three different types of elements:
- Boolean operators (OR, NOT, AND) => BE
- other operators (e.g. =) => QO
- leaves (last items) => LEAF
I would like to get something like this:
"LEAF QO LEAF BE LEAF QO LEAF "
I am currently using a recursive method: I am checking the current tree element and re-running the method on my children, depending on the type of elements I have. For each step, I would fill in my last line.
public class SingleTest { static String [] booleanElements = { "", "", "not" };
public static void main(String[] args) throws Exception {
CommonTree tree = (CommonTree)parser.parse().getTree();
if(true){
String where = "";
printWhere(tree, where);
System.out.println(where);
}
}
public static boolean isBooleanElement(CommonTree t){
return Arrays.asList(booleanElements).contains(t.toString().toLowerCase());
}
public static String printWhere(CommonTree t, String where){
if (isBooleanElement(t)){
for ( int i = 0; i < t.getChildCount(); i++ ) {
printWhere((CommonTree)t.getChild(i), where+ "BE");
}
}
else if(t.getChildCount() == 0 ){
where = where + "LEAF";
}
else{
for ( int i = 0; i < t.getChildCount(); i++ ) {
printWhere((CommonTree)t.getChild(i), where + "QO");
}
}
return where;
}
, :
String where = "";
System.out.println(printWhere(tree, where));
"" ( - ).
, : ?
,
.
, , , - :)
EDIT:
( ) - .
:
public static String printWhere(CommonTree t, String where){
if (isBooleanElement(t)){
for ( int i = 0; i < t.getChildCount(); i++ ) {
where = printWhere((CommonTree)t.getChild(i), where) + "BE";
}
}
else if(t.getChildCount() == 0 ){
where = where + "LEAF";
}
else{
for ( int i = 0; i < t.getChildCount(); i++ ) {
where = printWhere((CommonTree)t.getChild(i), where ) + "QO";
}
}
return where;
}