-7

I need to write an efficient algorithm to do the following task:

Given a list of nodes which can represent either a file or folder, construct the tree structure of the folder hierarchy. The node contains: 1. The parent path of the current node in String. 2. Whether this node is a file or folder.

I have spent one day to think about this but could not work out the answer. Is this something do-able?

Many thanks.

Timmy O'Mahony
  • 53,000
  • 18
  • 155
  • 177
Kevin
  • 5,972
  • 17
  • 63
  • 87
  • 14
  • 3
    good luck with the homeworks, hope you'll get an A. – shem May 04 '12 at 13:01
  • 5
    [What have you tried](http://mattgemmell.com/2008/12/08/what-have-you-tried/)? I guess that during the day you have spent attempting to solve this task you tried various things or at least get some thought about how it could be solved. Could you share your progress so far? – Darin Dimitrov May 04 '12 at 13:01
  • possible duplicate of [Recursively list files in Java](http://stackoverflow.com/questions/2056221/recursively-list-files-in-java) – Donal Fellows May 04 '12 at 13:01
  • Eel is just pure baiting the guy :-P – Thihara May 04 '12 at 13:02
  • Clarify and provide additional details. I wanted to help despite being a not-so-cleverly disguised homework, but the details are insufficient anyway. – Luka Ramishvili May 04 '12 at 13:02
  • 1
    I thought Eel's answer had as much careful consideration behind it as the question. ;-) – Tony Ennis May 04 '12 at 13:03
  • 2
    My answer is the only one that fully, completely *and* correctly addressed the only question present in the original post. – Hovercraft Full Of Eels May 04 '12 at 13:04
  • I just realized that we should create a separate site for homework! Apart from a place we can goto relax and laugh, it can actually help out the really motivated people who have the misfortune to be stuck with a crappy lecturer!!! – Thihara May 04 '12 at 13:10

2 Answers2

5

Dude I think a simple recursive file search will suffice for this.. Refer this link

Recursively list files in Java

After that it's just using a JTree. And may I suggest you look around before posting a common question?

Community
  • 1
  • 1
Thihara
  • 7,031
  • 2
  • 29
  • 56
0

Assuming there is a way to identify the parent node of a given node (e.g. by having a path) this is absolutely doable.

Just a few tips, since this seems to be designed as a learning experience for you:

Use a map nodeKey->node (e.g. path->node) and when iterating over the nodes extract the parent node's key and look it up in the map. If you get the parent, add the current node as a child of that parent.

Thomas
  • 87,414
  • 12
  • 119
  • 157