tags:

views:

179

answers:

3

Hello Guys,

I am stuck in my project for a while and decided to contact you guys for ideas because am short of ideas how to get it done. This is the task: I have information like this from the server and have to present the information in a tree form structure to the user. The data from the server comes in this form:

APP          net        main 
Account      net        main
Address      net        main
APP          net        main 
Person       book       optel
Person       book       ggggg
Person       book       show 
Bindings     apple      parse
Bindings     apple      findProject
Bindings     apple      show

The positions of the columns is not fixed. The first column can become the second or the third depending on the preference of the user.

1) The task is this: In each row , the first data must be the parent of the second data, the second data must be the parent of the third data and if there appears to be a fourth data then it will be the child of the third and it follows like that...... I have to be able to build this tree like structure whiles am looping over the information from the server.

It start from the left handside(column) , i mean the left column will be the first parent. I must present the information from the server in a tree-like form like this:

APP
  net
     main



Account
    net
       main


...

 Bindings
   apple
       show




etc etc...

Optional Requirement :: And if it is possible i must also be able to use the structure to create a table.

I have spent much time(weeks after weeks) figuring how to begin but am not getting it so am asking if someone can help me with ideas how to get this done or point me to a tutorial where i can have a deep understanding of how i can do it.

A friend of mine suggested using linkedlist but i don't know if he is right and if he is right how do i use it.

I am asking you guys to help me with ideas so that i can implement it.

Thank for your help.

+1  A: 
Geo
A: 

Hello, if it is just presentation, you don't actually have to store them in persistent memory, hence you don't actually need data structures. (Unless it's explicitly stated that you need to use them)

Just an idea, and how I would have tackled this problem given only the constraints you have specified. I assume you already know how to read them by rows. For the first row, think of how you are going to break individual words into a 'node' of its own. (Hint: Java has a String.split() function). There are other ways to do it too.

After that, store them somewhere, in an array or something else, as long as you can get them back in order. Linked lists will do too, so does queues.

While looping to print them out, add a line break and some space in between them to create the output look. Then, you can even recycle the array that you declared earlier for the next row. Good luck!

wai
+1  A: 

Step 1, parse the data into a table structure.

You'll need a simple object to represent a Tree node. Here's a starting point:

// Implement a classic tree "Node" class
abstract class Node {
    String label;
    Map<String, Node> childNodes;

    abstract Node getChild(String label);
    abstract void addChild(Node child);
}

And here is how I would approach parsing the input into a Tree using this structure:

Node root = new Node("root");

Reader r = new InputStreamReader(new BufferedInputStream(...));

String line;

while ((line = r.readLine) != null) {
    String[] colunms = line.split("\\s*");
    // Read in the data in tabular form

    // Build a tree structure of Node objects

    // NOTE: This could be optimized quite a bit to take
    // advantage of the sorted input.

    Node curr = null;
    Node prev = root;
    for (int i = 0; i < columns.length; i++) {
        curr = prev.getChild(columns[i]);

        // If this node has not been seen yet, create it
        if (curr == null) {
             curr = new Node(columns[i]);
             prev.addChild(curr);
        }
        prev = curr;
    }
}

After this you'll have all of the data attached to the root Node. To display a table, you can reverse this process, doing a depth-first traversal and printing the full path to each node as a column of the output.

For a tree, you've got the structure already. If you adapt an existing Model class as your Node object (for example, Swing) then you would simply be able to hand the tree data to a JTreeNode and you're done. Otherwise it's trivial to transform the tree in other ways (render as static HTML for example or to JSON to render in web browser).

Mark Renouf
Thanks for your reply and al the other people who have spent time to reply. I missed one point in my posting . The data i get from the server is in Array of Object(Object[]) and each element in the array is also an array. So i looped through the arrays to present it to you guys to see it. I am sorry i didn't bring this point in my post. I will not read them like strings but rather from arrays of objects. Thanks for your reply. It has opened my mind alot. Any additional recommendations are welcome. Thanks.