New answers tagged

3

If you look at how the file system handles folder within their structure, there is one vital difference making stuff easier for traversing and that is keeping track of the parent as well as the folders (and files). In your case this means that you should keep track of the parent of any given Folder, and make a decision regarding the top level to either ...


7

The following algorithm should do almost what you've asked for. paths : List<String> ;; your list of paths tree : Folder = NEW Folder("") FOREACH path IN paths DO node : Folder = tree FOREACH component IN split(path, "/") DO next : Folder = node.getChild(component) IF next IS NULL THEN next := NEW ...


5

Let's assume you have an additional method GetOrCreate(name) that returns a folder for a given name, or creates it if no such folder exist, and that you have a root folder that contains your whole hierarchy. Then given a list of strings path, we can easily implement an iterative solution: Folder current = root; for (name in path) current = ...



Top 50 recent answers are included