Solved

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

Posted on 2016-09-23
2
43 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 27

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

This is an explanation of a simple data model to help parse a JSON feed
In this post we will learn different types of Android Layout and some basics of an Android App.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

739 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