Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java将txt文件读入树数据结构_Java - Fatal编程技术网

Java将txt文件读入树数据结构

Java将txt文件读入树数据结构,java,Java,我需要帮助来实现树结构,我创建了addchild函数来将数据添加到树中,但是在添加子函数时似乎出现了问题 我希望这棵树看起来像什么: 日期 / | \ / | \ / | \ / | \ 20160101 20160102 20160103 / | | \ 12:00 13:00 12

我需要帮助来实现树结构,我创建了addchild函数来将数据添加到树中,但是在添加子函数时似乎出现了问题

我希望这棵树看起来像什么:

日期
/      |      \
/       |        \
/         |         \
/           |           \
20160101     20160102     20160103
/               |       |        \
12:00           13:00    12:00      13:00
/    \           /   \     |       /    \
这里,这里,这里
txt文件示例:

Date,Time,Location 20160101,12:00,Here 20160101,12:00,There 20160102,13:00,Here 20160102,13:00,There 20160103,12:00, Here 20160103,13:00, Here 20160103,13:00, There 日期、时间、地点 20160101,12:00,这里 20160101,12:00,那里 20160102,13:00,这里 20160102,13:00,那里 20160103,12:00,这里 20160103,13:00,这里 20160103,13:00,那里 日期的输出似乎很好,它显示了2个日期,因为我不希望同一个日期显示两次,但时间和位置是错误的

预期:

20160101 12:00 Here There 20160102 13:00 Here There 20160103 12:00 Here 13:00 Here There 20160101 12:00 在这里 那里 20160102 13:00 在这里 那里 20160103 12:00 在这里 13:00 在这里 那里 实际:

20160101 12:00 Here There 13:00 Here There 20160102 12:00 Here There 13:00 Here There 20160103 12:00 Here There 13:00 Here There 20160101 12:00 在这里 那里 13:00 在这里 那里 20160102 12:00 在这里 那里 13:00 在这里 那里 20160103 12:00 在这里 那里 13:00 在这里 那里 我感谢对我的代码的任何帮助或反馈

public class Tree {
    List<Tree> children = new ArrayList<Tree>();
    Tree parent = null;
    String data = null;

    public Tree(String data) {
        this.data = data;
    }

    public Tree(String data, Tree parent){
        this.data = data;
        this.parent = parent;
    }

    public void addChild(String data) {
        Tree child = new Tree(data);
        child.parent = this;
        Boolean match = false;
        for (int i = 0; i < this.children.size(); i++) {
            if (this.children.get(i).data.equals(child.data)) {
                match = true;
                break;
            }
        }
        if (!match) {
            this.children.add(child);
        }
    }

    public void addChild(Tree child) {
        Boolean match = false;
        for (int i = 0; i < this.children.size(); i++) {
            if (this.children.get(i).data.equals(child.data)) {
                match = true;
                break;
            }
        }
        if (!match) {
            this.children.add(child);
        }
    }

    public static void main(String[] args) throws IOException {
        long startTime = System.nanoTime();
        Scanner scanFile = new Scanner(new File("example.txt"));
        String line = "";
        line = scanFile.nextLine();
        Tree parentNode = new Tree(line.split(",")[0]);
        Tree dateNode = new Tree(null, parentNode);
        Tree timeNode = new Tree(null, dateNode);
        Tree locationNode = new Tree(null, timeNode);
        System.out.println(parentNode.data);

        while(scanFile.hasNext()) {
            line = scanFile.nextLine();

            timeNode.addChild(line.split(",")[2]);
            dateNode.addChild(line.split(",")[1]);
            parentNode.addChild(line.split(",")[0]);
        }
        scanFile.close();



        for(int i =0; i < parentNode.children.size(); i++) {
            System.out.println(parentNode.children.get(i).data);
            for(int j = 0; j < dateNode.children.size(); j++) {
                System.out.println(dateNode.children.get(j).data);
                for(int k = 0; k < timeNode.children.size(); k++) {
                    System.out.println(timeNode.children.get(k).data);
            }
    }

    long endTime = System.nanoTime();
    System.out.println("Time taken: " + (endTime - startTime) / 1E9 + "s");
    }
}
公共类树{
List children=new ArrayList();
树父级=null;
字符串数据=null;
公共树(字符串数据){
这个数据=数据;
}
公共树(字符串数据,树父级){
这个数据=数据;
this.parent=parent;
}
公共void addChild(字符串数据){
树子级=新树(数据);
child.parent=这个;
布尔匹配=假;
for(int i=0;i
试试这些:

List<String> lines = new ArrayList<>();
Map<String,Map<String,List<String>>> tree = new TreeMap<String,Map<String,List<String>>>();

    for(int i=0;i<lines.size();i++){

        String[] parts = lines.get(i).split(",");

        if(!tree.containsKey(parts[0])){
            tree.put(parts[0],new TreeMap<String,List<String>>());
        }

        Map<String,List<String>> tree2 = tree.get(parts[0]);

        if(!tree.containsKey(parts[1])){
            tree2.put(parts[1],new ArrayList<String>());
        }

        List<String> tree3 = tree2.get(parts[1]);

        if(!tree3.contains(parts[2])){
            tree3.add(parts[2]);
        }

    }
List line=new ArrayList();
映射树=新树映射();

对于(int i=0;i您的树逻辑应该是什么?您的打印只是针对
parentNode
的每个元素从
dateNode
timeNode
反复打印相同的数据。它应该为
的每个元素打印
timeNode
的所有子数据和
dateNode
的所有子数据>parentNode
,问题是
dateNode.children
timeNode的大小。children
仅为1增益,问题是树结构没有意义(您没有解释应该构建什么树),并且您的打印是错误的。在完成扫描后,您没有为
timeNode
dateNode
分配任何内容,因此它们保留您扫描的最后一个值。您应该回答这个问题,解释树的逻辑应该是什么。学习使用调试器以便能够遵循y是很重要的我们的程序一步一步地运行,并查看节点真正包含的内容。编辑后,尝试更改代码,现在看来
dateNode
的每个子节点都有两个时间和两个位置,但这里仍然有问题。代码无法编译,因为它使用
字符串
参数调用
addChild
nd您的
addChild
方法只接受一个
。因此,要么有一个方法没有显示,要么这不是真正的代码。请确保该代码是生成您所写输出的确切代码。