Reputation:
I have information like this from a 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.
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 while I am looping over the information from the server.
It starts from the left side (column), meaning 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...
Optional Requirement: And if it is possible I must also be able to use the structure to create a table. A friend of mine suggested using a Linked List but I don't know if he is right and if he is right how do I use it.
Upvotes: 0
Views: 1087
Reputation: 96957
You could use a JTree to show a tree structure to the user. You have an example presented here. This is an image of how this could look like, see if it's what you want :
(source: sun.com)
If you want to use the strings mentioned above, you can use the StringTokenizer along with your tree model to build the structure. Pretty simple.
Upvotes: 1
Reputation: 31020
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).
Upvotes: 1
Reputation: 9143
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!
Upvotes: 0