0

I want to implement a Generic Tree in java and i want to use a single table for the same.where the structure of the table is as below.

public class MyTreeNode {    
  private int id;    
  private int parentId;    
  private MyTreeNode parent;   
  private List<MyTreeNode> childNodes;   
  // further fields..  
}
NAME OF TABLE IS **TREE_TABLE**  
PK_ID       | PARENT  
----------  |----------  
1           | null  
2           | 1  
3           | 1  
4           | 2

Meaning, the nodes with PK_ID 2 and 3 have node 1 as parent, 4 has 2 as parent, and 1 has no parent (null).

how to implement this in Java code.Any sample code if you have for this or if you can share the code,Please Share it across.

Requirement: Access the above table from underlyng SQL server database and rebuild the tree structure such that the relationship between Parent and Child are set.

Regards Deepak

2
  • 4
    Is this homework? What have you done so far? Where are you stuck at? Commented Nov 17, 2010 at 17:53
  • @jjnguy,How should my class for tree structure look like.im unable to complete this.Please help me with this part.MyTreeNode is the class which i have created,Could you help me to complete the assignment,its pending for while a time. Commented Dec 6, 2010 at 14:12

3 Answers 3

2

Put all your Nodes in an arrayList, created by order. This algorithm assumes the get the table rows by order of PK_ID.

nodes = emptyArrayList

for each row in your table:
   node n = new Node(PK_ID)
   nodes.add(n)
   if (PARENT != null)
       nodes.get(PARENT).addChild(n)

after that, nodes[0] is the root of your tree. I'm not going to show you how to create a tree data structure.

EDIT: about the tree

As for your tree, each node should only know about the nodes under it, not above. So your parent id and parent node fields aren't needed. Otherwise, it looks good.

EDIT: More about your tree structure:

As I said, your tree structure is fine. It can just be a class with an id and the list of children nodes. That's all you need.

Sign up to request clarification or add additional context in comments.

7 Comments

is this solution in accordance with the data structure which i have created. ? how about the sql part.
this is pseudo code. I think this is a homework assignment, so I'm leaving the tree structure and sql querying up to you.
shoebox could you help me to complete the tree data structure,its incomplete.it would be appreciated if you could help me for this
not if this is homework. sorry man. but i will give you some pointers on it.
shoebox could you help me to complete the tree data structure,its incomplete.it would be appreciated if you could help me for this
|
1

The basics on doing a tree in OO languages:

  • You'll need some type of a Node class to hold the data and references to (parents, children, siblings, etc - depending on implementation)

  • When dealing with trees, recursion is your friend.

Comments

0

I would use a HashMap:

HashMap<Integer, Integer> table = new HashMap<Integer, Integer>();

table.put(1, null);
table.put(2, 1);
table.put(3, 1);
table.put(4, 2);

7 Comments

yes i want a Tree datastructure.i will post the data structure which is incomplete
He wants to represent a tree and a HashMap can represent a tree structure in the way that he described.
There is no tree data structure in Java?
I think you need to edit your original question and define what you are actually asking for.
@deepak add this to your original question
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.