Is a recursive method returning a string at every step?

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.

tree example

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);
    }       

}   

/*
 * Print to where tests
 */
public static boolean isBooleanElement(CommonTree t){
    return Arrays.asList(booleanElements).contains(t.toString().toLowerCase());
}


public static String printWhere(CommonTree t, String where){
    //---------------------
    // Checking node type
    //---------------------

    // Boolean Element 
    if (isBooleanElement(t)){
        // Continue parsing the tree                
        for ( int i = 0; i < t.getChildCount(); i++ ) {
            printWhere((CommonTree)t.getChild(i), where+ "BE");
        }               
    }

    // Last element of tree (LEAF)
    else if(t.getChildCount() == 0 ){
        where = where + "LEAF";
    }

    // query operator
    else{
        // Continue parsing the tree    
        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){
    //---------------------
    // Checking node type
    //---------------------

    // Boolean Element 
    if (isBooleanElement(t)){
        // Continue parsing the tree                
        for ( int i = 0; i < t.getChildCount(); i++ ) {
            where = printWhere((CommonTree)t.getChild(i), where) + "BE";
        }               
    }

    // Last element of tree (LEAF)
    else if(t.getChildCount() == 0 ){
        where = where + "LEAF";
    }

    // query operator
    else{
        // Continue parsing the tree    
        for ( int i = 0; i < t.getChildCount(); i++ ) {
            where = printWhere((CommonTree)t.getChild(i), where ) + "QO";
        }                   
    }

    //---------------------
    return where;
}
+3
1

, printWhere ! where, Java , , .

where . . .

+3

All Articles