Solved

How to traverse a tree(input) and build another tree based on the certain condition?

Posted on 2016-09-23
2
48 Views
Last Modified: 2016-10-02
If the file system is the input tree, what I would like to do is to start from one folder, and scan each file within this folder and use the file name to search another place in the file system using some conditions like: filename = foldername, for each foldetname returned in this search, add this foldetname as a child to the output tree and search again until not more match.


So the input is a tree and output is also a tree. Recursion is what I think is required to solved the problem and hopefully someone can share ideas or examples or links , really appreciate!
0
Comment
Question by:Steven Ni
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 28

Accepted Solution

by:
dpearson earned 500 total points
ID: 41818530
Maybe you're looking for a structure like this (in pseudo-code):

Tree scan(File folder) {
   Tree result = new Tree() ;
   List<File> files = folder.list() ;   // This is where you "scan the input tree"
   for (File file : files) {
      if (condition(file)) {
          Tree subtree = scan(file) ;
           result.add(subtree) ;   // Need to insert this in the correct part of the tree
      }
   }
   return tree ;
}

Open in new window


If you have no way to retrieve a list of files inside a folder from a folder object, then you'd also want to pass in the input tree.  So it would be:

Tree scan(File folder, Tree input) {
}

and you just pass the input tree around as well.

Any help?

Doug
0
 

Author Closing Comment

by:Steven Ni
ID: 41825245
Thanks for the solution and sorry for the late reply as was on vacation.
0

Featured Post

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Starting up a Project

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question