Solved

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
This is about my first experience with programming Arduino.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
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…

728 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