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 284161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:26:05+00:00 2026-05-12T05:26:05+00:00

I iterate through a tree structure to collect the paths of the leaf nodes.

  • 0

I iterate through a tree structure to collect the paths of the leaf nodes. Which way do you prefer to collect the result of the operation:

a) merge the results of the children and return this

private Collection<String> extractPaths(final Element element, final IPath parentPath) {
    final IPath path = parentPath.append(element.getLabel());
    final Collection<Element> children = getElementChildren(element);
    if (children.isEmpty())
        return Collections.singletonList(path.toString());

    final Set<String> result = new TreeSet<String>();
    for (final Element child : children)
        result.addAll(extractPaths(child, path));
    return result;
}

b) provide the result collection as a parameter and add new elements in each recursion step

private void extractPaths(final Element element, final IPath parentPath, final Set<String> result) {
    final IPath path = parentPath.append(element.getLabel());
    final Collection<Element> children = getElementChildren(element);
    if (children.isEmpty())
        result.add(path.toString());

    for (final Element child : children)
       extractPaths(child, path, result);
}
  • 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-05-12T05:26:05+00:00Added an answer on May 12, 2026 at 5:26 am

    The final solution I found after some refactoring is to implement variant b) but to pass a Visitor instead of the result collection:

    private void traverse(final Element element, final Visitor... visitors) {
       for (final Visitor visitor : visitors)
           // push e.g. the parent path to the stack
           visitor.push(visitor.visit(element)); 
    
       for (final Element child: getElementChildren(element))
           traverse(child, visitors);
    
       for (final Visitor visitor : visitors)
           visitor.pop();
    }
    

    The Visitor provides also a stack to carry the information about the parent path. This solution allows me to separate the traversal logic from the collection logic, without the need of the more complex TreeIterator implementation.

    private class CollectPathsVisitor extends ElementVisitor {
        public final Set<String> paths = new TreeSet<String>();
        public Object visit(Element element) {
            final IPath parentPath = (IPath) peek();
            final IPath path = parentPath.append(element.getLabel());
            if (!hasChildren(element))
                paths.add(path);
            return path;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to iterate through the items (strings) in a CComboBox to check which
How to iterate through table if it has this structure: <table> <tr> <td><input class
I'm using the standard visitor pattern to iterate through a LINQ expression tree in
Possible Duplicate: How to iterate through a DOM tree? Could you advise me how
How can one easily iterate through all nodes in a TreeView, examine their .Checked
I'm creating a tree-structure of categories with parentid's which can be called from children
How do you iterate through every file/directory recursively in standard C++?
If you had to iterate through a loop 7 times, would you use: for
Some ways to iterate through the characters of a string in Java are: Using
I'm trying to iterate through an array of elements. jQuery's documentation says: jquery.Each() documentation

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.