Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7961813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:09:20+00:00 2026-06-04T05:09:20+00:00

I have a slight algorithmic problem. I think I miss something but can’t exactly

  • 0

I have a slight algorithmic problem. I think I miss something but can’t exactly figure out what.

I want to walk to a tree containing strings and get out with a unique string.

Here is a graphical example of a tree I would like to parse.

tree example

My trees would have three different types of elements :

  • Boolean operators (OR, NOT, AND) => BE
  • other operators (like the =) => QO
  • leaves (last elements) =>LEAF

I would like to end up with something like this :

"LEAF QO LEAF BE LEAF QO LEAF "

For now, I use a recursive method: I check the current element of the tree, and re run the method on its children depending on the type of elements I have. For each step I would populate my final string.

public class SingleTest {
static String[] booleanElements = {“or”, “and”, “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;
}

My problem is that this code :

        String where = "";
        System.out.println(printWhere(tree, where));

returns “” (Which is logical due to my implementation).

So my question is, how can I get to have a non void string as final output ?

Hope this is clear enough
Thank you for your help

Please note that this class is used for test purpose only, and I know that putting static everywhere is bad practice 🙂

EDIT :

The problem was (as expected) due to my lack of experience with recursion.
Here is my final code :

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T05:09:21+00:00Added an answer on June 4, 2026 at 5:09 am

    The problem is that you method printWhere does not return anything! You’re appending the value to new where string, but since Java passes parameters by value, this newly created string is thrown away when you leave the method.

    Make this method return string and return where at the end of it. Then, concatenate the result of a recursive call with the string from the above level. That’s how recursion works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a slight problem and cannot for the life of me figure out
I have a slight CSS problem I cannot figure out. If you look in
people. I have slight problem with GD2 text on image. I have everything working
I have a slight problem with a path: D:\\Music\\DJ Ti%C3%ABsto\\Tiesto\\Adagio For Strings (Spirit of
I have a slight problem... we made a change to our url structure the
I have a slight problem, I am trying to capture the input of two
I have a slight issue where I can't seem to select a User Control
am new here. i have a slight problem; PLease look at the following code
hey everyone, here is the site SEE BELOW I have a slight jquery problem
I've wrote this simple piece of code. And I have a slight problem with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.