1

I have binary tree class and certainly node class

public class Node {
     SomeClass somedata;
     Node leftChld;
     Node rightChld;
}

I need to show my binary in JTree swing gui component . Please give example hot to do this. Thx in advance .

Sorry for topic, I have done it by myself Here it is

 public void showInTree(DefaultMutableTreeNode rtCm,BNode rt){
  if (rt!=null) {
    StringBuilder str = new StringBuilder(String.valueOf(rt.numVal));
    if (rt.chVal!='\0'){
        str.append("--" + "(").append(String.valueOf(rt.chVal)).append(")");
    }
     DefaultMutableTreeNode newnode = new DefaultMutableTreeNode(str);
    rtCm.add(newnode);
    if (rt.leftBNode != null) {
        showInTree(newnode, rt.leftBNode);
}   
    if (rt.rightBNode!=null) {
        showInTree(newnode,rt.rightBNode);
    }
  }
}
CROSP
  • 4,499
  • 4
  • 38
  • 89
  • Might be you can get a bit of logic from this [thread](http://stackoverflow.com/q/4965335/1057230) :-) – nIcE cOw Nov 24 '13 at 14:35
  • Answering your own questions should be done by adding an answer (see below) rather than edit the question. – Assimiz Nov 24 '13 at 16:03

1 Answers1

1

Here it is

 public void showInTree(DefaultMutableTreeNode rtCm,BNode rt){
  if (rt!=null) {
    StringBuilder str = new StringBuilder(String.valueOf(rt.numVal));
    if (rt.chVal!='\0'){
        str.append("--" + "(").append(String.valueOf(rt.chVal)).append(")");
    }
     DefaultMutableTreeNode newnode = new DefaultMutableTreeNode(str);
    rtCm.add(newnode);
    if (rt.leftBNode != null) {
        showInTree(newnode, rt.leftBNode);
}   
    if (rt.rightBNode!=null) {
        showInTree(newnode,rt.rightBNode);
    }
  }
}
CROSP
  • 4,499
  • 4
  • 38
  • 89