Take the 2-minute tour ×

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..  
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

share|improve this question
Is this homework? What have you done so far? Where are you stuck at? –  Santa Nov 17 '10 at 17:53
Is this a binary tree? –  jjnguy Nov 17 '10 at 18:06
@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. –  Deepak Dec 6 '10 at 14:12

3 Answers 3

up vote 2 down vote accepted

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)
   if (PARENT != null)

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.

share|improve this answer
is this solution in accordance with the data structure which i have created. ? how about the sql part. –  Deepak Nov 17 '10 at 18:16
this is pseudo code. I think this is a homework assignment, so I'm leaving the tree structure and sql querying up to you. –  shoebox639 Nov 17 '10 at 18:19
shoebox could you help me to complete the tree data structure,its incomplete.it would be appreciated if you could help me for this –  Deepak Nov 17 '10 at 18:30
not if this is homework. sorry man. but i will give you some pointers on it. –  shoebox639 Nov 17 '10 at 19:34
shoebox could you help me to complete the tree data structure,its incomplete.it would be appreciated if you could help me for this –  Deepak Dec 1 '10 at 19:02

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.

share|improve this answer
+1 Great advice, especially the second part. –  helpermethod Nov 17 '10 at 19:26

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);
share|improve this answer
You mean Map/HashMap? –  jjnguy Nov 17 '10 at 17:55
I think you mean HashMap –  Eternal Noob Nov 17 '10 at 17:56
Yea, I just changed it –  Marcus Whybrow Nov 17 '10 at 17:56
He wants a tree though... –  shoebox639 Nov 17 '10 at 17:57
yes i want a Tree datastructure.i will post the data structure which is incomplete –  Deepak Nov 17 '10 at 17:58

Your Answer


