tags:

views:

165

answers:

3

Hello Guys,

I just posted the following question but i made a mistake. What i want is not how to use Eclipse but how to build the tree-like structure. An element can have more than one children. So show me how to build the tree; It is not actually tree but to build the structure as i have show below. Thanks

I am haveng this problem with my application and need help. I have data from the server in this form

Food Apple Fruit Seed etc..

Table Chair pen School et..

...

Food Mango Peer Melon etc..

Reading from the left, i want to make for eg; in the first row,

Food parent of Apple

Apple parent of Fruit

Fruit parent of Seed

and if there are more data then etc.. I will itereate over the above data from the server with loop; Example:::

+Food
    Apple
       Fruit
           Seed
            etc..

+Table
      Chair
          Pen
             School
              etc..

In the above code, "Food" and "Table" belong to the same level. My problem is how do i begin. I have googled the whole day but can't find something that i really understand. How can i for instance make "Food" parent and make Apple its child and then make Fruit the child of apple and so forth. Show me how to build the tree.

Need your help.

Thanks

A: 

Create a class called Node and make Node have an ArrayList of nodes and a variable containing the parent node. As such:

class Node {

private List<Node> children; 
private Node parent;

public Node() {
    children = new ArrayList<Node>(); 
}
//Constructors, accessors, mutators, etc.

}

This is by far the simplest way. You can make node an abstract class, then have chair, desk, school, etc, all extend Node.

public class School extends Node {

    public School() {
        super();
    }

    public void addChild(Node node) {
        children.add(node);       
    }

    public void setParent(Node node) {
        parent = node;
    }
}

Edit: I've added some sample methods to show you what needs to happen in order to add children or set the parent. In some other class you can create your School, Desk, etc. objects and then create addChild and setParent methods in order to add children to the node or set the node's parent.

AlbertoPL
Thanks for your reply. If am adding the chair,desk etc.. How do they become child(ren) of one another. Can you please explain to me the inner workings. Infact this is my first time of using this so am totally confuse. Give me some explanations so that i can understand it well. thanks
Kap
I really thank you soo much. I have now understood what you mean. Thank you alot. I have one more question, sorry for disturbing you. The data i get from the server are in arrays like this :[School Table Chair Book]My question is how can i loop through the data so that i can create the tree-like structure so that :School becomes the parent of "Table" Table parent of "Chair"Chair parent of "Book" . I am finding it difficult how to use the for() loop or while() loop to loop through the elements to create the tree structure. Need your advice here.Thanks for helping me.
Kap
Keep track of the previous element in the array, with a variable such as prevNode. When you first start looping, prevNode is null, so create a School node and do not set the parent (since School has no parent). Also, set prevNode equal to School. Then, when you loop again, set the parent of Table equal to prevNode (which equals School), and add Table to the list of children in school. If you do this all in the for loop it should work for all of the elements in the array.
AlbertoPL
A: 

You could try using the javax.swing.tree.DefaultMutableTreeNode class. While it is in the swing package, it does not contain any graphic code.

KitsuneYMG
+1  A: 

Here is a simple implementation whereby each node has a human-readable String name and references zero or more child Nodes.

public interface Node {
  String getName();

  void addChild(Node node);

  List<? extends Node> getChildren();
}

public class NodeImpl implements Node {
  private final String name;
  private final List<Node> nodes;

  public NodeImpl(String name) {
    this.name = name;
    this.nodes = new LinkedList<Node>();
  }

  public String getName() {
    return name;
  }

  public void addChild(Node node) {
    nodes.add(node);
  }

  public List<? extends Node> getChildren() {
    return Collections.unmodifiableList(nodes);
  }
}

An example usage would be:

Node root = new Node("Root"); // Create root node.
Node food = new Node("Food"); // Create level 1 child nodes.
Node table = new Node("Table");

root.addChild(foo); // Add level 1 child nodes to root.
root.addChild(table);

Node apple = new Node("Apple"); // Create and add other child nodes.
food.addChild(apple); // etc.
Adamski
I also thank you soo much for your reply. The data i get from the server are in arrays like this :[School Table Chair Book]My question is how can i loop through the data so that i can create the tree-like structure so that :School becomes the parent of "Table" Table parent of "Chair"Chair parent of "Book" . I am finding it difficult how to use the for() loop or while() loop to loop through the elements to create the tree structure. Need your advice here.Thanks for helping me.
Kap