Hello,
I am trying to traverse and tree and want to assign the node id for each node in the tree.
It would be great if someone helps me with it.
Here is the code which i am using and the problem is the nodes in the tree are repeating everytime and i want to visit each node only once and assign the node id to it.
protected void walk(TreeModel model, Object o){
int cc;
int x=1;
cc = model.getChildC ount(o);
System.out.prin tln(" childcount ==> "+cc);
for( int i=0; i < cc; i++) {
Object child = model.getChild( o, i );
if (model.isLeaf(c hild)){
System.out.prin tln(" Leaf ==> "+child.toStrin g());
TreeNode[] pathToRoot = ((DefaultMutabl eTreeNode)child ).getPath();
System.out.prin tln("Path==> "+((DefaultMuta bleTreeNode)chi ld).getPath().t oString());
for (int j= 1; j < (pathToRoot.len gth)-1 ; j++ )
{
System.out.prin t(" ==> "+pathToRoo t[j]);
System.out.prin t("insert into Node (Doc_ID, PNode_ID, Element) values ("+x +" ,"+ y +" , '" + pathToRoot[j] + "')");
storeDB("insert into Node (Doc_ID, PNode_ID, Element) values ('" + x +"' ,'"+ y +"' , '" + pathToRoot[j] + "')");
System.out.prin tln("");
y++;
}
}
else {
System.out.prin t(child.toStrin g()+"--");
walk(model,chil d );
}
}
}
public void storeDB (String insertSQL){
try {
Statement st = con.createState ment();
System.out.prin tln("inserting* **");
st.executeUpdat e(insertSQL);
} catch (Exception e) {
System.err.prin tln("Got an exception! ");
System.err.prin tln(e.getMessag e());
}
}
I am trying to traverse and tree and want to assign the node id for each node in the tree.
It would be great if someone helps me with it.
Here is the code which i am using and the problem is the nodes in the tree are repeating everytime and i want to visit each node only once and assign the node id to it.
protected void walk(TreeModel model, Object o){
int cc;
int x=1;
cc = model.getChildC ount(o);
System.out.prin tln(" childcount ==> "+cc);
for( int i=0; i < cc; i++) {
Object child = model.getChild( o, i );
if (model.isLeaf(c hild)){
System.out.prin tln(" Leaf ==> "+child.toStrin g());
TreeNode[] pathToRoot = ((DefaultMutabl eTreeNode)child ).getPath();
System.out.prin tln("Path==> "+((DefaultMuta bleTreeNode)chi ld).getPath().t oString());
for (int j= 1; j < (pathToRoot.len gth)-1 ; j++ )
{
System.out.prin t(" ==> "+pathToRoo t[j]);
System.out.prin t("insert into Node (Doc_ID, PNode_ID, Element) values ("+x +" ,"+ y +" , '" + pathToRoot[j] + "')");
storeDB("insert into Node (Doc_ID, PNode_ID, Element) values ('" + x +"' ,'"+ y +"' , '" + pathToRoot[j] + "')");
System.out.prin tln("");
y++;
}
}
else {
System.out.prin t(child.toStrin g()+"--");
walk(model,chil d );
}
}
}
public void storeDB (String insertSQL){
try {
Statement st = con.createState ment();
System.out.prin tln("inserting* **");
st.executeUpdat e(insertSQL);
} catch (Exception e) {
System.err.prin tln("Got an exception! ");
System.err.prin tln(e.getMessag e());
}
}
Comment