0

In database table, i have maintained parent and child folder relationship as shown below datatable. Using that relationship i need to create hierarchical structure in a jsp web page. To display that structure i used http://myfaces.apache.org/tomahawk t:tree2 component.My requirement is, I need to fetch data from database and store that relationship in java variable. To do that i need a recursive technique to store tree structure or(hierarchical structure) in java variable. Please help to find answer.

DataTable: enter image description here

hierarchical View of Directory structure: enter image description here

Thank you

nagesh
  • 307
  • 2
  • 10
  • 22
  • Tricky. http://stackoverflow.com/questions/38801/sql-how-to-store-and-navigate-hierarchies – NickJ Oct 16 '13 at 09:53
  • Showing the present implemented code has no use because it's not traversing to depth of the constructing tree. It's constructing one level tree. – nagesh Oct 16 '13 at 12:04

3 Answers3

0

Create a Node class having list of child nodes and other required state variables like isdirectory etc. Request you to search again on SO with "Tree Like Data Structure in Java". I hope this helps.

bsingh
  • 145
  • 1
  • 6
0

You can use recursion to iterate over it.

void processChilds(Item child) {
List<Item> childs = selectChilds(child);
    for(Item i: childs) {
       //do smth
       processChilds(i);
    }
}

Or you need to select all records at once and then parse it into your own structure of objects. It can be HashMap or your own tree-like structure.

class Item {
  List<Item> childs;
}

I suggest you go with the first one (because it's simple to code) unless your tree is really deep

Bitman
  • 1,996
  • 1
  • 15
  • 18
  • what selectChilds(child) method does in that context?!! – nagesh Oct 16 '13 at 10:40
  • database call like "select colum2 from table where column1 = ?" and ? is the name of current folder. but you'll need to add ROOT folder to your db – Bitman Oct 16 '13 at 11:03
  • k, processChilds(Item child) also have list type parameter. I'm using java 1.4 which does not supports generics and for loop(use used). I guess i need to pass single folderName to processChilds()method so that i can get childs of it from selectChilds(child) method. Sorry if i'm bothering you. Thanks – nagesh Oct 16 '13 at 11:14
  • I suggest to use java 7 since even java 6 has reached it's end of life. You need to write 'selectChilds' by your own. So parameters are up to you. You can write 'List childs' instead of 'List childs' – Bitman Oct 16 '13 at 12:04
  • I used recursive method(one you suggested) to accomplish requisite task. Thank you:) – nagesh Oct 17 '13 at 05:55
0

Creating a tree structure in Java is almost identical as in any other language. You should read up on Data Structures and Algorithms.

Depending on how you are going to use it you data structure will vary. A perfectly good way that is easy to traverse upward is this:

class Node
{
   private Node parent;
   private int value;
}

Together with a hash lookup on leafs (or nodes if you want the whole tree searchable) you have a perfect way to find the path to any file and it's path using such a structure. This structure cannot be used if you have a node and want to find it's children. Then a more conventional tree already shown as one of the answers can be used.

Sylwester
  • 47,942
  • 4
  • 47
  • 79