Solved

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

Posted on 2016-09-23
2
36 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
2 Comments
 
LVL 26

Accepted Solution

by:
dpearson earned 500 total points
Comment Utility
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
Comment Utility
Thanks for the solution and sorry for the late reply as was on vacation.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Exchange and Third Party application. 8 59
Fibonacci challenge 11 82
wordcount challenge 11 74
Math homework question 5 60
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.
This is about my first experience with programming Arduino.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now