有人自己写出来吗 不要抄网上的 我需要一个稍简单的思路

img


帮个忙 对于我来说有点难 我要一个稍微简单点的思路 当然了不简单也可以 不要抄网上的 要求c语言实现

把每一行输入用\分割开,如何相邻的建立一条边,最后dfs一下就行了

根据文件要求写了一版,可以讨论学习一下

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

public class DirectoryShow {
    static class TreeNode{
        TreeNode(String pathName){
            curName=pathName;
            directoryChildren=new ArrayList<>();
            fileChildren=new ArrayList<>();
        }

        /**
         * 名称
         */
        String curName;
        /**
         * 目录子节点
         */
        List<TreeNode> directoryChildren;

        /**
         * 文件子节点
         */
        List<TreeNode> fileChildren;
    }






    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        TreeNode root=new TreeNode("root");
        //阻塞等待读取输入
        if (scan.hasNextInt()) {
            //读取路径数量
            int directoryCount = scan.nextInt();

            //依次读取路径并构建目录树
            while (directoryCount>0){
                if (scan.hasNext()) {
                    String path = scan.next();
                    //路径挂入根路径
                    resolvePath(path,root);
                    directoryCount--;
                }
            }
        }
        scan.close();
        print(root,0);
    }

    /**
     * 路径处理
     * @param path 路径
     * @param root 根节点
     */
    public static void resolvePath(String path,TreeNode root){
        //判断是否全都是目录
        boolean allDirectory=path.endsWith("\\");
        //拆分路径
        String[] split = path.split("\\\\");
        List<TreeNode> curDChildren=root.directoryChildren;
        TreeNode cur=root;

        //如果全都是目录,则全遍历,否则留最后一个元素,单独处理
        int len=allDirectory?split.length:split.length-1;

        //遍历路径
        for (int i = 0; i < len; i++) {
            boolean has=false;
            for (TreeNode treeNode : curDChildren) {
                if (treeNode.curName.equals(split[i])){
                    curDChildren=treeNode.directoryChildren;
                    cur=treeNode;
                    //如果路径在其中找到了,就继续遍历其子路径
                    has=true;
                    break;
                }
            }
            //如果已经存在当前路径则继续遍历
            if (has)continue;
            //如果没有找到,则直接根创建其余路径
            for (; i < len; i++) {
                TreeNode buf=new TreeNode(split[i]);
                cur.directoryChildren.add(buf);
                cur=buf;
            }
            break;
        }
        if (!allDirectory){
            for (TreeNode treeNode : cur.fileChildren) {
                if (treeNode.curName.equals(split[len])){
                    return;
                }
            }
            cur.fileChildren.add(new TreeNode(split[len]));
        }
    }


    /**
     * 深度优先 进行打印
     * @param root 根节点
     * @param level 路径层次
     */
    public static void print(TreeNode root,int level){
        StringBuilder sbu=new StringBuilder();
        for (int i = 0; i < level; i++) {
            sbu.append("  ");
        }
        sbu.append(root.curName);
        System.out.println(sbu);
        level++;
        root.directoryChildren.sort(Comparator.comparing(t -> t.curName));
        for (TreeNode childD : root.directoryChildren) {
            print(childD,level);
        }
        root.fileChildren.sort(Comparator.comparing(t -> t.curName));
        for (TreeNode childF : root.fileChildren) {
            print(childF,level);
        }
    }

}