从Bd分割并创建树

时间:2018-03-08 08:55:37

标签: java

我有一个SQL表,其结构如下

name     father
-----------------
alex     kmal jury salama

我已尝试使用此代码......但它不起作用

con=DriverManager.getConnection("jdbc:ucanaccess://C:/bd/Base8.accdb");
java.sql.Statement stmt= con.createStatement();
ResultSet result= stmt.executeQuery("SELECT name,father FROM Base1");
     while(result.next())          
       splitword(result.getString("father"),result.getString("name"));  
       Node.print(root);




public static void  splitword(String father,String son) {
    ArrayList<String> result = new ArrayList<>(Arrays.asList(father.split(" ") ));
    Collections.reverse(result);
    result.add(son);
    System.out.println(result);


    root2=new Node(result.get(0));
    result.remove(0);

    CreateTree(result,root2);


}
private static void CreateTree(ArrayList<String> result,Node tree) {
      if(result!=null) {
          Node  child=new Node(result.get(0));
          System.out.println(result.get(0));
          result.remove(0);
          tree.addChild(child);
          CreateTree(result,child);  
      }

我的班级节点:

public class Node {
        private String data;
        private Node parent;
        private List<Node> children;


        public Node(String data) {
            this.data = data;
            parent = null;
            children = new ArrayList<Node>(); //Empty list of children  
        }

如何创建下面所示的树

          salama
           |
          jury
           |
          kmal
           |
          alex
salama是kmal的陪审团和陪审团之父,并且喜欢这个...... 它只是实现一棵大树的一个例​​子 谢谢你

1 个答案:

答案 0 :(得分:1)

检查这个帮助你的希望

private static void createTree(ArrayList<String> result, Node tree) {
    if (result != null && !resultat.isEmpty()) {
        Node child = new Node(result.get(0));
        tree.addChild(child);

        createTree(result.subList(1, result.size()), child);  
    }