I have a graph which will be fed using an external source. So far, a rough structure is something like this:

. In there, the red lines represent siblings of the node, which might be dependency but not a parenthood itself. It might or might not exist.
Currently, I have this code for each node:
public class TreeNode {
private int id;
private int container;
private int status;
private int value;
private boolean visited;
private String node_name;
private ArrayList children = new ArrayList();
private ArrayList siblings = new ArrayList();
private ArrayList parents = new ArrayList();
public TreeNode()
{
this.id = 0;
this.status = 0;
this.visited = false;
this.node_name="";
}
//Getters and setters below.
//parents/siblings/children are added through addParent(treeNode);
}
Then, I have this code to set the values:
public class TreeSetter {
public static void main(String[] args) {
TreeNode A = new TreeNode();
TreeNode B = new TreeNode();
TreeNode C = new TreeNode();
TreeNode D = new TreeNode();
TreeNode E = new TreeNode();
TreeNode F = new TreeNode();
TreeNode G = new TreeNode();
TreeNode H = new TreeNode();
A.setId(1);
A.setNode_name("A");
A.setStatus(1);
A.addParent(null);
B.setId(2);
B.setNode_name("B");
B.setStatus(1);
B.addParent(A);
A.addChildren(B);
C.setId(3);
C.setNode_name("C");
C.setStatus(1);
C.addParent(A);
A.addChildren(C);
D.setId(4);
D.setNode_name("D");
D.setStatus(1);
D.addParent(A);
A.addChildren(D);
E.setId(5);
E.setNode_name("E");
E.setStatus(1);
E.addParent(B);
E.addParent(C);
E.addParent(D);
B.addChildren(E);
C.addChildren(E);
D.addChildren(E);
E.addSiblings(F);
E.addSiblings(G);
E.addSiblings(H);
F.setId(6);
F.setNode_name("F");
F.setStatus(1);
F.addParent(B);
F.addParent(C);
F.addParent(D);
B.addChildren(F);
C.addChildren(F);
D.addChildren(F);
F.addSiblings(E);
F.addSiblings(G);
F.addSiblings(H);
G.setId(7);
G.setNode_name("G");
G.setStatus(1);
G.addParent(B);
G.addParent(C);
G.addParent(D);
B.addChildren(G);
C.addChildren(G);
D.addChildren(G);
G.addSiblings(E);
G.addSiblings(F);
G.addSiblings(H);
H.setId(8);
H.setNode_name("H");
H.setStatus(1);
H.addParent(B);
H.addParent(C);
H.addParent(D);
B.addChildren(H);
C.addChildren(H);
D.addChildren(H);
H.addSiblings(E);
H.addSiblings(F);
H.addSiblings(G);
//Set all other nodes
//Set all node values.
}
}
So, what I need is, let’s say that given H, I need to know:
- What is the value from H -> I -> L (H+I+L)
- Who will be affected if H changes. H -> B,C,D -> A
- What are the dependencies of H? F, G, E.
Given this, my troubles are:
-
How do I create the tree dynamically? For example, let’s say that instead of 12 nodes, you have 1000. Using my code, I will need a lot of lines just to set the values and relations because I am creating every single object by hand. Should I use reflection, factory paradigm to create the 1000 objects?
-
How do I walk the tree? For example, given D, move D->H->I->L (and so on).
I know recursion would be the easiest and cleanest way to do it, but I don’t know how to implement it 🙁
How do I create the tree dynamically:
This is a simple way to dynamically add nodes.