简体   繁体   中英

How to display BinaryTree in JTree java swing

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);
    }
  }
}

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);
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM