如何从数据库中拿取数据来还原一颗指定用户的多叉树

该博客主要展示了如何从数据库中获取数据并构建一个多叉树的文件管理系统。代码实现了递归遍历节点,通过预序、中序和后序遍历打印节点信息。程序使用Java实现,涉及数据库连接和SQL查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码如下:

package 算法and数据结构.数据结构.多叉树.文件管理系统.从数据库中拿取数据并建树;

import 算法and数据结构.数据结构.多叉树.文件管理系统.Node.Node;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import static 算法and数据结构.数据结构.多叉树.文件管理系统.连接数据库.getMySQL.getConnection;

public class creat {

    String select = "select file_name from file_system where parent_file_name = '{' and user_name = '{'";

    public creat(Node root){
//        select(root,"893295815@qq.com");

        select(root,"NCEPU_RuanCong");
        System.out.println("=======================前======================");
        text.pre(root);
        System.out.println("=======================中======================");
        text.mid(root);
        System.out.println("=======================后======================");
        text.pos(root);
    }

    private String getSelectSQL(String ...args) {
        StringBuilder stringBuilder = new StringBuilder(args[0]);
        int j = 1;
        for (int i = 0; i < stringBuilder.length(); i++) {
            if (stringBuilder.charAt(i) == '{'){
                stringBuilder.replace(i,i+1,args[j]);
                i=i+args[1].length();
                j++;
            }
        }
        return stringBuilder.toString();
    }

    private void select(Node root,String username){
        if (root == null)return;
        Connection connection=getConnection();
        Statement statement=null;
        ResultSet resultSet=null;
        try {
            statement=getConnection().createStatement();
            System.out.println(getSelectSQL(select,root.date,username));
            resultSet=statement.executeQuery(getSelectSQL(select,root.date,username));
            while (resultSet.next()){
                root.children.add(new Node(resultSet.getString("file_name"),root,1));
            }
            for (int i = 0; i < root.children.size(); i++) {
                select(root.children.get(i),username);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            if (connection != null){
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
           if (statement != null){
               try {
                   statement.close();
               } catch (SQLException throwables) {
                   throwables.printStackTrace();
               }
           }
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }
}
class text{
    public static void main(String[] args) {
//        new creat(new Node("Is 893295815@qq.com Computer",null,1));
        new creat(new Node("Is NCEPU_RuanCong Computer",null,1));

    }
    public static void pre(){

    }
    public static void pre(Node root){
        if(root == null)return;
        System.out.println(root.date);
        for(int i=0 ; i< root.children.size() ;i++){
            pre(root.children.get(i));
        }
    }
    public static void mid(Node root){
        if(root == null)return;
        if (root.children.size() != 0){
            mid(root.children.get(0));
        }
        System.out.println(root.date);
        for(int i = 1 ; i < root.children.size() ; i++){
            mid(root.children.get(i));
        }
    }
    public static void pos(Node root){
        if (root == null){
            return;
        }
        for (int i = 0; i < root.children.size(); i++) {
            pos(root.children.get(i));
        }
        System.out.println(root.date);
    }
}

以下是代码运行的结果:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值